Package org.jppf.utils
Class ReflectionUtils
java.lang.Object
org.jppf.utils.ReflectionUtils
Collection of static utility methods for dealing with reflection-based APIs.
- Author:
- Laurent Cohen
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Class<?>[]createTypeArray(Object[] args) Generate an array of the types of the specified arguments.static ObjectPerform a deep copy of an object.static StringdumpObject(Object o, String... names) Generates a string that displays all return values for all getters of an object.static StringdumpObject(Object o, String separator, boolean displaySimpleClassName, boolean displayHashCode, String... names) Generates a string that displays all return values for all getters of an object.static Method[]getAllBeanMethods(Class<?> clazz, boolean getters) Obtain all the getters or setters of a specified class.static StringGet the name of the method that called this one.static StringGet the name of the current class in the call stack.static StringGet the name of the method that called this one.static MethodGet a getter with a given name from a class.static MethodgetGetterForAttribute(Class<?> clazz, String attrName) Get a getter corresponding to a specified instance variable name from a class.static Constructor<?>getMatchingConstructor(Class<?> clazz, Object[] args) Get the constructor with in the specified declaring class, that matches the number, order and types of the specified arguments.static MethodgetMatchingMethod(Class<?> clazz, String name, Object[] args) Get the method with the specified name, in the specified declaring class, that matches the number, order and types of the specified arguments.static StringgetMBeanAttributeName(Method meth) Get the name of an MBean attribute based on a getter or setter method.static StringgetMBeanAttributeName(String name) Get the name of an MBean attribute based on the name of a getter or setter method.static MethodGet a setter with a given name from a class.static MethodgetSetterForAttribute(Class<?> clazz, String attrName) Get a setter corresponding to a specified instance variable name from a class.static booleanDetermines whether a method is a getter (accessor), according to Sun's naming conventions.static booleanDetermines whether a method is a setter (mutator), according to Sun's naming conventions.static Class<?>mapPrimitveType(Class<?> type) Map a primitive type to its corresponding wrapper type.static booleanmatchingTypes(Class<?>[] argTypes, Class<?>[] types) Determine whether a set of (possibly null) types loosely matches another set of types.static booleansameSignature(Method method1, Method method2) Determine whether two methids have the same signature.static StringsimpleDump(Object o) Returns a string in format simple_class_name@identity_hashcode from the specified object.
-
Constructor Details
-
ReflectionUtils
public ReflectionUtils()
-
-
Method Details
-
dumpObject
Generates a string that displays all return values for all getters of an object. This method uses a new line character a field separator.- Parameters:
o- the object whose getter return values are to be dumped into a string.names- the names of the attributes to dump. If null or empty, then all attributes that have a getter, except "class", are dumped. This parameter is used as a filter on the attributes to dump and defines the order in which the fields are dumped.- Returns:
- a string with the classname, hashcode, and the value of each attribute that has a corresponding getter.
-
dumpObject
public static String dumpObject(Object o, String separator, boolean displaySimpleClassName, boolean displayHashCode, String... names) Generates a string that displays all return values for all getters of an object.- Parameters:
o- the object whose getter return values are to be dumped into a string.separator- separator between field values, like a comma or a new line.displaySimpleClassName-trueto use the object's class simple name,falsefor the fully qualified name.displayHashCode- whether to append '@hashcode_value' to the name of the class.names- the names of the attributes to dump. If null or empty, then all attributes that have a getter, except "class", are dumped. This parameter is used as a filter on the attributes to dump and defines the order in which the fields are dumped.- Returns:
- a string with the classname, hashcode, and the value of each attribute that has a corresponding getter.
-
getMBeanAttributeName
Get the name of an MBean attribute based on a getter or setter method.- Parameters:
meth- the method to analyse.- Returns:
- the name of an MBean attribute following the
javax.managementcoventions, ornullif the input method is not a setter or getter.
-
getMBeanAttributeName
Get the name of an MBean attribute based on the name of a getter or setter method.- Parameters:
name- the name of the method to analyse.- Returns:
- the name of an MBean attribute following the
javax.managementcoventions, ornullif the input method is not a setter or getter.
-
isGetter
Determines whether a method is a getter (accessor), according to Sun's naming conventions.- Parameters:
meth- the method to analyse.- Returns:
trueif the method is a getter,falseotherwise.
-
isSetter
Determines whether a method is a setter (mutator), according to Sun's naming conventions.- Parameters:
meth- the method to analyse.- Returns:
trueif the method is a setter,falseotherwise.
-
getGetter
Get a getter with a given name from a class.- Parameters:
clazz- the class enclosing the getter.name- the name of the getter to look for.- Returns:
- a
Methodobject, or null if the class has no getter with the specified name.
-
getSetter
Get a setter with a given name from a class.- Parameters:
clazz- the class enclosing the setter.name- the name of the setter to look for.- Returns:
- a
Methodobject, or null if the class has no setter with the specified name.
-
getGetterForAttribute
Get a getter corresponding to a specified instance variable name from a class.- Parameters:
clazz- the class enclosing the instance variable.attrName- the name of the instance variable to look for.- Returns:
- a
Methodobject, or null if the class has no getter for the specified instance variable name.
-
getSetterForAttribute
Get a setter corresponding to a specified instance variable name from a class.- Parameters:
clazz- the class enclosing the instance variable.attrName- the name of the instance variable to look for.- Returns:
- a
Methodobject, or null if the class has no setter for the specified instance variable name.
-
getAllBeanMethods
Obtain all the getters or setters of a specified class.- Parameters:
clazz- the class to get the methods from.getters- if true, indicates that the getters should be looked up, otherwise it should be the setters.- Returns:
- an array of
Methodinstances.
-
deepCopy
Perform a deep copy of an object. This method uses reflection to perform the copy through public accessors and mutators (getters and setters).- Parameters:
o- the object to copy.- Returns:
- an object whose state is a copy of that of the input object.
-
getMatchingMethod
Get the method with the specified name, in the specified declaring class, that matches the number, order and types of the specified arguments.- Parameters:
clazz- the class in which to look for the method.name- the name of the method to look for.args- the arguments of the method.- Returns:
- a matching
Methodinstance, or null if no match could be found.
-
getMatchingConstructor
Get the constructor with in the specified declaring class, that matches the number, order and types of the specified arguments.- Parameters:
clazz- the class in which to look for the constructor.args- the arguments of the method.- Returns:
- a matching
Constructorinstance, or null if no match could be found.
-
matchingTypes
Determine whether a set of (possibly null) types loosely matches another set of types.- Parameters:
argTypes- the types to match, null values are considered wildcards (matching any type).types- the set of types to match.- Returns:
- true if the methods match, false otherwise.
-
mapPrimitveType
Map a primitive type to its corresponding wrapper type.- Parameters:
type- a primitive type.- Returns:
- a
Classinstance.
-
createTypeArray
Generate an array of the types of the specified arguments. If the array of arguments is null this method will return an empty array.- Parameters:
args- the arguments to get the types from.- Returns:
- an array of
Classinstances.
-
getCurrentMethodName
Get the name of the method that called this one.- Returns:
- the name of the invoking method as a string.
-
getCurrentClassAndMethod
Get the name of the method that called this one.- Returns:
- the name of the invoking method as a string.
-
getCurrentClassName
Get the name of the current class in the call stack.- Returns:
- the name of the invoking method as a string.
-
simpleDump
Returns a string in format simple_class_name@identity_hashcode from the specified object.- Parameters:
o- the object to dump.- Returns:
- a string representing the specified object, or "null" if the object is null.
-
sameSignature
Determine whether two methids have the same signature.- Parameters:
method1- the first method to compare.method2- the second method to compare.- Returns:
trueif the methods have the same signature,falseotherwise.
-