T - parameter representing the class to be accessed.public final class Accessor<T> extends Object
i
are given by i.getClass()
(except for instances of primitive types)
but also if no instance is available,
the class object is given by
<classname>.class for all types even the primitive ones
and also for arrays
(int.class and int[].class are valid)
but not for hidden inner classes, e.g. private ones,
Boolean.TYPE,
Character.TYPE and so on for the primitive types
(which is superfluous because boolean.class
works as well as Boolean.TYPE,
except for java.lang.Void.TYPE which I never used so far),
getInnerClass(Class,String) and
getInnerClass(Class,String[]) for hidden inner classes.
Note that formally this even works for anonymous classes
but since names of anonymous classes may change
if another anonymous inner class is inserted,
this feature should be used with caution.
I personally feel it is best not to use it at all.
Class.forName(String className).
The method getField(Class,String)
returns the value of the specified static field.
Note that if the field has primitive type,
the wrapper of its content is returned.
Correspondingly, getField(Object,String)
returns the specified instant field.
Note that since the class Class is final,
and has no (non-public) fields, there should be no reason,
to access a Class-object.
In case of a very special application,
casts like getField((Object)ls,name)
should resolve the ambiguity.
The two methods described above, should cover the typical situations.
In some special cases however,
including overwritten fields and fields of inner classes,
getField(Class,Object,String)
may provide the only way to access a field.
What can be said about the methods getField
applies correspondingly to methods setField.
Note that there is no way to change fields
which are declared final.
To be more precise, the pointer in a final field
Similarly for invoking methods:
Except for very special cases,
invokeStatic(Class,String,Object...) for static methods and
invoke(Object,String,Object...) for instance methods
will be sufficient, but for some special cases
including overwriting and inner classes
invoke(Class,Object,String,Object...)
may be the only way to invoke a method.
Note that these methods apply to parameters with primitive types as well
by passing the appropriate wrapper object:
For example Integer.toString(int i) may be invoked by
invoke(Integer.class,"toString",new Integer(i)).
Still there is a problem:
if wrapping parameters makes signatures ambiguous,
the parameter types must be specified explicitly
using method invoke(Class,Object,String,Class[],Object[]).
For this case, which seems to be quite rare,
there are no convenience methods.
Note that the return type is always Object.
Of course, the return value may be casted
to the return type of the specified method,
provided the return type is an object and
neither void nor a primitive type.
In the latter case, the return value is wrapped in the corresponding
wrapper class unlike the parameters.
For the last case, creating objects using constructors,
in most cases create(Class,Object[]) is sufficient;
to avoid ambiguities, one has to specify the types of the parameters
and use create(Class,Class[],Object[]) instead.
| Modifier and Type | Field and Description |
|---|---|
private static String |
INNER_SEPARATOR
The separator between a class and its enclosing class
for inner classes.
|
private static String |
STR_DNE |
private static String |
STR_IN_CLS |
private static String |
STR_SPEC_NULL_CLS |
private static String |
UNSPECIFIED_CLASS
String denoting an unspecified class.
|
| Modifier | Constructor and Description |
|---|---|
private |
Accessor()
Formally to create a new
Accessor instance
but intended in contrary
to prevent an Accessor from being instantiated. |
| Modifier and Type | Method and Description |
|---|---|
private static <T> boolean |
constructorMatches(Constructor<T> cand,
Object... parameters)
Returns whether the given constructor matches the parameter list.
|
static <T> T |
create(Class<T> aClass,
Class<?>[] paramCls,
Object... parameters)
Returns an object created by the specified constructor.
|
static <T> T |
create(Class<T> aClass,
Object... parameters)
Returns an object created by the specified constructor.
|
private static <T> T |
create(Constructor<T> toBeInvoked,
Object... parameters)
Invoke the specified constructor with the given arguments.
|
private static <T> Constructor<T> |
getConstructor(Class<T> aClass,
Object... parameters)
Returns the specified constructor
if
aClas offers exactly one possibility
with the given parameters. |
static Object |
getField(Class<?> aClass,
Object target,
String fieldName)
Returns the value of the specified static field
or member field resp. its wrapper.
|
static Object |
getField(Class<?> aClass,
String fieldName)
Returns the value of the specified static field resp. its wrapper.
|
static Object |
getField(Object target,
String fieldName)
Returns the value of the specified member field resp. its wrapper.
|
private static Field |
getFieldObj(Class<?> aClass,
String fieldName,
boolean shouldBeStatic)
Returns the specified
Field object if possible. |
static Class<?> |
getInnerClass(Class<?> enclosingCls,
String innerClsName)
Returns the inner class of
enclosingCls
with the specified name innerClsName. |
static Class<?> |
getInnerClass(Class<?> enclosingCls,
String[] pathToInner)
Returns the inner class of
enclosingCls
with the specified name innerClsName. |
private static Method |
getMethod(Class<?> aClass,
String methodName,
Method[] cands,
Object... parameters)
Returns the specified method
if
cands offers exactly one possibility. |
private static Class<?>[] |
getParamCls(Object... parameters)
Returns the classes of the given parameters.
|
(package private) static Method |
getToBeInvoked(Class<?> aClass,
String methodName,
Class<?>... paramCls)
Returns the specified method if it exists;
otherwise
null. |
static Object |
invoke(Class<?> aClass,
Object target,
String methodName,
Class<?>[] paramCls,
Object[] parameters)
If more than one method with the same parameter types
is declared in a class,
and one of these methods has a return type
that is more specific than any of the others,
that method is returned;
otherwise one of the methods is chosen arbitrarily.
|
static Object |
invoke(Class<?> aClass,
Object target,
String methodName,
Object... parameters)
Invokes the specified method with the given parameters
and returns the value (which may be void of course. )
CAUTION: This may cause an exception although the runtime system
finds out which method is ment.
|
private static Object |
invoke(Method method,
Object target,
Object... parameters)
Invokes the specified method with the given parameters
and returns the value (which may be void of course. )
|
static Object |
invoke(Object target,
String methodName,
Object... parameters)
For invoking member methods.
|
static Object |
invokeStatic(Class<?> aClass,
String methodName,
Object... parameters)
For invoking static methods.
|
private static boolean |
methodMatches(Method cand,
String methodName,
Object... parameters)
Returns whether the given method
matches the name and the parameter list.
|
private static boolean |
paramsMatch(Class<?>[] paramTypes,
Object... parameters)
Returns whether the given the parameter list matches the classes.
|
private static String |
paramsToString(Class<?>... paramCls)
Converts a list of classes into their string-representation.
|
static void |
setField(Class<?> aClass,
Object target,
String fieldName,
Object value)
If the type of the specified field extends
Object,
invoking this method acts like target.field = value;
otherwise the argument value is unwrapped first. |
static void |
setField(Class<?> aClass,
String fieldName,
Object value)
If the type of the specified field extends
Object,
invoking this method acts like aClass.fieldName = value;
otherwise the argument value is unwrapped first. |
static void |
setField(Object target,
String fieldName,
Object value)
If the type of the specified field extends
Object,
invoking this method acts like target.fieldName = value;
otherwise the argument value is unwrapped first. |
private static final String INNER_SEPARATOR
private static final String UNSPECIFIED_CLASS
private static final String STR_DNE
private static final String STR_IN_CLS
private static final String STR_SPEC_NULL_CLS
private Accessor()
Accessor instance
but intended in contrary
to prevent an Accessor from being instantiated.
Note that the modifiers final and abstract
are mutually exclusive
and so this trick is the only remaining possibility.private static String paramsToString(Class<?>... paramCls)
paramCls - an array of Classes.
The entries in the array may well be null.private static Object invoke(Method method, Object target, Object... parameters) throws InvocationTargetException
method - a Method.target - the target to which the specified method is to be applied.
the target must be null
if and only if the method specified is static.parameters - the list of parameters used when invoking method.
Note that parameters of elementary types
have to be wrapped in an object
(e.g. write new Integer(8)
instead of just 8).null is returned.IllegalArgumentException - target != null.
target == null.
IllegalStateException - with message "Method should be accessible; still is not. "
if the method is not accessible.InvocationTargetException - to wrap an exception thrown by the method invoked.
Unwrap it using Throwable.getCause().private static Class<?>[] getParamCls(Object... parameters)
parameters - a parameter list represented as an array of Objects.null-parameter,
the null-class is returned.public static Object getField(Object target, String fieldName) throws NoSuchFieldException
target - the instance for which a field is to be accessed.fieldName - the name of the field.Object,
this object is returned directly;
otherwise the value of the field is wrapped
in an object of the appropriate class.
E.g. new Integer(5) is returned
instead of the int-value 5.NoSuchFieldException - if the specified object
does not contain a field with the given name,
e.g. because fieldName == null.IllegalArgumentException - if the target is null
or if the specified field is static.setField(Object, String, Object)public static Object getField(Class<?> aClass, String fieldName) throws NoSuchFieldException
aClass - the class for which a static field is to be accessed.
Typically one will use the expression
<classname>.class to determine the class-object.fieldName - the name of the field.new Integer(5) is returned
instead of the int-value 5.NoSuchFieldException - if the specified class
does not contain a field with the given name,
e.g. because fieldName == null.IllegalArgumentException - if the class-parameter is null
or if the specified field is not static.setField(Class, String, Object)private static Field getFieldObj(Class<?> aClass, String fieldName, boolean shouldBeStatic) throws NoSuchFieldException
Field object if possible.
This method is commonly used by the methods
named getField and setField.aClass - a Class object.
Typically one will use the expression
<classname>.class
to determine the class-object.fieldName - the name of a field to look for in the specified class
and its superclasses.shouldBeStatic - whether the specified field should static.Field object
(Should never return null)
made accessible if it exists.IllegalArgumentException - if the "null-class" is specified.NoSuchFieldException - if the specified class and none of its superclasses
containd a field with the given name,
e.g. because fieldName == null.public static Object getField(Class<?> aClass, Object target, String fieldName) throws NoSuchFieldException
getField(Class, String) or
getField(Object, String) if possible.aClass - Some class object.
Either target == null
or target instanceof aClass.target - the object for which a field is to be accessed.
For static fields, this must be null;
whereas for memeber fields
this has to be the corresponding instance.fieldName - the name of the field.new Integer(5) is returned
instead of the int-value 5.NoSuchFieldException - if the specified class
does not contain a field with the given name,
e.g. because fieldName == null.IllegalArgumentException - null-class is specified
null
whereas the specified field is a member field.
null
whereas the specified field is static.
setField(Class, Object, String, Object)public static void setField(Object target, String fieldName, Object value) throws NoSuchFieldException
Object,
invoking this method acts like target.fieldName = value;
otherwise the argument value is unwrapped first.
Note that there is no way to assign a value
to a field which is declared final.target - the object for which a field is to be accessed.fieldName - the name of the field.value - If the type of the specified field extends Object,
the type of value must be a supertype;
otherwise it must be the corresponding wrapper.
E.g. the field declared by int intField;
is set by setField(target, "intField", new Integer(5))
instead of setField(target, "intField", 5).NoSuchFieldException - if the specified class
does not contain a field with the given name.IllegalArgumentException - if the target is null
or if the specified field is static
or if the specified field is declared final.getField(Object, String)public static void setField(Class<?> aClass, String fieldName, Object value) throws NoSuchFieldException
Object,
invoking this method acts like aClass.fieldName = value;
otherwise the argument value is unwrapped first.
Note that there is no way to assign a value
to a field which is declared final.aClass - the class for which a static field is to be accessed.
Typically one will use the expression
<classname>.class
to determine the class-object.fieldName - the name of the field.value - If the type of the specified field extends Object,
the type of value must be a supertype;
otherwise it must be the corresponding wrapper.
E.g. the field declared by static int intField;
is set by setField(aClass, "intField", new Integer(5))
instead of setField(aClass, "intField", 5).NoSuchFieldException - if the specified class
does not contain a field with the given name.IllegalArgumentException - if aClass == null
or if the specified field is not static
or if the specified field is declared final.getField(Class, String)public static void setField(Class<?> aClass, Object target, String fieldName, Object value) throws NoSuchFieldException
Object,
invoking this method acts like target.field = value;
otherwise the argument value is unwrapped first.
Note that there is no way to assign a value
to a field which is declared final.aClass - Some class object.
Either target == null
or target instanceof aClass.target - the object for which a field is to be accessed.fieldName - the name of the field.value - If the type of the specified field extends Object,
the type of value must be the same;
otherwise it must be the corresponding wrapper.NoSuchFieldException - if the specified class
does not contain a field with the given name.IllegalArgumentException - null-class is specified
null
whereas the specified field is a member field.
null
whereas the specified field is static.
final.
null-value is tried to be assigned.
getField(Class, Object, String)public static Object invokeStatic(Class<?> aClass, String methodName, Object... parameters) throws InvocationTargetException
aClass - The class of the static method to be invoked.
The method is looked up recursively
in the superclasses of aClass.
Typically one will use the expression
<classname>.class
to determine the class-object.methodName - the short name of a static method.
Short means without package or class.parameters - the list of parameters used when invoking methodName.
Note that parameters of elementary types
have to be wrapped in an object
(e.g. write new Integer(8)
instead of just 8).Object or a subclass,
the return value is returned directly;
new Integer(5) is returned
instead of the int-value 5.
java.lang.Void.TYPE,
null is returned.
IllegalArgumentException - aClass == null.
InvocationTargetException - to wrap an exception thrown by the method invoked.
Unwrap it using Throwable.getCause().invoke(Class, Object, String, Object...)public static Object invoke(Object target, String methodName, Object... parameters) throws InvocationTargetException
target - the target on which the specified member method is to be applied.methodName - the short name of a static method.
Short means without package or class.parameters - the list of parameters used when invoking methodName.
Note that parameters of elementary types
have to be wrapped in an object
(e.g. write new Integer(8)
instead of just 8).Object or a subclass,
the return value is returned directly;
new Integer(5) is returned
instead of the int-value 5.
java.lang.Void.TYPE,
null is returned.
IllegalArgumentException - target == null.
InvocationTargetException - to wrap an exception thrown by the method invoked.
Unwrap it using Throwable.getCause().invoke(Class, Object, String, Object...)public static Object invoke(Class<?> aClass, Object target, String methodName, Object... parameters) throws InvocationTargetException
CAUTION: This may cause an exception although the runtime system
finds out which method is ment.
This is the case if for example
methods exa(int) and exa(Integer)
are present:
both are invoked with
invoke(cls, target, "exa", new Integer(0))}.
In this case
use invoke(Class, Object, String, Class[], Object[]) instead.
aClass - The class of the method to be invoked.
The method is looked up recursively
in the superclasses of aClass.
Typically one will use the expression
<classname>.class
to determine the class-object.target - the target to which the specified method is to be applied.
the target must be null
if and only if the method specified is static.methodName - the short name of a method.
Short means without package or class.parameters - the list of parameters used when invoking methodName.
Note that parameters of elementary types
have to be wrapped in an object
(e.g. write new Integer(8)
instead of just 8).Object or a subclass,
the return value is returned directly;
new Integer(5) is returned
instead of the int-value 5.
java.lang.Void.TYPE,
null is returned.
InvocationTargetException - to wrap an exception thrown by the method invoked.
Unwrap it using Throwable.getCause().IllegalArgumentException - aClass == null.
target != null.
target == null.
public static Object invoke(Class<?> aClass, Object target, String methodName, Class<?>[] paramCls, Object[] parameters) throws InvocationTargetException
aClass - The class of the method to be invoked.
The method is looked up recursively
in the superclasses of aClass.
Typically one will use the expression
<classname>.class
to determine the class-object.target - the target to which the specified method is to be applied.
If the method specified is static, the target is ignored.
Convention: should be null in this case.methodName - the short name of a method.
Short means without package or class.paramCls - the types specifying the parameter list of the desired method.
Typically one will use the expression
<classname>.class
to denote a class, even a primitive one.
For primitive types, the alternatives
java.lang.Boolean.TYPE
java.lang.Character.TYPE
java.lang.Byte.TYPE
java.lang.Short.TYPE
java.lang.Integer.TYPE
java.lang.Long.TYPE
java.lang.Float.TYPE
java.lang.Double.TYPE
(java.lang.Void.TYPE)
are available.
For hidden inner classes, e.g. private ones,
getInnerClass(Class, String[]) and
getInnerClass(Class, String)
may be used.
If an object i of a desired class is present
and if the class of is i not primitive,
i.getClass() returns the desired class object as well.parameters - the list of parameters used when invoking methodName.
Note that parameters of elementary types
have to be wrapped in an object
(e.g. write new Integer(8)
instead of just 8).Object or a subclass,
the return value is returned directly;
new Integer(5) is returned
instead of the int-value 5.
java.lang.Void.TYPE,
null is returned.
IllegalArgumentException - if the specified method does not exist.InvocationTargetException - to wrap an exception thrown by the method invoked.
Unwrap it using Throwable.getCause().invoke(Class, Object, String, Class[], Object[])static Method getToBeInvoked(Class<?> aClass, String methodName, Class<?>... paramCls)
null.
Note that it is searched in the superclasses as well.aClass - the Class to start searching the method.
Typically one will use the expression
<classname>.class
to determine the class-object.methodName - the name of the method to be returned.paramCls - the types specifying the parameter list of the desired method.candClass
and descends recursively until a method is found.
If no method is found, null is returned.public static <T> T create(Class<T> aClass, Object... parameters) throws InstantiationException, InvocationTargetException
aClass - the class of the instance to be created.
Typically one will use the expression
<classname>.class
to determine the class-object.parameters - the list of parameters of the specified constructor.
Note that parameters of elementary types
have to be wrapped in an object
(e.g. write new Integer(8)
instead of just 8).
Note also that for static inner classes, formally the surrounding instance is prefixed as a parameter. ****** i think this does not apply to methods, but actually i did not try yet.
Object created by the specified constructor.InstantiationException - if the instantiation with the specified constructor failed.IllegalArgumentException - if the specified constructor does not exist or is not unique.InvocationTargetException - to wrap an exception thrown by the constructor invoked.
Unwrap it using Throwable.getCause().public static <T> T create(Class<T> aClass, Class<?>[] paramCls, Object... parameters) throws NoSuchMethodException, InstantiationException, InvocationTargetException
aClass - the class of the instance to be created
Typically one will use the expression
<classname>.class
to determine the class-object.paramCls - the types specifying the parameter list of the desired method.
Typically one will use the expression
<classname>.class
to denote a class, even a primitive one.
For primitive types, the alternatives
java.lang.Boolean.TYPE
java.lang.Character.TYPE
java.lang.Byte.TYPE
java.lang.Short.TYPE
java.lang.Integer.TYPE
java.lang.Long.TYPE
java.lang.Float.TYPE
java.lang.Double.TYPE
(java.lang.Void.TYPE)
are available.
For hidden inner classes, e.g. private ones,
getInnerClass(Class, String[]) and
getInnerClass(Class, String)
may be used.
If an object i of a desired class is present
and if the class of is i not primitive,
i.getClass() returns the desired class object as well.
Note also that for static inner classes, formally the surrounding class is prefixed as a parameter. ****** i think this does not apply to methods, but actually i did not try yet.
parameters - the list of parameters of the specified constructor.
Note that parameters of elementary types
have to be wrapped in an object
(e.g. write new Integer(8)
instead of just 8).
Note also that for static inner classes, formally the surrounding instance is prefixed as a parameter. ****** i think this does not apply to methods, but actually i did not try yet.
Object created by the specified constructor.InstantiationException - if the instantiation with the specified constructor failed.NoSuchMethodException - if the specified constructor does not exist.InvocationTargetException - to wrap an exception thrown by the constructor invoked.
Unwrap it using Throwable.getCause().private static <T> T create(Constructor<T> toBeInvoked, Object... parameters) throws InstantiationException, InvocationTargetException
toBeInvoked - some Constructor.parameters - parameters fitting the given Constructor.InstantiationException - if the instantiation with the specified constructor failed.InvocationTargetException - to wrap an exception thrown by the constructor invoked.
Unwrap it using Throwable.getCause().private static boolean methodMatches(Method cand, String methodName, Object... parameters)
cand - either a Constructor or a Method.methodName - the required name of a Method.
If cand is a constructor, this field is ignored;
if it is a method the name of which does not match,
null is returned.parameters - the list of parameters.true if and only if
all of the following conditions are satisfied:
cand is either a non-abstract method
named methodName.
cand
matches parameters
as specified for paramsMatch(java.lang.Class<?>[], java.lang.Object...).
IllegalStateException - if cand is neither a method nor a constructor.private static <T> boolean constructorMatches(Constructor<T> cand, Object... parameters)
cand - either a Constructor or a Method.parameters - the list of parameters.true if and only if
the parameter list of cand
matches parameters
as specified for paramsMatch(java.lang.Class<?>[], java.lang.Object...).IllegalStateException - if cand is neither a method nor a constructor.private static boolean paramsMatch(Class<?>[] paramTypes, Object... parameters)
paramTypes - the list of classes of parameters.parameters - the list of parameters.true if and only if
the parameter list of cand
matches parameters
up to equivalence of primitive types and their wrappers.
This includes that the two arrays have the same length.IllegalStateException - if cand is neither a method nor a constructor.private static Method getMethod(Class<?> aClass, String methodName, Method[] cands, Object... parameters)
cands offers exactly one possibility.aClass - the Class
in which to search a Method.
For error/exception messages only.methodName - the name of the desired Method.cands - an array of Methods
the return value of this method is choosen from.parameters - the list of parameters.Method; possibly null
if the specified Method
does not exist in the specified class.IllegalArgumentException - if the specified constructor or method is not unique.private static <T> Constructor<T> getConstructor(Class<T> aClass, Object... parameters)
aClas offers exactly one possibility
with the given parameters.aClass - the Class
in which to search a Constructor.
For error/exception messages only.parameters - the list of parameters.Constructor or null
if the specified constructor
does not exist in the specified class.IllegalArgumentException - if the specified constructor or method is not unique.public static Class<?> getInnerClass(Class<?> enclosingCls, String[] pathToInner)
enclosingCls
with the specified name innerClsName.
By inner classes we mean both static inner classes and member classes.
Also inherited classes are included.enclosingCls - a Class object which may also be an inner class,
static or not.pathToInner - a short path of a class enclosed by enclosingCls.
If the name of the enclosing class is enclosing
then the name of the inner class has the form
enclosing$shortName.
Here, "$" is as specified in INNER_SEPARATOR.
This remains also true for nested inner classes.
In this case, shortName itself has the form
cls1$...$clsN.
The the corresponding short path is
new String[] {cls1, ..., clsN}.
For paths with length 1
one may use getInnerClass(Class, String) instead.Class object represented by the parameters.IllegalArgumentException - if either of the parameters is null.IllegalArgumentException - if the specified class does not exist.getInnerClass(Class, String)public static Class<?> getInnerClass(Class<?> enclosingCls, String innerClsName)
enclosingCls
with the specified name innerClsName.
By inner classes we mean both static inner classes and member classes.
Also inherited classes are included.enclosingCls - a Class object which may also be an inner class,
static or not.innerClsName - a short name of a class enclosed by enclosingCls.
If the name of the enclosing class is enclosing
then the name of the inner class has the form
enclosing$shortName.
Here, "$" is as specified in INNER_SEPARATOR.
This remains also true for nested inner classes.
In this case, shortName itself has the form
cls1$...$clsN.
CAUTION
In this case apply method getInnerClass(Class, String[])
instead.Class object represented by the parameters.IllegalArgumentException - if either of the parameters is null or
if the specified class does not exist.getInnerClass(Class, String[])Copyright © 2012–2018 Simuline Organization (l2r). All rights reserved.