Top Description Fields Constructors Methods
org.hibernate.internal.util

public final Class ReflectHelper

extends Object
Class Inheritance
Annotations
@SuppressWarnings:unchecked
Imports
java.beans.Introspector, java.lang.reflect.AccessibleObject, .Constructor, .Field, .Member, .Method, .Modifier, .ParameterizedType, .TypeVariable, .WildcardType, java.util.Locale, org.hibernate.AssertionFailure, .MappingException, .PropertyNotFoundException, org.hibernate.boot.registry.classloading.spi.ClassLoaderService, .ClassLoadingException, org.hibernate.internal.util.collections.ArrayHelper, org.hibernate.property.access.internal.PropertyAccessStrategyMixedImpl, org.hibernate.property.access.spi.Getter, org.hibernate.type.BasicType, .Type, org.hibernate.type.descriptor.java.spi.PrimitiveJavaType, jakarta.persistence.Transient

Utility class for various reflection operations.
Authors
Gavin King, Steve Ebersole, Chris Cranford

Field Summary

Modifier and TypeField and Description
private static final Method
private static final Method
private static final Method
public static final Class<T>[]
private static final Method
private static final Method
private static final Class<?>
public static final Class<T>[]

Constructor Summary

AccessConstructor and Description
private
ReflectHelper()

Disallow instantiation of ReflectHelper.

Method Summary

Modifier and TypeMethod and Description
public static void
checkGetAndIsVariants(Class<T> containerClass, String propertyName, Method getMethod, Method isMethod)

public static Class<T>

Returns:

The class reference.
classForName
(String
The class name
name
,
Class<T>
The class from which this call originated (in order to access that class's loader).
caller
)

Perform resolution of a class name.

public static Class<T>

Returns:

The class reference.
classForName
(String
The class name
name
)

Deprecated Depending on context, either ClassLoaderService or org.hibernate.boot.spi.ClassLoaderAccess should be preferred
Perform resolution of a class name.
public static void
public static Method

Returns:

The equals method reference
extractEqualsMethod
(Class<T>
The class from which to extract the equals method.
clazz
)

Encapsulation of getting hold of a class's equals method.

public static Method

Returns:

The hashCode method reference
extractHashCodeMethod
(Class<T>
The class from which to extract the hashCode method.
clazz
)

Encapsulation of getting hold of a class's hashCode method.

public static Field
findField(Class<T> containerClass, String propertyName)

public static Method
findGetterMethod(Class<T> containerClass, String propertyName)

public static Method
findGetterMethodForFieldAccess(Field field, String propertyName)

Similar to getterMethodOrNull, except that here we are just looking for the corresponding getter for a field (defined as field access) if one exists.

public static Method
findSetterMethod(final Class<T> containerClass, final String propertyName, final Class<T> propertyType)

public static <T> Class<T>
getClass(Type type)

public static Constructor<T>

Returns:

The matching constructor.
getConstructor
(Class<T>
The class needing instantiation
clazz
,
Type[]
The types representing the required ctor param signature
types
)

Retrieve a constructor for the given class, with arguments matching the specified Hibernate mapping types.

public static <T> Constructor<T>
getConstructor(Class<T> clazz, Class<T>... constructorArgs)

public static <T> Constructor<T>

Returns:

The default constructor.
getDefaultConstructor
(Class<T>
The class for which to retrieve the default ctor.
clazz
)

Retrieve the default (no arg) constructor from the given class.

private static Method
getGetterOrNull(Class<T>[] interfaces, String propertyName)

public static Method

Returns:

The getter method, or null if there is none.
getGetterOrNull
(Class<T>
The Class which contains the property
containerClass
,
String
The name of the property
propertyName
)

Find the method that can be used as the getter for this property.

public static Method
getMethod(Class<T> clazz, Method method)

public static Method
getMethod(Class<T> clazz, String methodName, Class<T>... paramTypes)

public static Class<?>
public static String[]
public static Class<?>[]
private static Getter
getter(Class<T> clazz, String name)

public static Method
getterMethodOrNull(Class<T> containerJavaType, String propertyName)

public static boolean

Returns:

True if the class does implement the interface, false otherwise.
implementsInterface
(Class<T>
The class to check
clazz
,
Class<T>
The interface to check it against.
intf
)

Determine if the given class implements the given interface.

public static boolean

Returns:

True if the class is abstract, false otherwise.
isAbstractClass
(Class<T>
The class to check.
clazz
)

Determine if the given class is declared abstract.

public static boolean
isClass(Class<?> resultClass)

public static boolean

Returns:

True if the class is final, false otherwise.
isFinalClass
(Class<T>
The class to check.
clazz
)

Determine is the given class is declared final.

public static boolean

Returns:

True if the member is publicly accessible, false otherwise.
isPublic
(Class<T>
The class which defines the member
clazz
,
Member
The member.
member
)

Is this member publicly accessible.

public static boolean
isRecord(Class<?> declaringClass)

public static boolean
private static Field
locateField(Class<T> clazz, String propertyName)

public static boolean

Returns:

True if clazz defines an equals override.
overridesEquals
(Class<T>
The class to check
clazz
)

Determine if the given class defines an Object#equals override.

public static boolean

Returns:

True if clazz defines an hashCode override.
overridesHashCode
(Class<T>
The class to check
clazz
)

Determine if the given class defines a Object#hashCode override.

public static Class<T>

Returns:

The type of the property.
reflectedPropertyClass
(String
The name of the class owning the property.
className
,
String
The name of the property.
name
,
ClassLoaderService
ClassLoader services
classLoaderService
)

Attempt to resolve the specified property type through reflection.

public static Class<T>

Returns:

The type of the property.
reflectedPropertyClass
(Class<T>
The class owning the property.
clazz
,
String
The name of the property.
name
)

Attempt to resolve the specified property type through reflection.

public static Type
reflectedPropertyType(String className, String name, ClassLoaderService classLoaderService)

public static Method
setterMethodOrNull(final Class<T> containerClass, final String propertyName, final Class<T> propertyType)

public static Method
setterMethodOrNullBySetterName(final Class<T> containerClass, final String setterName, final Class<T> propertyType)

private static Method
setterOrNull(Class<T>[] interfaces, String propertyName, Class<T> propertyType)

private static Method
setterOrNull(Class<T> theClass, String propertyName, Class<T> propertyType)

private static Method
setterOrNullBySetterName(Class<T>[] interfaces, String setterName, Class<T> propertyType)

private static Method
setterOrNullBySetterName(Class<T> theClass, String setterName, Class<T> propertyType)

public static void
verifyNoGetVariantExists(Class<T> containerClass, String propertyName, Method isMethod, String stemName)

public static void
verifyNoIsVariantExists(Class<?> containerClass, String propertyName, Method getMethod, String stemName)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

GET_NAMEback to summary
private static final Method GET_NAME
GET_RECORD_COMPONENTSback to summary
private static final Method GET_RECORD_COMPONENTS
GET_TYPEback to summary
private static final Method GET_TYPE
NO_PARAM_SIGNATUREback to summary
public static final Class<T>[] NO_PARAM_SIGNATURE
OBJECT_EQUALSback to summary
private static final Method OBJECT_EQUALS
OBJECT_HASHCODEback to summary
private static final Method OBJECT_HASHCODE
RECORD_CLASSback to summary
private static final Class<?> RECORD_CLASS
SINGLE_OBJECT_PARAM_SIGNATUREback to summary
public static final Class<T>[] SINGLE_OBJECT_PARAM_SIGNATURE

Constructor Detail

ReflectHelperback to summary
private ReflectHelper()

Disallow instantiation of ReflectHelper.

Method Detail

checkGetAndIsVariantsback to summary
public static void checkGetAndIsVariants(Class<T> containerClass, String propertyName, Method getMethod, Method isMethod)
classForNameback to summary
public static Class<T> classForName(String name, Class<T> caller) throws ClassNotFoundException

Perform resolution of a class name.

Here we first check the context classloader, if one, before delegating to Class#forName(String, boolean, ClassLoader) using the caller's classloader

Parameters
name:String

The class name

caller:Class<T>

The class from which this call originated (in order to access that class's loader).

Returns:Class<T>

The class reference.

Exceptions
ClassNotFoundException:
From Class#forName(String, boolean, ClassLoader).
classForNameback to summary
public static Class<T> classForName(String name) throws ClassNotFoundException

Deprecated

Depending on context, either ClassLoaderService or org.hibernate.boot.spi.ClassLoaderAccess should be preferred

Perform resolution of a class name.

Same as classForName(String, Class) except that here we delegate to Class#forName(String) if the context classloader lookup is unsuccessful.

Parameters
name:String

The class name

Returns:Class<T>

The class reference.

Annotations
@Deprecated
Exceptions
ClassNotFoundException:
From Class#forName(String).
ensureAccessibilityback to summary
public static void ensureAccessibility(AccessibleObject accessibleObject)
extractEqualsMethodback to summary
public static Method extractEqualsMethod(Class<T> clazz) throws NoSuchMethodException

Encapsulation of getting hold of a class's equals method.

Parameters
clazz:Class<T>

The class from which to extract the equals method.

Returns:Method

The equals method reference

Exceptions
NoSuchMethodException:
Should indicate an attempt to extract equals method from interface.
extractHashCodeMethodback to summary
public static Method extractHashCodeMethod(Class<T> clazz) throws NoSuchMethodException

Encapsulation of getting hold of a class's hashCode method.

Parameters
clazz:Class<T>

The class from which to extract the hashCode method.

Returns:Method

The hashCode method reference

Exceptions
NoSuchMethodException:
Should indicate an attempt to extract hashCode method from interface.
findFieldback to summary
public static Field findField(Class<T> containerClass, String propertyName)
findGetterMethodback to summary
public static Method findGetterMethod(Class<T> containerClass, String propertyName)
findGetterMethodForFieldAccessback to summary
public static Method findGetterMethodForFieldAccess(Field field, String propertyName)

Similar to getterMethodOrNull, except that here we are just looking for the corresponding getter for a field (defined as field access) if one exists. We do not look at supers, although conceivably the super could declare the method as an abstract - but again, that is such an edge case...

findSetterMethodback to summary
public static Method findSetterMethod(final Class<T> containerClass, final String propertyName, final Class<T> propertyType)
getClassback to summary
public static <T> Class<T> getClass(Type type)
getConstructorback to summary
public static Constructor<T> getConstructor(Class<T> clazz, Type[] types) throws PropertyNotFoundException

Retrieve a constructor for the given class, with arguments matching the specified Hibernate mapping types.

Parameters
clazz:Class<T>

The class needing instantiation

types:Type[]

The types representing the required ctor param signature

Returns:Constructor<T>

The matching constructor.

Exceptions
PropertyNotFoundException:
Indicates we could not locate an appropriate constructor (todo : again with PropertyNotFoundException???)
getConstructorback to summary
public static <T> Constructor<T> getConstructor(Class<T> clazz, Class<T>... constructorArgs)
getDefaultConstructorback to summary
public static <T> Constructor<T> getDefaultConstructor(Class<T> clazz) throws PropertyNotFoundException

Retrieve the default (no arg) constructor from the given class.

Parameters
clazz:Class<T>

The class for which to retrieve the default ctor.

Returns:Constructor<T>

The default constructor.

Exceptions
PropertyNotFoundException:
Indicates there was not publicly accessible, no-arg constructor (todo : why PropertyNotFoundException???)
getGetterOrNullback to summary
private static Method getGetterOrNull(Class<T>[] interfaces, String propertyName)
getGetterOrNullback to summary
public static Method getGetterOrNull(Class<T> containerClass, String propertyName)

Find the method that can be used as the getter for this property.

Parameters
containerClass:Class<T>

The Class which contains the property

propertyName:String

The name of the property

Returns:Method

The getter method, or null if there is none.

Exceptions
MappingException:
If the containerClass has both a get- and an is- form.
getMethodback to summary
public static Method getMethod(Class<T> clazz, Method method)
getMethodback to summary
public static Method getMethod(Class<T> clazz, String methodName, Class<T>... paramTypes)
getPropertyTypeback to summary
public static Class<?> getPropertyType(Member member)
getRecordComponentNamesback to summary
public static String[] getRecordComponentNames(Class<?> javaType)
getRecordComponentTypesback to summary
public static Class<?>[] getRecordComponentTypes(Class<?> javaType)
getterback to summary
private static Getter getter(Class<T> clazz, String name) throws MappingException
getterMethodOrNullback to summary
public static Method getterMethodOrNull(Class<T> containerJavaType, String propertyName)
implementsInterfaceback to summary
public static boolean implementsInterface(Class<T> clazz, Class<T> intf)

Determine if the given class implements the given interface.

Parameters
clazz:Class<T>

The class to check

intf:Class<T>

The interface to check it against.

Returns:boolean

True if the class does implement the interface, false otherwise.

isAbstractClassback to summary
public static boolean isAbstractClass(Class<T> clazz)

Determine if the given class is declared abstract.

Parameters
clazz:Class<T>

The class to check.

Returns:boolean

True if the class is abstract, false otherwise.

isClassback to summary
public static boolean isClass(Class<?> resultClass)
isFinalClassback to summary
public static boolean isFinalClass(Class<T> clazz)

Determine is the given class is declared final.

Parameters
clazz:Class<T>

The class to check.

Returns:boolean

True if the class is final, false otherwise.

isPublicback to summary
public static boolean isPublic(Class<T> clazz, Member member)

Is this member publicly accessible.

Parameters
clazz:Class<T>

The class which defines the member

member:Member

The member.

Returns:boolean

True if the member is publicly accessible, false otherwise.

isRecordback to summary
public static boolean isRecord(Class<?> declaringClass)
isStaticFieldback to summary
public static boolean isStaticField(Field field)
locateFieldback to summary
private static Field locateField(Class<T> clazz, String propertyName)
overridesEqualsback to summary
public static boolean overridesEquals(Class<T> clazz)

Determine if the given class defines an Object#equals override.

Parameters
clazz:Class<T>

The class to check

Returns:boolean

True if clazz defines an equals override.

overridesHashCodeback to summary
public static boolean overridesHashCode(Class<T> clazz)

Determine if the given class defines a Object#hashCode override.

Parameters
clazz:Class<T>

The class to check

Returns:boolean

True if clazz defines an hashCode override.

reflectedPropertyClassback to summary
public static Class<T> reflectedPropertyClass(String className, String name, ClassLoaderService classLoaderService) throws MappingException

Attempt to resolve the specified property type through reflection.

Parameters
className:String

The name of the class owning the property.

name:String

The name of the property.

classLoaderService:ClassLoaderService

ClassLoader services

Returns:Class<T>

The type of the property.

Exceptions
MappingException:
Indicates we were unable to locate the property.
reflectedPropertyClassback to summary
public static Class<T> reflectedPropertyClass(Class<T> clazz, String name) throws MappingException

Attempt to resolve the specified property type through reflection.

Parameters
clazz:Class<T>

The class owning the property.

name:String

The name of the property.

Returns:Class<T>

The type of the property.

Exceptions
MappingException:
Indicates we were unable to locate the property.
reflectedPropertyTypeback to summary
public static Type reflectedPropertyType(String className, String name, ClassLoaderService classLoaderService) throws MappingException
setterMethodOrNullback to summary
public static Method setterMethodOrNull(final Class<T> containerClass, final String propertyName, final Class<T> propertyType)
setterMethodOrNullBySetterNameback to summary
public static Method setterMethodOrNullBySetterName(final Class<T> containerClass, final String setterName, final Class<T> propertyType)
setterOrNullback to summary
private static Method setterOrNull(Class<T>[] interfaces, String propertyName, Class<T> propertyType)
setterOrNullback to summary
private static Method setterOrNull(Class<T> theClass, String propertyName, Class<T> propertyType)
setterOrNullBySetterNameback to summary
private static Method setterOrNullBySetterName(Class<T>[] interfaces, String setterName, Class<T> propertyType)
setterOrNullBySetterNameback to summary
private static Method setterOrNullBySetterName(Class<T> theClass, String setterName, Class<T> propertyType)
verifyNoGetVariantExistsback to summary
public static void verifyNoGetVariantExists(Class<T> containerClass, String propertyName, Method isMethod, String stemName)
verifyNoIsVariantExistsback to summary
public static void verifyNoIsVariantExists(Class<?> containerClass, String propertyName, Method getMethod, String stemName)