6
6
* Common utility functions for reflection. These are intended to be used to test APIs before they
7
7
* are added to the SDK. There should be not uses of this class checked in to the repository.
8
8
*/
9
- public class ReflectionUtil {
9
+ public final class ReflectionUtil {
10
10
/**
11
11
* Calls a method on an object with the given arguments. This can be used when the method is not
12
12
* in the SDK. If any arguments are {@code null} or primitive types you must use
@@ -22,15 +22,15 @@ public class ReflectionUtil {
22
22
* {@link #invoke(Object, String, Class[], Object[])}.
23
23
* @return The result of the invocation. {@code null} if {@code void}.
24
24
*/
25
- public static Object invoke (Object obj , String methodName , Object ... args )
25
+ public static < T > T invoke (Object obj , String methodName , Object ... args )
26
26
throws ReflectionIsTemporaryException {
27
27
return invoke (obj .getClass (), obj , methodName , args );
28
28
}
29
29
30
30
/**
31
31
* Same as {@link #invoke(Object, String, Object...)} but for static methods.
32
32
*/
33
- public static Object invoke (Class <?> clazz , String methodName , Object ... args )
33
+ public static < T > T invoke (Class <?> clazz , String methodName , Object ... args )
34
34
throws ReflectionIsTemporaryException {
35
35
return invoke (clazz , null , methodName , args );
36
36
}
@@ -49,21 +49,21 @@ public static Object invoke(Class<?> clazz, String methodName, Object... args)
49
49
* @param args The arguments to pass to the method.
50
50
* @return The result of the invocation. {@code null} if {@code void}.
51
51
*/
52
- public static Object invoke (Object obj , String methodName , Class <?>[] parameterTypes ,
52
+ public static < T > T invoke (Object obj , String methodName , Class <?>[] parameterTypes ,
53
53
Object ... args ) throws ReflectionIsTemporaryException {
54
54
return invoke (obj .getClass (), obj , methodName , parameterTypes , args );
55
55
}
56
56
57
57
/**
58
58
* Same as {@link #invoke(Object, String, Class[], Object...)} but for static methods.
59
59
*/
60
- public static Object invoke (Class <?> clazz , String methodName , Class <?>[] parameterTypes ,
60
+ public static < T > T invoke (Class <?> clazz , String methodName , Class <?>[] parameterTypes ,
61
61
Object ... args ) throws ReflectionIsTemporaryException {
62
62
return invoke (clazz , null , methodName , parameterTypes , args );
63
63
}
64
64
65
65
/** Resolve the parameter types and invoke the method. */
66
- private static Object invoke (Class <?> clazz , Object obj , String methodName , Object ... args )
66
+ private static < T > T invoke (Class <?> clazz , Object obj , String methodName , Object ... args )
67
67
throws ReflectionIsTemporaryException {
68
68
Class <?> parameterTypes [] = new Class <?>[args .length ];
69
69
for (int i = 0 ; i < args .length ; ++i ) {
@@ -73,11 +73,13 @@ private static Object invoke(Class<?> clazz, Object obj, String methodName, Obje
73
73
}
74
74
75
75
/** Resolve the method and invoke it. */
76
- private static Object invoke (Class <?> clazz , Object obj , String methodName ,
76
+ private static < T > T invoke (Class <?> clazz , Object obj , String methodName ,
77
77
Class <?>[] parameterTypes , Object ... args )
78
78
throws ReflectionIsTemporaryException {
79
79
try {
80
- return clazz .getMethod (methodName , parameterTypes ).invoke (obj , args );
80
+ @ SuppressWarnings ("unchecked" )
81
+ T result = (T ) clazz .getMethod (methodName , parameterTypes ).invoke (obj , args );
82
+ return result ;
81
83
} catch (SecurityException | NoSuchMethodException | IllegalArgumentException
82
84
| IllegalAccessException | InvocationTargetException e ) {
83
85
throw new ReflectionIsTemporaryException ("Failed to invoke method" , e );
@@ -136,4 +138,8 @@ public ReflectionIsTemporaryException(String message, Throwable cause) {
136
138
super (message , cause );
137
139
}
138
140
}
141
+
142
+ private ReflectionUtil () {
143
+ throw new UnsupportedOperationException ("provides only static methods" );
144
+ }
139
145
}
0 commit comments