public class Classes
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
Classes.MethodInfo
The method info class used for
parseMethod(String signature) . |
Modifier and Type | Field and Description |
---|---|
static int |
B_GET
Search the get method; not imply B_METHODONLY.
|
static int |
B_METHOD_ONLY
Only search for methods; excluding fields.
|
static int |
B_PUBLIC_ONLY
Only search for public methods or fields.
|
static int |
B_SET
Search the set method; not imply B_METHODONLY.
|
Constructor and Description |
---|
Classes() |
Modifier and Type | Method and Description |
---|---|
static java.lang.Object |
coerce(java.lang.Class<?> cls,
java.lang.Object val)
Converts an object to the specified class.
|
static java.lang.Object |
coerce(java.lang.Class<?> cls,
java.lang.Object val,
boolean nullable)
Converts to the specified type.
|
static boolean |
containsMethod(java.lang.Class<?> cls,
java.lang.String name,
java.lang.Class<?>[] paramTypes)
Tests whether a class contains the specified method.
|
static java.lang.String |
correctFieldName(java.lang.String name)
Corrects a string to a valid Java name.
|
static boolean |
existsByThread(java.lang.String clsnm)
Returns whether the specified class exists for the current thread's
context class loader.
|
static java.lang.Class<?> |
forNameByThread(java.lang.String clsName)
Returns the Class object of the specified class name, using
the current thread's context class loader.
|
static java.lang.reflect.AccessibleObject |
getAccessibleObject(java.lang.Class<?> cls,
java.lang.String name,
java.lang.Class<?>[] argTypes,
int flags)
Gets the specified accessible object, either a method or a field, by
searching the specified name.
|
static java.lang.Class<?>[] |
getAllInterfaces(java.lang.Class<?> cls)
Returns all interfaces that are implemented by the specified class.
|
static java.lang.reflect.Field |
getAnyField(java.lang.Class<?> cls,
java.lang.String name)
Gets the specified field by searching all fields including
any access control and any base class.
|
static java.lang.reflect.Method |
getAnyMethod(java.lang.Class<?> cls,
java.lang.String name,
java.lang.Class<?>[] argTypes)
Gets the specified method by searching all methods including
any access control and any base class.
|
static java.lang.reflect.Method |
getCloseMethod(java.lang.Class<?> cls,
java.lang.String name,
java.lang.Class<?>[] argTypes)
Gets one of the close methods -- a close method is a method
with the same name and the compatible argument type.
|
static java.lang.reflect.Method |
getCloseMethodBySubclass(java.lang.Class<?> cls,
java.lang.String name,
java.lang.Class<?>[] argTypes)
Like
getCloseMethod(java.lang.Class<?>, java.lang.String, java.lang.Class<?>[]) to get a 'close' method, but
it look for subclass of the argument (instead of superclass). |
static java.lang.reflect.Method[] |
getCloseMethods(java.lang.Class<?> cls,
java.lang.String name,
java.lang.Class<?>[] argTypes)
Returns all close methods that match the specified condition, or
a zero-length array if none is found.
|
static java.lang.reflect.Method[] |
getCloseMethodsBySubclass(java.lang.Class<?> cls,
java.lang.String name,
java.lang.Class<?>[] argTypes)
Like
getCloseMethods(java.lang.Class<?>, java.lang.String, java.lang.Class<?>[]) to get all 'close' methods, but
it look for subclass of the argument (instead of superclass). |
static java.lang.reflect.Method |
getMethodByObject(java.lang.Class<?> cls,
java.lang.String name,
java.lang.Object[] args)
Gets one of the close method by specifying the arguments, rather
than the argument types.
|
static java.lang.reflect.Method |
getMethodBySignature(java.lang.Class<?> cls,
java.lang.String signature,
java.util.Collection<java.lang.String> params)
Gets the method based on the signature.
|
static java.lang.reflect.Method |
getMethodBySignature(java.lang.Class<?> cls,
java.lang.String signature,
java.util.Collection<java.lang.String> params,
ClassResolver resolver)
Gets the method based on the signature with a class resolver.
|
static java.lang.reflect.Method |
getMethodInPublic(java.lang.Class<?> cls,
java.lang.String name,
java.lang.Class<?>[] argTypes)
Gets the method that is declared in a public class/interface.
|
static java.lang.Class<?>[] |
getSuperClasses(java.lang.Class<?> cls,
java.lang.Class<?>[] clsToCheck)
Searches thru each element of the specified array of classes, and
returns classes that are super-classes (or equal) of
the specified class.
|
static java.lang.Class<?> |
getTopmostInterface(java.lang.Class<?> cls,
java.lang.Class<?> subIF)
Gets the topmost interface of the specified class or interface that
implements or extends the specified interface.
|
static boolean |
isAttribute(java.lang.String methodName)
Tests if a method name is an attribute, i.e., prefixing with is,
get or set.
|
static boolean |
isNumeric(java.lang.Class<?> cls,
boolean extend)
Checks whether the specified class is a numeric class.
|
static boolean |
isPrimitiveWrapper(java.lang.Class<?> cls)
Check whether the specified class is a primitive or a primitive wrapper.
|
static java.lang.Object |
newInstance(java.lang.Class<?> cls,
java.lang.Class<?>[] argTypes,
java.lang.Object[] args)
Instantiates a new instance of the specified class with
the specified arguments and argument types.
|
static java.lang.Object |
newInstance(java.lang.Class<?> cls,
java.lang.Object[] args)
Instantiates a new instance of the specified class with the
specified argument.
|
static java.lang.Object |
newInstance(java.lang.String clsName,
java.lang.Class<?>[] argTypes,
java.lang.Object[] args)
Instantiates a new instance of the specified class name
with the specified arguments.
|
static java.lang.Object |
newInstanceByThread(java.lang.String clsName)
Creates and initializes a new instance of the specified class name
with default constructor, by use of
forNameByThread(java.lang.String) . |
static java.lang.Object |
newInstanceByThread(java.lang.String clsName,
java.lang.Class<?>[] argTypes,
java.lang.Object[] args)
Creates and initializes a new instance of the specified class name
with the specified arguments, by use of
forNameByThread(java.lang.String) . |
static Classes.MethodInfo |
parseMethod(java.lang.String signature)
Gets the method information from a signature.
|
static java.lang.String |
toAttributeName(java.lang.String methodName)
Converts a method name to an attribute name by removing the prefix
is, get or set, or null if it doesn't start with is, get or set.
|
static java.lang.String |
toInternalForm(java.lang.String clsName)
Change class name to internal form (e.g. byte[] -> [B).
|
static java.lang.String |
toMethodName(java.lang.String attrName,
java.lang.String prefix)
Convert an attribute name, returned by toAttributeName, to
a method name.
|
public static final int B_GET
public static final int B_SET
public static final int B_PUBLIC_ONLY
public static final int B_METHOD_ONLY
public static final java.lang.Object newInstance(java.lang.Class<?> cls, java.lang.Class<?>[] argTypes, java.lang.Object[] args) throws java.lang.NoSuchMethodException, java.lang.InstantiationException, java.lang.reflect.InvocationTargetException, java.lang.IllegalAccessException
Note only public constructors are searched.
cls
- the class of the instance to createargTypes
- the argument types of the constructor to invokeargs
- the arguments to initialize the instancejava.lang.NoSuchMethodException
- if a matching method is not foundjava.lang.InstantiationException
- if the class that declares the
underlying constructor represents an abstract classjava.lang.reflect.InvocationTargetException
- if the underlying constructor
throws an exceptionjava.lang.IllegalAccessException
newInstance(String, Class[], Object[])
public static final java.lang.Object newInstance(java.lang.Class<?> cls, java.lang.Object[] args) throws java.lang.NoSuchMethodException, java.lang.InstantiationException, java.lang.reflect.InvocationTargetException, java.lang.IllegalAccessException
It searches all constructors for the first one that matches the specified arguments.
java.lang.NoSuchMethodException
java.lang.InstantiationException
java.lang.reflect.InvocationTargetException
java.lang.IllegalAccessException
public static final java.lang.Object newInstance(java.lang.String clsName, java.lang.Class<?>[] argTypes, java.lang.Object[] args) throws java.lang.NoSuchMethodException, java.lang.InstantiationException, java.lang.reflect.InvocationTargetException, java.lang.ClassNotFoundException, java.lang.IllegalAccessException
It uses Class.forName to get the class.
clsName
- the class name of the instance to createargTypes
- the argument types of the constructor to invokeargs
- the arguments to initialize the instancejava.lang.NoSuchMethodException
- if a matching method is not foundjava.lang.InstantiationException
- if the class that declares the
underlying constructor represents an abstract classjava.lang.reflect.InvocationTargetException
- if the underlying constructor
throws an exceptionjava.lang.ClassNotFoundException
- if the specified class name is not a classjava.lang.IllegalAccessException
newInstance(Class, Class[], Object[])
public static final java.lang.Object newInstanceByThread(java.lang.String clsName, java.lang.Class<?>[] argTypes, java.lang.Object[] args) throws java.lang.NoSuchMethodException, java.lang.InstantiationException, java.lang.reflect.InvocationTargetException, java.lang.ClassNotFoundException, java.lang.IllegalAccessException
forNameByThread(java.lang.String)
.
It uses forNameByThread(java.lang.String)
to get the class.
clsName
- the class name of the instance to createargTypes
- the argument types of the constructor to invokeargs
- the arguments to initialize the instancejava.lang.NoSuchMethodException
- if a matching method is not foundjava.lang.InstantiationException
- if the class that declares the
underlying constructor represents an abstract classjava.lang.reflect.InvocationTargetException
- if the underlying constructor
throws an exceptionjava.lang.ClassNotFoundException
- if the specified class name is not a classjava.lang.IllegalAccessException
newInstance(Class, Class[], Object[])
public static final java.lang.Object newInstanceByThread(java.lang.String clsName) throws java.lang.NoSuchMethodException, java.lang.InstantiationException, java.lang.reflect.InvocationTargetException, java.lang.ClassNotFoundException, java.lang.IllegalAccessException
forNameByThread(java.lang.String)
.java.lang.NoSuchMethodException
java.lang.InstantiationException
java.lang.reflect.InvocationTargetException
java.lang.ClassNotFoundException
java.lang.IllegalAccessException
public static final java.lang.Class<?> forNameByThread(java.lang.String clsName) throws java.lang.ClassNotFoundException
It first tries Thread.currentThread().getContextClassLoader(),
and then Classes
's class loader if not found.
In additions, it handles the primitive types, such as int and double.
clsName
- fully qualified name of the desired classjava.lang.ClassNotFoundException
- if the class cannot be located by the specified class loaderpublic static final boolean existsByThread(java.lang.String clsnm)
clsnm
- the class name to testpublic static final java.lang.String toInternalForm(java.lang.String clsName)
public static java.lang.Class<?> getTopmostInterface(java.lang.Class<?> cls, java.lang.Class<?> subIF)
The interfaces implemented by the specified class is checked first, and then the subclass.
cls
- the class or interfacesubIF
- the sub-interfacepublic static java.lang.Class<?>[] getAllInterfaces(java.lang.Class<?> cls)
Unlike Class.getInterfaces()
, it recursively searches
for all derived classes.
public static final boolean containsMethod(java.lang.Class<?> cls, java.lang.String name, java.lang.Class<?>[] paramTypes)
cls
- the class to testname
- the method nameparamTypes
- the list of parameter typespublic static final java.lang.String correctFieldName(java.lang.String name)
public static final java.lang.String toMethodName(java.lang.String attrName, java.lang.String prefix)
toMethodName("true", "is") => "isTrue"
toMethodName("true", "") => "true"
attrName
- the attribute nameprefix
- the prefix; one of is, get and settoAttributeName(java.lang.String)
public static final boolean isAttribute(java.lang.String methodName)
Note 'set' is considered as an attribute, whose name is an empty string.
methodName
- the method name to testpublic static final java.lang.String toAttributeName(java.lang.String methodName)
The code is optimized for better performance.
methodName
- the method nametoMethodName(java.lang.String, java.lang.String)
public static final Classes.MethodInfo parseMethod(java.lang.String signature) throws IllegalSyntaxException
signature
- the method signature.IllegalSyntaxException
public static final java.lang.reflect.Method getMethodBySignature(java.lang.Class<?> cls, java.lang.String signature, java.util.Collection<java.lang.String> params, ClassResolver resolver) throws java.lang.NoSuchMethodException, java.lang.ClassNotFoundException
Like getMethodInPublic(Class, String, Class[])
, it returns
only public method in a public class/interface.
For example, "find(java.lang.String name)" will return the method with one String-typed argument and params will hold "name". The return type is optional (actually ignored).
If params is null, the parameter names are not returned and the signature could be simplified as "find(java.lang.String)".
A cache mechanism is implemented, so you don't need to cache it again in the caller.
cls
- the class to looksignature
- the method signature; the return type is optional.resolver
.params
- the collection to hold the parameter names returned;
null means no parameter names to returnresolver
- the class resolver used to resolve the class specified
in the signature. Ignored if null.java.lang.NoSuchMethodException
java.lang.ClassNotFoundException
public static final java.lang.reflect.Method getMethodBySignature(java.lang.Class<?> cls, java.lang.String signature, java.util.Collection<java.lang.String> params) throws java.lang.NoSuchMethodException, java.lang.ClassNotFoundException
Like getMethodInPublic(Class, String, Class[])
, it returns
only public method in a public class/interface.
For example, "find(java.lang.String name)" will return the method with one String-typed argument and params will hold "name". The return type is optional (actually ignored).
If params is null, the parameter names are not returned and the signature could be simplified as "find(java.lang.String)".
A cache mechanism is implemented, so you don't need to cache it again in the caller.
cls
- the class to looksignature
- the method signature; the return type is optionalparams
- the collection to hold the parameter names returned;
null means no parameter names to returnjava.lang.NoSuchMethodException
java.lang.ClassNotFoundException
public static final java.lang.reflect.Method getMethodInPublic(java.lang.Class<?> cls, java.lang.String name, java.lang.Class<?>[] argTypes) throws java.lang.NoSuchMethodException
Class.getMethod returns a public method but the class itself might not be public. However, in many cases, that class also implements a public interface or class.
This method will search all its public classes to look for the method that is 'real' public.
NoSuchMethodException is thrown if no public class/interface is found to have the method.
java.lang.NoSuchMethodException
public static final java.lang.reflect.Method getMethodByObject(java.lang.Class<?> cls, java.lang.String name, java.lang.Object[] args) throws java.lang.NoSuchMethodException
getCloseMethod(java.lang.Class<?>, java.lang.String, java.lang.Class<?>[])
.java.lang.NoSuchMethodException
public static final java.lang.reflect.Method getCloseMethod(java.lang.Class<?> cls, java.lang.String name, java.lang.Class<?>[] argTypes) throws java.lang.NoSuchMethodException
It might not be the best fit one, unless there is a method whose argument types are exactly argTypes.
You might specify the exact number in argTypes. If any of them is
unknown, use null. Example, in the following, the first argument could
be anything and the second is anything deriving from MyClass:
new Class[] {null, MyClass.class}
Note: if an argument accepts int, then Integer is considered as compatible (unlike Class.getMethod). So are long, byte...
A cache mechanism is implemented, so you don't need to cache it again in the caller.
cls
- the class to locate the methodname
- the method nameargTypes
- an array of the argument classes;
null to denote no argument at all (i.e., exact match).
Any argTypes[i] could be null to denote any class.java.lang.NoSuchMethodException
- if the method is not foundpublic static final java.lang.reflect.Method getCloseMethodBySubclass(java.lang.Class<?> cls, java.lang.String name, java.lang.Class<?>[] argTypes) throws java.lang.NoSuchMethodException
getCloseMethod(java.lang.Class<?>, java.lang.String, java.lang.Class<?>[])
to get a 'close' method, but
it look for subclass of the argument (instead of superclass).
In other words, it looks for the method whose argument type is
the same as or a subclass of the specified one.java.lang.NoSuchMethodException
public static final java.lang.reflect.Method[] getCloseMethods(java.lang.Class<?> cls, java.lang.String name, java.lang.Class<?>[] argTypes)
Unlike getCloseMethod(java.lang.Class<?>, java.lang.String, java.lang.Class<?>[])
, we don't cache the searched result,
and it won't throw any exception.
public static final java.lang.reflect.Method[] getCloseMethodsBySubclass(java.lang.Class<?> cls, java.lang.String name, java.lang.Class<?>[] argTypes)
getCloseMethods(java.lang.Class<?>, java.lang.String, java.lang.Class<?>[])
to get all 'close' methods, but
it look for subclass of the argument (instead of superclass).
In other words, it looks for the method whose argument type is
the same as or a subclass of the specified one.public static final java.lang.reflect.AccessibleObject getAccessibleObject(java.lang.Class<?> cls, java.lang.String name, java.lang.Class<?>[] argTypes, int flags) throws java.lang.NoSuchMethodException
The search sequence is: (assume field name is body)>
getBody(...)
isBody(...)
body(...)
body
If B_SET is specified, setBody(...) is searched instead of getBody(...) and isBody(...). The field is searched only if argsType.length is 0 or 1.
Note: it uses getCloseMethod(java.lang.Class<?>, java.lang.String, java.lang.Class<?>[])
to get the method.
A cache mechanism is implemented, so you don't need to cache it again in the caller.
cls
- the class to findname
- the name of the accessible objectargTypes
- the parameter type of the method to findflags
- a combination of B_xxx or zerojava.lang.NoSuchMethodException
- if neither the set method of
specified field nor the field itself not foundjava.lang.SecurityException
- if access to the information is deniedpublic static final java.lang.reflect.Method getAnyMethod(java.lang.Class<?> cls, java.lang.String name, java.lang.Class<?>[] argTypes) throws java.lang.NoSuchMethodException
getCloseMethod(java.lang.Class<?>, java.lang.String, java.lang.Class<?>[])
, and
getMethodInPublic(java.lang.Class<?>, java.lang.String, java.lang.Class<?>[])
are what you need.
The search sequence is: this class's methods, and then the superclass's methods.
Note: public methods don't be treated different. If the caller wants to search public methods first, it has to call Class.getMethod first.
cls
- the class to searchname
- the method nameargTypes
- the parameter array of typesjava.lang.NoSuchMethodException
- if a matching method is not found.java.lang.SecurityException
- if access to the information is denied.getAccessibleObject(Class, String, Class[], int)
,
getAnyField(Class, String)
public static final java.lang.reflect.Field getAnyField(java.lang.Class<?> cls, java.lang.String name) throws java.lang.NoSuchFieldException
Note: public fields don't be treated different. If the caller wants to search public fields first, it has to call Class.getField first.
cls
- the class to searchname
- the field namejava.lang.NoSuchFieldException
- if a matching field is not found.java.lang.SecurityException
- if access to the information is denied.getAccessibleObject(Class, String, Class[], int)
,
getAnyMethod(Class, String, Class[])
public static final java.lang.Class<?>[] getSuperClasses(java.lang.Class<?> cls, java.lang.Class<?>[] clsToCheck)
cls
- the specified class; null is not allowedclsToCheck
- the class array to check; null is acceptablepublic static final boolean isPrimitiveWrapper(java.lang.Class<?> cls)
public static final boolean isNumeric(java.lang.Class<?> cls, boolean extend)
extend
- whether to consider Date, char, boolean, Character
and Boolean as a numeric object.public static java.lang.Object coerce(java.lang.Class<?> cls, java.lang.Object val) throws java.lang.ClassCastException
val
- the value.java.lang.ClassCastException
- if failed to convertcoerce(Class, Object, boolean)
public static java.lang.Object coerce(java.lang.Class<?> cls, java.lang.Object val, boolean nullable) throws java.lang.ClassCastException
nullable
- whether the result could be null.
If false, 0 is used for number, the default constructor is used
for others. coerce(Class, Object)
is a special case that
is equivalent to nullable=true.java.lang.ClassCastException
- if failed to convertCopyright © 2005-2011 Potix Corporation. All Rights Reserved.