Package jadex.common
Class SReflect
java.lang.Object
jadex.common.SReflect
This class provides several useful static reflection methods.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final Map
Method lookup cache (class->(name->method[])), includes non-public methods.protected static final Map
Mapping from basic class name -> basic type(class).Class lookup cache (classloader(weak)->Map([name, import]->class)).protected static final Set
String convertable types.protected static final Object[]
protected static final Map
Field lookup cache (class->(name->field[])).protected static final MethodHandle
Reflective access to Class.getModule().static final boolean
This is set to true if the VM has a working GUI environment available.protected static final Map
Inner class name lookup cache.protected static final MethodHandle
Reflective access to Module.isExported().protected static final Map
Method lookup cache (class->(name->method[])).static final Object
Null object.protected static final Map
Mapping from basic class -> object type(class). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Class
<?> classForName
(String name, ClassLoader classloader) Extension for Class.forName(), because primitive types are not supported.static Class
<?> classForName0
(String name, boolean initialize, ClassLoader classloader) Extension for Class.forName(), because primitive types are not supported.static Class
<?> classForName0
(String name, ClassLoader classloader) Extension for Class.forName(), because primitive types are not supported.static Object
convertWrappedValue
(Object value, Class clazz) Convert a value to the correct wrapped type.static Object
createComposite
(Type type, Collection<?> values) Create an fill an object of a class that is some kind of collection.static String
Cut package off from classname.static Object[]
fillArguments
(Object[] args, Class<?>[] paramtypes) Map arguments to parameters using varargs, if necessary.static <T> Class
<T> findClass
(String clname, String[] imports, ClassLoader classloader) Find a class.static <T> Class
<T> findClass0
(String clname, String[] imports, ClassLoader classloader) Find a class.static Field[]
getAllFields
(Class clazz) Get all fields of a class including public, protected and private fields of the class and its superclasses.static Method[]
getAllMethods
(Class<?> clazz) Get all methods of a class including public, protected and private methods of the class and its superclasses.static Method[]
getAllMethods
(Class<?> clazz, String name) Get all method(s) of the class by name, including public, protected and private methods.static <T> T
getAnnotationDefaultValue
(Class<? extends Annotation> clazz, String name) Get the default value of an annotation method.static Method
getAnyMethod
(Class<?> clazz, String name, Class<?>[] types) Get a method of the class.static Object
Get an array for an arbitrary collection object.static Field
getCachedField
(Class clazz, String name) Get a cached field.static Class
<?> Get the class for a type.static String
getClassName
(Class<?> clazz) Beautifies names of arrays (eg 'String[]' instead of '[LString;').static Object
getDeclared
(Object o, String fieldname) Get the declared object, doesnt cumulate through superclasses.static final Method
getDeclaredMethod0
(Class<?> clazz, String methodname, Class<?>... parametertypes) Gets a declared methods similar to Class.getDeclaredMethod() but returns null instead of throwing exception.static final Class
<?> getDeclaringInterface
(Class<?> iface, String methodname, Class<?>... parametertypes) Finds the declaring interface of a method in a multiple-inheritance interface using a breadth-first approach.static Object
getDefaultValue
(Class clazz) Get default value for basic types.static final Method
getExportedMethod
(Class<?> clazz, String name, Class<?>... params) Gets a method that is exported by the module.static Field
Get a field of the class, or any of it's superclasses.static String
getGenericClassName
(Type t, Class<?> c) Returns generic type name.static String
getInnerClassName
(Class<?> clazz) Get inner class name.static Type
getInnerGenericType
(Type type) Unwrap a generic type.static <T> Iterable
<T> getIterable
(Object collection) Get an iterator for an arbitrary collection object.static Class
getIterableComponentType
(Type type) Get the component type of a class that is some kind of collection.static <T> Iterator
<T> getIterator
(Object collection) Get an iterator for an arbitrary collection object.static Method
Get a public method of the class.static String
Get the current method name from the caller.static Method[]
getMethods
(Class clazz, String name) Get public method(s) of the class by name.static String
getMethodSignature
(Method method) Get the generic signature of a method.static String
getPackageName
(Class<?> clazz) Get the package of a class.static Class[]
getSuperInterfaces
(Class[] interfaces) Get the super interfaces.static String
getTypeName
(String name) Get unqualified type name.static String
getUnqualifiedClassName
(Class clazz) Get unqualified class name.static String
getUnqualifiedTypeName
(String name) Get unqualified type name.static Class
<?> getWrappedType
(Class<?> clazz) Get the wrapped type.static boolean
hasGui()
Test if there is a gui available.static boolean
instanceOf
(Object o, Class c) Is an object instanceof a class or its superclasses.static boolean
isBasicType
(Class<?> clazz) Is basic type.static final boolean
isExported
(Class<?> clazz) Tests if the class is part of a package that the containing module has exported.static boolean
isIterable
(Object obj) Test if object is some kind of collection.static boolean
isIterableClass
(Class clazz) Test if class is some kind of collection.static boolean
isStringConvertableType
(Class clazz) Test if the class is automatically convertable from/to a string.static boolean
isSupertype
(Class<?> clazz1, Class<?> clazz2) Check if a class is a supertype of, or the same as another class.static void
static String
makeNiceArrayNotation
(String name) Process a type name and replace array notation with nice one.static int[]
matchArgumentTypes
(Class[] argtypes, Class[][] paramtypes) Match the given argument types to a set of parameter type arrays.static final Object
newInstance
(String classname, ClassLoader cl, Object... conparams) Reflectively instantiates an object by heuristically matching the constructor parameters.static final Object
newInstance
(String classname, Object... conparams) Reflectively instantiates an object by heuristically matching the constructor parameters.static Class
<?> unwrapGenericType
(Type type) Unwrap a generic type.static Object
wrapValue
(boolean val) static Object
wrapValue
(byte val) static Object
wrapValue
(char val) static Object
wrapValue
(double val) static Object
wrapValue
(float val) static Object
wrapValue
(int val) static Object
wrapValue
(long val) static Object
wrapValue
(short val) static Object
-
Field Details
-
classcache
Class lookup cache (classloader(weak)->Map([name, import]->class)). -
innerclassnamecache
Inner class name lookup cache. -
methodcache
Method lookup cache (class->(name->method[])). -
allmethodcache
Method lookup cache (class->(name->method[])), includes non-public methods. -
fieldcache
Field lookup cache (class->(name->field[])). -
basictypes
Mapping from basic class name -> basic type(class). -
wrappedtypes
Mapping from basic class -> object type(class). -
convertabletypes
String convertable types. -
HAS_GUI
public static final boolean HAS_GUIThis is set to true if the VM has a working GUI environment available. -
NULL
Null object. -
EMPTY_ARRAY
-
GET_MODULE
Reflective access to Class.getModule(). -
IS_EXPORTED
Reflective access to Module.isExported().
-
-
Constructor Details
-
SReflect
public SReflect()
-
-
Method Details
-
getWrappedType
Get the wrapped type. This method converts basic types such as boolean or int to the object types Boolean, Integer.- Parameters:
clazz
- The basic class.- Returns:
- The wrapped type, return clazz when it is no basic type.
-
unwrapGenericType
Unwrap a generic type.- Parameters:
type
- The generic type.- Returns:
- The unwrapped class.
-
getInnerGenericType
Unwrap a generic type.- Parameters:
type
- The generic type.- Returns:
- The unwrapped class.
-
getClass
Get the class for a type. -
isStringConvertableType
Test if the class is automatically convertable from/to a string. Is valid for all basic types and their object pendants.- Parameters:
clazz
- The class.- Returns:
- True, if convertable.
-
isBasicType
Is basic type.- Returns:
- True, if the class is a basic type.
-
wrapValue
-
wrapValue
-
wrapValue
-
wrapValue
-
wrapValue
-
wrapValue
-
wrapValue
-
wrapValue
-
wrapValue
-
classForName0
Extension for Class.forName(), because primitive types are not supported. Uses static cache to speed up lookup.- Parameters:
name
- The class name.- Returns:
- The class, or null if not found.
-
classForName0
Extension for Class.forName(), because primitive types are not supported.- Parameters:
name
- The class name.- Returns:
- The class, or null if not found.
-
classForName
public static Class<?> classForName(String name, ClassLoader classloader) throws ClassNotFoundException Extension for Class.forName(), because primitive types are not supported. Uses static cache to speed up lookup.- Parameters:
name
- The class name.- Returns:
- The class.
- Throws:
ClassNotFoundException
-
getClassName
Beautifies names of arrays (eg 'String[]' instead of '[LString;').- Returns:
- The beautified name of a class.
-
getGenericClassName
Returns generic type name.- Parameters:
t
- The type.c
- The class, used when type is variable declaration.- Returns:
- The name of the type.
-
getUnqualifiedClassName
Get unqualified class name. Also beautifies names of arrays (eg 'String[]' instead of '[LString;').- Returns:
- The unqualified (without package) name of a class.
-
getUnqualifiedTypeName
Get unqualified type name.- Returns:
- The unqualified (without package) name of a class.
-
getTypeName
Get unqualified type name.- Returns:
- The unqualified (without package) name of a class.
-
makeNiceArrayNotation
Process a type name and replace array notation with nice one. -
cutPackageFromClassName
Cut package off from classname. -
main
-
getInnerClassName
Get inner class name.- Returns:
- The inner class's name (without declaring class).
-
getPackageName
Get the package of a class.- Returns:
- The name of the package.
-
getMethodSignature
Get the generic signature of a method.- Parameters:
method
- The method.- Returns:
- The signature name.
-
getField
Get a field of the class, or any of it's superclasses. UnlikeClass.getField(String)
, this will also return nonpublic fields (except when running in netscape :-( ).- Parameters:
clazz
- The class to search.name
- The name of the field to search for.- Returns:
- The field (or null if not found).
-
getCachedField
Get a cached field.- Parameters:
clazz
- The clazz.name
- The name.- Returns:
- The field.
- Throws:
NoSuchFieldException
-
getDeclared
Get the declared object, doesnt cumulate through superclasses.- Parameters:
o
- The object.fieldname
- The name of the array.
-
getMethod
Get a public method of the class. UnlikeClass.getMethod(String, Class[])
, this uses the methodcache.- Parameters:
clazz
- The class to search.name
- The name of the method to search for.types
- The parameter types.- Returns:
- The method (or null if not found).
-
getAnyMethod
Get a method of the class. UnlikeClass.getMethod(String, Class[])
, this uses the methodcache.- Parameters:
clazz
- The class to search.name
- The name of the method to search for.types
- The parameter types.- Returns:
- The method (or null if not found).
-
getMethods
Get public method(s) of the class by name.- Parameters:
clazz
- The class to search.name
- The name of the method to search for.- Returns:
- The method(s).
-
getAllMethods
Get all method(s) of the class by name, including public, protected and private methods.- Parameters:
clazz
- The class to search.name
- The name of the method to search for.- Returns:
- The method(s).
-
getAllMethods
Get all methods of a class including public, protected and private methods of the class and its superclasses.- Returns:
- Array of all methods starting from the current class upwards towards Object.class.
-
getDeclaredMethod0
public static final Method getDeclaredMethod0(Class<?> clazz, String methodname, Class<?>... parametertypes) Gets a declared methods similar to Class.getDeclaredMethod() but returns null instead of throwing exception.- Parameters:
clazz
- The class being operated on.methodname
- Name of the method.parametertypes
- The parameter types.- Returns:
- Method, if declared method is found in the class, null otherwise.
-
getDeclaringInterface
public static final Class<?> getDeclaringInterface(Class<?> iface, String methodname, Class<?>... parametertypes) Finds the declaring interface of a method in a multiple-inheritance interface using a breadth-first approach.- Parameters:
iface
- The starting interface.methodname
- The method name.parametertypes
- The method parameter types.- Returns:
- The declaring interface or null if none is found.
-
getAllFields
Get all fields of a class including public, protected and private fields of the class and its superclasses.- Returns:
- Array of all fields starting from the current class upwards towards Object.class.
-
findClass
public static <T> Class<T> findClass(String clname, String[] imports, ClassLoader classloader) throws ClassNotFoundException Find a class. When the class name is not fully qualified, the list of imported packages is searched for the class.- Parameters:
clname
- The class name.imports
- The comma separated list of imported packages.- Throws:
ClassNotFoundException
- when the class is not found in the imports.
-
findClass0
Find a class. Also supports basic types and arrays. When the class name is not fully qualified, the list of imported packages is searched for the class.- Parameters:
clname
- The class name.imports
- The comma separated list of imported packages.- Returns:
- null, when the class is not found in the imports.
-
newInstance
Reflectively instantiates an object by heuristically matching the constructor parameters.- Parameters:
classname
- Name of the object class.conparams
- Constructor parameters.- Returns:
- Instantiated object.
-
newInstance
Reflectively instantiates an object by heuristically matching the constructor parameters.- Parameters:
classname
- Name of the object class.cl
- The classloader to find the class.conparams
- Constructor parameters.- Returns:
- Instantiated object.
-
matchArgumentTypes
Match the given argument types to a set of parameter type arrays. The argument type array may contain null values, when an argument type is unknown. For convenience, the length of a parameter type array does not have to equal the argument array length. (although it will of course never match). The method returns the indices of the matching parameter type arrays. An empty array is returned, if no match is found. The returned matches are sorted by quality (best match first).- Parameters:
argtypes
- The array of argument types.paramtypes
- The array of parameter type arrays.- Returns:
- The indices of the matching parameter type arrays.
-
fillArguments
Map arguments to parameters using varargs, if necessary. -
isSupertype
Check if a class is a supertype of, or the same as another class. Maps basic types to wrapped types, and respects the basic type hierarchy.- Parameters:
clazz1
- The assumed supertype.clazz2
- The assumed subtype.- Returns:
- True, if clazz1 is a supertype of, or the same as clazz2.
-
convertWrappedValue
Convert a value to the correct wrapped type. Assumes that the conversion is possible.- Parameters:
value
- The value.clazz
- The target clazz.- Returns:
- The converted value.
- See Also:
-
getIterable
Get an iterator for an arbitrary collection object. Supports iterators, enumerations, java.util.Collections, java.util.Maps, arrays. Null is converted to empty iterator.- Parameters:
collection
- The collection object.- Returns:
- An iterator over the collection.
- Throws:
IllegalArgumentException
- when argument is not one of (Iterator, Enumeration, Collection, Map, Array).
-
getIterator
Get an iterator for an arbitrary collection object. Supports iterators, enumerations, java.util.Collections, java.util.Maps, arrays. Null is converted to empty iterator.- Parameters:
collection
- The collection object.- Returns:
- An iterator over the collection.
- Throws:
IllegalArgumentException
- when argument is not one of (Iterator, Enumeration, Collection, Map, Array).
-
isIterable
Test if object is some kind of collection.- Parameters:
obj
- The object.- Returns:
- True if is iterable.
-
isIterableClass
Test if class is some kind of collection.- Parameters:
clazz
- The class.- Returns:
- True if is iterable.
-
getIterableComponentType
Get the component type of a class that is some kind of collection.- Parameters:
clazz
- The class.- Returns:
- The component type, i.e. type of contained elements as defined in collection class.
-
createComposite
Create an fill an object of a class that is some kind of collection.- Parameters:
values
- The values.clazz
- The class.
-
getArray
Get an array for an arbitrary collection object. Supports iterators, enumerations, java.util.Collections, java.util.Maps, arrays. Null is converted to empty array.- Parameters:
collection
- The collection object.- Returns:
- An array over the collection.
- Throws:
IllegalArgumentException
- when argument is not one of (Iterator, Enumeration, Collection, Map, Array).
-
instanceOf
Is an object instanceof a class or its superclasses.- Parameters:
o
- The object.c
- The class.- Returns:
- True, when o is instance of class c.
-
getSuperInterfaces
Get the super interfaces.- Parameters:
interfaces
- The interfaces- Returns:
- All super interfaces.
-
getDefaultValue
Get default value for basic types. -
getExportedMethod
Gets a method that is exported by the module.- Parameters:
clazz
- Class with the method.name
- Name of the method.params
- Method parameters.- Returns:
- The method or null if not found.
-
isExported
Tests if the class is part of a package that the containing module has exported.- Parameters:
clazz
- The class.- Returns:
- True, if exported.
-
getMethodName
Get the current method name from the caller.- Returns:
- The method name.
-
getAnnotationDefaultValue
Get the default value of an annotation method.- Parameters:
clazz
- The annotation class.name
- The method name.- Returns:
- The value.
-
hasGui
public static boolean hasGui()Test if there is a gui available.
-