public class ClassUtils
extends java.lang.Object
Title: Framework Support Library
Description: The class holds general utilities for working with classes.
Copyright: Copyright (c) 2009
Company: StreamScape Technologies
Modifier and Type | Field and Description |
---|---|
static java.lang.Object[] |
NO_ARGS |
static java.lang.Class[] |
NO_ARGS_TYPE |
Constructor and Description |
---|
ClassUtils() |
Modifier and Type | Method and Description |
---|---|
static java.lang.Object[] |
castMethodArguments(java.lang.reflect.Method method,
java.lang.Object[] arguments) |
static boolean |
compare(java.lang.Class[] matcherClassArray,
java.lang.Class[] sourceClassArray,
boolean genericsOk)
Compare a source array of
Class objects to a matcher array of Class objects. |
static boolean |
compareAllowNulls(java.lang.Class[] matcherClassArray,
java.lang.Class[] sourceClassArray,
boolean genericsOk,
boolean allowNulls,
boolean withNumericsCast)
Compare a source array of
Class objects to a matcher array of Class objects. |
static java.lang.Object |
createFromClassName(java.lang.Object caller,
java.lang.String className,
boolean onThread)
Helper method that allows for arbitrary class instantiation.
|
static java.lang.Object |
createFromClassName(java.lang.Object caller,
java.lang.String className,
java.lang.ClassLoader loader)
Convenience method that allows to create instance of the specified class based on the specified class loader.
|
static java.lang.annotation.Annotation |
doGetAnnotationOnMethod(java.lang.Class clazz,
java.lang.String methodName,
java.lang.Class[] parameterTypes,
java.lang.Class annotationClass) |
static java.util.List |
findAllMethods(java.lang.Class clazz,
java.lang.String methodName,
int numOfParams)
Searches for all methods in the provided class and its super classes which have given name and specified number of parameters
|
static java.lang.reflect.Method |
findBestDeclaredOrInheritedMethod(java.lang.Class clazz,
java.lang.String methodName,
java.lang.Class[] argumentClasses,
boolean allowNulls) |
static java.util.List |
findSatisfiableMethods(java.lang.Class clazz,
java.lang.String methodName,
java.lang.Class[] methodParms,
boolean genericsOk,
boolean allowNulls)
Returns a matching list of methods for the given name and parameters on the specified class
If the
methodParms arguments is null it will return the first matching
method on the class. |
static java.util.List |
findSatisfiableMethodsWithReturnType(java.lang.reflect.Method[] methods,
java.lang.Class returnType,
boolean genericsOk)
Returns a list of methods that match the return type specified.
|
static java.lang.Class[] |
getAllClassInterfaces(java.lang.Class clazz)
Return all interfaces of a given class implements as Set, including ones implemented by superclasses.
|
static java.util.List |
getAllFields(java.lang.Class clazz) |
static void |
getAllFields(java.lang.Class clazz,
java.util.List fields) |
static java.util.List |
getAllMethods(java.lang.Class clazz) |
static void |
getAllMethods(java.lang.Class clazz,
java.util.List methods) |
static java.lang.annotation.Annotation |
getAnnotationOnMethod(java.lang.reflect.Method method,
java.lang.Class annotationClass) |
static java.lang.reflect.Field |
getClassField(java.lang.Class clazz,
java.lang.String fieldName)
Returns field of the particular class.
|
static java.util.Set |
getClassInterfacesAsSet(java.lang.Class clazz)
Return all interfaces of a given class implements as Set, including ones implemented by superclasses.
|
static java.util.List |
getClassLoaderJars(java.lang.ClassLoader loader)
Returns the Java Archives currently loaded by the specified class loader.
|
static java.lang.String |
getClassPackageAsResourcePath(java.lang.Class clazz)
Returns a string which consists of the supplied class's package name as a pathname,
i.e., all dots ('.') are replaced by slashes ('/').
|
static java.lang.Object[] |
getClassPath()
This method returns a list of Files in the CLASSPATH of the current
Java application as full pathed file names.
|
static java.lang.reflect.Constructor |
getConstructor(java.lang.Class clazz,
java.lang.Class[] paramTypes) |
static java.lang.reflect.Method |
getDeclaredOrInheritedMethod(java.lang.Class clazz,
java.lang.String methodName,
java.lang.Class[] parameters,
boolean allowNulls)
Returns a matching method for the given name and parameters of the specified class
If the
parameters argument is null it will return the first matching
method on the class. |
static java.lang.reflect.Method |
getDeclaredOrInheritedMethodCastNumerics(java.lang.Class clazz,
java.lang.String methodName,
java.lang.Class[] argumentClasses) |
static java.lang.ClassLoader |
getDefaultClassLoader()
Return a default ClassLoader to use (never
null ). |
static java.util.List |
getLoadedClassList()
Returns an iterator of classes that are loaded by the default Loader on this Thread.
|
static java.util.List |
getLoadedClassList(java.lang.ClassLoader CL)
Returns an iterator of all classes loaded by a given
Class Loader . |
static java.util.Vector |
getLoadedClassNames()
Returns an iterator of classes that are loaded by the default Loader on this Thread.
|
static java.util.Iterator |
getLoadedClassNames(java.lang.Class clazz)
Returns an iteration of classes loaded starting with a specific
Class Loader
and going all the way back to its parent chain. |
static java.lang.reflect.Method |
getMethod(java.lang.Class clazz,
java.lang.String methodName,
java.lang.Class[] parameters)
Returns a matching method for the given name and parameters of the specified class
If the
parameters arguments is null it will return the first matching
method on the class. |
static java.lang.reflect.Method |
getMethodAllowNulls(java.lang.Class clazz,
java.lang.String methodName,
java.lang.Class[] parameters,
boolean allowNulls)
Returns a matching method for the given name and parameters of the specified class
If the
parameters argument is null it will return the first matching
method on the class. |
static java.lang.reflect.Constructor |
getSatisfiableConstructor(java.lang.Class clazz,
java.lang.Class[] paramTypes) |
static java.lang.reflect.Method |
getStaticMethod(java.lang.Class clazz,
java.lang.String methodName,
java.lang.Class[] parameters) |
static java.util.Vector |
getSystemClassLoaderJars()
Returns the Java Archives currently loaded by the system class loader.
|
static java.lang.Object |
invokeMethod(java.lang.Object methodClass,
java.lang.reflect.Method method)
Invoke a method and return its parameters.
|
static java.lang.Object |
invokeMethod(java.lang.Object methodClass,
java.lang.reflect.Method method,
java.lang.Object[] parameters)
Invoke a method and return its parameters.
|
static void |
isAssignable(java.lang.Class superType,
java.lang.Class subType)
Checks that
superType.isAssignableFrom(subType) is true . |
static boolean |
isClassOnPath(java.lang.String className,
java.lang.Class currentClass,
boolean onThread)
Finds and loads a class using either the
Thread local class loader
or the one associated with the calling class. |
static void |
isInstanceOf(java.lang.Class clazz,
java.lang.Object obj)
Checks that the provided object is an instance of the provided class.
|
static java.lang.Class |
loadClass(java.lang.String name,
java.lang.ClassLoader classLoader)
Loads a class with the specified name from the specified class loader.
|
static void |
printLoadedClassnames()
Lists classes loaded by the default class loader on this thread.
|
static void |
printLoadedClassNames(java.lang.Class clazz)
List all classes loaded under the Class Loader of this class.
|
static java.lang.Class |
validateFromClassName(java.lang.Object parent,
java.lang.String className,
boolean onThread)
Helper method that resolves the class but does not initialize it.
|
public static final java.lang.Object[] NO_ARGS
public static final java.lang.Class[] NO_ARGS_TYPE
public static java.lang.Object createFromClassName(java.lang.Object caller, java.lang.String className, boolean onThread) throws UtilitiesException
onThread
flag is specified then the
class loader of the thread (the default class loader) is used. This allows for multi-path loading to
occur if necessary.caller
- the caller object that is attempting the operation.className
- the specified class name.onThread
- the flag indicating if the Class Loader
of the current thread will be used.UtilitiesException
- if some error occurs during this operation execution.public static java.lang.Object createFromClassName(java.lang.Object caller, java.lang.String className, java.lang.ClassLoader loader) throws UtilitiesException
caller
- the caller object that is attempting the operation.className
- the specified class name.loader
- the specified Class Loader
.UtilitiesException
- if some error occurs during this operation execution.public static java.util.Vector getSystemClassLoaderJars()
public static java.util.List getClassLoaderJars(java.lang.ClassLoader loader)
public static java.lang.Class validateFromClassName(java.lang.Object parent, java.lang.String className, boolean onThread) throws UtilitiesException
parent
- Object The parent object that is attempting the operation (for simpler debugging).className
- String representing Class Name.onThread
- boolean If true
the Class Loader
used is of the current thread, not the calling object.UtilitiesException
public static java.lang.reflect.Method getMethod(java.lang.Class clazz, java.lang.String methodName, java.lang.Class[] parameters)
parameters
arguments is null it will return the first matching
method on the class. The method lookup mechanism searches all declared methods
including private and protected ones.clazz
- The class to find the method on.methodName
- The method name to find.parameters
- An array of argument types or nullpublic static java.lang.reflect.Method getMethodAllowNulls(java.lang.Class clazz, java.lang.String methodName, java.lang.Class[] parameters, boolean allowNulls)
parameters
argument is null it will return the first matching
method on the class. If allowNulls
is true then some of the parameters
can have null value, which matches any argument class of a checked method. The method
lookup mechanism searches all declared methods including private and protected ones.clazz
- The class to find the method on.methodName
- The method name to find.parameters
- An array of argument types or nullallowNulls
- Allows some of the parameters to have null valuepublic static java.lang.reflect.Method getDeclaredOrInheritedMethod(java.lang.Class clazz, java.lang.String methodName, java.lang.Class[] parameters, boolean allowNulls)
parameters
argument is null it will return the first matching
method on the class. If allowNulls
is true then some of the parameters
can have null value, which matches any argument class of a checked method. The method
lookup mechanism searches all inherited methods and all declared methods including
private and protected ones. At first the method is looked for in the current class,
then in its parent, then grand-parent and so on.clazz
- The class to find the method on.methodName
- The method name to find.parameters
- An array of argument types or nullallowNulls
- Allows some of the parameters to have null valuepublic static java.lang.reflect.Constructor getConstructor(java.lang.Class clazz, java.lang.Class[] paramTypes)
public static java.lang.reflect.Constructor getSatisfiableConstructor(java.lang.Class clazz, java.lang.Class[] paramTypes)
public static java.util.List findSatisfiableMethods(java.lang.Class clazz, java.lang.String methodName, java.lang.Class[] methodParms, boolean genericsOk, boolean allowNulls)
methodParms
arguments is null
it will return the first matching
method on the class.clazz
- Class The class to inspect.methodName
- String The name of the method to look formethodParms
- Class[] An array of method parameter classes. Primitives are not supported.
If the list is empty or null
a method with no parameters is matched.genericsOk
- boolean Allows the target method to specify generic Object.class
types as parameters.UtilitiesException
public static java.lang.reflect.Field getClassField(java.lang.Class clazz, java.lang.String fieldName) throws UtilitiesException
clazz
- Class The class to inspect.fieldName
- String The name of the field to look forUtilitiesException
public static java.util.List findSatisfiableMethodsWithReturnType(java.lang.reflect.Method[] methods, java.lang.Class returnType, boolean genericsOk) throws UtilitiesException
void
return parameters and generics (Object
types) may be specified. This method expects a list of candidate methods as
parameters. A single return type is also specified that will match against the return types of the candidate methods. If null
is specified as a return the method assumes that void
is returned.methods
- Method[] Candidate methods to search for a return type.returnType
- Class The return type of this method. This may be set to null
if return type is void
.genericsOk
- boolean Allows return values to use Object
instances to match.UtilitiesException
public static boolean isClassOnPath(java.lang.String className, java.lang.Class currentClass, boolean onThread)
Thread
local class loader
or the one associated with the calling class. The class is not initialized, hence
this method does not result in memory allocation for the instance.className
- The class name to look forcurrentClass
- The calling classonThread
- If true
use the thread-local class loader.public static boolean compare(java.lang.Class[] matcherClassArray, java.lang.Class[] sourceClassArray, boolean genericsOk)
Class
objects to a matcher array of Class
objects.
If support for generics
is enabled the comparator will allow an Object.class
instance to match. This effectivley makes the comparator validate the number of parameters regardless
of type.matcherClassArray
- Class[] The list of classes that matches to the source class list.sourceClassArray
- Class[] The source list of class objects.genericsOk
- boolean Allows for generic Object
instances to match to a class instance.True
if all the clases the source and matcher array are equal.public static boolean compareAllowNulls(java.lang.Class[] matcherClassArray, java.lang.Class[] sourceClassArray, boolean genericsOk, boolean allowNulls, boolean withNumericsCast)
Class
objects to a matcher array of Class
objects.
If support for generics
is enabled the comparator will allow an Object.class
instance to match. This effectively makes the comparator validate the number of parameters regardless
of type. If allowNulls
is true then some of the sourceClassArray
items can
be null and match any Class
in matcherClassArray
.matcherClassArray
- Class[] The list of classes that matches to the source class list.sourceClassArray
- Class[] The source list of class objects.genericsOk
- boolean Allows for generic Object
instances to match to a class instance.allowNulls
- boolean Allows some of the sourceClassArray
items to be null.True
if all the classes the source and matcher array are equal.public static java.lang.reflect.Method getDeclaredOrInheritedMethodCastNumerics(java.lang.Class clazz, java.lang.String methodName, java.lang.Class[] argumentClasses)
public static java.lang.reflect.Method findBestDeclaredOrInheritedMethod(java.lang.Class clazz, java.lang.String methodName, java.lang.Class[] argumentClasses, boolean allowNulls)
public static java.util.List findAllMethods(java.lang.Class clazz, java.lang.String methodName, int numOfParams)
clazz
- Source classmethodName
- Name of the methodnumOfParams
- Number of parameters which method takespublic static java.lang.Object invokeMethod(java.lang.Object methodClass, java.lang.reflect.Method method) throws UtilitiesException, java.lang.reflect.InvocationTargetException
methodClass
- Class.method
- Method.UtilitiesException
java.lang.reflect.InvocationTargetException
public static java.lang.Object invokeMethod(java.lang.Object methodClass, java.lang.reflect.Method method, java.lang.Object[] parameters) throws UtilitiesException, java.lang.reflect.InvocationTargetException
methodClass
- Class.method
- Method.parameters
- Object[] Parameter array.UtilitiesException
java.lang.reflect.InvocationTargetException
public static java.lang.Class[] getAllClassInterfaces(java.lang.Class clazz)
If the class itself is an interface, it gets returned as sole interface.
clazz
- Classpublic static java.util.Set getClassInterfacesAsSet(java.lang.Class clazz)
If the class itself is an interface, it gets returned as sole interface.
clazz
- the class to analyse for interfacespublic static java.lang.String getClassPackageAsResourcePath(java.lang.Class clazz)
clazz
- the input class. A null value or the default (empty) package will result in an empty string ("") being returned.ClassLoader.getResource(java.lang.String)
,
Class.getResource(java.lang.String)
public static void isInstanceOf(java.lang.Class clazz, java.lang.Object obj) throws UtilitiesException
ClassUtils.instanceOf(Foo.class, foo);
clazz
- the required classobj
- the object to checkUtilitiesException
- if the object is not an instance of clazzClass.isInstance(java.lang.Object)
public static void isAssignable(java.lang.Class superType, java.lang.Class subType) throws UtilitiesException
superType.isAssignableFrom(subType)
is true
.
ClassUtils.isAssignable(Some.class, myClass);
superType
- the super type to checksubType
- the sub type to checkUtilitiesException
- if the classes are not assignablepublic static java.lang.Object[] getClassPath()
public static java.lang.ClassLoader getDefaultClassLoader()
null
).
Returns the thread context ClassLoader, if available.
The ClassLoader that loaded the ClassUtils class will be used as fallback.
Call this method if you intend to use the thread context ClassLoader
in a scenario where you absolutely need a non-null ClassLoader reference:
for example, for class path resource loading (but not necessarily for
Class.forName
, which accepts a null
ClassLoader
reference as well).
Thread.getContextClassLoader()
public static java.util.List getLoadedClassList(java.lang.ClassLoader CL) throws java.lang.NoSuchFieldException, java.lang.IllegalAccessException
Class Loader
.CL
- ClassLoaderjava.lang.NoSuchFieldException
java.lang.IllegalAccessException
public static java.util.List getLoadedClassList() throws java.lang.NoSuchFieldException, java.lang.IllegalAccessException
java.lang.NoSuchFieldException
java.lang.IllegalAccessException
public static void printLoadedClassnames() throws java.lang.IllegalAccessException, java.lang.NoSuchFieldException
java.lang.IllegalAccessException
java.lang.NoSuchFieldException
public static java.util.Vector getLoadedClassNames() throws java.lang.IllegalAccessException, java.lang.NoSuchFieldException
java.lang.IllegalAccessException
java.lang.NoSuchFieldException
public static void printLoadedClassNames(java.lang.Class clazz) throws java.lang.IllegalAccessException, java.lang.NoSuchFieldException
this
as a reference. In this way the user
could compare the content of two loaders to check for conflict.clazz
- Class The name of the class for which to obtain the Class Loader
.java.lang.IllegalAccessException
java.lang.NoSuchFieldException
public static java.util.Iterator getLoadedClassNames(java.lang.Class clazz) throws java.lang.IllegalAccessException, java.lang.NoSuchFieldException
Class Loader
and going all the way back to its parent chain. This method will show you what the class loader
chain has loaded. It does not show peer loaders with the same parent. In this way the user can
compare the content of two loaders (for example in order to spot conflicts and duplicates).clazz
- Classjava.lang.IllegalAccessException
java.lang.NoSuchFieldException
public static java.lang.Class loadClass(java.lang.String name, java.lang.ClassLoader classLoader) throws java.lang.ClassNotFoundException
name
- the name of the class to be loaded.classLoader
- the class loader from which the class will be loaded.java.lang.ClassNotFoundException
- if the class is not found.public static java.util.List getAllFields(java.lang.Class clazz)
public static void getAllFields(java.lang.Class clazz, java.util.List fields)
public static java.util.List getAllMethods(java.lang.Class clazz)
public static void getAllMethods(java.lang.Class clazz, java.util.List methods)
public static java.lang.Object[] castMethodArguments(java.lang.reflect.Method method, java.lang.Object[] arguments)
public static java.lang.annotation.Annotation getAnnotationOnMethod(java.lang.reflect.Method method, java.lang.Class annotationClass)
public static java.lang.annotation.Annotation doGetAnnotationOnMethod(java.lang.Class clazz, java.lang.String methodName, java.lang.Class[] parameterTypes, java.lang.Class annotationClass)
public static java.lang.reflect.Method getStaticMethod(java.lang.Class clazz, java.lang.String methodName, java.lang.Class[] parameters)
Copyright © 2015-2024 StreamScape Technologies. All rights reserved.