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, java.util.Locale, java.util.regex.Pattern, javax.persistence.Transient, org.hibernate.AssertionFailure, .MappingException, .PropertyNotFoundException, org.hibernate.boot.registry.classloading.spi.ClassLoaderService, .ClassLoadingException, org.hibernate.engine.spi.SessionFactoryImplementor, org.hibernate.property.access.internal.PropertyAccessStrategyMixedImpl, org.hibernate.property.access.spi.Getter, org.hibernate.type.PrimitiveType, .Type

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

Field Summary

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

Constructor Summary

AccessConstructor and Description
private
ReflectHelper()

Disallow instantiation of ReflectHelper.

Method Summary

Modifier and TypeMethod and Description
private 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
)

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 Object
getConstantValue(String name, SessionFactoryImplementor factory)

Deprecated as a consequence of SessionFactoryImplementor being deprecated.
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)

private static Method
getGetterOrNull(Class<T> containerClass, String propertyName)

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

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

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

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
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 Method
setterMethodOrNull(final Class<T> containerClass, final String propertyName, 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 void
verifyNoGetVariantExists(Class<T> containerClass, String propertyName, Method isMethod, String stemName)

private static void
verifyNoIsVariantExists(Class<T> containerClass, String propertyName, Method getMethod, String stemName)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

JAVA_CONSTANT_PATTERNback to summary
private static final Pattern JAVA_CONSTANT_PATTERN
NO_PARAM_SIGNATUREback to summary
public static final Class<T>[] NO_PARAM_SIGNATURE
NO_PARAMSback to summary
public static final Object[] NO_PARAMS
OBJECT_EQUALSback to summary
private static final Method OBJECT_EQUALS
OBJECT_HASHCODEback to summary
private static final Method OBJECT_HASHCODE
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
private 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

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)
getConstantValueback to summary
public static Object getConstantValue(String name, SessionFactoryImplementor factory)

Deprecated

as a consequence of SessionFactoryImplementor being deprecated. See corresponding docs for further information.

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
private static Method getGetterOrNull(Class<T> containerClass, String propertyName)
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)
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.

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.

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.
setterMethodOrNullback to summary
public static Method setterMethodOrNull(final Class<T> containerClass, final String propertyName, 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)
verifyNoGetVariantExistsback to summary
private static void verifyNoGetVariantExists(Class<T> containerClass, String propertyName, Method isMethod, String stemName)
verifyNoIsVariantExistsback to summary
private static void verifyNoIsVariantExists(Class<T> containerClass, String propertyName, Method getMethod, String stemName)