net.sf.spif.util
Class Reflection

java.lang.Object
  |
  +--net.sf.spif.util.Reflection

public class Reflection
extends java.lang.Object

Static convenience methods for finding methods and making reflection invocation methods without being drowned in exception handling

Version:
$Id: Reflection.java,v 1.3 2003/02/06 22:55:11 bratseth Exp $
Author:
Jon S Bratseth

Method Summary
static java.lang.reflect.Method findAdderMatching(java.lang.Class type, java.lang.reflect.Method collectionGetter)
          Returns the first (any) adder matching the given getter collection, or null if none is found
static java.lang.reflect.Method findAdderMatching(java.lang.Class type, java.lang.reflect.Method collectionGetter, java.lang.Class adderType)
          Returns an adder matching the given getter collection, or null if none is found
static java.util.List findCollectionGetters(java.lang.Class type)
          Returns the collection getters of a given type.
static java.lang.reflect.Constructor findConstructor(java.lang.Class type, java.lang.Class[] argTypes)
          Finds a constructor and returns it, or null if it was not found
static java.lang.reflect.Method findGetter(java.lang.Class type, java.lang.String propertyName)
          Returns a getter for the given name, or null if there is no such getter Case is not significant.
static java.util.List findGetters(java.lang.Class type)
          Returns all the getters in the given type.
static java.lang.reflect.Method findMethod(java.lang.Class type, java.lang.String name)
          Finds a method witrh no arguments and returns it, or null if it was not found
static java.lang.reflect.Method findMethod(java.lang.Class type, java.lang.String name, java.lang.Class[] argTypes)
          Finds a method and returns it, or null if it was not found
static java.lang.reflect.Method findSetter(java.lang.Class type, java.lang.String propertyName, java.lang.Class argType)
          Returns a setter for the given name, or null if there is no such setter.
static java.lang.reflect.Method findSetterMatching(java.lang.Class type, java.lang.reflect.Method getter)
          Returns a setter matching the given getter, or null if none is found
static java.util.List findSetters(java.lang.Class type)
          Returns all the setters in the given type.
static java.lang.Object invoke(java.lang.reflect.Constructor constructor)
          Invokes a constructor taking no arguments
static java.lang.Object invoke(java.lang.reflect.Constructor constructor, java.lang.Object argument)
          Invokes a constructor taking one argument
static java.lang.Object invoke(java.lang.reflect.Constructor constructor, java.lang.Object[] arguments)
          Invokes a constructor
static java.lang.Object invoke(java.lang.Object on, java.lang.reflect.Method method)
          Invokes a no-argument method on an object
static java.lang.Object invoke(java.lang.Object on, java.lang.reflect.Method method, java.lang.Object argument)
          Invokes a single argument method on an object
static java.lang.Object invoke(java.lang.Object on, java.lang.reflect.Method method, java.lang.Object[] arguments)
          Invokes a method on an object
static java.lang.Object newInstance(java.lang.Class type)
          Creates an instance of the given class if possible
static java.lang.Object newInstance(java.lang.Class type, java.lang.Object argument)
          Creates an instance of the given class if possible
static java.lang.String toName(java.lang.Class type)
          Returns the short name of a class
static java.lang.String toPropertyName(java.lang.reflect.Method accessor)
          Returns the name of the property the given method is a getter or setter for, in lowercase only.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

invoke

public static java.lang.Object invoke(java.lang.Object on,
                                      java.lang.reflect.Method method)
                               throws ReflectionException
Invokes a no-argument method on an object

Throws:
a - runtime ReflectionException for any error
ReflectionException

invoke

public static java.lang.Object invoke(java.lang.Object on,
                                      java.lang.reflect.Method method,
                                      java.lang.Object argument)
                               throws ReflectionException
Invokes a single argument method on an object

Throws:
a - runtime ReflectionException for any error
ReflectionException

invoke

public static java.lang.Object invoke(java.lang.Object on,
                                      java.lang.reflect.Method method,
                                      java.lang.Object[] arguments)
                               throws ReflectionException
Invokes a method on an object

Throws:
a - runtime ReflectionException for any error
ReflectionException

invoke

public static java.lang.Object invoke(java.lang.reflect.Constructor constructor)
Invokes a constructor taking no arguments

Throws:
a - runtime ReflectionException for any error

invoke

public static java.lang.Object invoke(java.lang.reflect.Constructor constructor,
                                      java.lang.Object argument)
Invokes a constructor taking one argument

Throws:
a - runtime ReflectionException for any error

invoke

public static java.lang.Object invoke(java.lang.reflect.Constructor constructor,
                                      java.lang.Object[] arguments)
                               throws ReflectionException
Invokes a constructor

Throws:
a - runtime ReflectionException for any error
ReflectionException

newInstance

public static java.lang.Object newInstance(java.lang.Class type)
Creates an instance of the given class if possible

Returns:
the instance or null if there is no default constructor in the class
Throws:
ReflectionException - if an instance could not be created

newInstance

public static java.lang.Object newInstance(java.lang.Class type,
                                           java.lang.Object argument)
Creates an instance of the given class if possible

Returns:
the instance or null if there is no matching constructor in the class

findConstructor

public static java.lang.reflect.Constructor findConstructor(java.lang.Class type,
                                                            java.lang.Class[] argTypes)
Finds a constructor and returns it, or null if it was not found


findMethod

public static java.lang.reflect.Method findMethod(java.lang.Class type,
                                                  java.lang.String name)
Finds a method witrh no arguments and returns it, or null if it was not found


findMethod

public static java.lang.reflect.Method findMethod(java.lang.Class type,
                                                  java.lang.String name,
                                                  java.lang.Class[] argTypes)
Finds a method and returns it, or null if it was not found


findSetterMatching

public static java.lang.reflect.Method findSetterMatching(java.lang.Class type,
                                                          java.lang.reflect.Method getter)
Returns a setter matching the given getter, or null if none is found


findAdderMatching

public static java.lang.reflect.Method findAdderMatching(java.lang.Class type,
                                                         java.lang.reflect.Method collectionGetter,
                                                         java.lang.Class adderType)
Returns an adder matching the given getter collection, or null if none is found

Parameters:
type - the type containing the adder
collectionGetter - the getter to match
adderType - the type of the adder, or null to return the first name match

findAdderMatching

public static java.lang.reflect.Method findAdderMatching(java.lang.Class type,
                                                         java.lang.reflect.Method collectionGetter)
Returns the first (any) adder matching the given getter collection, or null if none is found

Parameters:
type - the type containing the adder
collectionGetter - the getter to match

findGetter

public static java.lang.reflect.Method findGetter(java.lang.Class type,
                                                  java.lang.String propertyName)
Returns a getter for the given name, or null if there is no such getter Case is not significant.


findSetter

public static java.lang.reflect.Method findSetter(java.lang.Class type,
                                                  java.lang.String propertyName,
                                                  java.lang.Class argType)
Returns a setter for the given name, or null if there is no such setter. Case is not significant.


findGetters

public static java.util.List findGetters(java.lang.Class type)
Returns all the getters in the given type. A getter is a public, non-static method beginning with "get" or "is" and taking no arguments.

Returns:
a List of all the getters in the given type, or an emtpy list if there is no getters

findCollectionGetters

public static java.util.List findCollectionGetters(java.lang.Class type)
Returns the collection getters of a given type. Collection getters are public, non-static no-arg methods of the form get*Iterator, and returning an iterator


findSetters

public static java.util.List findSetters(java.lang.Class type)
Returns all the setters in the given type. A getter is a public, non-static method beginning with "set" and taking one argument.

Returns:
a List of all the setters in the given type, or an emtpy list if there is no setters

toName

public static java.lang.String toName(java.lang.Class type)
Returns the short name of a class


toPropertyName

public static java.lang.String toPropertyName(java.lang.reflect.Method accessor)
Returns the name of the property the given method is a getter or setter for, in lowercase only. Works for collection getters too.

Returns:
the property name, or null if the given method is not an accessor


Spif 1.01 (2003-04-03). http://spif.sf.net