APIdia Launch Partner
Top Description Fields Constructors Methods
sun.reflect

public Class ReflectionFactory

extends Object
Class Inheritance
Imports
java.io.ObjectInputStream, .ObjectOutputStream, .ObjectStreamField, .OptionalDataException, java.lang.invoke.MethodHandle, java.lang.reflect.Constructor, .InvocationTargetException

ReflectionFactory supports custom serialization. Its methods support the creation of uninitialized objects, invoking serialization private methods for readObject, writeObject, readResolve, and writeReplace.

Field Summary

Modifier and TypeField and Description
private static final ReflectionFactory
private static final ReflectionFactory

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public final MethodHandle

Returns:

a direct MethodHandle for the synthetic readObject method or null if the class falls in a disallowed category
defaultReadObjectForSerialization
(Class<?>
a Serializable class
cl
)

Generate and return a direct MethodHandle which implements the general default behavior for serializable class's readObject.

public final MethodHandle

Returns:

a direct MethodHandle for the synthetic writeObject method or null if the class falls in a disallowed category
defaultWriteObjectForSerialization
(Class<?>
a Serializable class
cl
)

Generate and return a direct MethodHandle which implements the general default behavior for serializable class's writeObject.

public static ReflectionFactory

Returns:

the ReflectionFactory
getReflectionFactory
()

Provides the caller with the capability to instantiate reflective objects.

public final boolean

Returns:

true if the class has a static initializer, otherwise false
hasStaticInitializerForSerialization
(Class<?>
a serializable class
cl
)

Returns true if the class has a static initializer.

public final Constructor<?>

Returns:

A no-arg constructor for the class; returns null if the class does not implement java.io.Externalizable
newConstructorForExternalization
(Class<?>
the class to instantiate
cl
)

Returns an accessible no-arg constructor for an externalizable class to be initialized using a public no-argument constructor.

public Constructor<?>

Returns:

an accessible constructor
newConstructorForSerialization
(Class<?>
the class to instantiate
cl
,
Constructor<?>
the constructor to call
constructorToCall
)

Returns an accessible constructor capable of creating instances of the given class, initialized by the given constructor.

public final Constructor<?>

Returns:

a no-arg constructor for the class or null if the class or supertypes do not have a suitable no-arg constructor
newConstructorForSerialization
(Class<?>
the class to instantiate
cl
)

Returns an accessible no-arg constructor for a class.

public final OptionalDataException

Returns:

a new OptionalDataException
newOptionalDataExceptionForSerialization
(boolean
the value of eof in the created OptionalDataException
bool
)

Returns a new OptionalDataException with eof set to true or false.

public final MethodHandle

Returns:

a direct MethodHandle for the readObject method of the class or null if the class does not have a readObject method
readObjectForSerialization
(Class<?>
a Serializable class
cl
)

Returns a direct MethodHandle for the readObject method on a Serializable class.

public final MethodHandle

Returns:

a direct MethodHandle for the readObjectNoData method of the class or null if the class does not have a readObjectNoData method
readObjectNoDataForSerialization
(Class<?>
a Serializable class
cl
)

Returns a direct MethodHandle for the readObjectNoData method on a Serializable class.

public final MethodHandle

Returns:

a direct MethodHandle for the readResolve method of the class or null if the class does not have a readResolve method
readResolveForSerialization
(Class<?>
the Serializable class
cl
)

Returns a direct MethodHandle for the readResolve method on a serializable class.

public final ObjectStreamField[]

Returns:

the declared serialPersistentFields from a serializable class, or null if none is declared, the field is declared but not valid, or the class is not a valid serializable class
serialPersistentFields
(Class<?>
a Serializable class
cl
)

Returns the declared serialPersistentFields from a serializable class, or null if none is declared, the field is declared but not valid, or the class is not a valid serializable class.

public final MethodHandle

Returns:

a direct MethodHandle for the writeObject method of the class or null if the class does not have a writeObject method
writeObjectForSerialization
(Class<?>
a Serializable class
cl
)

Returns a direct MethodHandle for the writeObject method on a Serializable class.

public final MethodHandle

Returns:

a direct MethodHandle for the writeReplace method of the class or null if the class does not have a writeReplace method
writeReplaceForSerialization
(Class<?>
the Serializable class
cl
)

Returns a direct MethodHandle for the writeReplace method on a serializable class.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

delegateback to summary
private static final ReflectionFactory delegate
soleInstanceback to summary
private static final ReflectionFactory soleInstance

Constructor Detail

ReflectionFactoryback to summary
private ReflectionFactory()

Method Detail

defaultReadObjectForSerializationback to summary
public final MethodHandle defaultReadObjectForSerialization(Class<?> cl)

Generate and return a direct MethodHandle which implements the general default behavior for serializable class's readObject. The generated method behaves in accordance with the Java Serialization specification's rules for that method.

The generated method will invoke ObjectInputStream#readFields() to acquire the stream field values. The serialization fields of the class will then be populated from the stream values.

Only fields which are eligible for default serialization will be populated. This includes only fields which are not transient and not static (even if the field is final or private).

Requesting a default serialization method for a class in a disallowed category is not supported; null will be returned for such classes. The disallowed categories include (but are not limited to):

  • Classes which do not implement Serializable
  • Classes which implement Externalizable
  • Classes which are specially handled by the Java Serialization specification, including record classes, enum constant classes, Class, String, array classes, reflection proxy classes, and hidden classes
  • Classes which declare a valid serialPersistentFields value
  • Any special types which may possibly be added to the JDK or the Java language in the future which in turn might require special handling by the provisions of the corresponding future version of the Java Serialization specification

The generated method will accept the instance as its first argument and the ObjectInputStream as its second argument. The return type of the method is void.

Parameters
cl:Class<?>

a Serializable class

Returns:MethodHandle

a direct MethodHandle for the synthetic readObject method or null if the class falls in a disallowed category

Since
24
defaultWriteObjectForSerializationback to summary
public final MethodHandle defaultWriteObjectForSerialization(Class<?> cl)

Generate and return a direct MethodHandle which implements the general default behavior for serializable class's writeObject. The generated method behaves in accordance with the Java Serialization specification's rules for that method.

The generated method will invoke ObjectOutputStream#putFields to acquire the buffer for the stream field values. The buffer will be populated from the serialization fields of the class. The buffer will then be flushed to the stream using the ObjectOutputStream#writeFields() method.

Only fields which are eligible for default serialization will be written to the buffer. This includes only fields which are not transient and not static (even if the field is final or private).

Requesting a default serialization method for a class in a disallowed category is not supported; null will be returned for such classes. The disallowed categories include (but are not limited to):

  • Classes which do not implement Serializable
  • Classes which implement Externalizable
  • Classes which are specially handled by the Java Serialization specification, including record classes, enum constant classes, Class, String, array classes, reflection proxy classes, and hidden classes
  • Classes which declare a valid serialPersistentFields value
  • Any special types which may possibly be added to the JDK or the Java language in the future which in turn might require special handling by the provisions of the corresponding future version of the Java Serialization specification

The generated method will accept the instance as its first argument and the ObjectOutputStream as its second argument. The return type of the method is void.

Parameters
cl:Class<?>

a Serializable class

Returns:MethodHandle

a direct MethodHandle for the synthetic writeObject method or null if the class falls in a disallowed category

Since
24
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.

Returns:ReflectionFactory

the ReflectionFactory

hasStaticInitializerForSerializationback to summary
public final boolean hasStaticInitializerForSerialization(Class<?> cl)

Returns true if the class has a static initializer. The presence of a static initializer is used to compute the serialVersionUID.

Parameters
cl:Class<?>

a serializable class

Returns:boolean

true if the class has a static initializer, otherwise false

newConstructorForExternalizationback to summary
public final Constructor<?> newConstructorForExternalization(Class<?> cl)

Returns an accessible no-arg constructor for an externalizable class to be initialized using a public no-argument constructor.

Parameters
cl:Class<?>

the class to instantiate

Returns:Constructor<?>

A no-arg constructor for the class; returns null if the class does not implement java.io.Externalizable

newConstructorForSerializationback to summary
public Constructor<?> newConstructorForSerialization(Class<?> cl, Constructor<?> constructorToCall)

Returns an accessible constructor capable of creating instances of the given class, initialized by the given constructor.

Parameters
cl:Class<?>

the class to instantiate

constructorToCall:Constructor<?>

the constructor to call

Returns:Constructor<?>

an accessible constructor

newConstructorForSerializationback to summary
public final Constructor<?> newConstructorForSerialization(Class<?> cl)

Returns an accessible no-arg constructor for a class. The no-arg constructor is found searching the class and its supertypes.

Parameters
cl:Class<?>

the class to instantiate

Returns:Constructor<?>

a no-arg constructor for the class or null if the class or supertypes do not have a suitable no-arg constructor

newOptionalDataExceptionForSerializationback to summary
public final OptionalDataException newOptionalDataExceptionForSerialization(boolean bool)

Returns a new OptionalDataException with eof set to true or false.

Parameters
bool:boolean

the value of eof in the created OptionalDataException

Returns:OptionalDataException

a new OptionalDataException

readObjectForSerializationback to summary
public final MethodHandle readObjectForSerialization(Class<?> cl)

Returns a direct MethodHandle for the readObject method on a Serializable class. The first argument of MethodHandle#invoke is the serializable object and the second argument is the ObjectInputStream passed to readObject.

Parameters
cl:Class<?>

a Serializable class

Returns:MethodHandle

a direct MethodHandle for the readObject method of the class or null if the class does not have a readObject method

readObjectNoDataForSerializationback to summary
public final MethodHandle readObjectNoDataForSerialization(Class<?> cl)

Returns a direct MethodHandle for the readObjectNoData method on a Serializable class. The only argument of MethodHandle#invoke is the serializable object, which readObjectNoData is called on. No arguments are passed to the readObjectNoData method.

Parameters
cl:Class<?>

a Serializable class

Returns:MethodHandle

a direct MethodHandle for the readObjectNoData method of the class or null if the class does not have a readObjectNoData method

readResolveForSerializationback to summary
public final MethodHandle readResolveForSerialization(Class<?> cl)

Returns a direct MethodHandle for the readResolve method on a serializable class. The single argument of MethodHandle#invoke is the serializable object.

Parameters
cl:Class<?>

the Serializable class

Returns:MethodHandle

a direct MethodHandle for the readResolve method of the class or null if the class does not have a readResolve method

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

Returns the declared serialPersistentFields from a serializable class, or null if none is declared, the field is declared but not valid, or the class is not a valid serializable class. A class is a valid serializable class if it implements Serializable but not Externalizable. The serialPersistentFields field is valid if it meets the type and accessibility restrictions defined by the Java Serialization specification.

Parameters
cl:Class<?>

a Serializable class

Returns:ObjectStreamField[]

the declared serialPersistentFields from a serializable class, or null if none is declared, the field is declared but not valid, or the class is not a valid serializable class

Since
24
writeObjectForSerializationback to summary
public final MethodHandle writeObjectForSerialization(Class<?> cl)

Returns a direct MethodHandle for the writeObject method on a Serializable class. The first argument of MethodHandle#invoke is the serializable object and the second argument is the ObjectOutputStream passed to writeObject.

Parameters
cl:Class<?>

a Serializable class

Returns:MethodHandle

a direct MethodHandle for the writeObject method of the class or null if the class does not have a writeObject method

writeReplaceForSerializationback to summary
public final MethodHandle writeReplaceForSerialization(Class<?> cl)

Returns a direct MethodHandle for the writeReplace method on a serializable class. The single argument of MethodHandle#invoke is the serializable object.

Parameters
cl:Class<?>

the Serializable class

Returns:MethodHandle

a direct MethodHandle for the writeReplace method of the class or null if the class does not have a writeReplace method