Package org.uncommons.util.reflection
Class ReflectionUtils
- java.lang.Object
-
- org.uncommons.util.reflection.ReflectionUtils
-
public final class ReflectionUtils extends Object
Helper methods to simplify code that uses reflection. These methods handle the checked exceptions and throw only unchecked exceptions. They are useful for dealing with reflection when we know that there is no chance of a checked exception. We can use this class and avoid all of the boiler-plate exception handling.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Constructor<T>
findKnownConstructor(Class<T> aClass, Class<?>... paramTypes)
Looks up a constructor that is explicitly identified.static Method
findKnownMethod(Class<?> aClass, String name, Class<?>... paramTypes)
Looks up a method that is explicitly identified.static <T> T
invokeUnchecked(Constructor<T> constructor, Object... arguments)
Invokes the specified constructor without throwing any checked exceptions.static <T> T
invokeUnchecked(Method method, Object target, Object... arguments)
Invokes the specified method without throwing any checked exceptions.
-
-
-
Method Detail
-
invokeUnchecked
public static <T> T invokeUnchecked(Method method, Object target, Object... arguments)
Invokes the specified method without throwing any checked exceptions. This is only valid for methods that are not declared to throw any checked exceptions. Any unchecked exceptions thrown by the specified method will be re-thrown (in their original form, not wrapped in an InvocationTargetException as would be the case for a normal reflective invocation).- Type Parameters:
T
- The return type of the method. The compiler can usually infer the correct type.- Parameters:
method
- The method to invoke. Both the method and its class must have been declared public and non-abstract, otherwise they will be inaccessible.target
- The object on which to invoke the method.arguments
- The method arguments.- Returns:
- The result of invoking the method, or null if the method is void.
-
invokeUnchecked
public static <T> T invokeUnchecked(Constructor<T> constructor, Object... arguments)
Invokes the specified constructor without throwing any checked exceptions. This is only valid for constructors that are not declared to throw any checked exceptions. Any unchecked exceptions thrown by the specified constructor will be re-thrown (in their original form, not wrapped in an InvocationTargetException as would be the case for a normal reflective invocation).- Type Parameters:
T
- The return type of the method. The compiler can usually infer the correct type.- Parameters:
constructor
- The constructor to invoke. Both the constructor and its class must have been declared public, and the class must not be abstract, otherwise they will be inaccessible.arguments
- The method arguments.- Returns:
- The object created by invoking the specified constructor with the specified arguments.
-
findKnownMethod
public static Method findKnownMethod(Class<?> aClass, String name, Class<?>... paramTypes)
Looks up a method that is explicitly identified. This method should only be used for methods that definitely exist. It does not throw the checked NoSuchMethodException. If the method does not exist, it will instead fail with an unchecked IllegalArgumentException.- Parameters:
aClass
- The class in which the method exists.name
- The name of the method.paramTypes
- The types of the method's parameters.- Returns:
- The identified method.
-
findKnownConstructor
public static <T> Constructor<T> findKnownConstructor(Class<T> aClass, Class<?>... paramTypes)
Looks up a constructor that is explicitly identified. This method should only be used for constructors that definitely exist. It does not throw the checked NoSuchMethodException. If the constructor does not exist, it will instead fail with an unchecked IllegalArgumentException.- Type Parameters:
T
- The type of object that the constructor creates.- Parameters:
aClass
- The class in which the constructor exists.paramTypes
- The types of the constructor's parameters.- Returns:
- The identified constructor.
-
-