Package org.esa.snap.core.util
Class PrivilegedAccessor
java.lang.Object
org.esa.snap.core.util.PrivilegedAccessor
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 of Class.
Class c = Class.class; System.out.println(PrivilegedAccessor.invokeMethod(c, "resolveName","/ise/library/PrivilegedAccessor"));
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic FieldReturn the named field from the given class.static MethodReturn the named method with a method signature matching classTypes from the given class.static MethodGets the named method with a method signature matching classTypes from the given insance and returns it as an object.static ObjectgetNewInstance(String classname, Object[] args) Creates a new instance of the named class initialized with the given arguments.static ObjectgetStaticValue(Class c, String fieldName) Gets the value of the named static field and returns it as an object.static ObjectGets the value of the named field and returns it as an object.static ObjectinvokeMethod(Object instance, String methodName, Object arg) Calls a method on the given object instance with the given argument.static ObjectinvokeMethod(Object instance, String methodName, Object[] args) Calls a method on the given object instance with the given arguments.static voidsetStaticValue(Class c, String fieldName, Object value) Sets the value of the named static field.static void
-
Constructor Details
-
PrivilegedAccessor
public PrivilegedAccessor()
-
-
Method Details
-
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:
IllegalAccessExceptionNoSuchFieldException
-
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:
IllegalAccessExceptionNoSuchFieldException
-
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:
IllegalAccessExceptionNoSuchFieldException
-
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:
NoSuchMethodExceptionIllegalAccessExceptionInvocationTargetException- See Also:
-
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:
NoSuchMethodExceptionIllegalAccessExceptionInvocationTargetException- See Also:
-
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
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:
ClassNotFoundExceptionInstantiationExceptionNoSuchMethodExceptionIllegalAccessExceptionInvocationTargetException
-