From e8180375746519b6131fccc516368b20885d8ed7 Mon Sep 17 00:00:00 2001 From: Alkalus Date: Tue, 31 Mar 2020 09:34:44 +0100 Subject: $ Refactored package name of ReflectionFactory classes. --- .../reflectionfactory/AccessorGenerator.java | 715 ++++ .../BootstrapConstructorAccessorImpl.java | 52 + src/Java/shaded/reflectionfactory/ByteVector.java | 37 + .../reflectionfactory/ByteVectorFactory.java | 36 + .../shaded/reflectionfactory/ByteVectorImpl.java | 88 + .../shaded/reflectionfactory/CallerSensitive.java | 41 + .../shaded/reflectionfactory/ClassDefiner.java | 73 + .../reflectionfactory/ClassFileAssembler.java | 671 +++ .../reflectionfactory/ClassFileConstants.java | 140 + .../reflectionfactory/ConstructorAccessor.java | 41 + .../reflectionfactory/ConstructorAccessorImpl.java | 41 + .../DelegatingConstructorAccessorImpl.java | 51 + .../DelegatingMethodAccessorImpl.java | 49 + .../shaded/reflectionfactory/FieldAccessor.java | 96 + .../reflectionfactory/FieldAccessorImpl.java | 105 + .../shaded/reflectionfactory/ForgeEnumHelper.java | 70 + ...tantiationExceptionConstructorAccessorImpl.java | 51 + src/Java/shaded/reflectionfactory/Label.java | 76 + .../reflectionfactory/LangReflectAccess.java | 112 + .../reflectionfactory/MagicAccessorImpl.java | 47 + .../shaded/reflectionfactory/MethodAccessor.java | 40 + .../reflectionfactory/MethodAccessorGenerator.java | 780 ++++ .../reflectionfactory/MethodAccessorImpl.java | 48 + .../NativeConstructorAccessorImpl.java | 72 + .../NativeMethodAccessorImpl.java | 69 + .../reflectionfactory/PermissionFactory.java | 36 + src/Java/shaded/reflectionfactory/ReflectUtil.java | 338 ++ src/Java/shaded/reflectionfactory/Reflection.java | 361 ++ .../reflectionfactory/ReflectionFactory.java | 706 ++++ .../reflectionfactory/SecurityConstants.java | 227 + .../SerializationConstructorAccessorImpl.java | 45 + src/Java/shaded/reflectionfactory/UTF8.java | 76 + src/Java/shaded/reflectionfactory/Unsafe.java | 1146 +++++ .../UnsafeFieldAccessorFactory.java | 4446 ++++++++++++++++++++ .../reflectionfactory/UnsafeFieldAccessorImpl.java | 205 + .../UnsafeQualifiedFieldAccessorImpl.java | 50 + .../UnsafeQualifiedStaticFieldAccessorImpl.java | 42 + .../UnsafeStaticFieldAccessorImpl.java | 49 + 38 files changed, 11328 insertions(+) create mode 100644 src/Java/shaded/reflectionfactory/AccessorGenerator.java create mode 100644 src/Java/shaded/reflectionfactory/BootstrapConstructorAccessorImpl.java create mode 100644 src/Java/shaded/reflectionfactory/ByteVector.java create mode 100644 src/Java/shaded/reflectionfactory/ByteVectorFactory.java create mode 100644 src/Java/shaded/reflectionfactory/ByteVectorImpl.java create mode 100644 src/Java/shaded/reflectionfactory/CallerSensitive.java create mode 100644 src/Java/shaded/reflectionfactory/ClassDefiner.java create mode 100644 src/Java/shaded/reflectionfactory/ClassFileAssembler.java create mode 100644 src/Java/shaded/reflectionfactory/ClassFileConstants.java create mode 100644 src/Java/shaded/reflectionfactory/ConstructorAccessor.java create mode 100644 src/Java/shaded/reflectionfactory/ConstructorAccessorImpl.java create mode 100644 src/Java/shaded/reflectionfactory/DelegatingConstructorAccessorImpl.java create mode 100644 src/Java/shaded/reflectionfactory/DelegatingMethodAccessorImpl.java create mode 100644 src/Java/shaded/reflectionfactory/FieldAccessor.java create mode 100644 src/Java/shaded/reflectionfactory/FieldAccessorImpl.java create mode 100644 src/Java/shaded/reflectionfactory/ForgeEnumHelper.java create mode 100644 src/Java/shaded/reflectionfactory/InstantiationExceptionConstructorAccessorImpl.java create mode 100644 src/Java/shaded/reflectionfactory/Label.java create mode 100644 src/Java/shaded/reflectionfactory/LangReflectAccess.java create mode 100644 src/Java/shaded/reflectionfactory/MagicAccessorImpl.java create mode 100644 src/Java/shaded/reflectionfactory/MethodAccessor.java create mode 100644 src/Java/shaded/reflectionfactory/MethodAccessorGenerator.java create mode 100644 src/Java/shaded/reflectionfactory/MethodAccessorImpl.java create mode 100644 src/Java/shaded/reflectionfactory/NativeConstructorAccessorImpl.java create mode 100644 src/Java/shaded/reflectionfactory/NativeMethodAccessorImpl.java create mode 100644 src/Java/shaded/reflectionfactory/PermissionFactory.java create mode 100644 src/Java/shaded/reflectionfactory/ReflectUtil.java create mode 100644 src/Java/shaded/reflectionfactory/Reflection.java create mode 100644 src/Java/shaded/reflectionfactory/ReflectionFactory.java create mode 100644 src/Java/shaded/reflectionfactory/SecurityConstants.java create mode 100644 src/Java/shaded/reflectionfactory/SerializationConstructorAccessorImpl.java create mode 100644 src/Java/shaded/reflectionfactory/UTF8.java create mode 100644 src/Java/shaded/reflectionfactory/Unsafe.java create mode 100644 src/Java/shaded/reflectionfactory/UnsafeFieldAccessorFactory.java create mode 100644 src/Java/shaded/reflectionfactory/UnsafeFieldAccessorImpl.java create mode 100644 src/Java/shaded/reflectionfactory/UnsafeQualifiedFieldAccessorImpl.java create mode 100644 src/Java/shaded/reflectionfactory/UnsafeQualifiedStaticFieldAccessorImpl.java create mode 100644 src/Java/shaded/reflectionfactory/UnsafeStaticFieldAccessorImpl.java (limited to 'src/Java/shaded/reflectionfactory') diff --git a/src/Java/shaded/reflectionfactory/AccessorGenerator.java b/src/Java/shaded/reflectionfactory/AccessorGenerator.java new file mode 100644 index 0000000000..bbd854e892 --- /dev/null +++ b/src/Java/shaded/reflectionfactory/AccessorGenerator.java @@ -0,0 +1,715 @@ +/* + * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package shaded.reflectionfactory; + +import java.lang.reflect.*; + +/** Shared functionality for all accessor generators */ + +class AccessorGenerator implements ClassFileConstants { + static final Unsafe unsafe = Unsafe.getUnsafe(); + + // Constants because there's no way to say "short integer constant", + // i.e., "1S" + protected static final short S0 = (short) 0; + protected static final short S1 = (short) 1; + protected static final short S2 = (short) 2; + protected static final short S3 = (short) 3; + protected static final short S4 = (short) 4; + protected static final short S5 = (short) 5; + protected static final short S6 = (short) 6; + + // Instance variables for shared functionality between + // FieldAccessorGenerator and MethodAccessorGenerator + protected ClassFileAssembler asm; + protected int modifiers; + protected short thisClass; + protected short superClass; + protected short targetClass; + // Common constant pool entries to FieldAccessor and MethodAccessor + protected short throwableClass; + protected short classCastClass; + protected short nullPointerClass; + protected short illegalArgumentClass; + protected short invocationTargetClass; + protected short initIdx; + protected short initNameAndTypeIdx; + protected short initStringNameAndTypeIdx; + protected short nullPointerCtorIdx; + protected short illegalArgumentCtorIdx; + protected short illegalArgumentStringCtorIdx; + protected short invocationTargetCtorIdx; + protected short superCtorIdx; + protected short objectClass; + protected short toStringIdx; + protected short codeIdx; + protected short exceptionsIdx; + // Boxing + protected short booleanIdx; + protected short booleanCtorIdx; + protected short booleanUnboxIdx; + protected short byteIdx; + protected short byteCtorIdx; + protected short byteUnboxIdx; + protected short characterIdx; + protected short characterCtorIdx; + protected short characterUnboxIdx; + protected short doubleIdx; + protected short doubleCtorIdx; + protected short doubleUnboxIdx; + protected short floatIdx; + protected short floatCtorIdx; + protected short floatUnboxIdx; + protected short integerIdx; + protected short integerCtorIdx; + protected short integerUnboxIdx; + protected short longIdx; + protected short longCtorIdx; + protected short longUnboxIdx; + protected short shortIdx; + protected short shortCtorIdx; + protected short shortUnboxIdx; + + protected final short NUM_COMMON_CPOOL_ENTRIES = (short) 30; + protected final short NUM_BOXING_CPOOL_ENTRIES = (short) 72; + + // Requires that superClass has been set up + protected void emitCommonConstantPoolEntries() { + // + [UTF-8] "java/lang/Throwable" + // + [CONSTANT_Class_info] for above + // + [UTF-8] "java/lang/ClassCastException" + // + [CONSTANT_Class_info] for above + // + [UTF-8] "java/lang/NullPointerException" + // + [CONSTANT_Class_info] for above + // + [UTF-8] "java/lang/IllegalArgumentException" + // + [CONSTANT_Class_info] for above + // + [UTF-8] "java/lang/InvocationTargetException" + // + [CONSTANT_Class_info] for above + // + [UTF-8] "" + // + [UTF-8] "()V" + // + [CONSTANT_NameAndType_info] for above + // + [CONSTANT_Methodref_info] for NullPointerException's constructor + // + [CONSTANT_Methodref_info] for IllegalArgumentException's constructor + // + [UTF-8] "(Ljava/lang/String;)V" + // + [CONSTANT_NameAndType_info] for "(Ljava/lang/String;)V" + // + [CONSTANT_Methodref_info] for IllegalArgumentException's constructor taking a String + // + [UTF-8] "(Ljava/lang/Throwable;)V" + // + [CONSTANT_NameAndType_info] for "(Ljava/lang/Throwable;)V" + // + [CONSTANT_Methodref_info] for InvocationTargetException's constructor + // + [CONSTANT_Methodref_info] for "super()" + // + [UTF-8] "java/lang/Object" + // + [CONSTANT_Class_info] for above + // + [UTF-8] "toString" + // + [UTF-8] "()Ljava/lang/String;" + // + [CONSTANT_NameAndType_info] for "toString()Ljava/lang/String;" + // + [CONSTANT_Methodref_info] for Object's toString method + // + [UTF-8] "Code" + // + [UTF-8] "Exceptions" + asm.emitConstantPoolUTF8("java/lang/Throwable"); + asm.emitConstantPoolClass(asm.cpi()); + throwableClass = asm.cpi(); + asm.emitConstantPoolUTF8("java/lang/ClassCastException"); + asm.emitConstantPoolClass(asm.cpi()); + classCastClass = asm.cpi(); + asm.emitConstantPoolUTF8("java/lang/NullPointerException"); + asm.emitConstantPoolClass(asm.cpi()); + nullPointerClass = asm.cpi(); + asm.emitConstantPoolUTF8("java/lang/IllegalArgumentException"); + asm.emitConstantPoolClass(asm.cpi()); + illegalArgumentClass = asm.cpi(); + asm.emitConstantPoolUTF8("java/lang/reflect/InvocationTargetException"); + asm.emitConstantPoolClass(asm.cpi()); + invocationTargetClass = asm.cpi(); + asm.emitConstantPoolUTF8(""); + initIdx = asm.cpi(); + asm.emitConstantPoolUTF8("()V"); + asm.emitConstantPoolNameAndType(initIdx, asm.cpi()); + initNameAndTypeIdx = asm.cpi(); + asm.emitConstantPoolMethodref(nullPointerClass, initNameAndTypeIdx); + nullPointerCtorIdx = asm.cpi(); + asm.emitConstantPoolMethodref(illegalArgumentClass, initNameAndTypeIdx); + illegalArgumentCtorIdx = asm.cpi(); + asm.emitConstantPoolUTF8("(Ljava/lang/String;)V"); + asm.emitConstantPoolNameAndType(initIdx, asm.cpi()); + initStringNameAndTypeIdx = asm.cpi(); + asm.emitConstantPoolMethodref(illegalArgumentClass, initStringNameAndTypeIdx); + illegalArgumentStringCtorIdx = asm.cpi(); + asm.emitConstantPoolUTF8("(Ljava/lang/Throwable;)V"); + asm.emitConstantPoolNameAndType(initIdx, asm.cpi()); + asm.emitConstantPoolMethodref(invocationTargetClass, asm.cpi()); + invocationTargetCtorIdx = asm.cpi(); + asm.emitConstantPoolMethodref(superClass, initNameAndTypeIdx); + superCtorIdx = asm.cpi(); + asm.emitConstantPoolUTF8("java/lang/Object"); + asm.emitConstantPoolClass(asm.cpi()); + objectClass = asm.cpi(); + asm.emitConstantPoolUTF8("toString"); + asm.emitConstantPoolUTF8("()Ljava/lang/String;"); + asm.emitConstantPoolNameAndType(sub(asm.cpi(), S1), asm.cpi()); + asm.emitConstantPoolMethodref(objectClass, asm.cpi()); + toStringIdx = asm.cpi(); + asm.emitConstantPoolUTF8("Code"); + codeIdx = asm.cpi(); + asm.emitConstantPoolUTF8("Exceptions"); + exceptionsIdx = asm.cpi(); + } + + /** Constant pool entries required to be able to box/unbox primitive + types. Note that we don't emit these if we don't need them. */ + protected void emitBoxingContantPoolEntries() { + // * [UTF-8] "java/lang/Boolean" + // * [CONSTANT_Class_info] for above + // * [UTF-8] "(Z)V" + // * [CONSTANT_NameAndType_info] for above + // * [CONSTANT_Methodref_info] for above + // * [UTF-8] "booleanValue" + // * [UTF-8] "()Z" + // * [CONSTANT_NameAndType_info] for above + // * [CONSTANT_Methodref_info] for above + // * [UTF-8] "java/lang/Byte" + // * [CONSTANT_Class_info] for above + // * [UTF-8] "(B)V" + // * [CONSTANT_NameAndType_info] for above + // * [CONSTANT_Methodref_info] for above + // * [UTF-8] "byteValue" + // * [UTF-8] "()B" + // * [CONSTANT_NameAndType_info] for above + // * [CONSTANT_Methodref_info] for above + // * [UTF-8] "java/lang/Character" + // * [CONSTANT_Class_info] for above + // * [UTF-8] "(C)V" + // * [CONSTANT_NameAndType_info] for above + // * [CONSTANT_Methodref_info] for above + // * [UTF-8] "charValue" + // * [UTF-8] "()C" + // * [CONSTANT_NameAndType_info] for above + // * [CONSTANT_Methodref_info] for above + // * [UTF-8] "java/lang/Double" + // * [CONSTANT_Class_info] for above + // * [UTF-8] "(D)V" + // * [CONSTANT_NameAndType_info] for above + // * [CONSTANT_Methodref_info] for above + // * [UTF-8] "doubleValue" + // * [UTF-8] "()D" + // * [CONSTANT_NameAndType_info] for above + // * [CONSTANT_Methodref_info] for above + // * [UTF-8] "java/lang/Float" + // * [CONSTANT_Class_info] for above + // * [UTF-8] "(F)V" + // * [CONSTANT_NameAndType_info] for above + // * [CONSTANT_Methodref_info] for above + // * [UTF-8] "floatValue" + // * [UTF-8] "()F" + // * [CONSTANT_NameAndType_info] for above + // * [CONSTANT_Methodref_info] for above + // * [UTF-8] "java/lang/Integer" + // * [CONSTANT_Class_info] for above + // * [UTF-8] "(I)V" + // * [CONSTANT_NameAndType_info] for above + // * [CONSTANT_Methodref_info] for above + // * [UTF-8] "intValue" + // * [UTF-8] "()I" + // * [CONSTANT_NameAndType_info] for above + // * [CONSTANT_Methodref_info] for above + // * [UTF-8] "java/lang/Long" + // * [CONSTANT_Class_info] for above + // * [UTF-8] "(J)V" + // * [CONSTANT_NameAndType_info] for above + // * [CONSTANT_Methodref_info] for above + // * [UTF-8] "longValue" + // * [UTF-8] "()J" + // * [CONSTANT_NameAndType_info] for above + // * [CONSTANT_Methodref_info] for above + // * [UTF-8] "java/lang/Short" + // * [CONSTANT_Class_info] for above + // * [UTF-8] "(S)V" + // * [CONSTANT_NameAndType_info] for above + // * [CONSTANT_Methodref_info] for above + // * [UTF-8] "shortValue" + // * [UTF-8] "()S" + // * [CONSTANT_NameAndType_info] for above + // * [CONSTANT_Methodref_info] for above + // Boolean + asm.emitConstantPoolUTF8("java/lang/Boolean"); + asm.emitConstantPoolClass(asm.cpi()); + booleanIdx = asm.cpi(); + asm.emitConstantPoolUTF8("(Z)V"); + asm.emitConstantPoolNameAndType(initIdx, asm.cpi()); + asm.emitConstantPoolMethodref(sub(asm.cpi(), S2), asm.cpi()); + booleanCtorIdx = asm.cpi(); + asm.emitConstantPoolUTF8("booleanValue"); + asm.emitConstantPoolUTF8("()Z"); + asm.emitConstantPoolNameAndType(sub(asm.cpi(), S1), asm.cpi()); + asm.emitConstantPoolMethodref(sub(asm.cpi(), S6), asm.cpi()); + booleanUnboxIdx = asm.cpi(); + + // Byte + asm.emitConstantPoolUTF8("java/lang/Byte"); + asm.emitConstantPoolClass(asm.cpi()); + byteIdx = asm.cpi(); + asm.emitConstantPoolUTF8("(B)V"); + asm.emitConstantPoolNameAndType(initIdx, asm.cpi()); + asm.emitConstantPoolMethodref(sub(asm.cpi(), S2), asm.cpi()); + byteCtorIdx = asm.cpi(); + asm.emitConstantPoolUTF8("byteValue"); + asm.emitConstantPoolUTF8("()B"); + asm.emitConstantPoolNameAndType(sub(asm.cpi(), S1), asm.cpi()); + asm.emitConstantPoolMethodref(sub(asm.cpi(), S6), asm.cpi()); + byteUnboxIdx = asm.cpi(); + + // Character + asm.emitConstantPoolUTF8("java/lang/Character"); + asm.emitConstantPoolClass(asm.cpi()); + characterIdx = asm.cpi(); + asm.emitConstantPoolUTF8("(C)V"); + asm.emitConstantPoolNameAndType(initIdx, asm.cpi()); + asm.emitConstantPoolMethodref(sub(asm.cpi(), S2), asm.cpi()); + characterCtorIdx = asm.cpi(); + asm.emitConstantPoolUTF8("charValue"); + asm.emitConstantPoolUTF8("()C"); + asm.emitConstantPoolNameAndType(sub(asm.cpi(), S1), asm.cpi()); + asm.emitConstantPoolMethodref(sub(asm.cpi(), S6), asm.cpi()); + characterUnboxIdx = asm.cpi(); + + // Double + asm.emitConstantPoolUTF8("java/lang/Double"); + asm.emitConstantPoolClass(asm.cpi()); + doubleIdx = asm.cpi(); + asm.emitConstantPoolUTF8("(D)V"); + asm.emitConstantPoolNameAndType(initIdx, asm.cpi()); + asm.emitConstantPoolMethodref(sub(asm.cpi(), S2), asm.cpi()); + doubleCtorIdx = asm.cpi(); + asm.emitConstantPoolUTF8("doubleValue"); + asm.emitConstantPoolUTF8("()D"); + asm.emitConstantPoolNameAndType(sub(asm.cpi(), S1), asm.cpi()); + asm.emitConstantPoolMethodref(sub(asm.cpi(), S6), asm.cpi()); + doubleUnboxIdx = asm.cpi(); + + // Float + asm.emitConstantPoolUTF8("java/lang/Float"); + asm.emitConstantPoolClass(asm.cpi()); + floatIdx = asm.cpi(); + asm.emitConstantPoolUTF8("(F)V"); + asm.emitConstantPoolNameAndType(initIdx, asm.cpi()); + asm.emitConstantPoolMethodref(sub(asm.cpi(), S2), asm.cpi()); + floatCtorIdx = asm.cpi(); + asm.emitConstantPoolUTF8("floatValue"); + asm.emitConstantPoolUTF8("()F"); + asm.emitConstantPoolNameAndType(sub(asm.cpi(), S1), asm.cpi()); + asm.emitConstantPoolMethodref(sub(asm.cpi(), S6), asm.cpi()); + floatUnboxIdx = asm.cpi(); + + // Integer + asm.emitConstantPoolUTF8("java/lang/Integer"); + asm.emitConstantPoolClass(asm.cpi()); + integerIdx = asm.cpi(); + asm.emitConstantPoolUTF8("(I)V"); + asm.emitConstantPoolNameAndType(initIdx, asm.cpi()); + asm.emitConstantPoolMethodref(sub(asm.cpi(), S2), asm.cpi()); + integerCtorIdx = asm.cpi(); + asm.emitConstantPoolUTF8("intValue"); + asm.emitConstantPoolUTF8("()I"); + asm.emitConstantPoolNameAndType(sub(asm.cpi(), S1), asm.cpi()); + asm.emitConstantPoolMethodref(sub(asm.cpi(), S6), asm.cpi()); + integerUnboxIdx = asm.cpi(); + + // Long + asm.emitConstantPoolUTF8("java/lang/Long"); + asm.emitConstantPoolClass(asm.cpi()); + longIdx = asm.cpi(); + asm.emitConstantPoolUTF8("(J)V"); + asm.emitConstantPoolNameAndType(initIdx, asm.cpi()); + asm.emitConstantPoolMethodref(sub(asm.cpi(), S2), asm.cpi()); + longCtorIdx = asm.cpi(); + asm.emitConstantPoolUTF8("longValue"); + asm.emitConstantPoolUTF8("()J"); + asm.emitConstantPoolNameAndType(sub(asm.cpi(), S1), asm.cpi()); + asm.emitConstantPoolMethodref(sub(asm.cpi(), S6), asm.cpi()); + longUnboxIdx = asm.cpi(); + + // Short + asm.emitConstantPoolUTF8("java/lang/Short"); + asm.emitConstantPoolClass(asm.cpi()); + shortIdx = asm.cpi(); + asm.emitConstantPoolUTF8("(S)V"); + asm.emitConstantPoolNameAndType(initIdx, asm.cpi()); + asm.emitConstantPoolMethodref(sub(asm.cpi(), S2), asm.cpi()); + shortCtorIdx = asm.cpi(); + asm.emitConstantPoolUTF8("shortValue"); + asm.emitConstantPoolUTF8("()S"); + asm.emitConstantPoolNameAndType(sub(asm.cpi(), S1), asm.cpi()); + asm.emitConstantPoolMethodref(sub(asm.cpi(), S6), asm.cpi()); + shortUnboxIdx = asm.cpi(); + } + + // Necessary because of Java's annoying promotion rules + protected static short add(short s1, short s2) { + return (short) (s1 + s2); + } + + protected static short sub(short s1, short s2) { + return (short) (s1 - s2); + } + + protected boolean isStatic() { + return Modifier.isStatic(modifiers); + } + + protected boolean isPrivate() { + return Modifier.isPrivate(modifiers); + } + + /** Returns class name in "internal" form (i.e., '/' separators + instead of '.') */ + protected static String getClassName + (Class c, boolean addPrefixAndSuffixForNonPrimitiveTypes) + { + if (c.isPrimitive()) { + if (c == Boolean.TYPE) { + return "Z"; + } else if (c == Byte.TYPE) { + return "B"; + } else if (c == Character.TYPE) { + return "C"; + } else if (c == Double.TYPE) { + return "D"; + } else if (c == Float.TYPE) { + return "F"; + } else if (c == Integer.TYPE) { + return "I"; + } else if (c == Long.TYPE) { + return "J"; + } else if (c == Short.TYPE) { + return "S"; + } else if (c == Void.TYPE) { + return "V"; + } + throw new InternalError("Should have found primitive type"); + } else if (c.isArray()) { + return "[" + getClassName(c.getComponentType(), true); + } else { + if (addPrefixAndSuffixForNonPrimitiveTypes) { + return internalize("L" + c.getName() + ";"); + } else { + return internalize(c.getName()); + } + } + } + + private static String internalize(String className) { + return className.replace('.', '/'); + } + + protected void emitConstructor() { + // Generate code into fresh code buffer + ClassFileAssembler cb = new ClassFileAssembler(); + // 0 incoming arguments + cb.setMaxLocals(1); + cb.opc_aload_0(); + cb.opc_invokespecial(superCtorIdx, 0, 0); + cb.opc_return(); + + // Emit method + emitMethod(initIdx, cb.getMaxLocals(), cb, null, null); + } + + // The descriptor's index in the constant pool must be (1 + + // nameIdx). "numArgs" must indicate ALL arguments, including the + // implicit "this" argument; double and long arguments each count + // as 2 in this count. The code buffer must NOT contain the code + // length. The exception table may be null, but if non-null must + // NOT contain the exception table's length. The checked exception + // indices may be null. + protected void emitMethod(short nameIdx, + int numArgs, + ClassFileAssembler code, + ClassFileAssembler exceptionTable, + short[] checkedExceptionIndices) + { + int codeLen = code.getLength(); + int excLen = 0; + if (exceptionTable != null) { + excLen = exceptionTable.getLength(); + if ((excLen % 8) != 0) { + throw new IllegalArgumentException("Illegal exception table"); + } + } + int attrLen = 12 + codeLen + excLen; + excLen = excLen / 8; // No-op if no exception table + + asm.emitShort(ACC_PUBLIC); + asm.emitShort(nameIdx); + asm.emitShort(add(nameIdx, S1)); + if (checkedExceptionIndices == null) { + // Code attribute only + asm.emitShort(S1); + } else { + // Code and Exceptions attributes + asm.emitShort(S2); + } + // Code attribute + asm.emitShort(codeIdx); + asm.emitInt(attrLen); + asm.emitShort(code.getMaxStack()); + asm.emitShort((short) Math.max(numArgs, code.getMaxLocals())); + asm.emitInt(codeLen); + asm.append(code); + asm.emitShort((short) excLen); + if (exceptionTable != null) { + asm.append(exceptionTable); + } + asm.emitShort(S0); // No additional attributes for Code attribute + if (checkedExceptionIndices != null) { + // Exceptions attribute + asm.emitShort(exceptionsIdx); + asm.emitInt(2 + 2 * checkedExceptionIndices.length); + asm.emitShort((short) checkedExceptionIndices.length); + for (int i = 0; i < checkedExceptionIndices.length; i++) { + asm.emitShort(checkedExceptionIndices[i]); + } + } + } + + protected short indexForPrimitiveType(Class type) { + if (type == Boolean.TYPE) { + return booleanIdx; + } else if (type == Byte.TYPE) { + return byteIdx; + } else if (type == Character.TYPE) { + return characterIdx; + } else if (type == Double.TYPE) { + return doubleIdx; + } else if (type == Float.TYPE) { + return floatIdx; + } else if (type == Integer.TYPE) { + return integerIdx; + } else if (type == Long.TYPE) { + return longIdx; + } else if (type == Short.TYPE) { + return shortIdx; + } + throw new InternalError("Should have found primitive type"); + } + + protected short ctorIndexForPrimitiveType(Class type) { + if (type == Boolean.TYPE) { + return booleanCtorIdx; + } else if (type == Byte.TYPE) { + return byteCtorIdx; + } else if (type == Character.TYPE) { + return characterCtorIdx; + } else if (type == Double.TYPE) { + return doubleCtorIdx; + } else if (type == Float.TYPE) { + return floatCtorIdx; + } else if (type == Integer.TYPE) { + return integerCtorIdx; + } else if (type == Long.TYPE) { + return longCtorIdx; + } else if (type == Short.TYPE) { + return shortCtorIdx; + } + throw new InternalError("Should have found primitive type"); + } + + /** Returns true for widening or identity conversions for primitive + types only */ + protected static boolean canWidenTo(Class type, Class otherType) { + if (!type.isPrimitive()) { + return false; + } + + // Widening conversions (from JVM spec): + // byte to short, int, long, float, or double + // short to int, long, float, or double + // char to int, long, float, or double + // int to long, float, or double + // long to float or double + // float to double + + if (type == Boolean.TYPE) { + if (otherType == Boolean.TYPE) { + return true; + } + } else if (type == Byte.TYPE) { + if ( otherType == Byte.TYPE + || otherType == Short.TYPE + || otherType == Integer.TYPE + || otherType == Long.TYPE + || otherType == Float.TYPE + || otherType == Double.TYPE) { + return true; + } + } else if (type == Short.TYPE) { + if ( otherType == Short.TYPE + || otherType == Integer.TYPE + || otherType == Long.TYPE + || otherType == Float.TYPE + || otherType == Double.TYPE) { + return true; + } + } else if (type == Character.TYPE) { + if ( otherType == Character.TYPE + || otherType == Integer.TYPE + || otherType == Long.TYPE + || otherType == Float.TYPE + || otherType == Double.TYPE) { + return true; + } + } else if (type == Integer.TYPE) { + if ( otherType == Integer.TYPE + || otherType == Long.TYPE + || otherType == Float.TYPE + || otherType == Double.TYPE) { + return true; + } + } else if (type == Long.TYPE) { + if ( otherType == Long.TYPE + || otherType == Float.TYPE + || otherType == Double.TYPE) { + return true; + } + } else if (type == Float.TYPE) { + if ( otherType == Float.TYPE + || otherType == Double.TYPE) { + return true; + } + } else if (type == Double.TYPE) { + if (otherType == Double.TYPE) { + return true; + } + } + + return false; + } + + /** Emits the widening bytecode for the given primitive conversion + (or none if the identity conversion). Requires that a primitive + conversion exists; i.e., canWidenTo must have already been + called and returned true. */ + protected static void emitWideningBytecodeForPrimitiveConversion + (ClassFileAssembler cb, + Class fromType, + Class toType) + { + // Note that widening conversions for integral types (i.e., "b2s", + // "s2i") are no-ops since values on the Java stack are + // sign-extended. + + // Widening conversions (from JVM spec): + // byte to short, int, long, float, or double + // short to int, long, float, or double + // char to int, long, float, or double + // int to long, float, or double + // long to float or double + // float to double + + if ( fromType == Byte.TYPE + || fromType == Short.TYPE + || fromType == Character.TYPE + || fromType == Integer.TYPE) { + if (toType == Long.TYPE) { + cb.opc_i2l(); + } else if (toType == Float.TYPE) { + cb.opc_i2f(); + } else if (toType == Double.TYPE) { + cb.opc_i2d(); + } + } else if (fromType == Long.TYPE) { + if (toType == Float.TYPE) { + cb.opc_l2f(); + } else if (toType == Double.TYPE) { + cb.opc_l2d(); + } + } else if (fromType == Float.TYPE) { + if (toType == Double.TYPE) { + cb.opc_f2d(); + } + } + + // Otherwise, was identity or no-op conversion. Fall through. + } + + protected short unboxingMethodForPrimitiveType(Class primType) { + if (primType == Boolean.TYPE) { + return booleanUnboxIdx; + } else if (primType == Byte.TYPE) { + return byteUnboxIdx; + } else if (primType == Character.TYPE) { + return characterUnboxIdx; + } else if (primType == Short.TYPE) { + return shortUnboxIdx; + } else if (primType == Integer.TYPE) { + return integerUnboxIdx; + } else if (primType == Long.TYPE) { + return longUnboxIdx; + } else if (primType == Float.TYPE) { + return floatUnboxIdx; + } else if (primType == Double.TYPE) { + return doubleUnboxIdx; + } + throw new InternalError("Illegal primitive type " + primType.getName()); + } + + protected static final Class[] primitiveTypes = new Class[] { + Boolean.TYPE, + Byte.TYPE, + Character.TYPE, + Short.TYPE, + Integer.TYPE, + Long.TYPE, + Float.TYPE, + Double.TYPE + }; + + /** We don't consider "Void" to be a primitive type */ + protected static boolean isPrimitive(Class c) { + return (c.isPrimitive() && c != Void.TYPE); + } + + protected int typeSizeInStackSlots(Class c) { + if (c == Void.TYPE) { + return 0; + } + if (c == Long.TYPE || c == Double.TYPE) { + return 2; + } + return 1; + } + + private ClassFileAssembler illegalArgumentCodeBuffer; + protected ClassFileAssembler illegalArgumentCodeBuffer() { + if (illegalArgumentCodeBuffer == null) { + illegalArgumentCodeBuffer = new ClassFileAssembler(); + illegalArgumentCodeBuffer.opc_new(illegalArgumentClass); + illegalArgumentCodeBuffer.opc_dup(); + illegalArgumentCodeBuffer.opc_invokespecial(illegalArgumentCtorIdx, 0, 0); + illegalArgumentCodeBuffer.opc_athrow(); + } + + return illegalArgumentCodeBuffer; + } +} diff --git a/src/Java/shaded/reflectionfactory/BootstrapConstructorAccessorImpl.java b/src/Java/shaded/reflectionfactory/BootstrapConstructorAccessorImpl.java new file mode 100644 index 0000000000..000454b269 --- /dev/null +++ b/src/Java/shaded/reflectionfactory/BootstrapConstructorAccessorImpl.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package shaded.reflectionfactory; + +import java.lang.reflect.InvocationTargetException; + +import java.lang.reflect.Constructor; + +/** Uses Unsafe.allocateObject() to instantiate classes; only used for + bootstrapping. */ + +class BootstrapConstructorAccessorImpl extends ConstructorAccessorImpl { + private final Constructor constructor; + + BootstrapConstructorAccessorImpl(Constructor c) { + this.constructor = c; + } + + public Object newInstance(Object[] args) + throws IllegalArgumentException, InvocationTargetException + { + try { + return UnsafeFieldAccessorImpl.unsafe. + allocateInstance(constructor.getDeclaringClass()); + } catch (InstantiationException e) { + throw new InvocationTargetException(e); + } + } +} diff --git a/src/Java/shaded/reflectionfactory/ByteVector.java b/src/Java/shaded/reflectionfactory/ByteVector.java new file mode 100644 index 0000000000..edcad900e5 --- /dev/null +++ b/src/Java/shaded/reflectionfactory/ByteVector.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package shaded.reflectionfactory; + +/** A growable array of bytes. */ + +interface ByteVector { + public int getLength(); + public byte get(int index); + public void put(int index, byte value); + public void add(byte value); + public void trim(); + public byte[] getData(); +} diff --git a/src/Java/shaded/reflectionfactory/ByteVectorFactory.java b/src/Java/shaded/reflectionfactory/ByteVectorFactory.java new file mode 100644 index 0000000000..adfc5462c0 --- /dev/null +++ b/src/Java/shaded/reflectionfactory/ByteVectorFactory.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package shaded.reflectionfactory; + +class ByteVectorFactory { + static ByteVector create() { + return new ByteVectorImpl(); + } + + static ByteVector create(int sz) { + return new ByteVectorImpl(sz); + } +} diff --git a/src/Java/shaded/reflectionfactory/ByteVectorImpl.java b/src/Java/shaded/reflectionfactory/ByteVectorImpl.java new file mode 100644 index 0000000000..e9771fca15 --- /dev/null +++ b/src/Java/shaded/reflectionfactory/ByteVectorImpl.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package shaded.reflectionfactory; + +class ByteVectorImpl implements ByteVector { + private byte[] data; + private int pos; + + public ByteVectorImpl() { + this(100); + } + + public ByteVectorImpl(int sz) { + data = new byte[sz]; + pos = -1; + } + + public int getLength() { + return pos + 1; + } + + public byte get(int index) { + if (index >= data.length) { + resize(index); + pos = index; + } + return data[index]; + } + + public void put(int index, byte value) { + if (index >= data.length) { + resize(index); + pos = index; + } + data[index] = value; + } + + public void add(byte value) { + if (++pos >= data.length) { + resize(pos); + } + data[pos] = value; + } + + public void trim() { + if (pos != data.length - 1) { + byte[] newData = new byte[pos + 1]; + System.arraycopy(data, 0, newData, 0, pos + 1); + data = newData; + } + } + + public byte[] getData() { + return data; + } + + private void resize(int minSize) { + if (minSize <= 2 * data.length) { + minSize = 2 * data.length; + } + byte[] newData = new byte[minSize]; + System.arraycopy(data, 0, newData, 0, data.length); + data = newData; + } +} diff --git a/src/Java/shaded/reflectionfactory/CallerSensitive.java b/src/Java/shaded/reflectionfactory/CallerSensitive.java new file mode 100644 index 0000000000..4a52a3975e --- /dev/null +++ b/src/Java/shaded/reflectionfactory/CallerSensitive.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package shaded.reflectionfactory; + +import java.lang.annotation.*; +import static java.lang.annotation.ElementType.*; + +/** + * A method annotated @CallerSensitive is sensitive to its calling class, + * via {@link shaded.reflectionfactory.Reflection#getCallerClass Reflection.getCallerClass}, + * or via some equivalent. + * + * @author John R. Rose + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({METHOD}) +public @interface CallerSensitive { +} diff --git a/src/Java/shaded/reflectionfactory/ClassDefiner.java b/src/Java/shaded/reflectionfactory/ClassDefiner.java new file mode 100644 index 0000000000..2a6c0eb4c6 --- /dev/null +++ b/src/Java/shaded/reflectionfactory/ClassDefiner.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package shaded.reflectionfactory; + +import java.security.AccessController; +import java.security.PrivilegedAction; + +/** Utility class which assists in calling Unsafe.defineClass() by + creating a new class loader which delegates to the one needed in + order for proper resolution of the given bytecodes to occur. */ + +class ClassDefiner { + static final Unsafe unsafe = Unsafe.getUnsafe(); + + /**

We define generated code into a new class loader which + delegates to the defining loader of the target class. It is + necessary for the VM to be able to resolve references to the + target class from the generated bytecodes, which could not occur + if the generated code was loaded into the bootstrap class + loader.

+ +

There are two primary reasons for creating a new loader + instead of defining these bytecodes directly into the defining + loader of the target class: first, it avoids any possible + security risk of having these bytecodes in the same loader. + Second, it allows the generated bytecodes to be unloaded earlier + than would otherwise be possible, decreasing run-time + footprint.

+ */ + static Class defineClass(String name, byte[] bytes, int off, int len, + final ClassLoader parentClassLoader) + { + ClassLoader newLoader = AccessController.doPrivileged( + new PrivilegedAction() { + public ClassLoader run() { + return new DelegatingClassLoader(parentClassLoader); + } + }); + return unsafe.defineClass(name, bytes, off, len, newLoader, null); + } +} + + +// NOTE: this class's name and presence are known to the virtual +// machine as of the fix for 4474172. +class DelegatingClassLoader extends ClassLoader { + DelegatingClassLoader(ClassLoader parent) { + super(parent); + } +} diff --git a/src/Java/shaded/reflectionfactory/ClassFileAssembler.java b/src/Java/shaded/reflectionfactory/ClassFileAssembler.java new file mode 100644 index 0000000000..aa70dbc38a --- /dev/null +++ b/src/Java/shaded/reflectionfactory/ClassFileAssembler.java @@ -0,0 +1,671 @@ +/* + * Copyright (c) 2001, 2004, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package shaded.reflectionfactory; + +class ClassFileAssembler implements ClassFileConstants { + private ByteVector vec; + private short cpIdx = 0; + + public ClassFileAssembler() { + this(ByteVectorFactory.create()); + } + + public ClassFileAssembler(ByteVector vec) { + this.vec = vec; + } + + public ByteVector getData() { + return vec; + } + + /** Length in bytes */ + public short getLength() { + return (short) vec.getLength(); + } + + public void emitMagicAndVersion() { + emitInt(0xCAFEBABE); + emitShort((short) 0); + emitShort((short) 49); + } + + public void emitInt(int val) { + emitByte((byte) (val >> 24)); + emitByte((byte) ((val >> 16) & 0xFF)); + emitByte((byte) ((val >> 8) & 0xFF)); + emitByte((byte) (val & 0xFF)); + } + + public void emitShort(short val) { + emitByte((byte) ((val >> 8) & 0xFF)); + emitByte((byte) (val & 0xFF)); + } + + // Support for labels; package-private + void emitShort(short bci, short val) { + vec.put(bci, (byte) ((val >> 8) & 0xFF)); + vec.put(bci + 1, (byte) (val & 0xFF)); + } + + public void emitByte(byte val) { + vec.add(val); + } + + public void append(ClassFileAssembler asm) { + append(asm.vec); + } + + public void append(ByteVector vec) { + for (int i = 0; i < vec.getLength(); i++) { + emitByte(vec.get(i)); + } + } + + /** Keeps track of the current (one-based) constant pool index; + incremented after emitting one of the following constant pool + entries. Can fetch the current constant pool index for use in + later entries. Index points at the last valid constant pool + entry; initially invalid. It is illegal to fetch the constant + pool index before emitting at least one constant pool entry. */ + public short cpi() { + if (cpIdx == 0) { + throw new RuntimeException("Illegal use of ClassFileAssembler"); + } + return cpIdx; + } + + public void emitConstantPoolUTF8(String str) { + // NOTE: can not use str.getBytes("UTF-8") here because of + // bootstrapping issues with the character set converters. + byte[] bytes = UTF8.encode(str); + emitByte(CONSTANT_Utf8); + emitShort((short) bytes.length); + for (int i = 0; i < bytes.length; i++) { + emitByte(bytes[i]); + } + cpIdx++; + } + + public void emitConstantPoolClass(short index) { + emitByte(CONSTANT_Class); + emitShort(index); + cpIdx++; + } + + public void emitConstantPoolNameAndType(short nameIndex, short typeIndex) { + emitByte(CONSTANT_NameAndType); + emitShort(nameIndex); + emitShort(typeIndex); + cpIdx++; + } + + public void emitConstantPoolFieldref + (short classIndex, short nameAndTypeIndex) + { + emitByte(CONSTANT_Fieldref); + emitShort(classIndex); + emitShort(nameAndTypeIndex); + cpIdx++; + } + + public void emitConstantPoolMethodref + (short classIndex, short nameAndTypeIndex) + { + emitByte(CONSTANT_Methodref); + emitShort(classIndex); + emitShort(nameAndTypeIndex); + cpIdx++; + } + + public void emitConstantPoolInterfaceMethodref + (short classIndex, short nameAndTypeIndex) + { + emitByte(CONSTANT_InterfaceMethodref); + emitShort(classIndex); + emitShort(nameAndTypeIndex); + cpIdx++; + } + + public void emitConstantPoolString(short utf8Index) { + emitByte(CONSTANT_String); + emitShort(utf8Index); + cpIdx++; + } + + //---------------------------------------------------------------------- + // Opcodes. Keeps track of maximum stack and locals. Make a new + // assembler for each piece of assembled code, then append the + // result to the previous assembler's class file. + // + + private int stack = 0; + private int maxStack = 0; + private int maxLocals = 0; + + private void incStack() { + setStack(stack + 1); + } + + private void decStack() { + --stack; + } + + public short getMaxStack() { + return (short) maxStack; + } + + public short getMaxLocals() { + return (short) maxLocals; + } + + /** It's necessary to be able to specify the number of arguments at + the beginning of the method (which translates to the initial + value of max locals) */ + public void setMaxLocals(int maxLocals) { + this.maxLocals = maxLocals; + } + + /** Needed to do flow control. Returns current stack depth. */ + public int getStack() { + return stack; + } + + /** Needed to do flow control. */ + public void setStack(int value) { + stack = value; + if (stack > maxStack) { + maxStack = stack; + } + } + + /////////////// + // Constants // + /////////////// + + public void opc_aconst_null() { + emitByte(opc_aconst_null); + incStack(); + } + + public void opc_sipush(short constant) { + emitByte(opc_sipush); + emitShort(constant); + incStack(); + } + + public void opc_ldc(byte cpIdx) { + emitByte(opc_ldc); + emitByte(cpIdx); + incStack(); + } + + ///////////////////////////////////// + // Local variable loads and stores // + ///////////////////////////////////// + + public void opc_iload_0() { + emitByte(opc_iload_0); + if (maxLocals < 1) maxLocals = 1; + incStack(); + } + + public void opc_iload_1() { + emitByte(opc_iload_1); + if (maxLocals < 2) maxLocals = 2; + incStack(); + } + + public void opc_iload_2() { + emitByte(opc_iload_2); + if (maxLocals < 3) maxLocals = 3; + incStack(); + } + + public void opc_iload_3() { + emitByte(opc_iload_3); + if (maxLocals < 4) maxLocals = 4; + incStack(); + } + + public void opc_lload_0() { + emitByte(opc_lload_0); + if (maxLocals < 2) maxLocals = 2; + incStack(); + incStack(); + } + + public void opc_lload_1() { + emitByte(opc_lload_1); + if (maxLocals < 3) maxLocals = 3; + incStack(); + incStack(); + } + + public void opc_lload_2() { + emitByte(opc_lload_2); + if (maxLocals < 4) maxLocals = 4; + incStack(); + incStack(); + } + + public void opc_lload_3() { + emitByte(opc_lload_3); + if (maxLocals < 5) maxLocals = 5; + incStack(); + incStack(); + } + + public void opc_fload_0() { + emitByte(opc_fload_0); + if (maxLocals < 1) maxLocals = 1; + incStack(); + } + + public void opc_fload_1() { + emitByte(opc_fload_1); + if (maxLocals < 2) maxLocals = 2; + incStack(); + } + + public void opc_fload_2() { + emitByte(opc_fload_2); + if (maxLocals < 3) maxLocals = 3; + incStack(); + } + + public void opc_fload_3() { + emitByte(opc_fload_3); + if (maxLocals < 4) maxLocals = 4; + incStack(); + } + + public void opc_dload_0() { + emitByte(opc_dload_0); + if (maxLocals < 2) maxLocals = 2; + incStack(); + incStack(); + } + + public void opc_dload_1() { + emitByte(opc_dload_1); + if (maxLocals < 3) maxLocals = 3; + incStack(); + incStack(); + } + + public void opc_dload_2() { + emitByte(opc_dload_2); + if (maxLocals < 4) maxLocals = 4; + incStack(); + incStack(); + } + + public void opc_dload_3() { + emitByte(opc_dload_3); + if (maxLocals < 5) maxLocals = 5; + incStack(); + incStack(); + } + + public void opc_aload_0() { + emitByte(opc_aload_0); + if (maxLocals < 1) maxLocals = 1; + incStack(); + } + + public void opc_aload_1() { + emitByte(opc_aload_1); + if (maxLocals < 2) maxLocals = 2; + incStack(); + } + + public void opc_aload_2() { + emitByte(opc_aload_2); + if (maxLocals < 3) maxLocals = 3; + incStack(); + } + + public void opc_aload_3() { + emitByte(opc_aload_3); + if (maxLocals < 4) maxLocals = 4; + incStack(); + } + + public void opc_aaload() { + emitByte(opc_aaload); + decStack(); + } + + public void opc_astore_0() { + emitByte(opc_astore_0); + if (maxLocals < 1) maxLocals = 1; + decStack(); + } + + public void opc_astore_1() { + emitByte(opc_astore_1); + if (maxLocals < 2) maxLocals = 2; + decStack(); + } + + public void opc_astore_2() { + emitByte(opc_astore_2); + if (maxLocals < 3) maxLocals = 3; + decStack(); + } + + public void opc_astore_3() { + emitByte(opc_astore_3); + if (maxLocals < 4) maxLocals = 4; + decStack(); + } + + //////////////////////// + // Stack manipulation // + //////////////////////// + + public void opc_pop() { + emitByte(opc_pop); + decStack(); + } + + public void opc_dup() { + emitByte(opc_dup); + incStack(); + } + + public void opc_dup_x1() { + emitByte(opc_dup_x1); + incStack(); + } + + public void opc_swap() { + emitByte(opc_swap); + } + + /////////////////////////////// + // Widening conversions only // + /////////////////////////////// + + public void opc_i2l() { + emitByte(opc_i2l); + } + + public void opc_i2f() { + emitByte(opc_i2f); + } + + public void opc_i2d() { + emitByte(opc_i2d); + } + + public void opc_l2f() { + emitByte(opc_l2f); + } + + public void opc_l2d() { + emitByte(opc_l2d); + } + + public void opc_f2d() { + emitByte(opc_f2d); + } + + ////////////////// + // Control flow // + ////////////////// + + public void opc_ifeq(short bciOffset) { + emitByte(opc_ifeq); + emitShort(bciOffset); + decStack(); + } + + /** Control flow with forward-reference BCI. Stack assumes + straight-through control flow. */ + public void opc_ifeq(Label l) { + short instrBCI = getLength(); + emitByte(opc_ifeq); + l.add(this, instrBCI, getLength(), getStack() - 1); + emitShort((short) -1); // Must be patched later + } + + public void opc_if_icmpeq(short bciOffset) { + emitByte(opc_if_icmpeq); + emitShort(bciOffset); + setStack(getStack() - 2); + } + + /** Control flow with forward-reference BCI. Stack assumes straight + control flow. */ + public void opc_if_icmpeq(Label l) { + short instrBCI = getLength(); + emitByte(opc_if_icmpeq); + l.add(this, instrBCI, getLength(), getStack() - 2); + emitShort((short) -1); // Must be patched later + } + + public void opc_goto(short bciOffset) { + emitByte(opc_goto); + emitShort(bciOffset); + } + + /** Control flow with forward-reference BCI. Stack assumes straight + control flow. */ + public void opc_goto(Label l) { + short instrBCI = getLength(); + emitByte(opc_goto); + l.add(this, instrBCI, getLength(), getStack()); + emitShort((short) -1); // Must be patched later + } + + public void opc_ifnull(short bciOffset) { + emitByte(opc_ifnull); + emitShort(bciOffset); + decStack(); + } + + /** Control flow with forward-reference BCI. Stack assumes straight + control flow. */ + public void opc_ifnull(Label l) { + short instrBCI = getLength(); + emitByte(opc_ifnull); + l.add(this, instrBCI, getLength(), getStack() - 1); + emitShort((short) -1); // Must be patched later + decStack(); + } + + public void opc_ifnonnull(short bciOffset) { + emitByte(opc_ifnonnull); + emitShort(bciOffset); + decStack(); + } + + /** Control flow with forward-reference BCI. Stack assumes straight + control flow. */ + public void opc_ifnonnull(Label l) { + short instrBCI = getLength(); + emitByte(opc_ifnonnull); + l.add(this, instrBCI, getLength(), getStack() - 1); + emitShort((short) -1); // Must be patched later + decStack(); + } + + ///////////////////////// + // Return instructions // + ///////////////////////// + + public void opc_ireturn() { + emitByte(opc_ireturn); + setStack(0); + } + + public void opc_lreturn() { + emitByte(opc_lreturn); + setStack(0); + } + + public void opc_freturn() { + emitByte(opc_freturn); + setStack(0); + } + + public void opc_dreturn() { + emitByte(opc_dreturn); + setStack(0); + } + + public void opc_areturn() { + emitByte(opc_areturn); + setStack(0); + } + + public void opc_return() { + emitByte(opc_return); + setStack(0); + } + + ////////////////////// + // Field operations // + ////////////////////// + + public void opc_getstatic(short fieldIndex, int fieldSizeInStackSlots) { + emitByte(opc_getstatic); + emitShort(fieldIndex); + setStack(getStack() + fieldSizeInStackSlots); + } + + public void opc_putstatic(short fieldIndex, int fieldSizeInStackSlots) { + emitByte(opc_putstatic); + emitShort(fieldIndex); + setStack(getStack() - fieldSizeInStackSlots); + } + + public void opc_getfield(short fieldIndex, int fieldSizeInStackSlots) { + emitByte(opc_getfield); + emitShort(fieldIndex); + setStack(getStack() + fieldSizeInStackSlots - 1); + } + + public void opc_putfield(short fieldIndex, int fieldSizeInStackSlots) { + emitByte(opc_putfield); + emitShort(fieldIndex); +