Methods Summary |
---|
public static boolean | canCreateInstance(java.lang.Class clazz)This method calls {@link Class#newInstance()} to get a new instance. Use with care!
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 boolean | extendsType(java.lang.Class typeToCheck, java.lang.Class superType)Check if the given type extends a given super type
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 T | getDefaultInstance(java.lang.Class clazz)
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.Class | getPrimitiveWrapper(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
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 boolean | hasDesiredConstructor(java.lang.Class type, java.lang.Class[] parameter)
try{
return type.getConstructor(parameter) != null;
}catch (Exception e) {
return false;
}
|
public static boolean | implementsType(java.lang.Class typeToCheck, java.lang.Class superType)Check if the given type implements a given super type
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 boolean | isTypeOf(java.lang.Class typeToCheck, java.lang.Class superType)This method combines the extendsType and implementsType and checks interfaces and classes
return extendsType(typeToCheck,superType)||implementsType(typeToCheck,superType);
|