Package org.esa.snap.core.util
Class PrivilegedAccessor
- java.lang.Object
-
- org.esa.snap.core.util.PrivilegedAccessor
-
public class PrivilegedAccessor extends Object
a.k.a. The "ObjectMolester"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 ofClass
.Class c = Class.class; System.out.println(PrivilegedAccessor.invokeMethod(c, "resolveName","/ise/library/PrivilegedAccessor"));
-
-
Constructor Summary
Constructors Constructor Description PrivilegedAccessor()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method 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)
-
-
-
Method Detail
-
getValue
public static Object getValue(Object instance, String fieldName) throws IllegalAccessException, NoSuchFieldException
Gets the value of the named field and returns it as an object.- Parameters:
instance
- the object instancefieldName
- the name of the field- Returns:
- an object representing the value of the field
- Throws:
IllegalAccessException
NoSuchFieldException
-
setValue
public static void setValue(Object instance, String fieldName, Object value) throws IllegalAccessException, NoSuchFieldException
-
getStaticValue
public static Object getStaticValue(Class c, String fieldName) throws IllegalAccessException, NoSuchFieldException
Gets the value of the named static field and returns it as an object.- Parameters:
c
- the class containing the static fieldfieldName
- the name of the field- Returns:
- an object representing the value of the field
- Throws:
IllegalAccessException
NoSuchFieldException
-
setStaticValue
public static void setStaticValue(Class c, String fieldName, Object value) throws IllegalAccessException, NoSuchFieldException
Sets the value of the named static field.- Parameters:
c
- the class containing the static fieldfieldName
- the name of the field- Throws:
IllegalAccessException
NoSuchFieldException
-
invokeMethod
public static Object invokeMethod(Object instance, String methodName, Object arg) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
Calls a method on the given object instance with the given argument.- Parameters:
instance
- the object instancemethodName
- the name of the method to invokearg
- the argument to pass to the method- Throws:
NoSuchMethodException
IllegalAccessException
InvocationTargetException
- See Also:
invokeMethod(Object, String, Object[])
-
invokeMethod
public static Object invokeMethod(Object instance, String methodName, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
Calls a method on the given object instance with the given arguments.- Parameters:
instance
- the object instancemethodName
- the name of the method to invokeargs
- an array of objects to pass as arguments- Throws:
NoSuchMethodException
IllegalAccessException
InvocationTargetException
- See Also:
invokeMethod(Object, String, Object)
-
getMethod
public static Method getMethod(Object instance, String methodName, Class[] classTypes) throws NoSuchMethodException
Gets the named method with a method signature matching classTypes from the given insance and returns it as an object.- Parameters:
instance
- the object instancemethodName
- the method nameclassTypes
- the method argument types- Throws:
NoSuchMethodException
-
getField
public static Field getField(Class thisClass, String fieldName) throws NoSuchFieldException
Return the named field from the given class.- Parameters:
thisClass
- The classfieldName
- The field name- Throws:
NoSuchFieldException
-
getMethod
public static Method getMethod(Class thisClass, String methodName, Class[] classTypes) throws NoSuchMethodException
Return the named method with a method signature matching classTypes from the given class.- Parameters:
thisClass
- the class containing the methodmethodName
- the name of the methodclassTypes
- the method argument types- Throws:
NoSuchMethodException
-
getNewInstance
public static Object getNewInstance(String classname, Object[] args) throws ClassNotFoundException, InstantiationException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
Creates a new instance of the named class initialized with the given arguments.- Parameters:
classname
- the name of the class to instantiate.args
- the arguments to pass as parameters to the constructor of the class.- Returns:
- the instantiated object
- Throws:
ClassNotFoundException
InstantiationException
NoSuchMethodException
IllegalAccessException
InvocationTargetException
-
-