DESY ACOP Beans Home

com.cosylab.introspection
Class ClassIntrospector

java.lang.Object
  extended by com.cosylab.introspection.ClassIntrospector

public class ClassIntrospector
extends java.lang.Object

This class provides helper methods to easily manipulate an analyze classes and their members.

Author:
Miha Kadunc

Constructor Summary
ClassIntrospector()
           
 
Method Summary
static boolean areInModifiers(int targetModifiers, int modifiers)
          Returns true if the targetModifiers contains all of the specified modifiers.
static java.lang.Class forName(java.lang.String className)
           
static java.lang.reflect.Member[] getDeclaredMembers(java.lang.Class theClass, boolean fields, boolean constructors, boolean methods)
          Returns declared members of the specified class.
static java.lang.reflect.Field[] getEnumFields(java.lang.Class theClass)
          Returns array of fields are decleared by Java Enum pattern.
static java.lang.Object[] getEnums(java.lang.Class theClass)
          If class can be interpreted as Java enum pattern, then returns array this class instances as they would be enums.
static java.lang.reflect.Member[] getForModifiers(java.lang.reflect.Member[] members, int modifiers)
          Selects members that have the specified modifiers from an array of Member instances.
static java.lang.reflect.Member[] getForParameterTypes(java.lang.reflect.Member[] members, java.lang.Class[] parameterTypes)
          Returns all members (methods and constructors) that take the same parameters.
static java.lang.reflect.Member[] getForReturnType(java.lang.reflect.Member[] members, java.lang.Class returnType)
          Returns all members (methods and fields) that have the same declared return type (or field type).
static java.lang.reflect.Member[] getMembers(java.lang.Class theClass, boolean fields, boolean constructors, boolean methods)
          Returns all members of the specified class.
static java.lang.reflect.Member[] getMembers(java.lang.Class theClass, MemberFilter filter)
          Returns all members of the specified class that are accepted by the filter object passed as a parameter.
static java.lang.reflect.Method getMethod(java.lang.Class targetClass, java.lang.String name, java.lang.Class[] paramTypes)
           
static java.lang.String[] getParameterNames(java.lang.reflect.Member m)
          Returns names of parameters of a given method.
static java.lang.Class getPrimitiveWrapper(java.lang.Class primitiveType)
           
static java.lang.reflect.Member[] getPrivate(java.lang.reflect.Member[] members)
          Returns all private members from an array of Member instances.
static java.lang.reflect.Member[] getProtected(java.lang.reflect.Member[] members)
          Returns all protected members from an array of Member instances.
static java.lang.reflect.Member[] getPublic(java.lang.reflect.Member[] members)
          Returns all public members from an array of Member instances.
static java.lang.reflect.Member[] getPublicMembers(java.lang.Class theClass, boolean fields, boolean constructors, boolean methods)
          Returns all public members of the specified class.
static boolean isCompatible(java.lang.Class objectType, java.lang.Class targetType)
          Determines whether the objectType class can be used in introspection operations to set a value which is declared to be of the targetType class.
static boolean isEnum(java.lang.Class theClass)
          Identifies the Java Enum pattern.
static boolean isPrimitiveWrapper(java.lang.Class wrapperType)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClassIntrospector

public ClassIntrospector()
Method Detail

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class targetClass,
                                                 java.lang.String name,
                                                 java.lang.Class[] paramTypes)

forName

public static java.lang.Class forName(java.lang.String className)
                               throws java.lang.ClassNotFoundException
Throws:
java.lang.ClassNotFoundException

getDeclaredMembers

public static java.lang.reflect.Member[] getDeclaredMembers(java.lang.Class theClass,
                                                            boolean fields,
                                                            boolean constructors,
                                                            boolean methods)
Returns declared members of the specified class. By setting the fields, constructors or methods flags users can choose whether the returned members should contain these items.

Parameters:
theClass - Class
fields - boolean
constructors - boolean
methods - boolean
Returns:
Member[]

isEnum

public static boolean isEnum(java.lang.Class theClass)
Identifies the Java Enum pattern.

Parameters:
theClass - - the class that is to be inspected
Returns:
boolean

getPrimitiveWrapper

public static java.lang.Class getPrimitiveWrapper(java.lang.Class primitiveType)
Parameters:
primitiveType -
Returns:
the wrapper of the passed primitive type.

isPrimitiveWrapper

public static boolean isPrimitiveWrapper(java.lang.Class wrapperType)

isCompatible

public static boolean isCompatible(java.lang.Class objectType,
                                   java.lang.Class targetType)
Determines whether the objectType class can be used in introspection operations to set a value which is declared to be of the targetType class.

This method returns targetType.isAssignableFrom(objectType) for object types, and also returns true when targetType is primitive and objectType is its object wrapper.


getEnumFields

public static java.lang.reflect.Field[] getEnumFields(java.lang.Class theClass)
Returns array of fields are decleared by Java Enum pattern.

Parameters:
theClass - - the class that is to be inspected and interpreted as enum
Returns:
array of enum java.lang.reflect.Field objects

getEnums

public static java.lang.Object[] getEnums(java.lang.Class theClass)
                                   throws java.lang.IllegalAccessException
If class can be interpreted as Java enum pattern, then returns array this class instances as they would be enums. Method tries to order them by value() field.

Parameters:
theClass - - the class that is to be inspected and interpreted as enum
Returns:
array of enum instances objects
Throws:
java.lang.IllegalAccessException

getForModifiers

public static java.lang.reflect.Member[] getForModifiers(java.lang.reflect.Member[] members,
                                                         int modifiers)
Selects members that have the specified modifiers from an array of Member instances.

Parameters:
members - Member[]
modifiers - int - as specified by the java.lang.reflect.Modifier
Returns:
Member[]

getForParameterTypes

public static java.lang.reflect.Member[] getForParameterTypes(java.lang.reflect.Member[] members,
                                                              java.lang.Class[] parameterTypes)
Returns all members (methods and constructors) that take the same parameters.

Parameters:
members - Member[]
parameterTypes - Class[]
Returns:
Member[]

getForReturnType

public static java.lang.reflect.Member[] getForReturnType(java.lang.reflect.Member[] members,
                                                          java.lang.Class returnType)
Returns all members (methods and fields) that have the same declared return type (or field type).

Parameters:
members - Member[]
returnType - Class
Returns:
Member[]

getMembers

public static java.lang.reflect.Member[] getMembers(java.lang.Class theClass,
                                                    boolean fields,
                                                    boolean constructors,
                                                    boolean methods)
Returns all members of the specified class. By setting the fields, constructors or methods flags users can choose whether the returned members should contain these items.

Parameters:
theClass - Class
fields - boolean
constructors - boolean
methods - boolean
Returns:
Member[]

getPrivate

public static java.lang.reflect.Member[] getPrivate(java.lang.reflect.Member[] members)
Returns all private members from an array of Member instances.

Parameters:
members - Member[]
Returns:
Member[]

getProtected

public static java.lang.reflect.Member[] getProtected(java.lang.reflect.Member[] members)
Returns all protected members from an array of Member instances.

Parameters:
members - Member[]
Returns:
Member[]

getPublic

public static java.lang.reflect.Member[] getPublic(java.lang.reflect.Member[] members)
Returns all public members from an array of Member instances.

Parameters:
members - Member[]
Returns:
Member[]

getPublicMembers

public static java.lang.reflect.Member[] getPublicMembers(java.lang.Class theClass,
                                                          boolean fields,
                                                          boolean constructors,
                                                          boolean methods)
Returns all public members of the specified class. By setting the fields, constructors or methods flags users can choose whether the returned members should contain these items.

Parameters:
theClass - Class
fields - boolean
constructors - boolean
methods - boolean
Returns:
Member[]

getParameterNames

public static java.lang.String[] getParameterNames(java.lang.reflect.Member m)
Returns names of parameters of a given method.

Parameters:
m - Member
Returns:
String[]

areInModifiers

public static boolean areInModifiers(int targetModifiers,
                                     int modifiers)
Returns true if the targetModifiers contains all of the specified modifiers.

Parameters:
targetModifiers - int
modifiers - int
Returns:
boolean

getMembers

public static java.lang.reflect.Member[] getMembers(java.lang.Class theClass,
                                                    MemberFilter filter)
Returns all members of the specified class that are accepted by the filter object passed as a parameter.

Parameters:
theClass - Class
filter - MemberFilter
Returns:
Member[]


Copyright © 2010. All Rights Reserved.