FileDocCategorySizeDatePackage
ReflectionUtils.javaAPI DocApache Lucene 2.1.06666Wed Feb 14 10:46:04 GMT 2007org.apache.lucene.gdata.utils

ReflectionUtils

public class ReflectionUtils extends Object
A collection of static helper methods solve common reflection problems
author
Simon Willnauer

Fields Summary
Constructors Summary
Methods Summary
public static booleancanCreateInstance(java.lang.Class clazz)
This method calls {@link Class#newInstance()} to get a new instance. Use with care!

param
clazz - the class to instantiate
return
true if an instance could be created, otherwise false;

    if(clazz == null)
        return false;
    if(clazz.isPrimitive())
        clazz = getPrimitiveWrapper(clazz);
    try{
        Object o = clazz.newInstance();
        return o != null;
    }catch (Throwable e) {
        return false;
    }
public static booleanextendsType(java.lang.Class typeToCheck, java.lang.Class superType)
Check if the given type extends a given super type

param
typeToCheck - type supposed to extend an specific type
param
superType - the type to be extended by the type to check
return
true if and only if the super type is above in the type hierarchy of the given type, otherwise false

        if (typeToCheck == null)
            return false;
        if (typeToCheck.equals(Object.class))
            return false;
        if (typeToCheck.equals(superType))
            return true;
        
        return extendsType(typeToCheck.getSuperclass(),superType);
    
public static TgetDefaultInstance(java.lang.Class clazz)

param
the type of the class to instantiate
param
clazz - class object of the type
return
a new instance of T

    if(clazz == null)
        throw new ReflectionException("class must not be null");
    
    try{
    Constructor constructor = clazz.getConstructor(new Class[]{});
    return (T) constructor.newInstance(new Object[]{});
    }catch (Exception e) {
        throw new ReflectionException("can not instantiate type of class "+clazz.getName(),e);
    }
public static final java.lang.ClassgetPrimitiveWrapper(java.lang.Class primitive)
Returns the wrapper type for the given primitive type. Wrappers can be easily instantiated via reflection and will be boxed by the VM

param
primitive - the primitive type
return
- the corresponding wrapper type

    if(primitive == null )
        throw new ReflectionException("primitive must not be null");
    if(!primitive.isPrimitive())
        throw new ReflectionException("given class is not a primitive");
                
    if (primitive == Integer.TYPE)
        return Integer.class;
    if (primitive == Float.TYPE)
        return Float.class;
    if (primitive == Long.TYPE)
        return Long.class;
    if (primitive == Short.TYPE)
        return Short.class;
    if (primitive == Byte.TYPE)
        return Byte.class;
    if (primitive == Double.TYPE)
        return Double.class;
    if (primitive == Boolean.TYPE)
        return Boolean.class;

    return primitive;
public static booleanhasDesiredConstructor(java.lang.Class type, java.lang.Class[] parameter)

param
type - the type to check
param
parameter - the constructor parameter
return
true if and only if the type has a visible constructor with the desired parameters

        try{
        return type.getConstructor(parameter) != null;
        
        }catch (Exception e) {
            return false;
        }
    
public static booleanimplementsType(java.lang.Class typeToCheck, java.lang.Class superType)
Check if the given type implements a given super type

param
typeToCheck - type supposed to implement an interface
param
superType - the interface to be implemented by the type to check
return
true if and only if the super type is above in the type hierarchy of the given type, otherwise false

        if(superType == null)
            return false;
        if(!superType.isInterface())
            return  false;
        if (typeToCheck == null)
            return false;
        if (typeToCheck.equals(Object.class))
            return false;
        if (typeToCheck.equals(superType))
            return true;
        Class[] interfaces = typeToCheck.getInterfaces();
        for (int i = 0; i < interfaces.length; i++) {
            if (implementsType(interfaces[i], superType))
                return true;
        }
        return implementsType(typeToCheck.getSuperclass(),superType);
        
    
public static booleanisTypeOf(java.lang.Class typeToCheck, java.lang.Class superType)
This method combines the extendsType and implementsType and checks interfaces and classes

param
typeToCheck - type supposed to extend / implement an specific type
param
superType - the type to be extended / implemented by the type to check
return
true if and only if the super type is above in the type hierarchy of the given type, otherwise false

        return extendsType(typeToCheck,superType)||implementsType(typeToCheck,superType);