APIdia Launch Partner
Top Description Inners Fields Constructors Methods
jdk.internal.reflect

public Class ReflectionFactory

extends Object
Class Inheritance
Imports
java.io.Externalizable, .ObjectInputStream, .ObjectOutputStream, .ObjectStreamClass, .ObjectStreamField, .OptionalDataException, .Serializable, java.lang.classfile.ClassFile, java.lang.invoke.MethodHandle, .MethodHandles, java.lang.reflect.*, java.util.Set, jdk.internal.access.JavaLangReflectAccess, .SharedSecrets, jdk.internal.misc.VM, jdk.internal.vm.annotation.Stable

The master factory for all reflective objects, both those in java.lang.reflect (Fields, Methods, Constructors) as well as their delegates (FieldAccessors, MethodAccessors, ConstructorAccessors).

The methods in this class are extremely unsafe and can cause subversion of both the language and the verifier. For this reason, they are all instance methods, and access to the constructor of this factory is guarded by a security check, in similar style to jdk.internal.misc.Unsafe.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static record
ReflectionFactory.Config

The configurations for the reflection factory.

Field Summary

Modifier and TypeField and Description
private static ReflectionFactory.Config
config

The configuration is lazily initialized after the module system is initialized.

private static final ReflectionFactory.Config
private static volatile Method
private final JavaLangReflectAccess
private static final Set<Class<?>>
nonSerializableLeafClasses

These are specific leaf classes which appear to be Serializable, but which have special semantics according to the serialization specification.

private static final ReflectionFactory

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private final ClassFileFormatVersion
private static ReflectionFactory.Config
public <T> Constructor<T>
copyConstructor(Constructor<T> arg)

Makes a copy of the passed constructor.

public Field
copyField(Field arg)

Makes a copy of the passed field.

public Method
copyMethod(Method arg)

Makes a copy of the passed method.

public final MethodHandle
public final MethodHandle
private static boolean
private final MethodHandle
findReadWriteObjectForSerialization(Class<?> cl, String methodName, Class<?> streamClass)

private final Constructor<?>
generateConstructor(Class<?> cl, Constructor<?> constructorToCall)

public Class<?>[]
public byte[]
getExecutableTypeAnnotationBytes(Executable ex)

Gets the byte[] that encodes TypeAnnotations on an executable.

public static ReflectionFactory
getReflectionFactory()

Provides the caller with the capability to instantiate reflective objects.

private MethodHandle

Returns:

a MethodHandle for the method or null if not found or has the wrong signature.
getReplaceResolveForSerialization
(Class<?>
a serializable class
cl
,
String
the method name to find
methodName
)

Lookup readResolve or writeReplace on a class with specified signature constraints.

private static boolean
public final boolean
hasStaticInitializerForSerialization(Class<?> cl)

Returns true if the given class defines a static initializer method, false otherwise.

public Method
leafCopyMethod(Method arg)

Makes a copy of the passed method.

private static ReflectionFactory.Config
public ConstructorAccessor
public final Constructor<?>
public final Constructor<?>
newConstructorForSerialization(Class<?> cl, Constructor<?> constructorToCall)

public final Constructor<?>

Returns:

the generated constructor, or null if none is available
newConstructorForSerialization
(Class<?>
the class for which a constructor is to be found
cl
)

Returns a constructor that allocates an instance of cl and that then initializes the instance by calling the no-arg constructor of its first non-serializable superclass.

public FieldAccessor
newFieldAccessor(Field field, boolean override)

public <T> T
newInstance(Constructor<T> ctor, Object[] args, Class<?> caller)

public MethodAccessor
newMethodAccessor(Method method, boolean callerSensitive)

public final Constructor<OptionalDataException>

Returns:

the eof constructor for OptionalDataException
newOptionalDataExceptionForSerialization
()

Return the accessible constructor for OptionalDataException signaling eof.

private static boolean

Returns:

true if the two classes are in the same classloader and package
packageEquals
(Class<?>
a class
cl1
,
Class<?>
another class
cl2
)

Returns true if classes are defined in the classloader and same package, false otherwise.

public final Set<AccessFlag>
parseAccessFlags(int mask, AccessFlag.Location location, Class<?> classFile)

public final MethodHandle
public final MethodHandle
public final MethodHandle

Returns:

the writeReplace MethodHandle or null if not found
readResolveForSerialization
(Class<?>
a serializable class
cl
)

Returns a MethodHandle for readResolve on the serializable class or null if no match found.

public final ObjectStreamField[]
private boolean

Returns:

whether the superclass has a constructor accessible from cl
superHasAccessibleConstructor
(Class<?>
the class from which access is checked
cl
)

Given a class, determines whether its superclass has any constructors that are accessible from the class.

pack-priv static boolean
public final MethodHandle
public final MethodHandle

Returns:

the writeReplace MethodHandle or null if not found
writeReplaceForSerialization
(Class<?>
a serializable class
cl
)

Returns a MethodHandle for writeReplace on the serializable class or null if no match found.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

configback to summary
private static ReflectionFactory.Config config

The configuration is lazily initialized after the module system is initialized. The default config would be used before the proper config is loaded. The static initializer of ReflectionFactory is run before the system properties are set up. The class initialization is caused by the class initialization of java.lang.reflect.Method (more properly, caused by the class initialization for java.lang.reflect.AccessibleObject) that happens very early VM startup, initPhase1.

Annotations
@Stable
DEFAULT_CONFIGback to summary
private static final ReflectionFactory.Config DEFAULT_CONFIG
hasStaticInitializerMethodback to summary
private static volatile Method hasStaticInitializerMethod
langReflectAccessback to summary
private final JavaLangReflectAccess langReflectAccess
nonSerializableLeafClassesback to summary
private static final Set<Class<?>> nonSerializableLeafClasses

These are specific leaf classes which appear to be Serializable, but which have special semantics according to the serialization specification. We could theoretically include array classes here, but it is easier and clearer to just use `Class#isArray` instead.

soleInstanceback to summary
private static final ReflectionFactory soleInstance

Constructor Detail

ReflectionFactoryback to summary
private ReflectionFactory()

Method Detail

classFileFormatVersionback to summary
private final ClassFileFormatVersion classFileFormatVersion(Class<?> cl)
configback to summary
private static ReflectionFactory.Config config()
copyConstructorback to summary
public <T> Constructor<T> copyConstructor(Constructor<T> arg)

Makes a copy of the passed constructor. The returned constructor is a "child" of the passed one; see the comments in Constructor.java for details.

copyFieldback to summary
public Field copyField(Field arg)

Makes a copy of the passed field. The returned field is a "child" of the passed one; see the comments in Field.java for details.

copyMethodback to summary
public Method copyMethod(Method arg)

Makes a copy of the passed method. The returned method is a "child" of the passed one; see the comments in Method.java for details.

defaultReadObjectForSerializationback to summary
public final MethodHandle defaultReadObjectForSerialization(Class<?> cl)
defaultWriteObjectForSerializationback to summary
public final MethodHandle defaultWriteObjectForSerialization(Class<?> cl)
disableSerialConstructorChecksback to summary
private static boolean disableSerialConstructorChecks()
findReadWriteObjectForSerializationback to summary
private final MethodHandle findReadWriteObjectForSerialization(Class<?> cl, String methodName, Class<?> streamClass)
generateConstructorback to summary
private final Constructor<?> generateConstructor(Class<?> cl, Constructor<?> constructorToCall)
getExecutableSharedParameterTypesback to summary
public Class<?>[] getExecutableSharedParameterTypes(Executable ex)
getExecutableTypeAnnotationBytesback to summary
public byte[] getExecutableTypeAnnotationBytes(Executable ex)

Gets the byte[] that encodes TypeAnnotations on an executable.

getReflectionFactoryback to summary
public static ReflectionFactory getReflectionFactory()

Provides the caller with the capability to instantiate reflective objects.

The returned ReflectionFactory object should be carefully guarded by the caller, since it can be used to read and write private data and invoke private methods, as well as to load unverified bytecodes. It must never be passed to untrusted code.

getReplaceResolveForSerializationback to summary
private MethodHandle getReplaceResolveForSerialization(Class<?> cl, String methodName)

Lookup readResolve or writeReplace on a class with specified signature constraints.

Parameters
cl:Class<?>

a serializable class

methodName:String

the method name to find

Returns:MethodHandle

a MethodHandle for the method or null if not found or has the wrong signature.

hasDefaultOrNoSerializationback to summary
private static boolean hasDefaultOrNoSerialization(Class<?> cl)
hasStaticInitializerForSerializationback to summary
public final boolean hasStaticInitializerForSerialization(Class<?> cl)

Returns true if the given class defines a static initializer method, false otherwise.

leafCopyMethodback to summary
public Method leafCopyMethod(Method arg)

Makes a copy of the passed method. The returned method is NOT a "child" but a "sibling" of the Method in arg. Should only be used on non-root methods.

loadConfigback to summary
private static ReflectionFactory.Config loadConfig()
newConstructorAccessorback to summary
public ConstructorAccessor newConstructorAccessor(Constructor<?> c)
newConstructorForExternalizationback to summary
public final Constructor<?> newConstructorForExternalization(Class<?> cl)
newConstructorForSerializationback to summary
public final Constructor<?> newConstructorForSerialization(Class<?> cl, Constructor<?> constructorToCall)
newConstructorForSerializationback to summary
public final Constructor<?> newConstructorForSerialization(Class<?> cl)

Returns a constructor that allocates an instance of cl and that then initializes the instance by calling the no-arg constructor of its first non-serializable superclass. This is specified in the Serialization Specification, section 3.1, in step 11 of the deserialization process. If cl is not serializable, returns cl's no-arg constructor. If no accessible constructor is found, or if the class hierarchy is somehow malformed (e.g., a serializable class has no superclass), null is returned.

Parameters
cl:Class<?>

the class for which a constructor is to be found

Returns:Constructor<?>

the generated constructor, or null if none is available

newFieldAccessorback to summary
public FieldAccessor newFieldAccessor(Field field, boolean override)
newInstanceback to summary
public <T> T newInstance(Constructor<T> ctor, Object[] args, Class<?> caller) throws IllegalAccessException, InstantiationException, InvocationTargetException
newMethodAccessorback to summary
public MethodAccessor newMethodAccessor(Method method, boolean callerSensitive)
newOptionalDataExceptionForSerializationback to summary
public final Constructor<OptionalDataException> newOptionalDataExceptionForSerialization()

Return the accessible constructor for OptionalDataException signaling eof.

Returns:Constructor<OptionalDataException>

the eof constructor for OptionalDataException

packageEqualsback to summary
private static boolean packageEquals(Class<?> cl1, Class<?> cl2)

Returns true if classes are defined in the classloader and same package, false otherwise.

Parameters
cl1:Class<?>

a class

cl2:Class<?>

another class

Returns:boolean

true if the two classes are in the same classloader and package

parseAccessFlagsback to summary
public final Set<AccessFlag> parseAccessFlags(int mask, AccessFlag.Location location, Class<?> classFile)
readObjectForSerializationback to summary
public final MethodHandle readObjectForSerialization(Class<?> cl)
readObjectNoDataForSerializationback to summary
public final MethodHandle readObjectNoDataForSerialization(Class<?> cl)
readResolveForSerializationback to summary
public final MethodHandle readResolveForSerialization(Class<?> cl)

Returns a MethodHandle for readResolve on the serializable class or null if no match found.

Parameters
cl:Class<?>

a serializable class

Returns:MethodHandle

the writeReplace MethodHandle or null if not found

serialPersistentFieldsback to summary
public final ObjectStreamField[] serialPersistentFields(Class<?> cl)
superHasAccessibleConstructorback to summary
private boolean superHasAccessibleConstructor(Class<?> cl)

Given a class, determines whether its superclass has any constructors that are accessible from the class. This is a special purpose method intended to do access checking for a serializable class and its superclasses up to, but not including, the first non-serializable superclass. This also implies that the superclass is always non-null, because a serializable class must be a class (not an interface) and Object is not serializable.

Parameters
cl:Class<?>

the class from which access is checked

Returns:boolean

whether the superclass has a constructor accessible from cl

useNativeAccessorOnlyback to summary
pack-priv static boolean useNativeAccessorOnly()
writeObjectForSerializationback to summary
public final MethodHandle writeObjectForSerialization(Class<?> cl)
writeReplaceForSerializationback to summary
public final MethodHandle writeReplaceForSerialization(Class<?> cl)

Returns a MethodHandle for writeReplace on the serializable class or null if no match found.

Parameters
cl:Class<?>

a serializable class

Returns:MethodHandle

the writeReplace MethodHandle or null if not found

jdk.internal.reflect back to summary

private final Record ReflectionFactory.Config

extends Record
Class Inheritance
Record Components
useNativeAccessorOnly:boolean 
disableSerialConstructorChecks:boolean 

The configurations for the reflection factory. Configurable via system properties but only available after ReflectionFactory is loaded during early VM startup. Note that the default implementations of the object methods of this Config record (toString, equals, hashCode) use indy, which is available to use only after initPhase1. These methods are currently not called, but should they be needed, a workaround is to override them.

Field Summary

Modifier and TypeField and Description
private final boolean
private final boolean
useNativeAccessorOnly

Record Component accessed by useNativeAccessorOnly().

Constructor Summary

AccessConstructor and Description
private
Config(boolean useNativeAccessorOnly, boolean disableSerialConstructorChecks)

Method Summary

Modifier and TypeMethod and Description
public boolean
public final boolean
equals(Object o)

Implements abstract java.lang.Record.equals.

Indicates whether some other object is "equal to" this one.

public final int
hashCode()

Implements abstract java.lang.Record.hashCode.

Returns a hash code value for the record.

public final String
toString()

Implements abstract java.lang.Record.toString.

Returns a string representation of the record.

public boolean
useNativeAccessorOnly()

Record Component getter of useNativeAccessorOnly.

Field Detail

disableSerialConstructorChecksback to summary
private final boolean disableSerialConstructorChecks

Record Component accessed by disableSerialConstructorChecks().

useNativeAccessorOnlyback to summary
private final boolean useNativeAccessorOnly

Record Component accessed by useNativeAccessorOnly().

Constructor Detail

Configback to summary
private Config(boolean useNativeAccessorOnly, boolean disableSerialConstructorChecks)

Method Detail

disableSerialConstructorChecksback to summary
public boolean disableSerialConstructorChecks()

Record Component getter of disableSerialConstructorChecks.

equalsback to summary
public final boolean equals(Object o)

Implements abstract java.lang.Record.equals.

Doc from java.lang.Record.equals.

Indicates whether some other object is "equal to" this one. In addition to the general contract of Object.equals, record classes must further obey the invariant that when a record instance is "copied" by passing the result of the record component accessor methods to the canonical constructor, as follows:

    R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
hashCodeback to summary
public final int hashCode()

Implements abstract java.lang.Record.hashCode.

Doc from java.lang.Record.hashCode.

Returns a hash code value for the record. Obeys the general contract of Object.hashCode. For records, hashing behavior is constrained by the refined contract of Record.equals, so that any two records created from the same components must have the same hash code.

toStringback to summary
public final String toString()

Implements abstract java.lang.Record.toString.

Doc from java.lang.Record.toString.

Returns a string representation of the record. In accordance with the general contract of Object#toString(), the toString method returns a string that "textually represents" this record. The result should be a concise but informative representation that is easy for a person to read.

In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.

useNativeAccessorOnlyback to summary
public boolean useNativeAccessorOnly()

Record Component getter of useNativeAccessorOnly.