Class ClassUtils
java.lang.Object
ch.systemsx.cisd.common.reflection.ClassUtils
Operations on classes using reflection.
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidassertInterfaceWithOnlyVoidMethods(Class<?> clazz) Asserts that the specified class is an interface which has only methods with no return value.static final <T,C> T Creates a new instance of a class specified by its fully-qualified name.static final <T> TCreates a new instance of a class specified by its fully-qualified name.static final <T> T[]createArray(Class<T> clazz, int len) Creates an array of givenClasstype and of given len.static final <T> TcreateInstance(Class<T> clazz) Creates a new instance of given clazz and get rid of any checked exception.static final Collection<Class<?>> Gathers all classes and interfaces the specified object can be casted to.static final <C> booleanhasConstructor(Class<C> clazz, Object... arguments) Returnstrue, if the clazz has a constructor with the given arguments.static final ObjectinvokeMethod(Method method, Object obj, Object... args) Invokes given method and get rid of any checked exception.listClasses(String packageName, IClassFilter classFilterOrNull) Lists all the classes in the given packageName that passes the givenIClassFilter.static final booleansetFieldValue(Object object, String fieldName, Object newValue) Sets declared field named fieldName of given object to given new value newValue.static final FieldtryGetDeclaredField(Class<?> c, String fieldName) Gets declared field named fieldName in given class or superclass of it.
-
Method Details
-
gatherAllCastableClassesAndInterfacesFor
Gathers all classes and interfaces the specified object can be casted to. -
assertInterfaceWithOnlyVoidMethods
Asserts that the specified class is an interface which has only methods with no return value.- Throws:
AssertionError- if it isn't an interface or at least one method has a return value.
-
hasConstructor
Returnstrue, if the clazz has a constructor with the given arguments. -
create
Creates a new instance of a class specified by its fully-qualified name.- Parameters:
superClazz- Super classclassNamehas to be implemented or extended.clazz- Fully-qualified class.arguments- Optional constructor arguments. If(Object[])is an empty array, then the default constructor will be used.- Returns:
- an instance of type
interface.
-
create
Creates a new instance of a class specified by its fully-qualified name.- Parameters:
superClazz- Super classclassNamehas to be implemented or extended.className- Fully-qualified class name.arguments- Optional constructor arguments. If(Object[]) nullthen the empty constructor will be used. Note that(Object) nullis not interpreted asnullarguments but rather asnew Object[]{null}.- Returns:
- an instance of type
interface.
-
createInstance
Creates a new instance of given clazz and get rid of any checked exception.Wraps any checked exception in a
CheckedExceptionTunnel. -
invokeMethod
Invokes given method and get rid of any checked exception.Wraps any checked exception in a
CheckedExceptionTunnel. -
setFieldValue
Sets declared field named fieldName of given object to given new value newValue.This is useful when you want to set a
privatefield on which you do not have access. Note that this method should only be used in very special cases. You should consider it as a hack.- Returns:
- a
trueiffieldNamehas been modified.
-
tryGetDeclaredField
Gets declared field named fieldName in given class or superclass of it.Before returning it, it call
Field.setAccessible(boolean)withtrue.- Returns:
nullif given fieldName could not be found or it couldn't be made accessible.
-
createArray
Creates an array of givenClasstype and of given len. -
listClasses
Lists all the classes in the given packageName that passes the givenIClassFilter.This method does not work recursively.
-