public class Reflection extends Object
Modifier and Type | Class and Description |
---|---|
static class |
Reflection.InstanceField
This represents a Field that is tied to an instance of an Object.
|
Modifier and Type | Field and Description |
---|---|
static Class[] |
emptyClasses
A zero length Class array.
|
static Object[] |
emptyParameters
A zero length Object array.
|
static String |
encodedStringClass |
Modifier and Type | Method and Description |
---|---|
static String |
arrayToString(Object array) |
static Object |
bestReference(Object data,
Object classOrReflect)
This returns the best object to get reference info on a particular class.
|
static void |
cacheMethod(Class c,
Method m) |
static void |
cacheMethod(Class c,
String nameAndSpecs) |
static Object |
cast(Object obj,
Class asType)
If obj is an instance of asType, then return obj - otherwise return null.
|
static Object |
castArray(Object from,
Class destinationComponentType) |
static Object |
castObject(Object from,
Class destinationType) |
static boolean |
copy(Object source,
Object dest)
This copies data from the source to the destination.
|
static Object |
copyArray(Object array,
int offset,
int length) |
static boolean |
copyField(Field f,
Object source,
Object dest,
Class theClass,
Wrapper buffer)
This copies one field from the source object to the destination object using the
provided Reflect Object as the class specifier.
|
static boolean |
copyField(String field,
Object source,
Object dest,
Class theClass)
This copies one field from the source object to the destination object using the
provided Reflect Object as the class specifier.
|
static boolean |
copyField(String field,
Object source,
Object dest,
Class theClass,
Wrapper buffer)
This copies one field from the source object to the destination object using the
provided Reflect Object as the class specifier.
|
static Object |
createZeroValue(Class type)
Create a zero value Java wrapper object.
|
static Field[] |
fieldsFor(Object dataObject)
Get all the public fields for the data object, using the "_fields" variables for
the object if present.
|
static String |
findEncodedFieldOrMethodType(Class cl,
String fieldOrMethod)
Attempt to get the encoded field or method type.
|
static void |
fixObjectTypes(Object[] objects,
Class[] types) |
static void |
fixParameters(Method m,
Object[] parameters) |
static Class |
forEncodedName(String name)
Convert a Java encoded type into a Class representing the type.
|
static Class |
forName(String name)
Attempt to get a class using all possible class loaders.
|
static Class |
forName(String name,
Class requestor)
Attempt to get a class using the system class loader and the class loader
of the requestor class.
|
static Class |
getBaseClass(Class actualClass,
String targetName)
Given a Class, find the superclass which ends with targetName.
|
static Object |
getCachedCopy(Object obj)
Get a copy of an Object from cached re-used objects if possible.
|
static Method |
getCachedMethod(Class c,
String name,
Class[] pars) |
static Constructor |
getConstructor(Class c,
Class[] parameters)
Find a Class constructor.
|
static Constructor |
getConstructor(Class c,
String parameters)
Find a Class constructor.
|
static Object |
getCopyOrNew(Object obj)
Get a copy of an Object or a new instance of the object if possible.
|
static Constructor |
getDefaultConstructor(Class c)
Find a Class constructor.
|
static String |
getEncodedClassNameOnly(Class type)
This returns an encoded class name, but without the leading 'L' and
trailing ';' unless the class is an array of objects, in which case the 'L'
and ';' are left in the component specifier.
|
static String |
getEncodedMethod(Method m)
Convert a Method into a Java encoded method.
|
static String |
getEncodedMethod(String name,
Class[] parameters,
Class returnType) |
static StringBuffer |
getEncodedMethod(String name,
Class[] parameters,
Class returnType,
StringBuffer dest) |
static String |
getEncodedName(Class type)
Convert a Java type represented by the Class as a java encoded type string.
|
static StringBuffer |
getEncodedName(Class type,
StringBuffer destination)
Convert a Java type represented by the Class as a java encoded type string.
|
static String |
getEncodedParameters(Class[] parameters,
boolean encloseInBrackets)
Create a Java encoded parameter list from a list of Class types.
|
static StringBuffer |
getEncodedParameters(Class[] parameters,
boolean encloseInBrackets,
StringBuffer b) |
static char |
getEncodedType(Class type)
Convert a Java type represented by the Class as the first letter of the java encoded type of the class.
|
static Field |
getField(Class c,
String nameAndType,
boolean declaredOnly)
Get a class Field and optionally verify its type.
|
static Field |
getField(Class c,
String name,
Class type,
boolean declaredOnly)
Get a class Field and optionally verify its type.
|
static Field |
getField(Class c,
String name,
String type,
boolean declaredOnly)
Get a class Field and optionally verify its type.
|
static Wrapper |
getFieldValue(Class cl,
Object target,
String fieldNameAndType,
boolean declaredOnly)
Get a field value.
|
static Wrapper |
getFieldValue(Object target,
Field field,
Wrapper destination)
Get a field value using a eve.sys.Wrapper instead of a standard Java wrapper.
|
static Wrapper |
getFieldValue(Object classOrObject,
String fieldNameAndType)
Get a field value.
|
static Method |
getMethod(Class c,
String nameAndParametersAndReturnType,
boolean declaredOnly)
Find a Class method and optionally confirm the return type.
|
static Method |
getMethod(Class c,
String name,
Class[] parameters,
Class returnType,
boolean declaredOnly)
Find a Class method and optionally confirm the return type.
|
static Method |
getMethod(Class c,
String name,
String parametersAndReturnType,
boolean declaredOnly)
Find a Class method and optionally confirm the return type.
|
static Class[] |
getParameterTypes(String encodedTypes)
Convert an encoded paramter list into an array of Classes representing the
parameter types.
|
static char[] |
getParameterTypesAsEncodedType(String encodedTypes)
Convert an encoded paramter list into an array of chars representing the
parameter types as encoded types.
|
static char[] |
getParameterTypesAsEncodedType(SubString encodedTypes) |
static Class |
getReturnOrFieldType(String encodedParameterSpecsOrType)
Get the return or field type as a class.
|
static char |
getWrapperType(Class type)
Exactly the same as getEncodedType().
|
static Wrapper |
handleProxyObjectMethod(Object proxy,
Method method,
Object[] parameters)
This is used in InvocationHandler objects to handle standard Object
methods.
|
static void |
invoke(Object target,
Method method,
Wrapper[] parameters,
Wrapper result)
Invoke a method using Eve Wrapper values.
|
static Handle |
invokeAsync(Object target,
Method method,
Object[] parameters)
This method invokes the method in the background returning a Handle used to monitor
the running task.
|
static Handle |
invokeAsync(Object targetOrClass,
String nameAndParametersAndType,
Object[] parameters)
This method invokes the method in the background returning a Handle used to monitor
the running task.
|
static Wrapper |
invokeMethod(Class cl,
Object target,
String nameAndParametersAndType,
Object[] parameters,
boolean declaredOnly,
Wrapper dest)
This will find and invoke a method on an object or a static method on a class.
|
static Wrapper |
invokeMethod(Object objectOrClass,
String nameAndParametersAndType,
Object[] parameters)
This will find and invoke a method on an object or a static method on a class.
|
static Task |
invokeMethodTask(Object target,
Method method,
Object[] parameters)
Return a Task, which, when started or run() will invoke the specified
method on the specified target.
|
static boolean |
isInstanceof(String targetClass,
Class testClass)
Test if testClass is an instance of targetClass without loading
the Class for targetClass.
|
static boolean |
isTypeOf(Class c,
Class aType)
See if the Class c is of the type aType.
|
static boolean |
isTypeOf(Class c,
String aType)
Check if a Class is of the type aType, where aType is a Java encoded name.
|
static boolean |
isTypeOf(String c,
String aType)
Check if a Class is of the type aType, where aType is a Java encoded name.
|
static boolean |
methodThrows(Method m,
Throwable t)
Check if a thrown exception is acceptable as being thrown by the method -
i.e. if it is in its list of throws exception types.
|
static Object |
newInstance(Class c)
Create a new instance of an object, but do not throw an exception on failure,
return null instead.
|
static Object |
newInstance(Class c,
String parameterSpecs,
Object[] parameters)
Create a new instance of an object, but do not throw an exception on failure,
return null instead.
|
static Object |
newInstance(Constructor c,
Wrapper[] parameters)
Construct a new Object using Eve Wrapper values.
|
static Object |
newProxyInstance(ClassLoader c,
Class[] interfaces,
InvocationHandler h) |
static void |
setArrayComponent(Object destination,
int index,
Class destinationComponentType,
Object toSet) |
static boolean |
setFieldValue(Class cl,
Object target,
String fieldNameAndType,
boolean declaredOnly,
Object value)
Set a field value.
|
static void |
setFieldValue(Object target,
Field field,
Wrapper data)
Set a field value using a eve.sys.Wrapper instead of a standard Java wrapper.
|
static boolean |
setFieldValue(Object classOrObject,
String fieldNameAndType,
Object value)
Set a field value.
|
static Class |
toClass(Object sampleObjectOrClass)
A number of methods take either a Class specifying a type or an example object of the
type as a parameter.
|
static Object |
toObject(Object sampleObjectOrClass)
A number of methods take either a Class specifying a type or an example object of the
type as a parameter.
|
static Object |
toSingleArray(Object array,
int offset,
int length) |
static Object |
toWrapper(Class primitiveType,
boolean value)
Convert a boolean value into a Wrapper appropriate for
the specified primitive type, which should be boolean.
|
static Object |
toWrapper(Class primitiveType,
char value)
Convert an char value into a Wrapper appropriate for
the specified primitive type, which should be byte, short, char, int, long or boolean
|
static Object |
toWrapper(Class primitiveType,
double value)
Convert a floating point value into a Wrapper appropriate for
the specified primitive type, which should be double or float.
|
static Object |
toWrapper(Class primitiveType,
long value)
Convert an integer value into a Wrapper appropriate for
the specified primitive type, which should be byte, short, char, int, long or boolean
|
static boolean |
unwrapBoolean(Object value) |
static double |
unwrapDouble(Object value) |
static long |
unwrapLong(Object value) |
public static final String encodedStringClass
public static final Class[] emptyClasses
public static final Object[] emptyParameters
public static Object newProxyInstance(ClassLoader c, Class[] interfaces, InvocationHandler h)
public static Object getCachedCopy(Object obj)
obj
- the object to copy using a Cache if possible.RuntimeException
- if it could not create a new version of the Object.public static Object getCopyOrNew(Object obj)
obj
- public static Field[] fieldsFor(Object dataObject)
dataObject
- The dataObject to get the fields for.public static Object newInstance(Constructor c, Wrapper[] parameters) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException
c
- The Constructor to use.parameters
- the parameters as an array of Wrapper values.IllegalAccessException
IllegalArgumentException
InvocationTargetException
InstantiationException
public static void invoke(Object target, Method method, Wrapper[] parameters, Wrapper result) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
target
- the target Object. This can be null for static methods.method
- the Method to invoke.parameters
- the parameters as Wrapper values.result
- a Wrapper to hold the destination.IllegalAccessException
IllegalArgumentException
InvocationTargetException
public static Handle invokeAsync(Object targetOrClass, String nameAndParametersAndType, Object[] parameters)
targetOrClass
- The target object or a Class object if the target method
is static.nameAndParametersAndType
- the fully encoded name and type of the method: e.g.
invokeMethod(Ljava/lang/Class;Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;Z)Leve/sys/Wrapper;parameters
- the parameters to be sent to the method. Leave the parameter which
is supposed to be a provided Handle as null and a new Handle will be created and
provided.public static Handle invokeAsync(Object target, Method method, Object[] parameters)
targetOrClass
- The target object or a Class object if the target method
is static.nameAndParametersAndType
- the fully encoded name and type of the method: e.g.
invokeMethod(Ljava/lang/Class;Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;Z)Leve/sys/Wrapper;parameters
- the parameters to be sent to the method. Leave the parameter which
is supposed to be a provided Handle as null and a new Handle will be created and
provided.public static Wrapper getFieldValue(Object target, Field field, Wrapper destination) throws IllegalAccessException, IllegalArgumentException
target
- the target Object.field
- the Field to get.destination
- the Wrapper that will hold the data read in.IllegalAccessException
IllegalArgumentException
public static void setFieldValue(Object target, Field field, Wrapper data) throws IllegalAccessException, IllegalArgumentException
target
- the target Object.field
- the Field to set.data
- the Wrapper containing the data to be set.IllegalAccessException
IllegalArgumentException
public static Object newInstance(Class c, String parameterSpecs, Object[] parameters)
c
- The class to create the new instance of.parameterSpecs
- The java encoded parameter list for the constructor (e.g. "[BII").parameters
- the list of parameters using standard Java wrappers or you can use eve.sys.Wrapper objects.
If you use eve.sys.Wrapper objects, the array must be of the type Wrapper[].public static Object newInstance(Class c)
c
- The class to create the new instance of.public static Class forName(String name)
name
- The class name. This can be a Java encoded name or a non-encoded
name, and can specify an array, but it must not be a primitive value. For primitive
values use Reflection.forEncodedName()public static Class forName(String name, Class requestor)
name
- The class name. This can be a Java encoded name or a non-encoded
name, and can specify an array, but it must not be a primitive value. For primitive
values use Reflection.forEncodedName().requestor
- The requesting class.public static Class forEncodedName(String name)
name
- the Java encoded type name.public static char getEncodedType(Class type)
type
- The class to represent as an encoded type.public static char getWrapperType(Class type)
type
- The class to represent as an encoded type.public static StringBuffer getEncodedName(Class type, StringBuffer destination)
type
- The class to represent as an encoded type.destination
- a destination StringBuffer to append the type to.public static String getEncodedName(Class type)
type
- The class to represent as an encoded type.public static String getEncodedClassNameOnly(Class type) throws IllegalArgumentException
This will only work for arrays and classes - not primitive values.
type
- the Class to get the encoded name of.IllegalArgumentException
- if the type is primitive.public static Class getBaseClass(Class actualClass, String targetName)
actualClass
- the Class to search for.targetName
- the target class name.public static StringBuffer getEncodedParameters(Class[] parameters, boolean encloseInBrackets, StringBuffer b)
public static String getEncodedParameters(Class[] parameters, boolean encloseInBrackets)
parameters
- the parameter list as an array of Classes.encloseInBrackets
- true to place '(' around the list.public static StringBuffer getEncodedMethod(String name, Class[] parameters, Class returnType, StringBuffer dest)
public static String getEncodedMethod(String name, Class[] parameters, Class returnType)
public static String getEncodedMethod(Method m)
m
- the Method to convert.public static char[] getParameterTypesAsEncodedType(String encodedTypes)
encodedTypes
- a list of parameter types which may or may not be enclosed in "()" brackets.public static char[] getParameterTypesAsEncodedType(SubString encodedTypes)
public static Class[] getParameterTypes(String encodedTypes)
encodedTypes
- a list of parameter types which may or may not be enclosed in "()" brackets.public static Class getReturnOrFieldType(String encodedParameterSpecsOrType)
encodedParameterSpecsOrType
- either a single encoded type or parameters in brackets
followed by a single encoded type.public static Wrapper invokeMethod(Class cl, Object target, String nameAndParametersAndType, Object[] parameters, boolean declaredOnly, Wrapper dest)
cl
- The class of the Object.target
- The target Object, which may be null for static methods.nameAndParametersAndType
- the fully encoded name and type of the method: e.g.
invokeMethod(Ljava/lang/Class;Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;Z)Leve/sys/Wrapper;parameters
- the list of parameters using standard Java wrappers or you can use eve.sys.Wrapper objects,
but the array must be of the type Wrapper[].declaredOnly
- set true if you are looking for methods declared by that Class.public static Wrapper invokeMethod(Object objectOrClass, String nameAndParametersAndType, Object[] parameters)
objectOrClass
- for a static method this should be a Class object, for an instance method
it should be an instance of the class.nameAndParametersAndType
- the fully encoded name and type of the method: e.g.
invokeMethod(Ljava/lang/Class;Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;Z)Leve/sys/Wrapper;parameters
- the list of parameters using standard Java wrappers or you can use eve.sys.Wrapper objects,
but the array must be of the type Wrapper[].public static Method getMethod(Class c, String name, Class[] parameters, Class returnType, boolean declaredOnly)
c
- The Class to look in.name
- the name of the method.parameters
- The parameter list of the method.returnType
- The optional return type. If it is null the return type is not checked.declaredOnly
- set true if you are looking for methods declared by that Class.public static Method getMethod(Class c, String name, String parametersAndReturnType, boolean declaredOnly)
c
- The Class to look in.name
- the name of the method.parametersAndReturnType
- The parameter list and optional return type of the method as a Java encoded string.declaredOnly
- set true if you are looking for methods declared by that Class.public static Method getMethod(Class c, String nameAndParametersAndReturnType, boolean declaredOnly)
c
- The Class to look in.nameAndParametersAndReturnType
- The name and parameter list and optional return type of the method as a Java encoded string.declaredOnly
- set true if you are looking for methods declared by that Class.public static Wrapper handleProxyObjectMethod(Object proxy, Method method, Object[] parameters) throws Throwable
proxy
- the proxy being used.method
- the method being called.parameters
- the parameters for the method call.Throwable
public static Constructor getDefaultConstructor(Class c)
c
- The Class to look in.public static Constructor getConstructor(Class c, Class[] parameters)
c
- The Class to look in.parameters
- The parameter list of the constructor.public static Constructor getConstructor(Class c, String parameters)
c
- The Class to look in.parameters
- The parameter list of the constructor as a Java Type Encoded String - with
or without the brackets - i.e. "BII" or "(BII)".public static Field getField(Class c, String name, Class type, boolean declaredOnly)
c
- The class to search for the field.name
- the name of the field.type
- the optional type of the field. If it is null the type of the field will not
be verified.declaredOnly
- set true if you are looking for fields declared by that Class.public static Field getField(Class c, String name, String type, boolean declaredOnly)
c
- The class to search for the field.name
- the name of the field.type
- the optional type of the field in Java type encoded format.
If the type of the field is '$' it is assumed to mean java.lang.String.
If it is null the type of the field will not be verified.declaredOnly
- set true if you are looking for fields declared by that Class.public static Field getField(Class c, String nameAndType, boolean declaredOnly)
c
- The class to search for the field.nameAndType
- the name of the field optionally followed by a ':' and the type of the field.
If the type of the field is '$' it is assumed to mean java.lang.String. If no ':' and
field type is present the type of the field will not be verified.declaredOnly
- set true if you are looking for fields declared by that Class.public static Wrapper getFieldValue(Class cl, Object target, String fieldNameAndType, boolean declaredOnly)
cl
- The class to get the field value from.target
- The target object to get the field value from, which can be null for static fields.fieldNameAndType
- the name of the field optionally followed by a ':' and the type of the field.
If the type of the field is '$' it is assumed to mean java.lang.String. If no ':' and
field type is present the type of the field will not be verified.declaredOnly
- set true if you are looking for fields declared by that Class.public static Wrapper getFieldValue(Object classOrObject, String fieldNameAndType)
objectOrClass
- for a static field this should be a Class object, for an instance field
it should be an instance of the class.fieldNameAndType
- the name of the field optionally followed by a ':' and the type of the field.
If the type of the field is '$' it is assumed to mean java.lang.String. If no ':' and
field type is present the type of the field will not be verified.declaredOnly
- set true if you are looking for fields declared by that Class.public static boolean setFieldValue(Class cl, Object target, String fieldNameAndType, boolean declaredOnly, Object value)
cl
- The class to set the field value.target
- The target object to set the field value, which can be null for static fields.fieldNameAndType
- the name of the field optionally followed by a ':' and the type of the field.
If the type of the field is '$' it is assumed to mean java.lang.String. If no ':' and
field type is present the type of the field will not be verified.declaredOnly
- set true if you are looking for fields declared by that Class.value
- the value to set as a Java Wrapper object or an eve.sys.Wrapper object.public static boolean setFieldValue(Object classOrObject, String fieldNameAndType, Object value)
objectOrClass
- for a static field this should be a Class object, for an instance field
it should be an instance of the class.fieldNameAndType
- the name of the field optionally followed by a ':' and the type of the field.
If the type of the field is '$' it is assumed to mean java.lang.String. If no ':' and
field type is present the type of the field will not be verified.value
- the value to set as a Java Wrapper object or an eve.sys.Wrapper object.public static Object createZeroValue(Class type)
type
- the Class representing the type of the data.public static boolean isTypeOf(Class c, Class aType)
c
- The class to check.aType
- the type.public static boolean isTypeOf(Class c, String aType)
c
- The class to check.aType
- the Java encoded name of a type.public static boolean isTypeOf(String c, String aType)
c
- The class to check.aType
- the Java encoded name of a type.public static boolean copyField(String field, Object source, Object dest, Class theClass)
In order for this to work the Class AND the Field must be public.
field
- The field name.source
- The source object.dest
- The destination object.theClass
- A Class object representing the exact class which is having its field copied.public static boolean copyField(Field f, Object source, Object dest, Class theClass, Wrapper buffer)
In order for this to work the Class AND the Field must be public.
field
- The field name.source
- The source object.dest
- The destination object.theClass
- A Class object representing the exact class which is having its field copied.buffer
- an optional Wrapper that can be used as a buffer for the field transfer.public static boolean copyField(String field, Object source, Object dest, Class theClass, Wrapper buffer)
In order for this to work the Class AND the Field must be public.
field
- The field name.source
- The source object.dest
- The destination object.theClass
- A Class object representing the exact class which is having its field copied.buffer
- an optional Wrapper that can be used as a buffer for the field transfer.public static boolean copy(Object source, Object dest)
public static Class toClass(Object sampleObjectOrClass)
sampleObjectOrClass
- The Class of a type of an example object of the type.public static Object toObject(Object sampleObjectOrClass)
sampleObjectOrClass
- The Class of a type of an example object of the type.public static Object toWrapper(Class primitiveType, long value) throws IllegalArgumentException
primitiveType
- the primitive type.value
- the value to be wrapped.IllegalArgumentException
- if the value cannot be converted
to the primitive type.public static Object toWrapper(Class primitiveType, double value) throws IllegalArgumentException
primitiveType
- the primitive type.value
- the value to be wrapped.IllegalArgumentException
- if the value cannot be converted
to the primitive type.public static Object toWrapper(Class primitiveType, char value) throws IllegalArgumentException
primitiveType
- the primitive type.value
- the value to be wrapped.IllegalArgumentException
- if the value cannot be converted
to the primitive type.public static Object toWrapper(Class primitiveType, boolean value) throws IllegalArgumentException
primitiveType
- the primitive type.value
- the value to be wrapped.IllegalArgumentException
- if the value cannot be converted
to the primitive type.public static long unwrapLong(Object value) throws IllegalArgumentException
IllegalArgumentException
public static double unwrapDouble(Object value) throws IllegalArgumentException
IllegalArgumentException
public static boolean unwrapBoolean(Object value)
public static Object bestReference(Object data, Object classOrReflect)
data
- An instance of a class.classOrReflect
- A Class or Reflect object for the class.public static Task invokeMethodTask(Object target, Method method, Object[] parameters)
target
- the target object, which can be null for a static method.method
- the method to invoke.parameters
- the parameters to invoke on. This can be null for
an empty parameter list.public static boolean methodThrows(Method m, Throwable t)
m
- the method.t
- the thrown exception.public static String findEncodedFieldOrMethodType(Class cl, String fieldOrMethod)
cl
- the Class to look for the field.fieldOrMethod
- the name of the field or the name of the method
followed by '(' and then the Java type encoded parameters followed by
')'.public static boolean isInstanceof(String targetClass, Class testClass)
targetClass
- the target class.testClass
- the test class.public static Object cast(Object obj, Class asType)
obj
- the Object to test.asType
- the type to test.public static void setArrayComponent(Object destination, int index, Class destinationComponentType, Object toSet)