| java.lang.Object org.zkoss.lang.Classes
Classes | public class Classes (Code) | | Utilities to handle java.lang.Class
author: tomyeh |
Inner Class :public static class MethodInfo | |
Field Summary | |
final public static int | B_GET Search the get method; not imply B_METHODONLY. | final public static int | B_METHOD_ONLY Only search for methods; excluding fields. | final public static int | B_PUBLIC_ONLY Only search for public methods or fields. | final public static int | B_SET Search the set method; not imply B_METHODONLY. |
Method Summary | |
public static Object | coerce(Class cls, Object val) Converts an object to the specified class. | public static Object | coerce(Class cls, Object val, boolean nullable) Converts to the specified type.
Parameters: nullable - whether the result could be null.If false, 0 is used for number, the default constructor is usedfor others. | final public static boolean | containsMethod(Class cls, String name, Class[] paramTypes) Tests whether a class contains the specified method. | final public static String | correctFieldName(String name) Corrects a string to a valid Java name.
Currently, it only removes '-' and capitalizes the succeeding
character. | final public static Class | forNameByThread(String clsName) Returns the Class object of the specified class name, using
the current thread's context class loader. | final public static AccessibleObject | getAccessibleObject(Class cls, String name, Class[] argTypes, int flags) Gets the specified accessible object, either a method or a field, by
searching the specified name.
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(...). | public static Class[] | getAllInterfaces(Class cls) Returns all interfaces that are implemented by the specified class. | final public static Field | getAnyField(Class cls, String name) Gets the specified field by searching all fields including
any access control and any base class.
The search sequence is: this class's fields, and then
the superclass's fields.
Note: public fields don't be treated different. | final public static Method | getAnyMethod(Class cls, String name, Class[] argTypes) Gets the specified method by searching all methods including
any access control and any base class.
Note: you rarely need to call this metod. | final public static Method | getCloseMethod(Class cls, String name, Class[] argTypes) Gets one of the close methods -- a close method is a method
with the same name and the compatible argument type.
By compatiable we mean the real method's argument type is
the same as or a superclass of the specified one.
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. | final public static Method | getCloseMethodBySubclass(Class cls, String name, Class[] argTypes) Like
Classes.getCloseMethod to get a 'close' method, but
it look for subclass of the arguement (instead of superclass). | final public static Method[] | getCloseMethods(Class cls, String name, Class[] argTypes) Returns all close methods that match the specified condition, or
a zero-length array if none is found. | final public static Method[] | getCloseMethodsBySubclass(Class cls, String name, Class[] argTypes) Like
Classes.getCloseMethods to get all 'close' methods, but
it look for subclass of the arguement (instead of superclass). | final public static Method | getMethodByObject(Class cls, String name, Object[] args) Gets one of the close method by specifying the arguments, rather
than the argument types. | final public static Method | getMethodBySignature(Class cls, String signature, Collection params) Gets the method based on the signature. | final public static Method | getMethodInPublic(Class cls, String name, Class[] argTypes) Gets the method that is declared in a public class/interface.
Class.getMethod returns a public method but the class itself
might not be public. | final public static Class[] | getSuperClasses(Class cls, Class[] clsToCheck) Searches thru each element of the specified array of classes, and
returns classes that are super-classes (or equal) of
the sepcified class. | public static Class | getTopmostInterface(Class cls, Class subIF) Gets the topmost interface of the specified class or interface that
implements or extends the specified interface. | final public static boolean | isAttribute(String methodName) Tests if a method name is an attribute, i.e., prefixing with is,
get or set. | final public static boolean | isNumeric(Class cls, boolean extend) Checks whether the specified class is a numeric class. | final public static boolean | isPrimitiveWrapper(Class cls) Check whether the specified class is a primitive or a primitive wrapper. | final public static Object | newInstance(Class cls, Class[] argTypes, Object[] args) Instantiates a new instance of the specified class with
the specified arguments and argument types. | final public static Object | newInstance(Class cls, Object[] args) Instantiates a new instance of the specified class with the
specified argument. | final public static Object | newInstance(String clsName, Class[] argTypes, Object[] args) Instantiates a new instance of the specified class name
with the specified arguments. | final public static Object | newInstanceByThread(String clsName, Class[] argTypes, Object[] args) Creates and initializes a new instance of the specified class name
with the specified arguments, by use of
Classes.forNameByThread . | final public static Object | newInstanceByThread(String clsName) Creates and initializes a new instance of the specified class name
with default constructor, by use of
Classes.forNameByThread . | final public static MethodInfo | parseMethod(String signature) Gets the method information from a signature. | final public static String | toAttributeName(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. | final public static String | toInternalForm(String clsName) Change class name to internal form (e.g. | final public static String | toMethodName(String attrName, String prefix) Convert an attribute name, returned by toAttributeName, to
a method name. |
B_GET | final public static int B_GET(Code) | | Search the get method; not imply B_METHODONLY.
|
B_METHOD_ONLY | final public static int B_METHOD_ONLY(Code) | | Only search for methods; excluding fields.
|
B_PUBLIC_ONLY | final public static int B_PUBLIC_ONLY(Code) | | Only search for public methods or fields.
|
B_SET | final public static int B_SET(Code) | | Search the set method; not imply B_METHODONLY.
|
containsMethod | final public static boolean containsMethod(Class cls, String name, Class[] paramTypes)(Code) | | Tests whether a class contains the specified method.
Only public methods are tested.
Parameters: cls - the class to test Parameters: name - the method name Parameters: paramTypes - the list of parameter types true if it contains the method |
correctFieldName | final public static String correctFieldName(String name)(Code) | | Corrects a string to a valid Java name.
Currently, it only removes '-' and capitalizes the succeeding
character. Example, 'field-name' becomes 'fieldName'.
|
forNameByThread | final public static Class forNameByThread(String clsName) throws ClassNotFoundException(Code) | | Returns the Class object of the specified class name, using
the current thread's context class loader.
In additions, it handles the primitive types, such as int and double.
Parameters: clsName - fully qualified name of the desired class the Class object representing the desired class exception: ClassNotFoundException - if the class cannot be located by the specified class loader |
getAccessibleObject | final public static AccessibleObject getAccessibleObject(Class cls, String name, Class[] argTypes, int flags) throws NoSuchMethodException(Code) | | Gets the specified accessible object, either a method or a field, by
searching the specified name.
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
Classes.getCloseMethod to get the method.
A cache mechanism is implemented, so you don't need to cache it
again in the caller.
Parameters: cls - the class to find Parameters: name - the name of the accessible object Parameters: argTypes - the parameter type of the method to find Parameters: flags - a combination of B_xxx or zero the accessible object; either Field or Method exception: NoSuchMethodException - if neither the set method ofspecified field nor the field itself not found exception: SecurityException - if access to the information is denied |
getAllInterfaces | public static Class[] getAllInterfaces(Class cls)(Code) | | Returns all interfaces that are implemented by the specified class.
Unlike
Class.getInterfaces , it recursively searches
for all derived classes.
|
getAnyMethod | final public static Method getAnyMethod(Class cls, String name, Class[] argTypes) throws NoSuchMethodException(Code) | | Gets the specified method by searching all methods including
any access control and any base class.
Note: you rarely need to call this metod. In most cases,
Class.getMethod,
Classes.getCloseMethod , and
Classes.getMethodInPublic 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.
Parameters: cls - the class to search Parameters: name - the method name Parameters: argTypes - the parameter array of types the Method object exception: NoSuchMethodException - if a matching method is not found. exception: SecurityException - if access to the information is denied. See Also: Classes.getAccessibleObject(Class,String,Class[],int) See Also: Classes.getAnyField(Class,String) |
getCloseMethod | final public static Method getCloseMethod(Class cls, String name, Class[] argTypes) throws NoSuchMethodException(Code) | | Gets one of the close methods -- a close method is a method
with the same name and the compatible argument type.
By compatiable we mean the real method's argument type is
the same as or a superclass of the specified one.
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
unknwon, 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.
Parameters: cls - the class to locate the method Parameters: name - the method name Parameters: argTypes - 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. the method exception: NoSuchMethodException - if the method is not found |
getCloseMethodBySubclass | final public static Method getCloseMethodBySubclass(Class cls, String name, Class[] argTypes) throws NoSuchMethodException(Code) | | Like
Classes.getCloseMethod to get a 'close' method, but
it look for subclass of the arguement (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.
|
getCloseMethods | final public static Method[] getCloseMethods(Class cls, String name, Class[] argTypes)(Code) | | Returns all close methods that match the specified condition, or
a zero-length array if none is found.
Unlike
Classes.getCloseMethod , we don't cache the searched result,
and it won't throw any exception.
|
getCloseMethodsBySubclass | final public static Method[] getCloseMethodsBySubclass(Class cls, String name, Class[] argTypes)(Code) | | Like
Classes.getCloseMethods to get all 'close' methods, but
it look for subclass of the arguement (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.
|
getMethodBySignature | final public static Method getMethodBySignature(Class cls, String signature, Collection params) throws NoSuchMethodException, ClassNotFoundException(Code) | | Gets the method based on the signature. It also returns the parameter
names to the params list.
Like
Classes.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 (actualy 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.
Parameters: cls - the class to look Parameters: signature - the method signature; the return type is optional Parameters: params - the collection to hold the parameter names returned;null means no parameter names to return |
getMethodInPublic | final public static Method getMethodInPublic(Class cls, String name, Class[] argTypes) throws NoSuchMethodException(Code) | | Gets the method that is declared in a public class/interface.
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.
|
getSuperClasses | final public static Class[] getSuperClasses(Class cls, Class[] clsToCheck)(Code) | | Searches thru each element of the specified array of classes, and
returns classes that are super-classes (or equal) of
the sepcified class.
Parameters: cls - the specified class; null is not allowed Parameters: clsToCheck - the class array to check; null is acceptable a subset of clsToCheck that are super-class of cls;null if no one qualified |
getTopmostInterface | public static Class getTopmostInterface(Class cls, Class subIF)(Code) | | Gets the topmost interface of the specified class or interface that
implements or extends the specified interface.
For example, if A extends B, and C implements A,
then getTopInterface(C, B) returns A.
The interfaces implemented by the specified class is checked first,
and then the subclass.
Parameters: cls - the class or interface Parameters: subIF - the sub-interface the topmost interface extending subIF, or null if subIFis not implemented by cls |
isAttribute | final public static boolean isAttribute(String methodName)(Code) | | Tests if a method name is an attribute, i.e., prefixing with is,
get or set. Caller could then test if it is a setter or getter
by charAt(0)=='s'.
Note 'set' is considered as an attribute, whose name is an
empty string.
Parameters: methodName - the method name to test true if it is setter or getter |
isNumeric | final public static boolean isNumeric(Class cls, boolean extend)(Code) | | Checks whether the specified class is a numeric class.
Parameters: extend - whether to consider Date, char, boolean, Characterand Boolean as a numeric object. |
isPrimitiveWrapper | final public static boolean isPrimitiveWrapper(Class cls)(Code) | | Check whether the specified class is a primitive or a primitive wrapper.
|
parseMethod | final public static MethodInfo parseMethod(String signature) throws IllegalSyntaxException(Code) | | Gets the method information from a signature.
It returns a method info with the return type, method name and two collections
of arguments type and arguments name.
Parameters: signature - the method signature. MethodInfo The method information including return type, method nameand two collections for argument type annd arguments name. |
toAttributeName | final public static String toAttributeName(String methodName)(Code) | | 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.
The code is optimized for better performance.
Parameters: methodName - the method name the attribute name; null if it is not an attribute name See Also: Classes.toMethodName |
toInternalForm | final public static String toInternalForm(String clsName)(Code) | | Change class name to internal form (e.g. byte[] -> [B). If already in
internal form, then just return it.
|
toMethodName | final public static String toMethodName(String attrName, String prefix)(Code) | | Convert an attribute name, returned by toAttributeName, to
a method name.
toMethodName("true", "is") => "isTrue"
toMethodName("true", "") => "true"
Parameters: attrName - the attribute name Parameters: prefix - the prefix; one of is, get and set the method name See Also: Classes.toAttributeName |
|
|