com.neeve.util
Class UtlReflection

java.lang.Object
  extended by com.neeve.util.UtlReflection

public class UtlReflection
extends Object

Reflection related utilities.

This class IS NOT intended to be used by end users.


Constructor Summary
UtlReflection()
           
 
Method Summary
static void appendMethodAndCodeSource(Method method, StringBuilder builder)
          Append the method name and code source.
static Collection<Field> collectAllFields(Class<?> clazz)
           
static void collectAllFields(Class<?> clazz, Collection<Field> target)
          Collects non public method from the given class and its supertypes.
static Collection<Method> collectAllMethods(Class<?> clazz)
          Collects public and non public method from the given class and its supertypes (excluding those methods from Object itself)
static void collectNonPublicMethods(Class<?> clazz, Collection<Method> target)
          Collects non public method from the given class and its supertypes.
static
<T extends Annotation>
T
getAnnotation(Method method, Class<T> annotationClass)
          Tests if an annotation is present on a method, or present on a super class or interface declaration of the method.
static Set<String> getDecamelCasedNonObjectProperties(Class<?> type)
          Gets the properties of a class excluding properties from java.lang.Object This method return the properties for the given class eliminating possible duplicates from decamelcasing.
static Set<String> getDecamelcasedProperties(Class<?> type)
          Gets the properties of a class.
static Set<String> getDeclaredProperties(Class<?> type)
          Gets the properties declared by the class (not including super type properties)
static Method getGetter(Class<?> type, String property)
          Gets the accessor method for a given property of the type provided.
static boolean getHasPropertyCheckerValue(Object object, String name)
          Checks if an object has a hasXXX method for the given non nested field and if so will call it and return its value.
static Method getMethod(Class<?> type, String name, Class<?>... parameterTypes)
          Looks up a method for a class.
static Method getNonNestedAccessor(Class<?> type, String field)
          Gets the accessor for the given field or null if no such field accessor can be found.
static Method getNonNestedChecker(Class<?> type, String field)
          Gets the check for the given field or null if no such field checker can be found.
static
<T> T
getNonNestedProperty(Object object, String field)
          Gets a property for the object at the given bean path.
static Set<String> getNonObjectProperties(Class<?> type)
          Gets the properties of a class excluding properties from java.lang.Object Note this method returns property names in both first letter lowercase and first letter uppercase form.
static Set<String> getProperties(Class<?> type)
          Gets the properties of a class.
static
<T> T
getProperty(Object object, int pathOffset, String... path)
          Traverses the given bean path from the specified index to retrieve a value on the provided object.
static
<T> T
getProperty(Object object, String path)
          Gets a property for the object at the given bean path.
static Class<?> getUnwrappedReturnType(Class<?> type, String path)
          Gets the return type of the accessor at the given bean path relative to the provided type.
static boolean hasPath(Class<?> type, String path)
          Indicate whether the given type has the provided, possibly nested, unambiguous bean/field path.
static boolean hasPath(Object object, String path)
          Indicate whether the given object has the provided, possibly nested, bean/field path.
static boolean isAnnotationPresent(Method method, Class<? extends Annotation> annotationClass)
          Tests if an annotation is present on a method, or present on a super class declaration of the method.
static void setNonNestedProperty(Object object, String property, Object value)
          Looks for a setter method for the given property in the provided target object, and attempts to invoke the method with the supplied value.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UtlReflection

public UtlReflection()
Method Detail

collectNonPublicMethods

public static void collectNonPublicMethods(Class<?> clazz,
                                           Collection<Method> target)
Collects non public method from the given class and its supertypes.

Parameters:
clazz - The class for which to collect methods.
target - The target.

collectAllMethods

public static Collection<Method> collectAllMethods(Class<?> clazz)
Collects public and non public method from the given class and its supertypes (excluding those methods from Object itself)

Parameters:
clazz - The class for which to collect methods.

collectAllFields

public static Collection<Field> collectAllFields(Class<?> clazz)
Parameters:
clazz - The class for which to collect fields.
Returns:
The collection of all fields for the class.

collectAllFields

public static void collectAllFields(Class<?> clazz,
                                    Collection<Field> target)
Collects non public method from the given class and its supertypes.

Parameters:
clazz - The class for which to collect methods.
target - The target.

getUnwrappedReturnType

public static final Class<?> getUnwrappedReturnType(Class<?> type,
                                                    String path)
Gets the return type of the accessor at the given bean path relative to the provided type.

Parameters:
type - The type at the bean path root.
path - The bean path of the field relative to the type
Returns:
The return type or null if the bean path can't be unambiguously applied to the type to resolve a return type.

getGetter

public static Method getGetter(Class<?> type,
                               String property)
Gets the accessor method for a given property of the type provided.

Parameters:
type - The type
property - The bean path of the property relative to the type.
Returns:
The accessor for the property.

hasPath

public static boolean hasPath(Class<?> type,
                              String path)
Indicate whether the given type has the provided, possibly nested, unambiguous bean/field path.

Note that a return value of false does not mean that a concrete instance of the class would yield false for hasPath(Object, String). For example consider:

 Record.class, "attachment.zipCode"
 
if Record.getAttachment return type Object.class, then if an instance of Record has an Address object as its attachment field, it would yield a return value of true for zipCode while this method would return false.

Parameters:
type - The type to check.
path - The path to check.
Returns:
true If the path exists.

hasPath

public static boolean hasPath(Object object,
                              String path)
Indicate whether the given object has the provided, possibly nested, bean/field path.

Parameters:
object - The type to check.
path - The path to check.
Returns:
true If the path exists.

getProperty

public static <T> T getProperty(Object object,
                                String path)
Gets a property for the object at the given bean path. The bean path is treated as a standard bean path. This method will recurse into nested '.' separated values.

Parameters:
object - The object to introspect.
path - The bean path which may be a '.' separated nested path.
Returns:
The value at the given bean path or null if any intervening value is null or non existent

setNonNestedProperty

public static void setNonNestedProperty(Object object,
                                        String property,
                                        Object value)
                                 throws NoSuchMethodException,
                                        IllegalArgumentException,
                                        IllegalAccessException,
                                        InvocationTargetException
Looks for a setter method for the given property in the provided target object, and attempts to invoke the method with the supplied value.

Parameters:
object - The object on which to set the property.
property - The property name.
value - The value to set the property to (conversion will be attempted).
Throws:
NoSuchMethodException - if no property can be found for the given class.
InvocationTargetException - If there is an error setting the property.
IllegalAccessException - If the setter method is not accessible.
IllegalArgumentException - If the arguments are of the wrong type and cannot be converted.

getProperty

public static <T> T getProperty(Object object,
                                int pathOffset,
                                String... path)
Traverses the given bean path from the specified index to retrieve a value on the provided object.

Parameters:
object - The object to introspect.
pathOffset - The offset into the path to use for the bean path.
path - The bean path (potentially offset by path offset).
Returns:
The value at the given bean path or null if any intervening value is null or non existent

getNonNestedProperty

public static <T> T getNonNestedProperty(Object object,
                                         String field)
Gets a property for the object at the given bean path. The bean path must not be a nested bean path.

Parameters:
object - The object to introspect.
field - The bean path which may not be a '.' separated nested path.
Returns:
The value at the given bean path or null if any intervening value is null or non existent

getHasPropertyCheckerValue

public static final boolean getHasPropertyCheckerValue(Object object,
                                                       String name)
Checks if an object has a hasXXX method for the given non nested field and if so will call it and return its value. Otherwise, true is returned. This method allows distinguishing between and unset 'default' or null property value vs. an explicitly set default value.

Parameters:
object - The object.
name - The property.

getNonNestedAccessor

public static Method getNonNestedAccessor(Class<?> type,
                                          String field)
Gets the accessor for the given field or null if no such field accessor can be found.

Parameters:
type - The type.
field - The fields bean path name
Returns:
The accessor method for the field.

getNonNestedChecker

public static Method getNonNestedChecker(Class<?> type,
                                         String field)
Gets the check for the given field or null if no such field checker can be found. A checker method takes the form of has and returns boolean.

Parameters:
type - The type.
field - The fields bean path name
Returns:
The accessor method for the field.

getProperties

public static final Set<String> getProperties(Class<?> type)
Gets the properties of a class.

Note this method returns property names in both first letter lowercase and first letter uppercase form. Use getDecamelcasedProperties(Class) to avoid duplicates.

Parameters:
type - The type for which to return properties.

getDecamelcasedProperties

public static final Set<String> getDecamelcasedProperties(Class<?> type)
Gets the properties of a class.

This method return the properties for the given class eliminating possible duplicates from decamelcasing.

Parameters:
type - The type for which to return properties.

getNonObjectProperties

public static final Set<String> getNonObjectProperties(Class<?> type)
Gets the properties of a class excluding properties from java.lang.Object

Note this method returns property names in both first letter lowercase and first letter uppercase form. Use getDecamelcasedProperties(Class) to avoid duplicates.

Parameters:
type - The type for which to return properties.

getDecamelCasedNonObjectProperties

public static final Set<String> getDecamelCasedNonObjectProperties(Class<?> type)
Gets the properties of a class excluding properties from java.lang.Object

This method return the properties for the given class eliminating possible duplicates from decamelcasing.

Parameters:
type - The type for which to return properties.

getDeclaredProperties

public static Set<String> getDeclaredProperties(Class<?> type)
Gets the properties declared by the class (not including super type properties)

Parameters:
type - The type for which to return properties.

appendMethodAndCodeSource

public static void appendMethodAndCodeSource(Method method,
                                             StringBuilder builder)
Append the method name and code source.

For example:

 public void com.foo.Bar.someMethod(int) [/libs/myjar.jar]
 

Parameters:
method - The method.
builder - The builder to which to append.

getMethod

public static Method getMethod(Class<?> type,
                               String name,
                               Class<?>... parameterTypes)
Looks up a method for a class. This method checks the declared method for a given type for a method of the matching signature, and if not found then will check the supertype for a matching method if a super type exists. This method returns methods of any visibility.

Parameters:
type - The Type to search for the given method.
name - The method name.
Returns:
The method or null if no such method is found.

getAnnotation

public static <T extends Annotation> T getAnnotation(Method method,
                                                     Class<T> annotationClass)
Tests if an annotation is present on a method, or present on a super class or interface declaration of the method.

Parameters:
method - The method.
annotationClass - The annotation.
Returns:
True if the annotation is present.

isAnnotationPresent

public static boolean isAnnotationPresent(Method method,
                                          Class<? extends Annotation> annotationClass)
Tests if an annotation is present on a method, or present on a super class declaration of the method.

Parameters:
method - The method.
annotationClass - The annotation.
Returns:
True if the annotation is present.


Copyright © 2016 Neeve Research, LLC. All Rights Reserved.