Class Accessor<T>
- java.lang.Object
-
- eu.simuline.testhelpers.Accessor<T>
-
- Type Parameters:
T
- parameter representing the class to be accessed.
public final class Accessor<T> extends java.lang.Object
Provides access even to private fields, methods, constructors and inner classes, static or not, via reflection. This is necessary for white-box-tests. Note that class objects for instancesi
are given byi.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
andint[].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 becauseboolean.class
works as well asBoolean.TYPE
, except forjava.lang.Void.TYPE
which I never used so far), -
methods
getInnerClass(Class,String)
andgetInnerClass(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 classClass
is final, and has no (non-public) fields, there should be no reason, to access aClass
-object. In case of a very special application, casts likegetField((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 methodssetField
. Note that there is no way to change fields which are declaredfinal
. To be more precise, the pointer in a final fieldSimilarly for invoking methods: Except for very special cases,
invokeStatic(Class,String,Object...)
for static methods andinvoke(Object,String,Object...)
for instance methods will be sufficient, but for some special cases including overwriting and inner classesinvoke(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 exampleInteger.toString(int i)
may be invoked byinvoke(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 usecreate(Class,Class[],Object[])
instead.- Version:
- 1.0
- Author:
- Ernst Reissner
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
INNER_SEPARATOR
The separator between a class and its enclosing class for inner classes.private static java.lang.String
STR_DNE
private static java.lang.String
STR_IN_CLS
private static java.lang.String
STR_SPEC_NULL_CLS
private static java.lang.String
UNSPECIFIED_CLASS
String denoting an unspecified class.
-
Constructor Summary
Constructors Modifier Constructor Description private
Accessor()
Formally to create a newAccessor
instance but intended in contrary to prevent anAccessor
from being instantiated.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static <T> boolean
constructorMatches(java.lang.reflect.Constructor<T> cand, java.lang.Object... parameters)
Returns whether the given constructor matches the parameter list.static <T> T
create(java.lang.Class<T> aClass, java.lang.Class<?>[] paramCls, java.lang.Object... parameters)
Returns an object created by the specified constructor.static <T> T
create(java.lang.Class<T> aClass, java.lang.Object... parameters)
Returns an object created by the specified constructor.private static <T> T
create(java.lang.reflect.Constructor<T> toBeInvoked, java.lang.Object... parameters)
Invoke the specified constructor with the given arguments.private static <T> java.lang.reflect.Constructor<T>
getConstructor(java.lang.Class<T> aClass, java.lang.Object... parameters)
Returns the specified constructor ifaClas
offers exactly one possibility with the given parameters.static java.lang.Object
getField(java.lang.Class<?> aClass, java.lang.Object target, java.lang.String fieldName)
Returns the value of the specified static field or member field resp. its wrapper.static java.lang.Object
getField(java.lang.Class<?> aClass, java.lang.String fieldName)
Returns the value of the specified static field resp. its wrapper.static java.lang.Object
getField(java.lang.Object target, java.lang.String fieldName)
Returns the value of the specified member field resp. its wrapper.private static java.lang.reflect.Field
getFieldObj(java.lang.Class<?> aClass, java.lang.String fieldName, boolean shouldBeStatic)
Returns the specifiedField
object if possible.static java.lang.Class<?>
getInnerClass(java.lang.Class<?> enclosingCls, java.lang.String innerClsName)
Returns the inner class ofenclosingCls
with the specified nameinnerClsName
.static java.lang.Class<?>
getInnerClass(java.lang.Class<?> enclosingCls, java.lang.String[] pathToInner)
Returns the inner class ofenclosingCls
with the specified nameinnerClsName
.private static java.lang.reflect.Method
getMethod(java.lang.Class<?> aClass, java.lang.String methodName, java.lang.reflect.Method[] cands, java.lang.Object... parameters)
Returns the specified method ifcands
offers exactly one possibility.private static java.lang.Class<?>[]
getParamCls(java.lang.Object... parameters)
Returns the classes of the given parameters.(package private) static java.lang.reflect.Method
getToBeInvoked(java.lang.Class<?> aClass, java.lang.String methodName, java.lang.Class<?>... paramCls)
Returns the specified method if it exists; otherwisenull
.static java.lang.Object
invoke(java.lang.Class<?> aClass, java.lang.Object target, java.lang.String methodName, java.lang.Class<?>[] paramCls, java.lang.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 java.lang.Object
invoke(java.lang.Class<?> aClass, java.lang.Object target, java.lang.String methodName, java.lang.Object... parameters)
Invokes the specified method with the given parameters and returns the value (which may be void of course. )static java.lang.Object
invoke(java.lang.Object target, java.lang.String methodName, java.lang.Object... parameters)
For invoking member methods.private static java.lang.Object
invoke(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... parameters)
Invokes the specified method with the given parameters and returns the value (which may be void of course. )static java.lang.Object
invokeStatic(java.lang.Class<?> aClass, java.lang.String methodName, java.lang.Object... parameters)
For invoking static methods.private static boolean
methodMatches(java.lang.reflect.Method cand, java.lang.String methodName, java.lang.Object... parameters)
Returns whether the given method matches the name and the parameter list.private static boolean
paramsMatch(java.lang.Class<?>[] paramTypes, java.lang.Object... parameters)
Returns whether the given the parameter list matches the classes.private static java.lang.String
paramsToString(java.lang.Class<?>... paramCls)
Converts a list of classes into their string-representation.static void
setField(java.lang.Class<?> aClass, java.lang.Object target, java.lang.String fieldName, java.lang.Object value)
If the type of the specified field extendsObject
, invoking this method acts liketarget.field = value
; otherwise the argumentvalue
is unwrapped first.static void
setField(java.lang.Class<?> aClass, java.lang.String fieldName, java.lang.Object value)
If the type of the specified field extendsObject
, invoking this method acts likeaClass.fieldName = value
; otherwise the argumentvalue
is unwrapped first.static void
setField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value)
If the type of the specified field extendsObject
, invoking this method acts liketarget.fieldName = value
; otherwise the argumentvalue
is unwrapped first.
-
-
-
Field Detail
-
INNER_SEPARATOR
private static final java.lang.String INNER_SEPARATOR
The separator between a class and its enclosing class for inner classes.- See Also:
- Constant Field Values
-
UNSPECIFIED_CLASS
private static final java.lang.String UNSPECIFIED_CLASS
String denoting an unspecified class. This is needed for producing output only.
-
STR_DNE
private static final java.lang.String STR_DNE
- See Also:
- Constant Field Values
-
STR_IN_CLS
private static final java.lang.String STR_IN_CLS
- See Also:
- Constant Field Values
-
STR_SPEC_NULL_CLS
private static final java.lang.String STR_SPEC_NULL_CLS
- See Also:
- Constant Field Values
-
-
Method Detail
-
paramsToString
private static java.lang.String paramsToString(java.lang.Class<?>... paramCls)
Converts a list of classes into their string-representation.- Parameters:
paramCls
- an array ofClass
es. The entries in the array may well benull
.- Returns:
- a comma separated sequence of the classes given.
-
invoke
private static java.lang.Object invoke(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... parameters) throws java.lang.reflect.InvocationTargetException
Invokes the specified method with the given parameters and returns the value (which may be void of course. )- Parameters:
method
- aMethod
.target
- the target to which the specified method is to be applied. the target must benull
if and only if the method specified is static.parameters
- the list of parameters used when invokingmethod
. Note that parameters of elementary types have to be wrapped in an object (e.g. writenew Integer(8)
instead of just8
).- Returns:
- The result of invoking the specified method
with the given parameters.
If the method has no return value,
null
is returned. - Throws:
java.lang.IllegalArgumentException
--
if the specified method is static but
target != null
. -
if the specified method is a member method
but
target == null
.
-
if the specified method is static but
java.lang.IllegalStateException
- with message "Method should be accessible; still is not. " if the method is not accessible.java.lang.reflect.InvocationTargetException
- to wrap an exception thrown by the method invoked. Unwrap it usingThrowable.getCause()
.
-
getParamCls
private static java.lang.Class<?>[] getParamCls(java.lang.Object... parameters)
Returns the classes of the given parameters.- Parameters:
parameters
- a parameter list represented as an array ofObject
s.- Returns:
- the classes of the given parameters.
For a
null
-parameter, thenull
-class is returned.
-
getField
public static java.lang.Object getField(java.lang.Object target, java.lang.String fieldName) throws java.lang.NoSuchFieldException
Returns the value of the specified member field resp. its wrapper.- Parameters:
target
- the instance for which a field is to be accessed.fieldName
- the name of the field.- Returns:
- If the type of the specified field is
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 theint
-value5
. - Throws:
java.lang.NoSuchFieldException
- if the specified object does not contain a field with the given name, e.g. becausefieldName == null
.java.lang.IllegalArgumentException
- if the target isnull
or if the specified field is static.- See Also:
setField(Object, String, Object)
-
getField
public static java.lang.Object getField(java.lang.Class<?> aClass, java.lang.String fieldName) throws java.lang.NoSuchFieldException
Returns the value of the specified static field resp. its wrapper.- Parameters:
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.- Returns:
- If the value of the specified field is an 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 theint
-value5
. - Throws:
java.lang.NoSuchFieldException
- if the specified class does not contain a field with the given name, e.g. becausefieldName == null
.java.lang.IllegalArgumentException
- if the class-parameter isnull
or if the specified field is not static.- See Also:
setField(Class, String, Object)
-
getFieldObj
private static java.lang.reflect.Field getFieldObj(java.lang.Class<?> aClass, java.lang.String fieldName, boolean shouldBeStatic) throws java.lang.NoSuchFieldException
Returns the specifiedField
object if possible. This method is commonly used by the methods namedgetField
andsetField
.- Parameters:
aClass
- aClass
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.- Returns:
- the specified
Field
object (Should never returnnull
) made accessible if it exists. - Throws:
java.lang.IllegalArgumentException
- if the "null
-class" is specified.java.lang.NoSuchFieldException
- if the specified class and none of its superclasses containd a field with the given name, e.g. becausefieldName == null
.
-
getField
public static java.lang.Object getField(java.lang.Class<?> aClass, java.lang.Object target, java.lang.String fieldName) throws java.lang.NoSuchFieldException
Returns the value of the specified static field or member field resp. its wrapper. UsegetField(Class, String)
orgetField(Object, String)
if possible.- Parameters:
aClass
- Some class object. Eithertarget == null
ortarget instanceof aClass
.target
- the object for which a field is to be accessed. For static fields, this must benull
; whereas for memeber fields this has to be the corresponding instance.fieldName
- the name of the field.- Returns:
- If the value of the specified field is an 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 theint
-value5
. - Throws:
java.lang.NoSuchFieldException
- if the specified class does not contain a field with the given name, e.g. becausefieldName == null
.java.lang.IllegalArgumentException
--
if the
null
-class is specified -
if the target is
null
whereas the specified field is a member field. -
if the target is not
null
whereas the specified field is static.
-
if the
- See Also:
setField(Class, Object, String, Object)
-
setField
public static void setField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value) throws java.lang.NoSuchFieldException
If the type of the specified field extendsObject
, invoking this method acts liketarget.fieldName = value
; otherwise the argumentvalue
is unwrapped first. Note that there is no way to assign a value to a field which is declaredfinal
.- Parameters:
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 extendsObject
, the type ofvalue
must be a supertype; otherwise it must be the corresponding wrapper. E.g. the field declared byint intField;
is set bysetField(target, "intField", new Integer(5))
instead ofsetField(target, "intField", 5)
.- Throws:
java.lang.NoSuchFieldException
- if the specified class does not contain a field with the given name.java.lang.IllegalArgumentException
- if the target isnull
or if the specified field is static or if the specified field is declaredfinal
.- See Also:
getField(Object, String)
-
setField
public static void setField(java.lang.Class<?> aClass, java.lang.String fieldName, java.lang.Object value) throws java.lang.NoSuchFieldException
If the type of the specified field extendsObject
, invoking this method acts likeaClass.fieldName = value
; otherwise the argumentvalue
is unwrapped first. Note that there is no way to assign a value to a field which is declaredfinal
.- Parameters:
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 extendsObject
, the type ofvalue
must be a supertype; otherwise it must be the corresponding wrapper. E.g. the field declared bystatic int intField;
is set bysetField(aClass, "intField", new Integer(5))
instead ofsetField(aClass, "intField", 5)
.- Throws:
java.lang.NoSuchFieldException
- if the specified class does not contain a field with the given name.java.lang.IllegalArgumentException
- ifaClass == null
or if the specified field is not static or if the specified field is declaredfinal
.- See Also:
getField(Class, String)
-
setField
public static void setField(java.lang.Class<?> aClass, java.lang.Object target, java.lang.String fieldName, java.lang.Object value) throws java.lang.NoSuchFieldException
If the type of the specified field extendsObject
, invoking this method acts liketarget.field = value
; otherwise the argumentvalue
is unwrapped first. Note that there is no way to assign a value to a field which is declaredfinal
.- Parameters:
aClass
- Some class object. Eithertarget == null
ortarget 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 extendsObject
, the type ofvalue
must be the same; otherwise it must be the corresponding wrapper.- Throws:
java.lang.NoSuchFieldException
- if the specified class does not contain a field with the given name.java.lang.IllegalArgumentException
--
if the
null
-class is specified -
if the target is
null
whereas the specified field is a member field. -
if the target is not
null
whereas the specified field is static. -
if the specified field is declared
final
. -
if the specified field is primitive
but a
null
-value is tried to be assigned.
-
if the
- See Also:
getField(Class, Object, String)
-
invokeStatic
public static java.lang.Object invokeStatic(java.lang.Class<?> aClass, java.lang.String methodName, java.lang.Object... parameters) throws java.lang.reflect.InvocationTargetException
For invoking static methods.- Parameters:
aClass
- The class of the static method to be invoked. The method is looked up recursively in the superclasses ofaClass
. 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 invokingmethodName
. Note that parameters of elementary types have to be wrapped in an object (e.g. writenew Integer(8)
instead of just8
).- Returns:
-
If the return type of the specified method
is
Object
or a subclass, the return value is returned directly; -
if it is a primitive type, it is wrapped
in an object of the appropriate class.
E.g.
new Integer(5)
is returned instead of theint
-value5
. -
If the return type is void,
i.e.
java.lang.Void.TYPE
,null
is returned.
-
If the return type of the specified method
is
- Throws:
java.lang.IllegalArgumentException
--
if
aClass == null
. - if the specified method does not exist or is not unique.
-
if
java.lang.reflect.InvocationTargetException
- to wrap an exception thrown by the method invoked. Unwrap it usingThrowable.getCause()
.- See Also:
invoke(Class, Object, String, Object...)
-
invoke
public static java.lang.Object invoke(java.lang.Object target, java.lang.String methodName, java.lang.Object... parameters) throws java.lang.reflect.InvocationTargetException
For invoking member methods.- Parameters:
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 invokingmethodName
. Note that parameters of elementary types have to be wrapped in an object (e.g. writenew Integer(8)
instead of just8
).- Returns:
-
If the return type of the specified method
is
Object
or a subclass, the return value is returned directly; -
if it is a primitive type, it is wrapped
in an object of the appropriate class.
E.g.
new Integer(5)
is returned instead of theint
-value5
. -
If the return type is void,
i.e.
java.lang.Void.TYPE
,null
is returned.
-
If the return type of the specified method
is
- Throws:
java.lang.IllegalArgumentException
--
if
target == null
. - if the specified method does not exist or is not unique.
-
if
java.lang.reflect.InvocationTargetException
- to wrap an exception thrown by the method invoked. Unwrap it usingThrowable.getCause()
.- See Also:
invoke(Class, Object, String, Object...)
-
invoke
public static java.lang.Object invoke(java.lang.Class<?> aClass, java.lang.Object target, java.lang.String methodName, java.lang.Object... parameters) throws java.lang.reflect.InvocationTargetException
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. This is the case if for example methods
exa(int)
andexa(Integer)
are present: both are invoked withinvoke(cls, target, "exa", new Integer(0))}
. In this case useinvoke(Class, Object, String, Class[], Object[])
instead.- Parameters:
aClass
- The class of the method to be invoked. The method is looked up recursively in the superclasses ofaClass
. 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 benull
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 invokingmethodName
. Note that parameters of elementary types have to be wrapped in an object (e.g. writenew Integer(8)
instead of just8
).- Returns:
-
If the return type of the specified method
is
Object
or a subclass, the return value is returned directly; -
if it is a primitive type, it is wrapped
in an object of the appropriate class.
E.g.
new Integer(5)
is returned instead of theint
-value5
. -
If the return type is void,
i.e.
java.lang.Void.TYPE
,null
is returned.
-
If the return type of the specified method
is
- Throws:
java.lang.reflect.InvocationTargetException
- to wrap an exception thrown by the method invoked. Unwrap it usingThrowable.getCause()
.java.lang.IllegalArgumentException
--
if
aClass == null
. -
if the specified method is static but
target != null
. -
if the specified method is a member method
but
target == null
. - if the specified method does not exist or is not unique.
-
if
-
invoke
public static java.lang.Object invoke(java.lang.Class<?> aClass, java.lang.Object target, java.lang.String methodName, java.lang.Class<?>[] paramCls, java.lang.Object[] parameters) throws java.lang.reflect.InvocationTargetException
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.- Parameters:
aClass
- The class of the method to be invoked. The method is looked up recursively in the superclasses ofaClass
. 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 benull
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[])
andgetInnerClass(Class, String)
may be used. If an objecti
of a desired class is present and if the class of isi
not primitive,i.getClass()
returns the desired class object as well.parameters
- the list of parameters used when invokingmethodName
. Note that parameters of elementary types have to be wrapped in an object (e.g. writenew Integer(8)
instead of just8
).- Returns:
-
If the return type of the specified method
is
Object
or a subclass, the return value is returned directly; -
if it is a primitive type, it is wrapped
in an object of the appropriate class.
E.g.
new Integer(5)
is returned instead of theint
-value5
. -
If the return type is void,
i.e.
java.lang.Void.TYPE
,null
is returned.
-
If the return type of the specified method
is
- Throws:
java.lang.IllegalArgumentException
- if the specified method does not exist.java.lang.reflect.InvocationTargetException
- to wrap an exception thrown by the method invoked. Unwrap it usingThrowable.getCause()
.- See Also:
invoke(Class, Object, String, Class[], Object[])
-
getToBeInvoked
static java.lang.reflect.Method getToBeInvoked(java.lang.Class<?> aClass, java.lang.String methodName, java.lang.Class<?>... paramCls)
Returns the specified method if it exists; otherwisenull
. Note that it is searched in the superclasses as well.- Parameters:
aClass
- theClass
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.- Returns:
- the method with the specified name and parameter list.
The search is started in
candClass
and descends recursively until a method is found. If no method is found,null
is returned.
-
create
public static <T> T create(java.lang.Class<T> aClass, java.lang.Object... parameters) throws java.lang.InstantiationException, java.lang.reflect.InvocationTargetException
Returns an object created by the specified constructor. Note that this method is not applicable to interfaces, primitive types, array classes, or void. In the context of this package, these restrictions are not severe.- Parameters:
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. writenew Integer(8)
instead of just8
).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.
- Returns:
- an
Object
created by the specified constructor. - Throws:
java.lang.InstantiationException
- if the instantiation with the specified constructor failed.java.lang.IllegalArgumentException
- if the specified constructor does not exist or is not unique.java.lang.reflect.InvocationTargetException
- to wrap an exception thrown by the constructor invoked. Unwrap it usingThrowable.getCause()
.
-
create
public static <T> T create(java.lang.Class<T> aClass, java.lang.Class<?>[] paramCls, java.lang.Object... parameters) throws java.lang.NoSuchMethodException, java.lang.InstantiationException, java.lang.reflect.InvocationTargetException
Returns an object created by the specified constructor. Note that this method is not applicable to interfaces, primitive types, array classes, or void. In the context of this package, these restrictions are not severe.- Parameters:
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[])
andgetInnerClass(Class, String)
may be used. If an objecti
of a desired class is present and if the class of isi
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. writenew Integer(8)
instead of just8
).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.
- Returns:
- an
Object
created by the specified constructor. - Throws:
java.lang.InstantiationException
- if the instantiation with the specified constructor failed.java.lang.NoSuchMethodException
- if the specified constructor does not exist.java.lang.reflect.InvocationTargetException
- to wrap an exception thrown by the constructor invoked. Unwrap it usingThrowable.getCause()
.
-
create
private static <T> T create(java.lang.reflect.Constructor<T> toBeInvoked, java.lang.Object... parameters) throws java.lang.InstantiationException, java.lang.reflect.InvocationTargetException
Invoke the specified constructor with the given arguments.- Parameters:
toBeInvoked
- someConstructor
.parameters
- parameters fitting the givenConstructor
.- Throws:
java.lang.InstantiationException
- if the instantiation with the specified constructor failed.java.lang.reflect.InvocationTargetException
- to wrap an exception thrown by the constructor invoked. Unwrap it usingThrowable.getCause()
.
-
methodMatches
private static boolean methodMatches(java.lang.reflect.Method cand, java.lang.String methodName, java.lang.Object... parameters)
Returns whether the given method matches the name and the parameter list.- Parameters:
cand
- either aConstructor
or aMethod
.methodName
- the required name of aMethod
. Ifcand
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.- Returns:
true
if and only if all of the following conditions are satisfied:-
cand
is either a non-abstract method namedmethodName
. -
the parameter list of
cand
matchesparameters
as specified forparamsMatch(java.lang.Class<?>[], java.lang.Object...)
.
-
- Throws:
java.lang.IllegalStateException
- ifcand
is neither a method nor a constructor.
-
constructorMatches
private static <T> boolean constructorMatches(java.lang.reflect.Constructor<T> cand, java.lang.Object... parameters)
Returns whether the given constructor matches the parameter list.- Parameters:
cand
- either aConstructor
or aMethod
.parameters
- the list of parameters.- Returns:
- a
true
if and only if the parameter list ofcand
matchesparameters
as specified forparamsMatch(java.lang.Class<?>[], java.lang.Object...)
. - Throws:
java.lang.IllegalStateException
- ifcand
is neither a method nor a constructor.
-
paramsMatch
private static boolean paramsMatch(java.lang.Class<?>[] paramTypes, java.lang.Object... parameters)
Returns whether the given the parameter list matches the classes.- Parameters:
paramTypes
- the list of classes of parameters.parameters
- the list of parameters.- Returns:
- a
true
if and only if the parameter list ofcand
matchesparameters
up to equivalence of primitive types and their wrappers. This includes that the two arrays have the same length. - Throws:
java.lang.IllegalStateException
- ifcand
is neither a method nor a constructor.
-
getMethod
private static java.lang.reflect.Method getMethod(java.lang.Class<?> aClass, java.lang.String methodName, java.lang.reflect.Method[] cands, java.lang.Object... parameters)
Returns the specified method ifcands
offers exactly one possibility.- Parameters:
aClass
- theClass
in which to search aMethod
. For error/exception messages only.methodName
- the name of the desiredMethod
.cands
- an array ofMethod
s the return value of this method is choosen from.parameters
- the list of parameters.- Returns:
- a
Method
; possiblynull
if the specifiedMethod
does not exist in the specified class. - Throws:
java.lang.IllegalArgumentException
- if the specified constructor or method is not unique.
-
getConstructor
private static <T> java.lang.reflect.Constructor<T> getConstructor(java.lang.Class<T> aClass, java.lang.Object... parameters)
Returns the specified constructor ifaClas
offers exactly one possibility with the given parameters.- Parameters:
aClass
- theClass
in which to search aConstructor
. For error/exception messages only.parameters
- the list of parameters.- Returns:
- a
Constructor
ornull
if the specified constructor does not exist in the specified class. - Throws:
java.lang.IllegalArgumentException
- if the specified constructor or method is not unique.
-
getInnerClass
public static java.lang.Class<?> getInnerClass(java.lang.Class<?> enclosingCls, java.lang.String[] pathToInner)
Returns the inner class ofenclosingCls
with the specified nameinnerClsName
. By inner classes we mean both static inner classes and member classes. Also inherited classes are included.- Parameters:
enclosingCls
- aClass
object which may also be an inner class, static or not.pathToInner
- a short path of a class enclosed byenclosingCls
. 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 inINNER_SEPARATOR
. This remains also true for nested inner classes. In this case, shortName itself has the form cls1$...$clsN. The the corresponding short path isnew String[] {cls1, ..., clsN}
. For paths with length1
one may usegetInnerClass(Class, String)
instead.- Returns:
- the
Class
object represented by the parameters. - Throws:
java.lang.IllegalArgumentException
- if either of the parameters isnull
.java.lang.IllegalArgumentException
- if the specified class does not exist.- See Also:
getInnerClass(Class, String)
-
getInnerClass
public static java.lang.Class<?> getInnerClass(java.lang.Class<?> enclosingCls, java.lang.String innerClsName)
Returns the inner class ofenclosingCls
with the specified nameinnerClsName
. By inner classes we mean both static inner classes and member classes. Also inherited classes are included.- Parameters:
enclosingCls
- aClass
object which may also be an inner class, static or not.innerClsName
- a short name of a class enclosed byenclosingCls
. 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 inINNER_SEPARATOR
. This remains also true for nested inner classes. In this case, shortName itself has the form cls1$...$clsN. CAUTION In this case apply methodgetInnerClass(Class, String[])
instead.- Returns:
- the
Class
object represented by the parameters. - Throws:
java.lang.IllegalArgumentException
- if either of the parameters isnull
or if the specified class does not exist.- See Also:
getInnerClass(Class, String[])
-
-