org.arakhne.vmutil
Class ReflectionUtil

java.lang.Object
  extended by org.arakhne.vmutil.ReflectionUtil

public class ReflectionUtil
extends Object

This utility class provides a way to extend the reflection API and the Class class with autoboxing-compliant functions.

Since:
since JDK 1.5
Version:
6.3 (rev:285) 2011-10-14 12:02:41
Author:
Stéphane GALLAND
Maven Group Id:
org.arakhne.afc
Maven Artifact Id:
arakhneVmutils

Constructor Summary
ReflectionUtil()
           
 
Method Summary
static Class<?> forName(String name)
          Replies the type that corresponds to the specified class.
static Class<?> forName(String name, boolean typeInitialization, ClassLoader loader)
          Replies the type that corresponds to the specified class.
static Class<?> forName(String name, ClassLoader loader)
          Replies the type that corresponds to the specified class.
static
<T> Set<Class<?>>
getAllDirectInterfaces(Class<? extends T> lowestType, Class<T> highestType)
          Determines the interfaces implemented by the classes from the lowest type to the highestType which are extended the given interfaceType.
static
<T,I> Set<Class<? extends I>>
getAllDirectInterfaces(Class<? extends T> lowestType, Class<T> highestType, Class<I> interfaceType)
          Determines the interfaces implemented by the classes from the lowest type to the highestType which are extended the given interfaceType.
static Class<?> getCommonType(Class<?> type1, Class<?> type2)
          Replies the top-most type which is common to both given types.
static Class<?> getCommonType(Object instance1, Object instance2)
          Replies the top-most type which is common to both given objects.
static Collection<Class<?>> getPackageClasses(Package pkg)
          Replies the list of the classes in the given package.
static Collection<Class<?>> getPackageClasses(String packageName)
          Replies the list of the classes in the given package.
static
<T> Collection<Class<? extends T>>
getSubClasses(Class<T> className)
          Replies the list of all the subclasses of the given class in the current classpath.
static
<T> void
getSubClasses(Class<T> className, boolean allowAbstract, boolean allowInterface, boolean allowEnum, Collection<Class<? extends T>> result)
          Replies the list of all the subclasses of the given class in the current classpath.
static
<T> Collection<Class<? super T>>
getSuperClasses(Class<T> className)
          Replies the list of all the superclasses of the given class.
static boolean isAssignableFrom(Class<?> assignementTarget, Class<?> assignementSource)
          Determines if the assignmentTarget object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified assignementSource parameter.
static boolean isInstance(Class<?> type, Object obj)
          Determines if the specified Object is assignment-compatible with the object represented by the Class.
 
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

isInstance

public static boolean isInstance(Class<?> type,
                                 Object obj)
Determines if the specified Object is assignment-compatible with the object represented by the Class. This method extends Class.isInstance(Object) with autoboxing support.

Parameters:
type - is the class against which the object must be test
obj - is the object to check
Returns:
true if obj is an instance of the type
See Also:
Class.isInstance(Object)

isAssignableFrom

public static boolean isAssignableFrom(Class<?> assignementTarget,
                                       Class<?> assignementSource)
Determines if the assignmentTarget object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified assignementSource parameter. This method extends Class.isAssignableFrom(Class) with autoboxing support.

Parameters:
assignementTarget - is the class that is tested to be a super class.
assignementSource - is the class that is tested to be a sub class.
Returns:
true if an object of the assignementSource type could be assigned to a variable of assignementTarget type, otherwise false.

forName

public static Class<?> forName(String name)
                        throws ClassNotFoundException
Replies the type that corresponds to the specified class. If the name corresponds to a primitive type, the low-level type will be replied. This method extends Class.forName(String) with autoboxing support.

Parameters:
name - is the name of the class to load.
Returns:
the loaded class
Throws:
ClassNotFoundException - if name names an unknown class or primitive

forName

public static Class<?> forName(String name,
                               ClassLoader loader)
                        throws ClassNotFoundException
Replies the type that corresponds to the specified class. If the name corresponds to a primitive type, the low-level type will be replied. This method extends Class.forName(String) with autoboxing support.

Parameters:
name - is the name of the class to load.
loader - is the class loader to use.
Returns:
the loaded class
Throws:
ClassNotFoundException - if name names an unknown class or primitive

forName

public static Class<?> forName(String name,
                               boolean typeInitialization,
                               ClassLoader loader)
                        throws ClassNotFoundException
Replies the type that corresponds to the specified class. If the name corresponds to a primitive type, the low-level type will be replied. This method extends Class.forName(String) with autoboxing support.

Parameters:
name - is the name of the class to load.
typeInitialization - must be true to initialize the type, false otherwise.
loader - is the class loader to use.
Returns:
the loaded class
Throws:
ClassNotFoundException - if name names an unknown class or primitive

getPackageClasses

public static Collection<Class<?>> getPackageClasses(Package pkg)
Replies the list of the classes in the given package.

Parameters:
pkg - is the package to explore.
Returns:
the list of classes in the package.

getPackageClasses

public static Collection<Class<?>> getPackageClasses(String packageName)
Replies the list of the classes in the given package.

Parameters:
packageName - is the name of the package to explore.
Returns:
the list of classes in the package.

getSubClasses

public static <T> Collection<Class<? extends T>> getSubClasses(Class<T> className)
Replies the list of all the subclasses of the given class in the current classpath.

Type Parameters:
T - is the type of the superclass.
Parameters:
className - is the name of the class to explore.
Returns:
the list of subclasses.

getSubClasses

public static <T> void getSubClasses(Class<T> className,
                                     boolean allowAbstract,
                                     boolean allowInterface,
                                     boolean allowEnum,
                                     Collection<Class<? extends T>> result)
Replies the list of all the subclasses of the given class in the current classpath.

Type Parameters:
T - is the type of the superclass.
Parameters:
className - is the name of the class to explore.
allowAbstract - is true to allow abstract classes to be put in the replied list
allowInterface - is true to allow interfaces to be put in the replied list.
allowEnum - is true to allow enumeration to be put in the replied list.
result - is the list of subclasses which will be filled by this function.

getAllDirectInterfaces

public static <T,I> Set<Class<? extends I>> getAllDirectInterfaces(Class<? extends T> lowestType,
                                                                   Class<T> highestType,
                                                                   Class<I> interfaceType)
Determines the interfaces implemented by the classes from the lowest type to the highestType which are extended the given interfaceType.

Insteed of Class.getInterfaces(), this function is exploring the super classes. This function does not explore super-interfaces of implemented interfaces.


 interface IA {}
 interface IB extends IA {}
 interface IC {}
 interface ID extends IB, IC {}
 class CA implements IC {}
 class CB extends CA {}
 class CC extends CB implements IB {}
 
This function replies for:

Type Parameters:
T - is the highest type to explore in type hierarchy.
I - indicates the type of the replied interfaces.
Parameters:
lowestType - is the lowest type to explore in type hierarchy.
highestType - is the highest type to explore in type hierarchy.
interfaceType - indicates the type of the replied interfaces.
Returns:
the implemented interfaces.
Since:
5.0

getAllDirectInterfaces

public static <T> Set<Class<?>> getAllDirectInterfaces(Class<? extends T> lowestType,
                                                       Class<T> highestType)
Determines the interfaces implemented by the classes from the lowest type to the highestType which are extended the given interfaceType.

Insteed of Class.getInterfaces(), this function is exploring the super classes. This function does not explore super-interfaces of implemented interfaces.


 interface IA {}
 interface IB extends IA {}
 interface IC {}
 interface ID extends IB, IC {}
 class CA implements IC {}
 class CB extends CA {}
 class CC extends CB implements IB {}
 
This function replies for:

Type Parameters:
T - is the highest type to explore in type hierarchy.
Parameters:
lowestType - is the lowest type to explore in type hierarchy.
highestType - is the highest type to explore in type hierarchy.
Returns:
the implemented interfaces.
Since:
5.0

getSuperClasses

public static <T> Collection<Class<? super T>> getSuperClasses(Class<T> className)
Replies the list of all the superclasses of the given class.

This function does not replies Object.class.

Type Parameters:
T - is the type of the lowest class.
Parameters:
className - is the type of the lowest class.
Returns:
the list of superclasses.
Since:
5.0

getCommonType

public static Class<?> getCommonType(Class<?> type1,
                                     Class<?> type2)
Replies the top-most type which is common to both given types.

Parameters:
type1 -
type2 -
Returns:
the top-most type which is common to both given types.
Since:
6.0

getCommonType

public static Class<?> getCommonType(Object instance1,
                                     Object instance2)
Replies the top-most type which is common to both given objects.

Parameters:
instance1 -
instance2 -
Returns:
the top-most type which is common to both given objects.
Since:
6.0


Copyright © 2006-2011 Arakhnê.org Consortium. All rights reserved under LGPL license terms.