APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.lang.reflect

pack-priv final Class ProxyGenerator

extends Object
Class Inheritance
Imports
java.io.IOException, java.lang.classfile.*, java.lang.classfile.attribute.ExceptionsAttribute, .StackMapFrameInfo, .StackMapTableAttribute, java.lang.classfile.constantpool.*, java.lang.constant.ClassDesc, .MethodTypeDesc, java.nio.file.Files, .Path, java.util.ArrayList, .LinkedHashMap, .List, .ListIterator, .Map, .Objects, jdk.internal.constant.ClassOrInterfaceDescImpl, .ConstantUtils, .MethodTypeDescImpl

ProxyGenerator contains the code to generate a dynamic proxy class for the java.lang.reflect.Proxy API.

The external interface to ProxyGenerator is the static "generateProxyClass" method.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static enum
ProxyGenerator.PrimitiveTypeInfo

A PrimitiveTypeInfo object contains bytecode-related information about a primitive type in its instance fields.

private class
ProxyGenerator.ProxyMethod

A ProxyMethod object represents a proxy method in the proxy class being generated: a method whose implementation will encode and dispatch invocations to the proxy instance's invocation handler.

Field Summary

Modifier and TypeField and Description
private final int
accessFlags

Proxy class access flags

private static final ClassDesc
private static final ClassDesc
private static final ClassDesc
private static final ClassDesc
private static final ClassDesc
private static final ClassDesc
private static final ClassDesc
private static final ClassDesc
private static final ClassDesc
private static final ClassDesc
private static final ClassDesc
private static final ClassDesc
private static final ClassFile
private final ClassEntry
private final MethodRefEntry
private final MethodRefEntry
private final List<StackMapFrameInfo.VerificationTypeInfo>
private final ConstantPoolBuilder
private static final Class<?>[]
private final NameAndTypeEntry
private final FieldRefEntry
private final List<Class<?>>
interfaces

Proxy interfaces

private final InterfaceMethodRefEntry
private static final MethodTypeDesc
private static final MethodTypeDesc
private static final MethodTypeDesc
private static final MethodTypeDesc
private static final MethodTypeDesc
private static final MethodTypeDesc
private static final MethodTypeDesc
private static final MethodTypeDesc
private static final MethodTypeDesc
private static final MethodTypeDesc
private static final MethodTypeDesc
private static final MethodTypeDesc
private static final String
NAME_HANDLER_FIELD

name of field for storing a proxy instance's invocation handler

private static final String
private static final Method
private static final String
private static final Method
private static final String
private static final Method
private static final String
private final ClassEntry
private final ClassEntry
private int
proxyMethodCount

Ordinal of next ProxyMethod object added to proxyMethods.

private final Map<String, List<ProxyGenerator.ProxyMethod>>
proxyMethods

Maps method signature string to list of ProxyMethod objects for proxy methods with that signature.

private static final boolean
SAVE_GENERATED_FILES

debugging flag for saving generated class files

private final ClassEntry
thisClassCE

ClassEntry for this proxy class

private final MethodRefEntry
private final List<StackMapFrameInfo.VerificationTypeInfo>
private final ClassEntry
private final MethodRefEntry

Constructor Summary

AccessConstructor and Description
private
ProxyGenerator(String className, List<Class<?>> interfaces, int accessFlags)

Construct a ProxyGenerator to generate a proxy class with the specified name and for the given interfaces.

Method Summary

Modifier and TypeMethod and Description
private void
addProxyMethod(Method m, Class<?> fromClass)

Add another method to be proxied, either by creating a new ProxyMethod object or augmenting an old one for a duplicate method.

private void
addProxyMethod(ProxyGenerator.ProxyMethod
an existing ProxyMethod
pm
)

Add an existing ProxyMethod (hashcode, equals, toString).

private static void
checkReturnTypes(List<ProxyGenerator.ProxyMethod> methods)

For a given set of proxy methods with the same signature, check that their return types are compatible according to the Proxy specification.

private static void
collectCompatibleTypes(Class<?>[] from, Class<?>[] with, List<Class<?>> list)

Add to the given list all of the types in the "from" array that are not already contained in the list and are assignable to at least one of the types in the "with" array.

private static List<Class<?>>
computeUniqueCatchList(Class<?>[] exceptions)

Given the exceptions declared in the throws clause of a proxy method, compute the exceptions that need to be caught from the invocation handler's invoke method and rethrown intact in the method's implementation before catching other Throwables and wrapping them in UndeclaredThrowableExceptions.

private byte[]
generateClassFile()

Generate a class file for the proxy class.

private void
generateConstructor(ClassBuilder clb)

Generate the constructor method for the proxy class.

private void
generateLookupAccessor(ClassBuilder clb)

Generate the static lookup accessor method that returns the Lookup on this proxy class if the caller's lookup class is java.lang.reflect.Proxy; otherwise, IllegalAccessException is thrown

pack-priv static byte[]
generateProxyClass(ClassLoader loader, final String
the class name of the proxy class
name
,
List<Class<?>>
proxy interfaces
interfaces
,
int
access flags of the proxy class
accessFlags
)

Generate a proxy class given a name and a list of proxy interfaces.

private void
generateStaticInitializer(ClassBuilder clb)

Generate the class initializer.

private List<ProxyGenerator.ProxyMethod>
private static List<ClassEntry>

Returns:

the entries of the given type
toClassEntries
(ConstantPoolBuilder cp, List<Class<?>>
the Class objects, not primitive types nor array types
types
)

Returns the entries of the given type.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

accessFlagsback to summary
private final int accessFlags

Proxy class access flags

CD_Class_arrayback to summary
private static final ClassDesc CD_Class_array
CD_ClassLoaderback to summary
private static final ClassDesc CD_ClassLoader
CD_ClassNotFoundExceptionback to summary
private static final ClassDesc CD_ClassNotFoundException
CD_IllegalAccessExceptionback to summary
private static final ClassDesc CD_IllegalAccessException
CD_InvocationHandlerback to summary
private static final ClassDesc CD_InvocationHandler
CD_Methodback to summary
private static final ClassDesc CD_Method
CD_NoClassDefFoundErrorback to summary
private static final ClassDesc CD_NoClassDefFoundError
CD_NoSuchMethodErrorback to summary
private static final ClassDesc CD_NoSuchMethodError
CD_NoSuchMethodExceptionback to summary
private static final ClassDesc CD_NoSuchMethodException
CD_Object_arrayback to summary
private static final ClassDesc CD_Object_array
CD_Proxyback to summary
private static final ClassDesc CD_Proxy
CD_UndeclaredThrowableExceptionback to summary
private static final ClassDesc CD_UndeclaredThrowableException
CF_CONTEXTback to summary
private static final ClassFile CF_CONTEXT
classCEback to summary
private final ClassEntry classCE
classForNameback to summary
private final MethodRefEntry classForName
classGetMethodback to summary
private final MethodRefEntry classGetMethod
classLoaderLocalback to summary
private final List<StackMapFrameInfo.VerificationTypeInfo> classLoaderLocal
cpback to summary
private final ConstantPoolBuilder cp
EMPTY_CLASS_ARRAYback to summary
private static final Class<?>[] EMPTY_CLASS_ARRAY
exInitback to summary
private final NameAndTypeEntry exInit
handlerFieldback to summary
private final FieldRefEntry handlerField
interfacesback to summary
private final List<Class<?>> interfaces

Proxy interfaces

invocationHandlerInvokeback to summary
private final InterfaceMethodRefEntry invocationHandlerInvoke
MTD_booleanback to summary
private static final MethodTypeDesc MTD_boolean
MTD_Classback to summary
private static final MethodTypeDesc MTD_Class
MTD_Class_String_boolean_ClassLoaderback to summary
private static final MethodTypeDesc MTD_Class_String_boolean_ClassLoader
MTD_ClassLoaderback to summary
private static final MethodTypeDesc MTD_ClassLoader
MTD_Method_String_Class_arrayback to summary
private static final MethodTypeDesc MTD_Method_String_Class_array
MTD_MethodHandles$Lookupback to summary
private static final MethodTypeDesc MTD_MethodHandles$Lookup
MTD_MethodHandles$Lookup_MethodHandles$Lookupback to summary
private static final MethodTypeDesc MTD_MethodHandles$Lookup_MethodHandles$Lookup
MTD_Object_Object_Method_ObjectArrayback to summary
private static final MethodTypeDesc MTD_Object_Object_Method_ObjectArray
MTD_Stringback to summary
private static final MethodTypeDesc MTD_String
MTD_void_InvocationHandlerback to summary
private static final MethodTypeDesc MTD_void_InvocationHandler
MTD_void_Stringback to summary
private static final MethodTypeDesc MTD_void_String
MTD_void_Throwableback to summary
private static final MethodTypeDesc MTD_void_Throwable
NAME_HANDLER_FIELDback to summary
private static final String NAME_HANDLER_FIELD

name of field for storing a proxy instance's invocation handler

NAME_LOOKUP_ACCESSORback to summary
private static final String NAME_LOOKUP_ACCESSOR
OBJECT_EQUALS_METHODback to summary
private static final Method OBJECT_EQUALS_METHOD
OBJECT_EQUALS_SIGback to summary
private static final String OBJECT_EQUALS_SIG
OBJECT_HASH_CODE_METHODback to summary
private static final Method OBJECT_HASH_CODE_METHOD
OBJECT_HASH_CODE_SIGback to summary
private static final String OBJECT_HASH_CODE_SIG
OBJECT_TO_STRING_METHODback to summary
private static final Method OBJECT_TO_STRING_METHOD
OBJECT_TO_STRING_SIGback to summary
private static final String OBJECT_TO_STRING_SIG
objectCEback to summary
private final ClassEntry objectCE
proxyCEback to summary
private final ClassEntry proxyCE
proxyMethodCountback to summary
private int proxyMethodCount

Ordinal of next ProxyMethod object added to proxyMethods. Indexes are reserved for hashcode(0), equals(1), toString(2).

proxyMethodsback to summary
private final Map<String, List<ProxyGenerator.ProxyMethod>> proxyMethods

Maps method signature string to list of ProxyMethod objects for proxy methods with that signature. Kept in insertion order to make it easier to compare old and new.

SAVE_GENERATED_FILESback to summary
private static final boolean SAVE_GENERATED_FILES

debugging flag for saving generated class files

thisClassCEback to summary
private final ClassEntry thisClassCE

ClassEntry for this proxy class

throwableGetMessageback to summary
private final MethodRefEntry throwableGetMessage
throwableStackback to summary
private final List<StackMapFrameInfo.VerificationTypeInfo> throwableStack
uteCEback to summary
private final ClassEntry uteCE
uteInitback to summary
private final MethodRefEntry uteInit

Constructor Detail

ProxyGeneratorback to summary
private ProxyGenerator(String className, List<Class<?>> interfaces, int accessFlags)

Construct a ProxyGenerator to generate a proxy class with the specified name and for the given interfaces.

A ProxyGenerator object contains the state for the ongoing generation of a particular proxy class.

Method Detail

addProxyMethodback to summary
private void addProxyMethod(Method m, Class<?> fromClass)

Add another method to be proxied, either by creating a new ProxyMethod object or augmenting an old one for a duplicate method. "fromClass" indicates the proxy interface that the method was found through, which may be different from (a subinterface of) the method's "declaring class". Note that the first Method object passed for a given name and descriptor identifies the Method object (and thus the declaring class) that will be passed to the invocation handler's "invoke" method for a given set of duplicate methods.

addProxyMethodback to summary
private void addProxyMethod(ProxyGenerator.ProxyMethod pm)

Add an existing ProxyMethod (hashcode, equals, toString).

Parameters
pm:ProxyGenerator.ProxyMethod

an existing ProxyMethod

checkReturnTypesback to summary
private static void checkReturnTypes(List<ProxyGenerator.ProxyMethod> methods)

For a given set of proxy methods with the same signature, check that their return types are compatible according to the Proxy specification. Specifically, if there is more than one such method, then all of the return types must be reference types, and there must be one return type that is assignable to each of the rest of them.

collectCompatibleTypesback to summary
private static void collectCompatibleTypes(Class<?>[] from, Class<?>[] with, List<Class<?>> list)

Add to the given list all of the types in the "from" array that are not already contained in the list and are assignable to at least one of the types in the "with" array.

This method is useful for computing the greatest common set of declared exceptions from duplicate methods inherited from different interfaces.

computeUniqueCatchListback to summary
private static List<Class<?>> computeUniqueCatchList(Class<?>[] exceptions)

Given the exceptions declared in the throws clause of a proxy method, compute the exceptions that need to be caught from the invocation handler's invoke method and rethrown intact in the method's implementation before catching other Throwables and wrapping them in UndeclaredThrowableExceptions. The exceptions to be caught are returned in a List object. Each exception in the returned list is guaranteed to not be a subclass of any of the other exceptions in the list, so the catch blocks for these exceptions may be generated in any order relative to each other. Error and RuntimeException are each always contained by the returned list (if none of their superclasses are contained), since those unchecked exceptions should always be rethrown intact, and thus their subclasses will never appear in the returned list. The returned List will be empty if java.lang.Throwable is in the given list of declared exceptions, indicating that no exceptions need to be caught.

generateClassFileback to summary
private byte[] generateClassFile()

Generate a class file for the proxy class. This method drives the class file generation process.

generateConstructorback to summary
private void generateConstructor(ClassBuilder clb)

Generate the constructor method for the proxy class.

generateLookupAccessorback to summary
private void generateLookupAccessor(ClassBuilder clb)

Generate the static lookup accessor method that returns the Lookup on this proxy class if the caller's lookup class is java.lang.reflect.Proxy; otherwise, IllegalAccessException is thrown

generateProxyClassback to summary
pack-priv static byte[] generateProxyClass(ClassLoader loader, final String name, List<Class<?>> interfaces, int accessFlags)

Generate a proxy class given a name and a list of proxy interfaces.

Parameters
name:String

the class name of the proxy class

interfaces:List<Class<?>>

proxy interfaces

accessFlags:int

access flags of the proxy class

generateStaticInitializerback to summary
private void generateStaticInitializer(ClassBuilder clb)

Generate the class initializer.

proxyMethodsForback to summary
private List<ProxyGenerator.ProxyMethod> proxyMethodsFor(String sig)
toClassEntriesback to summary
private static List<ClassEntry> toClassEntries(ConstantPoolBuilder cp, List<Class<?>> types)

Returns the entries of the given type.

Parameters
types:List<Class<?>>

the Class objects, not primitive types nor array types

Returns:List<ClassEntry>

the entries of the given type

java.lang.reflect back to summary

private final Enum ProxyGenerator.PrimitiveTypeInfo

extends Enum<ProxyGenerator.PrimitiveTypeInfo>
Class Inheritance

A PrimitiveTypeInfo object contains bytecode-related information about a primitive type in its instance fields. The struct for a particular primitive type can be obtained using the static "get" method.

Field Summary

Modifier and TypeField and Description
public static final ProxyGenerator.PrimitiveTypeInfo
public static final ProxyGenerator.PrimitiveTypeInfo
public static final ProxyGenerator.PrimitiveTypeInfo
public static final ProxyGenerator.PrimitiveTypeInfo
public static final ProxyGenerator.PrimitiveTypeInfo
public static final ProxyGenerator.PrimitiveTypeInfo
public static final ProxyGenerator.PrimitiveTypeInfo
public static final ProxyGenerator.PrimitiveTypeInfo
private final String
unwrapMethodName

wrapper class method name for retrieving primitive value

private final MethodTypeDesc
unwrapMethodType

wrapper class method type for retrieving primitive value

private final ClassDesc
wrapperClass

wrapper class

private final MethodTypeDesc
wrapperMethodType

wrapper factory method type

Constructor Summary

AccessConstructor and Description
private
PrimitiveTypeInfo(Class<?> primitiveClass, ClassDesc baseType, ClassDesc wrapperClass)

Method Summary

Modifier and TypeMethod and Description
public static ProxyGenerator.PrimitiveTypeInfo
get(Class<?> cl)

public FieldRefEntry
public MethodRefEntry
public static ProxyGenerator.PrimitiveTypeInfo
public static ProxyGenerator.PrimitiveTypeInfo[]
public MethodRefEntry
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

BOOLEANback to summary
public static final ProxyGenerator.PrimitiveTypeInfo BOOLEAN
BYTEback to summary
public static final ProxyGenerator.PrimitiveTypeInfo BYTE
CHARback to summary
public static final ProxyGenerator.PrimitiveTypeInfo CHAR
DOUBLEback to summary
public static final ProxyGenerator.PrimitiveTypeInfo DOUBLE
FLOATback to summary
public static final ProxyGenerator.PrimitiveTypeInfo FLOAT
INTback to summary
public static final ProxyGenerator.PrimitiveTypeInfo INT
LONGback to summary
public static final ProxyGenerator.PrimitiveTypeInfo LONG
SHORTback to summary
public static final ProxyGenerator.PrimitiveTypeInfo SHORT
unwrapMethodNameback to summary
private final String unwrapMethodName

wrapper class method name for retrieving primitive value

unwrapMethodTypeback to summary
private final MethodTypeDesc unwrapMethodType

wrapper class method type for retrieving primitive value

wrapperClassback to summary
private final ClassDesc wrapperClass

wrapper class

wrapperMethodTypeback to summary
private final MethodTypeDesc wrapperMethodType

wrapper factory method type

Constructor Detail

PrimitiveTypeInfoback to summary
private PrimitiveTypeInfo(Class<?> primitiveClass, ClassDesc baseType, ClassDesc wrapperClass)

Method Detail

getback to summary
public static ProxyGenerator.PrimitiveTypeInfo get(Class<?> cl)
typeFieldRefback to summary
public FieldRefEntry typeFieldRef(ConstantPoolBuilder cp)
unwrapMethodRefback to summary
public MethodRefEntry unwrapMethodRef(ConstantPoolBuilder cp)
valueOfback to summary
public static ProxyGenerator.PrimitiveTypeInfo valueOf(String name)
valuesback to summary
public static ProxyGenerator.PrimitiveTypeInfo[] values()
wrapperMethodRefback to summary
public MethodRefEntry wrapperMethodRef(ConstantPoolBuilder cp)
java.lang.reflect back to summary

private Class ProxyGenerator.ProxyMethod

extends Object
Class Inheritance

A ProxyMethod object represents a proxy method in the proxy class being generated: a method whose implementation will encode and dispatch invocations to the proxy instance's invocation handler.

Field Summary

Modifier and TypeField and Description
private Class<?>[]
private final Class<?>
private final Method
private final FieldRefEntry
private final String
private final Class<?>
private final String

Constructor Summary

AccessConstructor and Description
private
ProxyMethod(Method method, String sig, Class<?> returnType, Class<?>[] exceptionTypes, Class<?> fromClass, String methodFieldName)

private
ProxyMethod(Method
The method for which to create a proxy
method
,
String sig, String methodFieldName)

Create a new specific ProxyMethod with a specific field name

Method Summary

Modifier and TypeMethod and Description
private void
codeClassForName(CodeBuilder cob, Class<?> cl)

Generate code to invoke the Class.forName with the name of the given class to get its Class object at runtime.

private void
codeFieldInitialization(CodeBuilder cob)

Generate code for initializing the static field that stores the Method object for this proxy method.

private void
codeUnwrapReturnValue(CodeBuilder cob, Class<?> type)

Generate code for unwrapping a return value of the given type from the invocation handler's "invoke" method (as type Object) to its correct type.

private void
codeWrapArgument(CodeBuilder cob, Class<?> type, int slot)

Generate code for wrapping an argument of the given type whose value can be found at the specified local variable index, in order for it to be passed (as an Object) to the invocation handler's "invoke" method.

private void
generateMethod(ClassBuilder clb)

Generate this method, including the code and exception table entry.

private Class<?>[]
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

exceptionTypesback to summary
private Class<?>[] exceptionTypes
fromClassback to summary
private final Class<?> fromClass
methodback to summary
private final Method method
methodFieldback to summary
private final FieldRefEntry methodField
methodFieldNameback to summary
private final String methodFieldName
returnTypeback to summary
private final Class<?> returnType
shortSignatureback to summary
private final String shortSignature

Constructor Detail

ProxyMethodback to summary
private ProxyMethod(Method method, String sig, Class<?> returnType, Class<?>[] exceptionTypes, Class<?> fromClass, String methodFieldName)
ProxyMethodback to summary
private ProxyMethod(Method method, String sig, String methodFieldName)

Create a new specific ProxyMethod with a specific field name

Parameters
method:Method

The method for which to create a proxy

Method Detail

codeClassForNameback to summary
private void codeClassForName(CodeBuilder cob, Class<?> cl)

Generate code to invoke the Class.forName with the name of the given class to get its Class object at runtime. The code is written to the supplied stream. Note that the code generated by this method may cause the checked ClassNotFoundException to be thrown. A class loader is anticipated at local variable index 0.

codeFieldInitializationback to summary
private void codeFieldInitialization(CodeBuilder cob)

Generate code for initializing the static field that stores the Method object for this proxy method. A class loader is anticipated at local variable index 0.

codeUnwrapReturnValueback to summary
private void codeUnwrapReturnValue(CodeBuilder cob, Class<?> type)

Generate code for unwrapping a return value of the given type from the invocation handler's "invoke" method (as type Object) to its correct type.

codeWrapArgumentback to summary
private void codeWrapArgument(CodeBuilder cob, Class<?> type, int slot)

Generate code for wrapping an argument of the given type whose value can be found at the specified local variable index, in order for it to be passed (as an Object) to the invocation handler's "invoke" method.

generateMethodback to summary
private void generateMethod(ClassBuilder clb)

Generate this method, including the code and exception table entry.

parameterTypesback to summary
private Class<?>[] parameterTypes()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override