Package org.hibernate.internal.util
Class ReflectHelper
java.lang.Object
org.hibernate.internal.util.ReflectHelper
Utility class for various reflection operations.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic void
checkGetAndIsVariants
(Class<?> containerClass, String propertyName, Method getMethod, Method isMethod) static Class<?>
classForName
(String name) Deprecated.static Class<?>
classForName
(String name, Class<?> caller) Perform resolution of a class name.static void
ensureAccessibility
(AccessibleObject accessibleObject) static Method
extractEqualsMethod
(Class<?> clazz) Encapsulation of getting hold of a class'sequals
method.static Method
extractHashCodeMethod
(Class<?> clazz) Encapsulation of getting hold of a class'shashCode
method.static Field
static Method
findGetterMethod
(Class<?> containerClass, String propertyName) static Method
findGetterMethodForFieldAccess
(Field field, String propertyName) Similar togetterMethodOrNull(java.lang.Class<?>, java.lang.String)
, except that here we are just looking for the corresponding getter for a field (defined as field access) if one exists.static Method
findSetterMethod
(Class<?> containerClass, String propertyName, Class<?> propertyType) static <T> Class<T>
static <T> Constructor<T>
getConstructorOrNull
(Class<T> clazz, Class<?>... constructorArgs) Retrieve a constructor for the given class, with arguments matching the specified Java types, or returnnull
if no such constructor exists.static <T> Constructor<T>
getDefaultConstructor
(Class<T> clazz) Retrieve the default (no arg) constructor from the given class.static <T> Supplier<T>
getDefaultSupplier
(Class<T> clazz) static Method
getGetterOrNull
(Class<?> containerClass, String propertyName) Find the method that can be used as the getter for this property.static Method
static Method
static Class<?>
getPropertyType
(Member member) static String[]
getRecordComponentNames
(Class<?> javaType) static Class<?>[]
getRecordComponentTypes
(Class<?> javaType) static Method
getterMethodOrNull
(Class<?> containerJavaType, String propertyName) static boolean
implementsInterface
(Class<?> clazz, Class<?> intf) Deprecated, for removal: This API element is subject to removal in a future version.No longer usedstatic boolean
isAbstractClass
(Class<?> clazz) Determine if the given class is declared abstract.static boolean
static boolean
isFinalClass
(Class<?> clazz) Determine is the given class is declared final.static boolean
Is this member publicly accessible.static boolean
static boolean
isStaticField
(Field field) static boolean
overridesEquals
(Class<?> clazz) Determine if the given class defines anObject.equals(java.lang.Object)
override.static boolean
overridesHashCode
(Class<?> clazz) Determine if the given class defines aObject.hashCode()
override.static Class<?>
reflectedPropertyClass
(Class<?> clazz, String name) Attempt to resolve the specified property type through reflection.static Class<?>
reflectedPropertyClass
(String className, String name, ClassLoaderService classLoaderService) Attempt to resolve the specified property type through reflection.static Type
reflectedPropertyType
(String className, String name, ClassLoaderService classLoaderService) static Method
setterMethodOrNull
(Class<?> containerClass, String propertyName, Class<?> propertyType) static Method
setterMethodOrNullBySetterName
(Class<?> containerClass, String setterName, Class<?> propertyType) static void
verifyNoGetVariantExists
(Class<?> containerClass, String propertyName, Method isMethod, String stemName) static void
verifyNoIsVariantExists
(Class<?> containerClass, String propertyName, Method getMethod, String stemName)
-
Field Details
-
NO_PARAM_SIGNATURE
-
SINGLE_OBJECT_PARAM_SIGNATURE
-
-
Method Details
-
extractEqualsMethod
Encapsulation of getting hold of a class'sequals
method.- Parameters:
clazz
- The class from which to extract the equals method.- Returns:
- The equals method reference
- Throws:
NoSuchMethodException
- Should indicate an attempt to extract equals method from interface.
-
extractHashCodeMethod
Encapsulation of getting hold of a class'shashCode
method.- Parameters:
clazz
- The class from which to extract the hashCode method.- Returns:
- The hashCode method reference
- Throws:
NoSuchMethodException
- Should indicate an attempt to extract hashCode method from interface.
-
overridesEquals
Determine if the given class defines anObject.equals(java.lang.Object)
override.- Parameters:
clazz
- The class to check- Returns:
- True if clazz defines an equals override.
-
overridesHashCode
Determine if the given class defines aObject.hashCode()
override.- Parameters:
clazz
- The class to check- Returns:
- True if clazz defines an hashCode override.
-
implementsInterface
@Deprecated(since="7", forRemoval=true) public static boolean implementsInterface(Class<?> clazz, Class<?> intf) Deprecated, for removal: This API element is subject to removal in a future version.No longer usedDetermine if the given class implements the given interface.- Parameters:
clazz
- The class to checkintf
- The interface to check it against.- Returns:
- True if the class does implement the interface, false otherwise.
-
classForName
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
- The class namecaller
- The class from which this call originated (in order to access that class's loader).- Returns:
- The class reference.
- Throws:
ClassNotFoundException
- FromClass.forName(String, boolean, ClassLoader)
.
-
classForName
Deprecated.Depending on context, eitherClassLoaderService
orClassLoaderAccess
should be preferredPerform resolution of a class name.Same as
classForName(String, Class)
except that here we delegate toClass.forName(String)
if the context classloader lookup is unsuccessful.- Parameters:
name
- The class name- Returns:
- The class reference.
- Throws:
ClassNotFoundException
- FromClass.forName(String)
.
-
isPublic
Is this member publicly accessible.- Parameters:
clazz
- The class which defines the membermember
- The member.- Returns:
- True if the member is publicly accessible, false otherwise.
-
reflectedPropertyClass
public static Class<?> reflectedPropertyClass(String className, String name, ClassLoaderService classLoaderService) throws MappingException Attempt to resolve the specified property type through reflection.- Parameters:
className
- The name of the class owning the property.name
- The name of the property.classLoaderService
- ClassLoader services- Returns:
- The type of the property.
- Throws:
MappingException
- Indicates we were unable to locate the property.
-
reflectedPropertyType
public static Type reflectedPropertyType(String className, String name, ClassLoaderService classLoaderService) throws MappingException - Throws:
MappingException
-
reflectedPropertyClass
Attempt to resolve the specified property type through reflection.- Parameters:
clazz
- The class owning the property.name
- The name of the property.- Returns:
- The type of the property.
- Throws:
MappingException
- Indicates we were unable to locate the property.
-
getDefaultConstructor
public static <T> Constructor<T> getDefaultConstructor(Class<T> clazz) throws PropertyNotFoundException Retrieve the default (no arg) constructor from the given class.- Parameters:
clazz
- The class for which to retrieve the default ctor.- Returns:
- The default constructor.
- Throws:
PropertyNotFoundException
- Indicates there was not publicly accessible, no-arg constructor (todo : why PropertyNotFoundException???)
-
getDefaultSupplier
-
isAbstractClass
Determine if the given class is declared abstract.- Parameters:
clazz
- The class to check.- Returns:
- True if the class is abstract, false otherwise.
-
isFinalClass
Determine is the given class is declared final.- Parameters:
clazz
- The class to check.- Returns:
- True if the class is final, false otherwise.
-
getConstructorOrNull
Retrieve a constructor for the given class, with arguments matching the specified Java types, or returnnull
if no such constructor exists.- Parameters:
clazz
- The class needing instantiationconstructorArgs
- The types representing the required ctor param signature- Returns:
- The matching constructor, or
null
-
getMethod
-
getMethod
-
findField
-
ensureAccessibility
-
isStaticField
-
findGetterMethod
-
getGetterOrNull
Find the method that can be used as the getter for this property.- Parameters:
containerClass
- The Class which contains the propertypropertyName
- The name of the property- Returns:
- The getter method, or
null
if there is none. - Throws:
MappingException
- If thecontainerClass
has both a get- and an is- form.
-
verifyNoIsVariantExists
-
checkGetAndIsVariants
-
verifyNoGetVariantExists
-
getterMethodOrNull
-
setterMethodOrNull
-
setterMethodOrNullBySetterName
-
findSetterMethod
-
findGetterMethodForFieldAccess
Similar togetterMethodOrNull(java.lang.Class<?>, java.lang.String)
, 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...
-
isRecord
-
getRecordComponentTypes
-
getRecordComponentNames
-
getClass
-
getPropertyType
-
isClass
-
ClassLoaderService
orClassLoaderAccess
should be preferred