public class ReflectionUtility extends Object
Modifier and Type | Method and Description |
---|---|
static Object |
invoke(String symbolicName,
ExtendedInterpreter interpreter,
Method method,
Object object,
List<RightValue> args)
Invoke the
method on the object using the args . |
static void |
setField(Class<?> klass,
String fieldName,
Object value)
Set the named static field in the class
klass . |
static void |
setField(Object object,
String fieldName,
Object value)
Set the named field of the object be it either inherited by the object
class from its parent or declared in the class of the object.
|
public static void setField(Object object, String fieldName, Object value) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException
Generally the use of this method is a signal of bad practice. Possible uses are copying values from one bean to another, e.g.: configuration value to bean and using the configuration keys to name the field names. When you design such code bear in mind that accepting the name of the field from some external source usually poses a security risk, you have to trust the source from where you get the field names, since any of the fields can be set using this method, even private fields.
object
- the object in which the field has to be set. Note that if the
object is a Class<?>
then the method setField(Class, String, Object)
will be invoked.fieldName
- the name of the fieldvalue
- the new value of the fieldNoSuchFieldException
- if the field is not declared in the class of the object and
also there is no such field inherited from any parentSecurityException
- if the code is not allowed to execute reflection code
controlled by the SecurityManagerIllegalArgumentException
- if reflection throws this exception, usually the type of the
value
is not compatible with the named fieldIllegalAccessException
- if the field can not be set. This is not expected to be
thrown since the code sets the accessibility of the field.public static Object invoke(String symbolicName, ExtendedInterpreter interpreter, Method method, Object object, List<RightValue> args) throws BasicRuntimeException
method
on the object
using the args
.
If object
is null
then call the static method.
Before the Java method call the hook method
beforeCallJavaFunction
is called.
After the Java method call the hook methodafterCallJavaFunction
is called.
interpreter
- the interpretermethod
- the method to callobject
- the object on which the call is to be performed or
null
in case the method is staticargs
- the arguments to the method callBasicRuntimeException
public static void setField(Class<?> klass, String fieldName, Object value) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException
klass
. This method
performs the same function as setField(Object, String, Object)
except that it sets static fields.
For more information on the arguments and the exceptions see the
documentation of the method setField(Object, String, Object)
.
klass
- the class of which the static field to be set.fieldName
- value
- NoSuchFieldException
SecurityException
IllegalArgumentException
IllegalAccessException
Copyright © 2013 Verhas and Verhas Software Craftsmen. All Rights Reserved.