jfun.yan.util
Class ReflectionUtil

java.lang.Object
  extended by jfun.yan.util.ReflectionUtil

public final class ReflectionUtil
extends java.lang.Object

The utility class to help reflection.

throughout this class, public members are defined as members defined with "public" keyword and declared in a public type. public members declared by a non-public class is considered non-public because access to it from outside is prohibited by the java access control anyway.

public members defined in public classes are always prefered even when we allow private/protected members and types to be visible. So if a non-public subtype and a public super type both have a field with the same name, the field in the public super type is always used.

Codehaus.org.

Author:
Ben Yu

Constructor Summary
ReflectionUtil()
           
 
Method Summary
static
<T> java.lang.reflect.Constructor<T>
getConstructor(java.lang.Class<T> c)
          To find the only public constructor in a class.
static
<T> java.lang.reflect.Constructor<T>
getConstructor(java.lang.Class<T> c, boolean suppress_security)
          To find the only constructor in a class.
static
<T> java.lang.reflect.Constructor<T>
getConstructor(java.lang.Class<T> c, java.lang.Class[] param_types)
          To find a public constructor in a class.
static
<T> java.lang.reflect.Constructor<T>
getConstructor(java.lang.Class<T> c, java.lang.Class[] param_types, boolean suppress_security)
          To find a constructor in a class.
static
<T> java.lang.reflect.Constructor<T>
getConstructor(java.lang.Class<T> c, int param_count)
          To find the only public constructor with the expected number of formal parameters in the class.
static
<T> java.lang.reflect.Constructor<T>
getConstructor(java.lang.Class<T> c, int param_count, boolean suppress_security)
          To find the only public constructor with the expected number of formal parameters in the class.
static java.lang.reflect.Field getField(java.lang.Class c, java.lang.String name)
          To find a public field in a class.
static java.lang.reflect.Field getField(java.lang.Class c, java.lang.String name, boolean suppress_security)
          To find a field in a class.
static java.lang.reflect.Field getInstanceField(java.lang.Class c, java.lang.String name)
          To find a public instance field in a class.
static java.lang.reflect.Field getInstanceField(java.lang.Class c, java.lang.String name, boolean suppress_security)
          To find an instance field in a class.
static java.lang.reflect.Method getInstanceMethod(java.lang.Class c, int param_count, java.lang.String name)
          To find a public instance method in a class.
static java.lang.reflect.Method getInstanceMethod(java.lang.Class c, java.lang.String name)
          To find a public instance method in a class.
static java.lang.reflect.Method getInstanceMethod(java.lang.Class c, java.lang.String name, boolean suppress_security)
          To find an instance method in a class.
static java.lang.reflect.Method getInstanceMethod(java.lang.Class c, java.lang.String name, int param_count, boolean suppress_security)
          To find an instance method in a class.
static java.lang.reflect.Method getMethod(java.lang.Class c, java.lang.String name)
          To find a public method in a class.
static java.lang.reflect.Method getMethod(java.lang.Class c, java.lang.String name, boolean suppress_security)
          To find a method in a class.
static java.lang.reflect.Method getMethod(java.lang.Class c, java.lang.String name, java.lang.Class[] param_types)
          To find a public method in a class with a certain signature.
static java.lang.reflect.Method getMethod(java.lang.Class c, java.lang.String name, java.lang.Class[] param_types, boolean suppress_security)
          To find a method in a class with a certain signature.
static java.lang.reflect.Method getMethod(java.lang.Class c, java.lang.String name, int param_count)
          To find a public method in a class.
static java.lang.reflect.Method getMethod(java.lang.Class c, java.lang.String name, int param_count, boolean suppress_security)
          To find a method in a class.
static java.lang.reflect.Field getStaticField(java.lang.Class c, java.lang.String name)
          To find a public static field in a class.
static java.lang.reflect.Field getStaticField(java.lang.Class c, java.lang.String name, boolean suppress_security)
          To find a static field in a class.
static java.lang.reflect.Method getStaticMethod(java.lang.Class c, java.lang.String name)
          To find a public static method in a class.
static java.lang.reflect.Method getStaticMethod(java.lang.Class c, java.lang.String name, boolean suppress_security)
          To find a static method in a class.
static java.lang.reflect.Method getStaticMethod(java.lang.Class c, java.lang.String name, int param_count)
          To find a public static method in a class.
static java.lang.reflect.Method getStaticMethod(java.lang.Class c, java.lang.String name, int param_count, boolean suppress_security)
          To find a static method in a class.
static boolean isAssignableFrom(java.lang.Class t1, java.lang.Class t2)
          To tell if an object of class t2 can be set to a parameter of type t1 using reflection.
static boolean isCompatible(java.lang.Class a, java.lang.Class b)
          To determine if two types are sub-type relationship.
static boolean isInstance(java.lang.Class t, java.lang.Object obj)
          To tell if an object can be assigned to a parameter type using reflection.
static java.lang.String toMethodString(java.lang.String name, int param_count)
          Create a string representation of a method with the given number of formal parameters.
static java.lang.Class toObjectType(java.lang.Class c)
          Convert a primitive type to the corresponding wrapper type if any.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionUtil

public ReflectionUtil()
Method Detail

toObjectType

public static java.lang.Class toObjectType(java.lang.Class c)
Convert a primitive type to the corresponding wrapper type if any.

Parameters:
c - the type that could be primitive.
Returns:
the wrapper type.

isAssignableFrom

public static boolean isAssignableFrom(java.lang.Class t1,
                                       java.lang.Class t2)
To tell if an object of class t2 can be set to a parameter of type t1 using reflection.

for example, Integer can be set to int.

Parameters:
t1 - the parameter type.
t2 - the object type.
Returns:
true if assignable.

isCompatible

public static boolean isCompatible(java.lang.Class a,
                                   java.lang.Class b)
To determine if two types are sub-type relationship.

Parameters:
a - the first type.
b - the second type.
Returns:
true if a is assignable from b or b is assignable from a.

isInstance

public static boolean isInstance(java.lang.Class t,
                                 java.lang.Object obj)
To tell if an object can be assigned to a parameter type using reflection.

for example, an object of Integer is an instance of int.

Parameters:
t - the parameter type.
obj - the object.
Returns:
true if the object can be viewed as an instance of the type.

getConstructor

public static <T> java.lang.reflect.Constructor<T> getConstructor(java.lang.Class<T> c,
                                                                  int param_count)
                                                       throws AmbiguityException
To find the only public constructor with the expected number of formal parameters in the class.

Parameters:
c - the type to find constructor.
param_count - the number of formal parameters.
Returns:
the Constructor object.
Throws:
AmbiguityException - if more than one are found.
java.lang.IllegalArgumentException - if no public constructor is found or if the class is not public.

getConstructor

public static <T> java.lang.reflect.Constructor<T> getConstructor(java.lang.Class<T> c,
                                                                  int param_count,
                                                                  boolean suppress_security)
To find the only public constructor with the expected number of formal parameters in the class.

Parameters:
c - the type to find constructor.
param_count - the number of formal parameters.
suppress_security - whether access to the private/protected/package private members is allowed.
Returns:
the Constructor object.
Throws:
AmbiguityException - if more than one are found.
java.lang.IllegalArgumentException - if no constructor is found.

getConstructor

public static <T> java.lang.reflect.Constructor<T> getConstructor(java.lang.Class<T> c)
                                                       throws AmbiguityException
To find the only public constructor in a class.

Parameters:
c - the type to find constructor.
Returns:
the Constructor object.
Throws:
AmbiguityException - if more than one are found.
java.lang.IllegalArgumentException - if no public constructor is found or if the class is not public.

getConstructor

public static <T> java.lang.reflect.Constructor<T> getConstructor(java.lang.Class<T> c,
                                                                  boolean suppress_security)
To find the only constructor in a class.

Parameters:
c - the type to find constructor.
suppress_security - whether access to the private/protected/package private members is allowed.
Returns:
the Constructor object.
Throws:
AmbiguityException - if more than one are found.
java.lang.IllegalArgumentException - if no constructor is found.

getConstructor

public static <T> java.lang.reflect.Constructor<T> getConstructor(java.lang.Class<T> c,
                                                                  java.lang.Class[] param_types)
To find a public constructor in a class.

Parameters:
c - the type to find constructor.
param_types - the parameter types.
Returns:
the Constructor object.
Throws:
java.lang.IllegalArgumentException - if constructor is not found or if the class is not public.

getConstructor

public static <T> java.lang.reflect.Constructor<T> getConstructor(java.lang.Class<T> c,
                                                                  java.lang.Class[] param_types,
                                                                  boolean suppress_security)
To find a constructor in a class.

Parameters:
c - the type to find constructor.
param_types - the parameter types.
suppress_security - whether access to the private/protected/package private members is allowed.
Returns:
the Constructor object.
Throws:
java.lang.IllegalArgumentException - if constructor is not found.

getStaticMethod

public static java.lang.reflect.Method getStaticMethod(java.lang.Class c,
                                                       java.lang.String name)
                                                throws AmbiguityException
To find a public static method in a class. There should be only one public static method with this name, otherwise considered ambiguity.

Parameters:
c - the type to search the method.
name - the method name.
Returns:
the Method object.
Throws:
AmbiguityException - when more than one methods are found.
java.lang.IllegalArgumentException - if the method is not found or if the class is not public.

getStaticMethod

public static java.lang.reflect.Method getStaticMethod(java.lang.Class c,
                                                       java.lang.String name,
                                                       boolean suppress_security)
                                                throws AmbiguityException
To find a static method in a class. There should be only one visible static method with this name, otherwise considered ambiguity.

Parameters:
c - the type to search the method.
name - the method name.
suppress_security - whether access to the private/protected/package private members is allowed.
Returns:
the Method object.
Throws:
AmbiguityException - if more than one methods are found.
java.lang.IllegalArgumentException - if the method is not found.

getStaticMethod

public static java.lang.reflect.Method getStaticMethod(java.lang.Class c,
                                                       java.lang.String name,
                                                       int param_count)
                                                throws AmbiguityException
To find a public static method in a class. There should be only one public static method with this name and the expected number of formal parameters, otherwise considered ambiguity.

Parameters:
c - the type to search the method.
name - the method name.
param_count - the number of formal parameters.
Returns:
the Method object.
Throws:
AmbiguityException - when more than one methods are found.
java.lang.IllegalArgumentException - if the method is not found or if the class is not public.

getStaticMethod

public static java.lang.reflect.Method getStaticMethod(java.lang.Class c,
                                                       java.lang.String name,
                                                       int param_count,
                                                       boolean suppress_security)
                                                throws AmbiguityException
To find a static method in a class. There should be only one visible static method with this name and the expected number of formal parameters, otherwise considered ambiguity.

Parameters:
c - the type to search the method.
name - the method name.
param_count - the number of formal parameters.
suppress_security - whether access to the private/protected/package private members is allowed.
Returns:
the Method object.
Throws:
AmbiguityException - if more than one methods are found.
java.lang.IllegalArgumentException - if the method is not found.

getField

public static java.lang.reflect.Field getField(java.lang.Class c,
                                               java.lang.String name)
To find a public field in a class.

Parameters:
c - the type to search the field.
name - the field name.
Returns:
the Field object.
Throws:
java.lang.IllegalArgumentException - if the field is not found.

getField

public static java.lang.reflect.Field getField(java.lang.Class c,
                                               java.lang.String name,
                                               boolean suppress_security)
To find a field in a class.

Parameters:
c - the type to search the field.
name - the field name.
suppress_security - whether access to the private/protected/package private members is allowed.
Returns:
the Field object.
Throws:
java.lang.IllegalArgumentException - if the field is not found.

getStaticField

public static java.lang.reflect.Field getStaticField(java.lang.Class c,
                                                     java.lang.String name)
To find a public static field in a class.

Parameters:
c - the type to search the field.
name - the field name.
Returns:
the Field object.
Throws:
java.lang.IllegalArgumentException - if the field is not found.

getStaticField

public static java.lang.reflect.Field getStaticField(java.lang.Class c,
                                                     java.lang.String name,
                                                     boolean suppress_security)
To find a static field in a class.

Parameters:
c - the type to search the field.
name - the field name.
suppress_security - whether access to the private/protected/package private members is allowed.
Returns:
the Field object.
Throws:
java.lang.IllegalArgumentException - if the field is not found.

getInstanceField

public static java.lang.reflect.Field getInstanceField(java.lang.Class c,
                                                       java.lang.String name)
To find a public instance field in a class.

Parameters:
c - the type to search the field.
name - the field name.
Returns:
the Field object.
Throws:
java.lang.IllegalArgumentException - if the field is not found.

getInstanceField

public static java.lang.reflect.Field getInstanceField(java.lang.Class c,
                                                       java.lang.String name,
                                                       boolean suppress_security)
To find an instance field in a class.

Parameters:
c - the type to search the field.
name - the field name.
suppress_security - whether access to the private/protected/package private members is allowed.
Returns:
the Field object.
Throws:
java.lang.IllegalArgumentException - if the field is not found.

getInstanceMethod

public static java.lang.reflect.Method getInstanceMethod(java.lang.Class c,
                                                         java.lang.String name)
                                                  throws AmbiguityException
To find a public instance method in a class. There should be only one public instance method with this name, otherwise considered ambiguity.

Parameters:
c - the type to search the method.
name - the method name.
Returns:
the Method object.
Throws:
AmbiguityException - if more than one methods are found.
java.lang.IllegalArgumentException - if the method is not found.

getInstanceMethod

public static java.lang.reflect.Method getInstanceMethod(java.lang.Class c,
                                                         java.lang.String name,
                                                         boolean suppress_security)
To find an instance method in a class. There should be only visible instance method with this name, otherwise considered ambiguity.

Parameters:
c - the type to search the method.
name - the method name.
suppress_security - whether access to the private/protected/package private members is allowed.
Returns:
the Method object.
Throws:
AmbiguityException - when more than one methods are found.
java.lang.IllegalArgumentException - if the method is not found.

getInstanceMethod

public static java.lang.reflect.Method getInstanceMethod(java.lang.Class c,
                                                         int param_count,
                                                         java.lang.String name)
                                                  throws AmbiguityException
To find a public instance method in a class. There should be only one public instance method with this name and the expected number of formal parameters, otherwise considered ambiguity.

Parameters:
c - the type to search the method.
name - the method name.
param_count - the number of formal parameters.
Returns:
the Method object.
Throws:
AmbiguityException - if more than one methods are found.
java.lang.IllegalArgumentException - if the method is not found.

getInstanceMethod

public static java.lang.reflect.Method getInstanceMethod(java.lang.Class c,
                                                         java.lang.String name,
                                                         int param_count,
                                                         boolean suppress_security)
To find an instance method in a class. There should be only visible instance method with this name and the expected number of formal parameters, otherwise considered ambiguity.

Parameters:
c - the type to search the method.
name - the method name.
param_count - the number of formal parameters.
suppress_security - whether access to the private/protected/package private members is allowed.
Returns:
the Method object.
Throws:
AmbiguityException - when more than one methods are found.
java.lang.IllegalArgumentException - if the method is not found.

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class c,
                                                 java.lang.String name)
To find a public method in a class. There should be only one public method with this name, otherwise considered ambiguity.

Parameters:
c - the type to search the method.
name - the method name.
Returns:
the Method object.
Throws:
AmbiguityException - if more than one are found.
java.lang.IllegalArgumentException - if the method is not found.

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class c,
                                                 java.lang.String name,
                                                 boolean suppress_security)
To find a method in a class. There should be only one visible method with this name, otherwise considered ambiguity.

Parameters:
c - the type to search the method.
name - the method name.
suppress_security - whether access to the private/protected/package private members is allowed.
Returns:
the Method object.
Throws:
java.lang.IllegalArgumentException - if the method is not found or more than one are found.

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class c,
                                                 java.lang.String name,
                                                 int param_count)
To find a public method in a class. There should be only one public method with this name and the expected number of formal parameters, otherwise considered ambiguity.

Parameters:
c - the type to search the method.
name - the method name.
param_count - the number of formal parameters.
Returns:
the Method object.
Throws:
AmbiguityException - if more than one are found.
java.lang.IllegalArgumentException - if the method is not found.

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class c,
                                                 java.lang.String name,
                                                 int param_count,
                                                 boolean suppress_security)
To find a method in a class. There should be only one visible method with this name and the expected number of formal parameters, otherwise considered ambiguity.

Parameters:
c - the type to search the method.
name - the method name.
param_count - the number of formal parameters.
suppress_security - whether access to the private/protected/package private members is allowed.
Returns:
the Method object.
Throws:
java.lang.IllegalArgumentException - if the method is not found or more than one are found.

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class c,
                                                 java.lang.String name,
                                                 java.lang.Class[] param_types)
To find a public method in a class with a certain signature.

Parameters:
c - the type to search the method.
name - the method name.
param_types - the parameter types.
Returns:
the Method object.
Throws:
java.lang.IllegalArgumentException - if the method is not found.

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class c,
                                                 java.lang.String name,
                                                 java.lang.Class[] param_types,
                                                 boolean suppress_security)
To find a method in a class with a certain signature.

Parameters:
c - the type to search the method.
name - the method name.
param_types - the parameter types.
suppress_security - whether access to the private/protected/package private members is allowed.
Returns:
the Method object.
Throws:
java.lang.IllegalArgumentException - if the method is not found.

toMethodString

public static java.lang.String toMethodString(java.lang.String name,
                                              int param_count)
Create a string representation of a method with the given number of formal parameters.

Parameters:
name - the method name.
param_count - the number of formal parameters.
Returns:
the string representation.