Package io.jans.orm.reflect.util
Class ReflectHelper
java.lang.Object
io.jans.orm.reflect.util.ReflectHelper
Utility class for various reflection operations.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Annotation[]static final Class<?>[]static final Object[] -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanassignableFrom(Class<?> clazz, Class<?> intf) Determine if the given class implements or extend the given class.static Class<?>classForName(String name) Perform resolution of a class name.static Class<?>classForName(String name, Class<?> caller) Perform resolution of a class name.static voidcopyObjectPropertyValues(Object fromEntry, Object toEntry, Getter[] propertyGetters, Setter[] propertySetters) static ObjectcreateArray(Class<?> clazz, int length) static <T> TcreateObjectByDefaultConstructor(Class<T> clazz) static MethodextractEqualsMethod(Class<?> clazz) Encapsulation of getting hold of a class'sequalsmethod.static MethodextractHashCodeMethod(Class<?> clazz) Encapsulation of getting hold of a class'shashCodemethod.static AnnotationgetAnnotationByType(Annotation[] annotations, Class<?> annotationType) static AnnotationgetAnnotationByType(List<Annotation> annotations, Class<?> annotationType) static List<Annotation>getClassAnnotations(Class<?> theClass, Class<?>... allowedAnnotations) static Constructor<?>getConstructor(Class<?> clazz, Class<?>... parameterTypes) Retrieve a constructor for the given class, with arguments matching the specified Hibernate mapping.static <T> Constructor<T>getDefaultConstructor(Class<T> clazz) Retrieve the default (no arg) constructor from the given class.static GetterDirectly retrieve theGetterreference via theBasicPropertyAccessor.static Class<?>getListType(Getter getter) static Class<?>getListType(Setter setter) static Methodstatic Methodstatic GettergetMethodOrPropertyGetter(Class<?> theClass, String name) static SettergetMethodOrPropertySetter(Class<?> theClass, String name) static Map<String,List<Annotation>> getPropertiesAnnotations(Class<?> theClass, Class<?>... allowedAnnotations) static List<Annotation>getPropertyAnnotations(Class<?> theClass, String propertyName, Class<?>... allowedAnnotations) static StringgetPropertyNameByType(Map<String, List<Annotation>> propertiesAnnotations, Class<?> annotationType) static ObjectgetPropertyValue(Object entry, Getter[] propertyGetters) static SetterDirectly retrieve theSetterreference via theBasicPropertyAccessor.static Class<?>getSetterType(Setter setter) static Objectstatic booleanisAbstractClass(Class<?> clazz) Determine if the given class is declared abstract.static booleanisFinalClass(Class<?> clazz) Determine is the given class is declared final.static booleanisNotPrimitiveClass(Class<?> theClass) static booleanIs this member publicly accessible.static booleanIs this member publicly accessible.static booleanoverridesEquals(Class<?> clazz) Determine if the given class defines anObject.equals(java.lang.Object)override.static booleanoverridesHashCode(Class<?> clazz) Determine if the given class defines aObject.hashCode()override.static Class<?>reflectedPropertyClass(String className, String name) Attempt to resolve the specified property type through reflection.static voidsumObjectPropertyValues(Object resultEntry, Object entryToAdd, Getter[] propertyGetters, Setter[] propertySetters)
-
Field Details
-
NO_PARAM_SIGNATURE
-
NO_PARAMS
-
NO_ANNOTATIONS
-
-
Method Details
-
extractEqualsMethod
Encapsulation of getting hold of a class'sequalsmethod.- 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'shashCodemethod.- 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.
-
assignableFrom
Determine if the given class implements or extend the given class.- 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 toClass.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
Perform resolution of a class name.
Same asclassForName(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.
Short-hand forisPublic(Class, Member)passing the member +Member.getDeclaringClass()- Parameters:
member- The member to check- Returns:
- True if the member is publicly accessible.
-
isPublic
Is this member publicly accessible.- Parameters:
clazz- The class which defines the membermember- The memeber.- Returns:
- True if the member is publicly accessible, false otherwise.
-
reflectedPropertyClass
public static Class<?> reflectedPropertyClass(String className, String name) 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.- Returns:
- The type of the property.
- Throws:
MappingException- Indicates we were unable to locate the property.
-
getGetter
Directly retrieve theGetterreference via theBasicPropertyAccessor.- Parameters:
theClass- The class owning the propertyname- The name of the property- Returns:
- The getter.
- Throws:
MappingException- Indicates we were unable to locate the property.
-
getMethodOrPropertyGetter
public static Getter getMethodOrPropertyGetter(Class<?> theClass, String name) throws MappingException - Throws:
MappingException
-
getSetter
Directly retrieve theSetterreference via theBasicPropertyAccessor.- Parameters:
theClass- The class owning the propertyname- The name of the property- Returns:
- The setter.
- Throws:
MappingException- Indicates we were unable to locate the property.
-
getMethodOrPropertySetter
public static Setter getMethodOrPropertySetter(Class<?> theClass, String name) throws MappingException - Throws:
MappingException
-
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???)
-
createObjectByDefaultConstructor
public static <T> T createObjectByDefaultConstructor(Class<T> clazz) throws PropertyNotFoundException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException -
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, flase otherwise.
-
getConstructor
public static Constructor<?> getConstructor(Class<?> clazz, Class<?>... parameterTypes) throws PropertyNotFoundException Retrieve a constructor for the given class, with arguments matching the specified Hibernate mapping.- Parameters:
clazz- The class needing instantiationparameterTypes- The types representing the required ctor param signature- Returns:
- The matching constructor.
- Throws:
PropertyNotFoundException- Indicates we could not locate an appropriate constructor (todo : again with PropertyNotFoundException???)
-
getMethod
-
getAnnotationByType
-
getAnnotationByType
-
getPropertyNameByType
-
isNotPrimitiveClass
-
getClassAnnotations
public static List<Annotation> getClassAnnotations(Class<?> theClass, Class<?>... allowedAnnotations) throws PropertyNotFoundException - Throws:
PropertyNotFoundException
-
getPropertyAnnotations
public static List<Annotation> getPropertyAnnotations(Class<?> theClass, String propertyName, Class<?>... allowedAnnotations) throws PropertyNotFoundException - Throws:
PropertyNotFoundException
-
getPropertiesAnnotations
-
getListType
-
getListType
-
getSetterType
-
getValue
- Throws:
MappingException
-
createArray
-
getPropertyValue
-
copyObjectPropertyValues
-
sumObjectPropertyValues
-
getMethod
public static Method getMethod(Class<?> clazz, String methodName, Class<?>[] methodParams) throws NoSuchMethodException - Throws:
NoSuchMethodException
-