Top Description Fields Constructors Methods
org.hibernate.util

public final Class ReflectHelper

extends Object
Class Inheritance
Imports
java.lang.reflect.Constructor, .Member, .Method, .Modifier, org.hibernate.AssertionFailure, .MappingException, .PropertyNotFoundException, org.hibernate.property.BasicPropertyAccessor, .DirectPropertyAccessor, .Getter, .PropertyAccessor, org.hibernate.type.PrimitiveType, .Type

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

Field Summary

Modifier and TypeField and Description
private static final PropertyAccessor
private static final PropertyAccessor
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
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 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 Object

Returns:

The value
getConstantValue
(String
The name
name
)

Resolve a constant to its actual value.

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

public static Getter

Returns:

The getter.
getGetter
(Class<T>
The class owning the property
theClass
,
String
The name of the property
name
)

Directly retrieve the Getter reference via the BasicPropertyAccessor.

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

private static Getter
getter(Class<T> clazz, String name)

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, flase 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.
isPublic
(Member
The member to check
member
)

Is this member publicly accessible.

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 memeber.
member
)

Is this member publicly accessible.

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
)

Attempt to resolve the specified property type through reflection.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BASIC_PROPERTY_ACCESSORback to summary
private static final PropertyAccessor BASIC_PROPERTY_ACCESSOR
DIRECT_PROPERTY_ACCESSORback to summary
private static final PropertyAccessor DIRECT_PROPERTY_ACCESSOR
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

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.

Exceptions
ClassNotFoundException:
From Class#forName(String).
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.
getConstantValueback to summary
public static Object getConstantValue(String name)

Resolve a constant to its actual value.

Parameters
name:String

The name

Returns:Object

The value

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???)
getDefaultConstructorback to summary
public static 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???)
getGetterback to summary
public static Getter getGetter(Class<T> theClass, String name) throws MappingException

Directly retrieve the Getter reference via the BasicPropertyAccessor.

Parameters
theClass:Class<T>

The class owning the property

name:String

The name of the property

Returns:Getter

The getter.

Exceptions
MappingException:
Indicates we were unable to locate the property.
getMethodback to summary
public static Method getMethod(Class<T> clazz, Method method)
getterback to summary
private static Getter getter(Class<T> clazz, String name) throws MappingException
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, flase otherwise.

isPublicback to summary
public static boolean isPublic(Member member)

Is this member publicly accessible.

Short-hand for isPublic(Class, Member) passing the member + Member#getDeclaringClass()

Parameters
member:Member

The member to check

Returns:boolean

True if the member is publicly accessible.

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 memeber.

Returns:boolean

True if the member is publicly accessible, false otherwise.

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) 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.

Returns:Class<T>

The type of the property.

Exceptions
MappingException:
Indicates we were unable to locate the property.