Package org.hotswap.agent.util
Class ReflectionHelper
- java.lang.Object
-
- org.hotswap.agent.util.ReflectionHelper
-
public class ReflectionHelper extends Object
Convenience methods on java reflection API.
-
-
Constructor Summary
Constructors Constructor Description ReflectionHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Objectget(Object target, Class<?> clazz, String fieldName)Convenience wrapper to reflection field access API.static Objectget(Object target, String fieldName)Convenience wrapper to reflection field access API.static ObjectgetNoException(Object target, Class<?> clazz, String fieldName)Convenience wrapper to reflection field access API.static ObjectgetNoException(Object target, String className, ClassLoader cl, String fieldName)Convenience wrapper to reflection field access API.static Objectinvoke(Object target, Class<?> clazz, String methodName, Class<?>[] parameterTypes, Object... args)Convenience wrapper to reflection method invoke API.static Objectinvoke(Object target, String methodName)Convenience wrapper to reflection method invoke API.static ObjectinvokeConstructor(String className, ClassLoader cl, Class<?>[] parameterTypes, Object... args)static ObjectinvokeNoException(Object target, String className, ClassLoader cl, String methodName, Class<?>[] parameterTypes, Object... args)Convenience wrapper to reflection method invoke API.static voidset(Object target, Class<?> clazz, String fieldName, Object value)Convenience wrapper to reflection field access API.static voidset(Object target, String fieldName, Object value)
-
-
-
Method Detail
-
invoke
public static Object invoke(Object target, Class<?> clazz, String methodName, Class<?>[] parameterTypes, Object... args)
Convenience wrapper to reflection method invoke API. Invoke the method and hide checked exceptions.- Parameters:
target- object to invoke the method on (or null for static methods)clazz- class namemethodName- method nameparameterTypes- parameter types to resolve method nameargs- actual arguments- Returns:
- invocation result or null
- Throws:
IllegalArgumentException- if method not foundIllegalStateException- for InvocationTargetException (exception in invoked method)
-
invokeNoException
public static Object invokeNoException(Object target, String className, ClassLoader cl, String methodName, Class<?>[] parameterTypes, Object... args)
Convenience wrapper to reflection method invoke API. Invoke the method and swallow exceptions due to missing methods. Use this method if you have multiple framework support and the method may not exist in current version.- Parameters:
target- object to invoke the method on (or null for static methods)clazz- className namecl- Classloader to load the classmethodName- method nameparameterTypes- parameter types to resolve method nameargs- actual arguments- Returns:
- invocation result or null
- Throws:
IllegalStateException- for InvocationTargetException (exception in invoked method)
-
invoke
public static Object invoke(Object target, String methodName)
Convenience wrapper to reflection method invoke API. Invoke the method and hide checked exceptions.- Parameters:
target- object to invoke the method on (or null for static methods)methodName- method name- Returns:
- invocation result or null
- Throws:
IllegalArgumentException- if method not foundIllegalStateException- for InvocationTargetException (exception in invoked method)
-
invokeConstructor
public static Object invokeConstructor(String className, ClassLoader cl, Class<?>[] parameterTypes, Object... args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException
-
get
public static Object get(Object target, String fieldName)
Convenience wrapper to reflection field access API. Get field value and hide checked exceptions. Field class is set by- Parameters:
target- object to get field value (or null for static methods)fieldName- field name- Returns:
- field value
- Throws:
IllegalArgumentException- if field not found
-
get
public static Object get(Object target, Class<?> clazz, String fieldName)
Convenience wrapper to reflection field access API. Get field value and hide checked exceptions.- Parameters:
target- object to get field value (or null for static methods)clazz- class namefieldName- field name- Returns:
- field value
- Throws:
IllegalArgumentException- if field not found
-
getNoException
public static Object getNoException(Object target, Class<?> clazz, String fieldName)
Convenience wrapper to reflection field access API. Get field value and swallow exceptions. Use this method if you have multiple framework support and the field may not exist in current version.- Parameters:
target- object to get field value (or null for static methods)clazz- class namefieldName- field name- Returns:
- field value or null if an exception
-
getNoException
public static Object getNoException(Object target, String className, ClassLoader cl, String fieldName)
Convenience wrapper to reflection field access API. Get field value and swallow exceptions. Use this method if you have multiple framework support and the field may not exist in current version.- Parameters:
target- object to get field value (or null for static methods)className- class namecl- class loader to load the target classfieldName- field name- Returns:
- field value or null if an exception
-
set
public static void set(Object target, Class<?> clazz, String fieldName, Object value)
Convenience wrapper to reflection field access API. Set field value and hide checked exceptions.- Parameters:
target- object to get field value (or null for static methods)clazz- class namefieldName- field namevalue- field value- Throws:
IllegalArgumentException- if field not found
-
-