org.modeshape.common.util
Class Reflection

java.lang.Object
  extended by org.modeshape.common.util.Reflection

@Immutable
public class Reflection
extends Object

Utility class for working reflectively with objects.


Constructor Summary
Reflection(Class<?> targetClass)
          Construct a Reflection instance that cache's some information about the target class.
 
Method Summary
static Class<?>[] buildArgumentClasses(Object... arguments)
          Build the list of classes that correspond to the list of argument objects.
static List<Class<?>> buildArgumentClassList(Object... arguments)
          Build the list of classes that correspond to the list of argument objects.
static List<Class<?>> convertArgumentClassesToPrimitives(Class<?>... arguments)
          Convert any argument classes to primitives.
 Method findBestMethodOnTarget(String methodName, Object... arguments)
          Find the best method on the target class that matches the signature specified with the specified name and the list of arguments.
 Method findBestMethodWithSignature(String methodName, Class<?>... argumentsClasses)
          Find the best method on the target class that matches the signature specified with the specified name and the list of argument classes.
 Method findFirstMethod(Pattern methodNamePattern)
          Find the method on the target class that matches the supplied method name.
 Method findFirstMethod(String methodName, boolean caseSensitive)
          Find the method on the target class that matches the supplied method name.
 Method[] findGetterMethods()
          Find the getter methods on the target class that begin with "get" or "is", that have no parameters, and that return something other than void.
 String[] findGetterPropertyNames()
          Find the property names with getter methods on the target class.
protected  String[] findMethodNames(String methodName)
           
 Method[] findMethods(Pattern methodNamePattern)
          Find the methods on the target class that matches the supplied method name.
 Method[] findMethods(String methodName, boolean caseSensitive)
          Find the method on the target class that matches the supplied method name.
static String getClassName(Class<?> clazz)
          Returns the name of the class.
 Class<?> getTargetClass()
          Return the class that is the target for the reflection methods.
 Object invokeBestMethodOnTarget(String[] methodNames, Object target, Object... arguments)
          Find and execute the best method on the target class that matches the signature specified with one of the specified names and the list of arguments.
 Object invokeGetterMethodOnTarget(String javaPropertyName, Object target)
          Find and execute the getter method on the target class for the supplied property name.
 Object invokeSetterMethodOnTarget(String javaPropertyName, Object target, Object argument)
          Find and execute the best setter method on the target class for the supplied property name and the supplied list of arguments.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Reflection

public Reflection(Class<?> targetClass)
Construct a Reflection instance that cache's some information about the target class. The target class is the Class object upon which the methods will be found.

Parameters:
targetClass - the target class
Throws:
IllegalArgumentException - if the target class is null
Method Detail

buildArgumentClasses

public static Class<?>[] buildArgumentClasses(Object... arguments)
Build the list of classes that correspond to the list of argument objects.

Parameters:
arguments - the list of argument objects.
Returns:
the list of Class instances that correspond to the list of argument objects; the resulting list will contain a null element for each null argument.

buildArgumentClassList

public static List<Class<?>> buildArgumentClassList(Object... arguments)
Build the list of classes that correspond to the list of argument objects.

Parameters:
arguments - the list of argument objects.
Returns:
the list of Class instances that correspond to the list of argument objects; the resulting list will contain a null element for each null argument.

convertArgumentClassesToPrimitives

public static List<Class<?>> convertArgumentClassesToPrimitives(Class<?>... arguments)
Convert any argument classes to primitives.

Parameters:
arguments - the list of argument classes.
Returns:
the list of Class instances in which any classes that could be represented by primitives (e.g., Boolean) were replaced with the primitive classes (e.g., Boolean.TYPE).

getClassName

public static String getClassName(Class<?> clazz)
Returns the name of the class. The result will be the fully-qualified class name, or the readable form for arrays and primitive types.

Parameters:
clazz - the class for which the class name is to be returned.
Returns:
the readable name of the class.

getTargetClass

public Class<?> getTargetClass()
Return the class that is the target for the reflection methods.

Returns:
the target class

findMethods

public Method[] findMethods(String methodName,
                            boolean caseSensitive)
Find the method on the target class that matches the supplied method name.

Parameters:
methodName - the name of the method that is to be found.
caseSensitive - true if the method name supplied should match case-sensitively, or false if case does not matter
Returns:
the Method objects that have a matching name, or an empty array if there are no methods that have a matching name.

findMethods

public Method[] findMethods(Pattern methodNamePattern)
Find the methods on the target class that matches the supplied method name.

Parameters:
methodNamePattern - the regular expression pattern for the name of the method that is to be found.
Returns:
the Method objects that have a matching name, or an empty array if there are no methods that have a matching name.

findGetterMethods

public Method[] findGetterMethods()
Find the getter methods on the target class that begin with "get" or "is", that have no parameters, and that return something other than void. This method skips the Object.getClass() method.

Returns:
the Method objects for the getters; never null but possibly empty

findGetterPropertyNames

public String[] findGetterPropertyNames()
Find the property names with getter methods on the target class. This method returns the property names for the methods returned by findGetterMethods().

Returns:
the Java Bean property names for the getters; never null but possibly empty

findFirstMethod

public Method findFirstMethod(String methodName,
                              boolean caseSensitive)
Find the method on the target class that matches the supplied method name.

Parameters:
methodName - the name of the method that is to be found.
caseSensitive - true if the method name supplied should match case-sensitively, or false if case does not matter
Returns:
the first Method object found that has a matching name, or null if there are no methods that have a matching name.

findFirstMethod

public Method findFirstMethod(Pattern methodNamePattern)
Find the method on the target class that matches the supplied method name.

Parameters:
methodNamePattern - the regular expression pattern for the name of the method that is to be found.
Returns:
the first Method object found that has a matching name, or null if there are no methods that have a matching name.

invokeBestMethodOnTarget

public Object invokeBestMethodOnTarget(String[] methodNames,
                                       Object target,
                                       Object... arguments)
                                throws NoSuchMethodException,
                                       SecurityException,
                                       IllegalArgumentException,
                                       IllegalAccessException,
                                       InvocationTargetException
Find and execute the best method on the target class that matches the signature specified with one of the specified names and the list of arguments. If no such method is found, a NoSuchMethodException is thrown.

This method is unable to find methods with signatures that include both primitive arguments and arguments that are instances of Number or its subclasses.

Parameters:
methodNames - the names of the methods that are to be invoked, in the order they are to be tried
target - the object on which the method is to be invoked
arguments - the array of Object instances that correspond to the arguments passed to the method.
Returns:
the Method object that references the method that satisfies the requirements, or null if no satisfactory method could be found.
Throws:
NoSuchMethodException - if a matching method is not found.
SecurityException - if access to the information is denied.
InvocationTargetException
IllegalAccessException
IllegalArgumentException

invokeSetterMethodOnTarget

public Object invokeSetterMethodOnTarget(String javaPropertyName,
                                         Object target,
                                         Object argument)
                                  throws NoSuchMethodException,
                                         SecurityException,
                                         IllegalArgumentException,
                                         IllegalAccessException,
                                         InvocationTargetException
Find and execute the best setter method on the target class for the supplied property name and the supplied list of arguments. If no such method is found, a NoSuchMethodException is thrown.

This method is unable to find methods with signatures that include both primitive arguments and arguments that are instances of Number or its subclasses.

Parameters:
javaPropertyName - the name of the property whose setter is to be invoked, in the order they are to be tried
target - the object on which the method is to be invoked
argument - the new value for the property
Returns:
the result of the setter method, which is typically null (void)
Throws:
NoSuchMethodException - if a matching method is not found.
SecurityException - if access to the information is denied.
InvocationTargetException
IllegalAccessException
IllegalArgumentException

invokeGetterMethodOnTarget

public Object invokeGetterMethodOnTarget(String javaPropertyName,
                                         Object target)
                                  throws NoSuchMethodException,
                                         SecurityException,
                                         IllegalArgumentException,
                                         IllegalAccessException,
                                         InvocationTargetException
Find and execute the getter method on the target class for the supplied property name. If no such method is found, a NoSuchMethodException is thrown.

Parameters:
javaPropertyName - the name of the property whose getter is to be invoked, in the order they are to be tried
target - the object on which the method is to be invoked
Returns:
the property value (the result of the getter method call)
Throws:
NoSuchMethodException - if a matching method is not found.
SecurityException - if access to the information is denied.
InvocationTargetException
IllegalAccessException
IllegalArgumentException

findMethodNames

protected String[] findMethodNames(String methodName)

findBestMethodOnTarget

public Method findBestMethodOnTarget(String methodName,
                                     Object... arguments)
                              throws NoSuchMethodException,
                                     SecurityException
Find the best method on the target class that matches the signature specified with the specified name and the list of arguments. This method first attempts to find the method with the specified arguments; if no such method is found, a NoSuchMethodException is thrown.

This method is unable to find methods with signatures that include both primitive arguments and arguments that are instances of Number or its subclasses.

Parameters:
methodName - the name of the method that is to be invoked.
arguments - the array of Object instances that correspond to the arguments passed to the method.
Returns:
the Method object that references the method that satisfies the requirements, or null if no satisfactory method could be found.
Throws:
NoSuchMethodException - if a matching method is not found.
SecurityException - if access to the information is denied.

findBestMethodWithSignature

public Method findBestMethodWithSignature(String methodName,
                                          Class<?>... argumentsClasses)
                                   throws NoSuchMethodException,
                                          SecurityException
Find the best method on the target class that matches the signature specified with the specified name and the list of argument classes. This method first attempts to find the method with the specified argument classes; if no such method is found, a NoSuchMethodException is thrown.

Parameters:
methodName - the name of the method that is to be invoked.
argumentsClasses - the list of Class instances that correspond to the classes for each argument passed to the method.
Returns:
the Method object that references the method that satisfies the requirements, or null if no satisfactory method could be found.
Throws:
NoSuchMethodException - if a matching method is not found.
SecurityException - if access to the information is denied.


Copyright © 2008-2010 JBoss, a division of Red Hat. All Rights Reserved.