diff --git a/src/java.base/share/classes/java/lang/reflect/ProxyGenerator.java b/src/java.base/share/classes/java/lang/reflect/ProxyGenerator.java index 08f971b0902f7..45355840b4e00 100644 --- a/src/java.base/share/classes/java/lang/reflect/ProxyGenerator.java +++ b/src/java.base/share/classes/java/lang/reflect/ProxyGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2024, 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 @@ -25,28 +25,27 @@ package java.lang.reflect; +import java.io.IOException; import java.lang.classfile.*; -import java.lang.classfile.constantpool.*; import java.lang.classfile.attribute.ExceptionsAttribute; -import sun.security.action.GetBooleanAction; - -import java.io.IOException; -import static java.lang.classfile.ClassFile.*; -import java.lang.classfile.attribute.StackMapFrameInfo; -import java.lang.classfile.attribute.StackMapTableAttribute; +import java.lang.classfile.constantpool.*; import java.lang.constant.ClassDesc; -import static java.lang.constant.ConstantDescs.*; import java.lang.constant.MethodTypeDesc; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; -import java.util.Arrays; import java.util.LinkedHashMap; import java.util.List; import java.util.ListIterator; import java.util.Map; import java.util.Objects; -import java.util.function.IntFunction; +import jdk.internal.constant.ConstantUtils; +import jdk.internal.constant.MethodTypeDescImpl; +import jdk.internal.constant.ReferenceClassDescImpl; +import sun.security.action.GetBooleanAction; + +import static java.lang.classfile.ClassFile.*; +import static java.lang.constant.ConstantDescs.*; /** * ProxyGenerator contains the code to generate a dynamic proxy class @@ -58,30 +57,32 @@ final class ProxyGenerator { private static final ClassDesc - CD_ClassLoader = ClassDesc.ofInternalName("java/lang/ClassLoader"), - CD_ClassNotFoundException = ClassDesc.ofInternalName("java/lang/ClassNotFoundException"), - CD_IllegalAccessException = ClassDesc.ofInternalName("java/lang/IllegalAccessException"), - CD_InvocationHandler = ClassDesc.ofInternalName("java/lang/reflect/InvocationHandler"), - CD_Method = ClassDesc.ofInternalName("java/lang/reflect/Method"), - CD_NoClassDefFoundError = ClassDesc.ofInternalName("java/lang/NoClassDefFoundError"), - CD_NoSuchMethodError = ClassDesc.ofInternalName("java/lang/NoSuchMethodError"), - CD_NoSuchMethodException = ClassDesc.ofInternalName("java/lang/NoSuchMethodException"), - CD_Proxy = ClassDesc.ofInternalName("java/lang/reflect/Proxy"), - CD_UndeclaredThrowableException = ClassDesc.ofInternalName("java/lang/reflect/UndeclaredThrowableException"); + CD_Class_array = ReferenceClassDescImpl.ofValidated("[Ljava/lang/Class;"), + CD_ClassLoader = ReferenceClassDescImpl.ofValidated("Ljava/lang/ClassLoader;"), + CD_ClassNotFoundException = ReferenceClassDescImpl.ofValidated("Ljava/lang/ClassNotFoundException;"), + CD_IllegalAccessException = ReferenceClassDescImpl.ofValidated("Ljava/lang/IllegalAccessException;"), + CD_InvocationHandler = ReferenceClassDescImpl.ofValidated("Ljava/lang/reflect/InvocationHandler;"), + CD_Method = ReferenceClassDescImpl.ofValidated("Ljava/lang/reflect/Method;"), + CD_NoClassDefFoundError = ReferenceClassDescImpl.ofValidated("Ljava/lang/NoClassDefFoundError;"), + CD_NoSuchMethodError = ReferenceClassDescImpl.ofValidated("Ljava/lang/NoSuchMethodError;"), + CD_NoSuchMethodException = ReferenceClassDescImpl.ofValidated("Ljava/lang/NoSuchMethodException;"), + CD_Object_array = ReferenceClassDescImpl.ofValidated("[Ljava/lang/Object;"), + CD_Proxy = ReferenceClassDescImpl.ofValidated("Ljava/lang/reflect/Proxy;"), + CD_UndeclaredThrowableException = ReferenceClassDescImpl.ofValidated("Ljava/lang/reflect/UndeclaredThrowableException;"); private static final MethodTypeDesc - MTD_boolean = MethodTypeDesc.of(CD_boolean), - MTD_void_InvocationHandler = MethodTypeDesc.of(CD_void, CD_InvocationHandler), - MTD_void_String = MethodTypeDesc.of(CD_void, CD_String), - MTD_void_Throwable = MethodTypeDesc.of(CD_void, CD_Throwable), - MTD_Class = MethodTypeDesc.of(CD_Class), - MTD_Class_String_boolean_ClassLoader = MethodTypeDesc.of(CD_Class, CD_String, CD_boolean, CD_ClassLoader), - MTD_ClassLoader = MethodTypeDesc.of(CD_ClassLoader), - MTD_MethodHandles$Lookup = MethodTypeDesc.of(CD_MethodHandles_Lookup), - MTD_MethodHandles$Lookup_MethodHandles$Lookup = MethodTypeDesc.of(CD_MethodHandles_Lookup, CD_MethodHandles_Lookup), - MTD_Method_String_ClassArray = MethodTypeDesc.of(CD_Method, CD_String, CD_Class.arrayType()), - MTD_Object_Object_Method_ObjectArray = MethodTypeDesc.of(CD_Object, CD_Object, CD_Method, CD_Object.arrayType()), - MTD_String = MethodTypeDesc.of(CD_String); + MTD_boolean = MethodTypeDescImpl.ofValidated(CD_boolean, ConstantUtils.EMPTY_CLASSDESC), + MTD_void_InvocationHandler = MethodTypeDescImpl.ofValidated(CD_void, new ClassDesc[]{CD_InvocationHandler}), + MTD_void_String = MethodTypeDescImpl.ofValidated(CD_void, new ClassDesc[]{CD_String}), + MTD_void_Throwable = MethodTypeDescImpl.ofValidated(CD_void, new ClassDesc[]{CD_Throwable}), + MTD_Class = MethodTypeDescImpl.ofValidated(CD_Class, ConstantUtils.EMPTY_CLASSDESC), + MTD_Class_String_boolean_ClassLoader = MethodTypeDescImpl.ofValidated(CD_Class, new ClassDesc[]{CD_String, CD_boolean, CD_ClassLoader}), + MTD_ClassLoader = MethodTypeDescImpl.ofValidated(CD_ClassLoader, ConstantUtils.EMPTY_CLASSDESC), + MTD_MethodHandles$Lookup = MethodTypeDescImpl.ofValidated(CD_MethodHandles_Lookup, ConstantUtils.EMPTY_CLASSDESC), + MTD_MethodHandles$Lookup_MethodHandles$Lookup = MethodTypeDescImpl.ofValidated(CD_MethodHandles_Lookup, new ClassDesc[]{CD_MethodHandles_Lookup}), + MTD_Method_String_ClassArray = MethodTypeDescImpl.ofValidated(CD_Method, new ClassDesc[]{CD_String, CD_Class_array}), + MTD_Object_Object_Method_ObjectArray = MethodTypeDescImpl.ofValidated(CD_Object, new ClassDesc[]{CD_Object, CD_Method, CD_Object_array}), + MTD_String = MethodTypeDescImpl.ofValidated(CD_String, ConstantUtils.EMPTY_CLASSDESC); private static final String NAME_LOOKUP_ACCESSOR = "proxyClassLookup"; @@ -90,126 +91,30 @@ final class ProxyGenerator { /** * name of field for storing a proxy instance's invocation handler */ - private static final String handlerFieldName = "h"; + private static final String NAME_HANDLER_FIELD = "h"; /** * debugging flag for saving generated class files */ @SuppressWarnings("removal") - private static final boolean saveGeneratedFiles = + private static final boolean SAVE_GENERATED_FILES = java.security.AccessController.doPrivileged( new GetBooleanAction( "jdk.proxy.ProxyGenerator.saveGeneratedFiles")); /* Preloaded ProxyMethod objects for methods in java.lang.Object */ - private static final ProxyMethod hashCodeMethod; - private static final ProxyMethod equalsMethod; - private static final ProxyMethod toStringMethod; - - private static final ClassModel TEMPLATE; - - private static final ClassEntry CE_Class; - private static final ClassEntry CE_ClassNotFoundException; - private static final ClassEntry CE_NoClassDefFoundError; - private static final ClassEntry CE_NoSuchMethodError; - private static final ClassEntry CE_NoSuchMethodException; - private static final ClassEntry CE_Object; - private static final ClassEntry CE_Throwable; - private static final ClassEntry CE_UndeclaredThrowableException; - - private static final FieldRefEntry FRE_Proxy_h; - - private static final InterfaceMethodRefEntry IMRE_InvocationHandler_invoke; - - private static final MethodRefEntry MRE_Class_forName; - private static final MethodRefEntry MRE_Class_getClassLoader; - private static final MethodRefEntry MRE_Class_getMethod; - private static final MethodRefEntry MRE_NoClassDefFoundError_init; - private static final MethodRefEntry MRE_NoSuchMethodError_init; - private static final MethodRefEntry MRE_Throwable_getMessage; - private static final MethodRefEntry MRE_UndeclaredThrowableException_init; - - private static final Utf8Entry UE_Method; - - private static final List THROWABLE_STACK; - - @SuppressWarnings("unchecked") - private static T entryByIndex(int index) { - return (T) TEMPLATE.constantPool().entryByIndex(index); - } + private static final ProxyMethod HASH_CODE_METHOD; + private static final ProxyMethod EQUALS_METHOD; + private static final ProxyMethod TO_STRING_METHOD; static { - // static template ClassModel holds pre-defined constant pool entries - // proxy transformed from the template shares the template constant pool - // each direct use of the template pool entry is significantly faster - var cc = ClassFile.of(); - var ei = new int[21]; - TEMPLATE = cc.parse(cc.build(CD_Proxy, clb -> { - clb.withSuperclass(CD_Proxy); - generateConstructor(clb); - generateLookupAccessor(clb); - var cp = clb.constantPool(); - - ei[0] = cp.classEntry(CD_Class).index(); - ei[1] = cp.classEntry(CD_ClassNotFoundException).index(); - ei[2] = cp.classEntry(CD_NoClassDefFoundError).index(); - ei[3] = cp.classEntry(CD_NoSuchMethodError).index(); - ei[4] = cp.classEntry(CD_NoSuchMethodException).index(); - ei[5] = cp.classEntry(CD_Object).index(); - ei[6] = cp.classEntry(CD_Throwable).index(); - ei[7] = cp.classEntry(CD_UndeclaredThrowableException).index(); - - ei[8] = cp.fieldRefEntry(CD_Proxy, handlerFieldName, CD_InvocationHandler).index(); - - ei[9] = cp.interfaceMethodRefEntry(CD_InvocationHandler, "invoke", MTD_Object_Object_Method_ObjectArray).index(); - - ei[10] = cp.methodRefEntry(CD_Class, "forName", MTD_Class_String_boolean_ClassLoader).index(); - ei[11] = cp.methodRefEntry(CD_Class, "getClassLoader", MTD_ClassLoader).index(); - ei[12] = cp.methodRefEntry(CD_Class, "getMethod", MTD_Method_String_ClassArray).index(); - ei[13] = cp.methodRefEntry(CD_NoClassDefFoundError, INIT_NAME, MTD_void_String).index(); - ei[14] = cp.methodRefEntry(CD_NoSuchMethodError, INIT_NAME, MTD_void_String).index(); - ei[15] = cp.methodRefEntry(CD_Throwable, "getMessage", MTD_String).index(); - ei[16] = cp.methodRefEntry(CD_UndeclaredThrowableException, INIT_NAME, MTD_void_Throwable).index(); - - ei[17] = cp.utf8Entry(CD_Method).index(); - - ei[18] = cp.utf8Entry("m0").index(); - ei[19] = cp.utf8Entry("m1").index(); - ei[20] = cp.utf8Entry("m2").index(); - })); - - CE_Class = entryByIndex(ei[0]); - CE_ClassNotFoundException = entryByIndex(ei[1]); - CE_NoClassDefFoundError = entryByIndex(ei[2]); - CE_NoSuchMethodError = entryByIndex(ei[3]); - CE_NoSuchMethodException = entryByIndex(ei[4]); - CE_Object = entryByIndex(ei[5]); - CE_Throwable = entryByIndex(ei[6]); - CE_UndeclaredThrowableException = entryByIndex(ei[7]); - - FRE_Proxy_h = entryByIndex(ei[8]); - - IMRE_InvocationHandler_invoke = entryByIndex(ei[9]); - - MRE_Class_forName = entryByIndex(ei[10]); - MRE_Class_getClassLoader = entryByIndex(ei[11]); - MRE_Class_getMethod = entryByIndex(ei[12]); - MRE_NoClassDefFoundError_init = entryByIndex(ei[13]); - MRE_NoSuchMethodError_init = entryByIndex(ei[14]); - MRE_Throwable_getMessage = entryByIndex(ei[15]); - MRE_UndeclaredThrowableException_init = entryByIndex(ei[16]); - - UE_Method = entryByIndex(ei[17]); - try { - hashCodeMethod = new ProxyMethod(Object.class.getMethod("hashCode"), entryByIndex(ei[18])); - equalsMethod = new ProxyMethod(Object.class.getMethod("equals", Object.class), entryByIndex(ei[19])); - toStringMethod = new ProxyMethod(Object.class.getMethod("toString"), entryByIndex(ei[20])); + HASH_CODE_METHOD = new ProxyMethod(Object.class.getMethod("hashCode"), "m0"); + EQUALS_METHOD = new ProxyMethod(Object.class.getMethod("equals", Object.class), "m1"); + TO_STRING_METHOD = new ProxyMethod(Object.class.getMethod("toString"), "m2"); } catch (NoSuchMethodException e) { throw new NoSuchMethodError(e.getMessage()); } - - THROWABLE_STACK = List.of(StackMapFrameInfo.ObjectVerificationTypeInfo.of(CE_Throwable)); } /** @@ -256,11 +161,10 @@ private static T entryByIndex(int index) { private ProxyGenerator(ClassLoader loader, String className, List> interfaces, int accessFlags) { this.classfileContext = ClassFile.of( - ClassFile.StackMapsOption.DROP_STACK_MAPS, ClassFile.ClassHierarchyResolverOption.of( ClassHierarchyResolver.ofClassLoading(loader).cached())); - this.cp = ConstantPoolBuilder.of(TEMPLATE); - this.classEntry = cp.classEntry(ClassDesc.of(className)); + this.cp = ConstantPoolBuilder.of(); + this.classEntry = cp.classEntry(ReferenceClassDescImpl.ofValidatedBinaryName(className)); this.interfaces = interfaces; this.accessFlags = accessFlags; } @@ -281,7 +185,7 @@ static byte[] generateProxyClass(ClassLoader loader, ProxyGenerator gen = new ProxyGenerator(loader, name, interfaces, accessFlags); final byte[] classFile = gen.generateClassFile(); - if (saveGeneratedFiles) { + if (SAVE_GENERATED_FILES) { java.security.AccessController.doPrivileged( new java.security.PrivilegedAction() { public Void run() { @@ -312,10 +216,10 @@ public Void run() { * {@return the entries of the given type} * @param types the {@code Class} objects, not primitive types nor array types */ - private static ClassEntry[] toClassEntries(ConstantPoolBuilder cp, List> types) { - var ces = new ClassEntry[types.size()]; - for (int i = 0; i < ces.length; i++) - ces[i] = cp.classEntry(cp.utf8Entry(types.get(i).getName().replace('.', '/'))); + private static List toClassEntries(ConstantPoolBuilder cp, List> types) { + var ces = new ArrayList(types.size()); + for (var t : types) + ces.add(cp.classEntry(ReferenceClassDescImpl.ofValidatedBinaryName(t.getName()))); return ces; } @@ -529,9 +433,9 @@ private byte[] generateClassFile() { * java.lang.Object take precedence over duplicate methods in the * proxy interfaces. */ - addProxyMethod(hashCodeMethod); - addProxyMethod(equalsMethod); - addProxyMethod(toStringMethod); + addProxyMethod(HASH_CODE_METHOD); + addProxyMethod(EQUALS_METHOD); + addProxyMethod(TO_STRING_METHOD); /* * Accumulate all of the methods from the proxy interfaces. @@ -539,7 +443,7 @@ private byte[] generateClassFile() { for (Class intf : interfaces) { for (Method m : intf.getMethods()) { if (!Modifier.isStatic(m.getModifiers())) { - addProxyMethod(m, intf, cp); + addProxyMethod(m, intf); } } } @@ -553,14 +457,15 @@ private byte[] generateClassFile() { } return classfileContext.build(classEntry, cp, clb -> { - TEMPLATE.forEach(clb); + clb.withSuperclass(CD_Proxy); clb.withFlags(accessFlags); clb.withInterfaces(toClassEntries(cp, interfaces)); + generateConstructor(clb); for (List sigmethods : proxyMethods.values()) { for (ProxyMethod pm : sigmethods) { // add static field for the Method object - clb.withField(pm.methodFieldName, UE_Method, ACC_PRIVATE | ACC_STATIC | ACC_FINAL); + clb.withField(pm.methodFieldName, CD_Method, ACC_PRIVATE | ACC_STATIC | ACC_FINAL); // Generate code for proxy method pm.generateMethod(clb, classEntry); @@ -568,6 +473,7 @@ private byte[] generateClassFile() { } generateStaticInitializer(clb); + generateLookupAccessor(clb); }); } @@ -584,7 +490,7 @@ private byte[] generateClassFile() { * passed to the invocation handler's "invoke" method for a given * set of duplicate methods. */ - private void addProxyMethod(Method m, Class fromClass, ConstantPoolBuilder cp) { + private void addProxyMethod(Method m, Class fromClass) { Class returnType = m.getReturnType(); Class[] exceptionTypes = m.getSharedExceptionTypes(); @@ -609,8 +515,7 @@ private void addProxyMethod(Method m, Class fromClass, ConstantPoolBuilder cp } } sigmethods.add(new ProxyMethod(m, sig, m.getSharedParameterTypes(), returnType, - exceptionTypes, fromClass, - cp.utf8Entry("m" + proxyMethodCount++))); + exceptionTypes, fromClass, "m" + proxyMethodCount++)); } /** @@ -644,34 +549,32 @@ private void generateStaticInitializer(ClassBuilder clb) { // Put ClassLoader at local variable index 0, used by // Class.forName(String, boolean, ClassLoader) calls cob.ldc(classEntry) - .invokevirtual(MRE_Class_getClassLoader) + .invokevirtual(CD_Class, "getClassLoader", MTD_ClassLoader) .astore(0); - var ts = cob.newBoundLabel(); + Label ts = cob.newBoundLabel(); for (List sigmethods : proxyMethods.values()) { for (ProxyMethod pm : sigmethods) { pm.codeFieldInitialization(cob, classEntry); } } cob.return_(); - var c1 = cob.newBoundLabel(); - cob.exceptionCatch(ts, c1, c1, CE_NoSuchMethodException) - .new_(CE_NoSuchMethodError) + Label c1 = cob.newBoundLabel(); + MethodRefEntry tgm = cp.methodRefEntry(CD_Throwable, "getMessage", MTD_String); + cob.exceptionCatch(ts, c1, c1, CD_NoSuchMethodException) + .new_(CD_NoSuchMethodError) .dup_x1() .swap() - .invokevirtual(MRE_Throwable_getMessage) - .invokespecial(MRE_NoSuchMethodError_init) + .invokevirtual(tgm) + .invokespecial(CD_NoSuchMethodError, INIT_NAME, MTD_void_String) .athrow(); - var c2 = cob.newBoundLabel(); - cob.exceptionCatch(ts, c1, c2, CE_ClassNotFoundException) - .new_(CE_NoClassDefFoundError) + Label c2 = cob.newBoundLabel(); + cob.exceptionCatch(ts, c1, c2, CD_ClassNotFoundException) + .new_(CD_NoClassDefFoundError) .dup_x1() .swap() - .invokevirtual(MRE_Throwable_getMessage) - .invokespecial(MRE_NoClassDefFoundError_init) - .athrow() - .with(StackMapTableAttribute.of(List.of( - StackMapFrameInfo.of(c1, List.of(), THROWABLE_STACK), - StackMapFrameInfo.of(c2, List.of(), THROWABLE_STACK)))); + .invokevirtual(tgm) + .invokespecial(CD_NoClassDefFoundError, INIT_NAME, MTD_void_String) + .athrow(); }); } @@ -716,12 +619,12 @@ private static class ProxyMethod { private final Class fromClass; private final Class[] parameterTypes; private final Class returnType; - private final Utf8Entry methodFieldName; + private final String methodFieldName; private Class[] exceptionTypes; private ProxyMethod(Method method, String sig, Class[] parameterTypes, Class returnType, Class[] exceptionTypes, - Class fromClass, Utf8Entry methodFieldName) { + Class fromClass, String methodFieldName) { this.method = method; this.shortSignature = sig; this.parameterTypes = parameterTypes; @@ -737,7 +640,7 @@ private ProxyMethod(Method method, String sig, Class[] parameterTypes, * @param method The method for which to create a proxy * @param methodFieldName the fieldName to generate */ - private ProxyMethod(Method method, Utf8Entry methodFieldName) { + private ProxyMethod(Method method, String methodFieldName) { this(method, method.toShortSignature(), method.getSharedParameterTypes(), method.getReturnType(), method.getSharedExceptionTypes(), method.getDeclaringClass(), methodFieldName); @@ -746,10 +649,13 @@ private ProxyMethod(Method method, Utf8Entry methodFieldName) { /** * Generate this method, including the code and exception table entry. */ - private void generateMethod(ClassBuilder clb, ClassEntry className) { + private void generateMethod(ClassBuilder clb, ClassEntry classEntry) { var cp = clb.constantPool(); - MethodTypeDesc desc = MethodTypeDesc.of(toClassDesc(returnType), - Arrays.stream(parameterTypes).map(ProxyGenerator::toClassDesc).toArray(ClassDesc[]::new)); + var pTypes = new ClassDesc[parameterTypes.length]; + for (int i = 0; i < pTypes.length; i++) { + pTypes[i] = toClassDesc(parameterTypes[i]); + } + MethodTypeDesc desc = MethodTypeDescImpl.ofTrusted(toClassDesc(returnType), pTypes); int accessFlags = (method.isVarArgs()) ? ACC_VARARGS | ACC_PUBLIC | ACC_FINAL : ACC_PUBLIC | ACC_FINAL; var catchList = computeUniqueCatchList(exceptionTypes); @@ -757,14 +663,14 @@ private void generateMethod(ClassBuilder clb, ClassEntry className) { mb.with(ExceptionsAttribute.of(toClassEntries(cp, List.of(exceptionTypes)))) .withCode(cob -> { cob.aload(cob.receiverSlot()) - .getfield(FRE_Proxy_h) + .getfield(CD_Proxy, NAME_HANDLER_FIELD, CD_InvocationHandler) .aload(cob.receiverSlot()) - .getstatic(cp.fieldRefEntry(className, cp.nameAndTypeEntry(methodFieldName, UE_Method))); + .getstatic(cp.fieldRefEntry(classEntry, cp.nameAndTypeEntry(methodFieldName, CD_Method))); if (parameterTypes.length > 0) { // Create an array and fill with the parameters converting primitives to wrappers cob.loadConstant(parameterTypes.length) - .anewarray(CE_Object); + .anewarray(CD_Object); for (int i = 0; i < parameterTypes.length; i++) { cob.dup() .loadConstant(i); @@ -775,7 +681,7 @@ private void generateMethod(ClassBuilder clb, ClassEntry className) { cob.aconst_null(); } - cob.invokeinterface(IMRE_InvocationHandler_invoke); + cob.invokeinterface(CD_InvocationHandler, "invoke", MTD_Object_Object_Method_ObjectArray); if (returnType == void.class) { cob.pop() @@ -791,14 +697,11 @@ private void generateMethod(ClassBuilder clb, ClassEntry className) { cob.athrow(); // just rethrow the exception var c2 = cob.newBoundLabel(); cob.exceptionCatchAll(cob.startLabel(), c1, c2) - .new_(CE_UndeclaredThrowableException) + .new_(CD_UndeclaredThrowableException) .dup_x1() .swap() - .invokespecial(MRE_UndeclaredThrowableException_init) - .athrow() - .with(StackMapTableAttribute.of(List.of( - StackMapFrameInfo.of(c1, List.of(), THROWABLE_STACK), - StackMapFrameInfo.of(c2, List.of(), THROWABLE_STACK)))); + .invokespecial(CD_UndeclaredThrowableException, INIT_NAME, MTD_void_Throwable) + .athrow(); } })); } @@ -813,7 +716,7 @@ private void codeWrapArgument(CodeBuilder cob, Class type, int slot) { if (type.isPrimitive()) { cob.loadLocal(TypeKind.from(type).asLoadable(), slot); PrimitiveTypeInfo prim = PrimitiveTypeInfo.get(type); - cob.invokestatic(prim.wrapperMethodRef); + cob.invokestatic(prim.wrapperMethodRef(cob.constantPool())); } else { cob.aload(slot); } @@ -829,7 +732,7 @@ private void codeUnwrapReturnValue(CodeBuilder cob, Class type) { PrimitiveTypeInfo prim = PrimitiveTypeInfo.get(type); cob.checkcast(prim.wrapperClass) - .invokevirtual(prim.unwrapMethodRef) + .invokevirtual(prim.unwrapMethodRef(cob.constantPool())) .return_(TypeKind.from(type).asLoadable()); } else { cob.checkcast(toClassDesc(type)) @@ -848,7 +751,7 @@ private void codeFieldInitialization(CodeBuilder cob, ClassEntry className) { cob.ldc(method.getName()) .loadConstant(parameterTypes.length) - .anewarray(CE_Class); + .anewarray(CD_Class); // Construct an array with the parameter types mapping primitives to Wrapper types for (int i = 0; i < parameterTypes.length; i++) { @@ -856,15 +759,15 @@ private void codeFieldInitialization(CodeBuilder cob, ClassEntry className) { .loadConstant(i); if (parameterTypes[i].isPrimitive()) { PrimitiveTypeInfo prim = PrimitiveTypeInfo.get(parameterTypes[i]); - cob.getstatic(prim.typeFieldRef); + cob.getstatic(prim.typeFieldRef(cob.constantPool())); } else { codeClassForName(cob, parameterTypes[i]); } cob.aastore(); } // lookup the method - cob.invokevirtual(MRE_Class_getMethod) - .putstatic(cp.fieldRefEntry(className, cp.nameAndTypeEntry(methodFieldName, UE_Method))); + cob.invokevirtual(CD_Class, "getMethod", MTD_Method_String_ClassArray) + .putstatic(cp.fieldRefEntry(className, cp.nameAndTypeEntry(methodFieldName, CD_Method))); } /* @@ -882,7 +785,7 @@ private void codeClassForName(CodeBuilder cob, Class cl) { cob.ldc(cl.getName()) .iconst_0() // false .aload(0)// classLoader - .invokestatic(MRE_Class_forName); + .invokestatic(CD_Class, "forName", MTD_Class_String_boolean_ClassLoader); } @Override @@ -891,7 +794,6 @@ public String toString() { } } - private static final ConstantPoolBuilder CP = ConstantPoolBuilder.of(); /** * A PrimitiveTypeInfo object contains bytecode-related information about * a primitive type in its instance fields. The struct for a particular @@ -908,28 +810,28 @@ private enum PrimitiveTypeInfo { BOOLEAN(boolean.class, CD_boolean, CD_Boolean); /** - * CP entry of corresponding wrapper class + * wrapper class */ - private final ClassEntry wrapperClass; + private final ClassDesc wrapperClass; /** - * CP entry for wrapper class "valueOf" factory method + * wrapper factory method type */ - private final MethodRefEntry wrapperMethodRef; + private final MethodTypeDesc wrapperMethodType; /** - * CP entry of wrapper class method for retrieving primitive value + * wrapper class method name for retrieving primitive value */ - private final MethodRefEntry unwrapMethodRef; + private final String unwrapMethodName; /** - * CP entry of wrapper class TYPE field + * wrapper class method type for retrieving primitive value */ - private final FieldRefEntry typeFieldRef; + private final MethodTypeDesc unwrapMethodType; PrimitiveTypeInfo(Class primitiveClass, ClassDesc baseType, ClassDesc wrapperClass) { assert baseType.isPrimitive(); - this.wrapperClass = CP.classEntry(wrapperClass); - this.wrapperMethodRef = CP.methodRefEntry(wrapperClass, "valueOf", MethodTypeDesc.of(wrapperClass, baseType)); - this.unwrapMethodRef = CP.methodRefEntry(wrapperClass, primitiveClass.getName() + "Value", MethodTypeDesc.of(baseType)); - this.typeFieldRef = CP.fieldRefEntry(wrapperClass, "TYPE", CD_Class); + this.wrapperClass = wrapperClass; + this.wrapperMethodType = MethodTypeDescImpl.ofValidated(wrapperClass, new ClassDesc[]{baseType}); + this.unwrapMethodName = primitiveClass.getName() + "Value"; + this.unwrapMethodType = MethodTypeDescImpl.ofValidated(baseType, ConstantUtils.EMPTY_CLASSDESC); } public static PrimitiveTypeInfo get(Class cl) { @@ -944,5 +846,17 @@ public static PrimitiveTypeInfo get(Class cl) { if (cl == double.class) return DOUBLE; throw new AssertionError(cl); } + + public MethodRefEntry wrapperMethodRef(ConstantPoolBuilder cp) { + return cp.methodRefEntry(wrapperClass, "valueOf", wrapperMethodType); + } + + public MethodRefEntry unwrapMethodRef(ConstantPoolBuilder cp) { + return cp.methodRefEntry(wrapperClass, unwrapMethodName, unwrapMethodType); + } + + public FieldRefEntry typeFieldRef(ConstantPoolBuilder cp) { + return cp.fieldRefEntry(wrapperClass, "TYPE", CD_Class); + } } }