public class PrivilegedAccessor extends Object
This class is used to access a method or field of an object no matter what the access modifier of the method or field. The syntax for accessing fields and methods is out of the ordinary because this class uses reflection to peel away protection.
Here is an example of using this to access a private member.
resolveName
is a private method of Class
.
Class c = Class.class; System.out.println(PrivilegedAccessor.invokeMethod(c, "resolveName","/ise/library/PrivilegedAccessor"));
Constructor and Description |
---|
PrivilegedAccessor() |
Modifier and Type | Method and Description |
---|---|
static Field |
getField(Class thisClass,
String fieldName)
Return the named field from the given class.
|
static Method |
getMethod(Class thisClass,
String methodName,
Class[] classTypes)
Return the named method with a method signature matching classTypes from the given class.
|
static Method |
getMethod(Object instance,
String methodName,
Class[] classTypes)
Gets the named method with a method signature matching classTypes from the given insance
and returns it as an object.
|
static Object |
getNewInstance(String classname,
Object[] args)
Creates a new instance of the named class initialized with the given arguments.
|
static Object |
getStaticValue(Class c,
String fieldName)
Gets the value of the named static field and returns it as an object.
|
static Object |
getValue(Object instance,
String fieldName)
Gets the value of the named field and returns it as an object.
|
static Object |
invokeMethod(Object instance,
String methodName,
Object arg)
Calls a method on the given object instance with the given argument.
|
static Object |
invokeMethod(Object instance,
String methodName,
Object[] args)
Calls a method on the given object instance with the given arguments.
|
static void |
setStaticValue(Class c,
String fieldName,
Object value)
Sets the value of the named static field.
|
static void |
setValue(Object instance,
String fieldName,
Object value) |
public static Object getValue(Object instance, String fieldName) throws IllegalAccessException, NoSuchFieldException
instance
- the object instancefieldName
- the name of the fieldIllegalAccessException
NoSuchFieldException
public static void setValue(Object instance, String fieldName, Object value) throws IllegalAccessException, NoSuchFieldException
public static Object getStaticValue(Class c, String fieldName) throws IllegalAccessException, NoSuchFieldException
c
- the class containing the static fieldfieldName
- the name of the fieldIllegalAccessException
NoSuchFieldException
public static void setStaticValue(Class c, String fieldName, Object value) throws IllegalAccessException, NoSuchFieldException
c
- the class containing the static fieldfieldName
- the name of the fieldIllegalAccessException
NoSuchFieldException
public static Object invokeMethod(Object instance, String methodName, Object arg) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
instance
- the object instancemethodName
- the name of the method to invokearg
- the argument to pass to the methodNoSuchMethodException
IllegalAccessException
InvocationTargetException
invokeMethod(Object, String, Object[])
public static Object invokeMethod(Object instance, String methodName, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
instance
- the object instancemethodName
- the name of the method to invokeargs
- an array of objects to pass as argumentsNoSuchMethodException
IllegalAccessException
InvocationTargetException
invokeMethod(Object, String, Object)
public static Method getMethod(Object instance, String methodName, Class[] classTypes) throws NoSuchMethodException
instance
- the object instancemethodName
- the method nameclassTypes
- the method argument typesNoSuchMethodException
public static Field getField(Class thisClass, String fieldName) throws NoSuchFieldException
thisClass
- The classfieldName
- The field nameNoSuchFieldException
public static Method getMethod(Class thisClass, String methodName, Class[] classTypes) throws NoSuchMethodException
thisClass
- the class containing the methodmethodName
- the name of the methodclassTypes
- the method argument typesNoSuchMethodException
public static Object getNewInstance(String classname, Object[] args) throws ClassNotFoundException, InstantiationException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
classname
- the name of the class to instantiate.args
- the arguments to pass as parameters to the constructor of the class.ClassNotFoundException
InstantiationException
NoSuchMethodException
IllegalAccessException
InvocationTargetException
Copyright © 2014–2022 European Space Agency (ESA). All rights reserved.