Skip to content

Commit

Permalink
Add public ReflectionTestUtils API to get non-public methods (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-lippert authored Nov 12, 2022
1 parent 11313a5 commit 581b5df
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
38 changes: 32 additions & 6 deletions src/main/java/de/tum/in/test/api/util/ReflectionTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ private static Object valueForAttribute(Object object, String attributeName, boo
}

/**
* Helper method that retrieves a method with arguments of a given object by its
* name.
* Helper method that retrieves a public method with arguments of a given object
* by its name.
*
* @param object instance of the class that defines the method.
* @param methodName the name of the method.
Expand All @@ -310,7 +310,7 @@ public static Method getMethod(Object object, String methodName, Class<?>... par
}

/**
* Retrieve a method with arguments of a given class by its name.
* Retrieve a public method with arguments of a given class by its name.
*
* @param declaringClass The class that declares this method.
* @param methodName The name of this method.
Expand All @@ -321,6 +321,32 @@ public static Method getMethod(Class<?> declaringClass, String methodName, Class
return getMethodAccessible(declaringClass, methodName, false, parameterTypes);
}

/**
* Helper method that retrieves a non-public method with arguments of a given
* object by its name.
*
* @param object Instance of the class that defines the method.
* @param methodName The name of the method.
* @param parameterTypes The parameter types of this method.
* @return The wanted method.
*/
public static Method getNonPublicMethod(Object object, String methodName, Class<?>... parameterTypes) {
requireNonNull(object, "reflection_test_utils.method_null_target", methodName); //$NON-NLS-1$
return getNonPublicMethod(object.getClass(), methodName, parameterTypes);
}

/**
* Retrieve a non-public method with arguments of a given class by its name.
*
* @param declaringClass The class that declares this method.
* @param methodName The name of this method.
* @param parameterTypes The parameter types of this method.
* @return The wanted method.
*/
public static Method getNonPublicMethod(Class<?> declaringClass, String methodName, Class<?>... parameterTypes) {
return getMethodAccessible(declaringClass, methodName, true, parameterTypes);
}

/**
* Retrieve a method with arguments of a given class by its name.
*
Expand Down Expand Up @@ -463,8 +489,8 @@ private static Object invokeMethodAccessible(Object object, Method method, boole
* @param object The instance of the class that should invoke the method.
* @param method The method that has to get invoked.
* @param params Parameter instances of the method.
* @throws Throwable the exception that was caught and which will be rethrown
* @return The return value of the method.
* @throws Throwable the exception that was caught and which will be rethrown
*/
public static Object invokeMethodRethrowing(Object object, Method method, Object... params) throws Throwable {
return invokeMethodRethrowingAccessible(object, method, false, params);
Expand All @@ -481,8 +507,8 @@ public static Object invokeMethodRethrowing(Object object, Method method, Object
* @param object The instance of the class that should invoke the method.
* @param method The method that has to get invoked.
* @param params Parameter instances of the method.
* @throws Throwable the exception that was caught and which will be rethrown
* @return The return value of the method.
* @throws Throwable the exception that was caught and which will be rethrown
*/
public static Object invokeNonPublicMethodRethrowing(Object object, Method method, Object... params)
throws Throwable {
Expand All @@ -499,8 +525,8 @@ public static Object invokeNonPublicMethodRethrowing(Object object, Method metho
* should be forced. Might fail with an
* {@link IllegalAccessException} otherwise.
* @param params Parameter instances of the method.
* @throws Throwable the exception that was caught and which will be rethrown
* @return The return value of the method.
* @throws Throwable the exception that was caught and which will be rethrown
*/
private static Object invokeMethodRethrowingAccessible(Object object, Method method, boolean forceAccess,
Object[] params) throws Throwable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ void testGetMethod_success() {
assertThat(method).isInstanceOf(Method.class);
}

@Test
void testGetNonPublicMethod_success() {
var method = getNonPublicMethod(CLASS_INSTANCE, "superSecretMethod");
assertThat(method).isInstanceOf(Method.class);
}

@Test
void testInvokeMethod_invocationTarget() {
invokeMethod(CLASS_INSTANCE, "throwException");
Expand Down

0 comments on commit 581b5df

Please sign in to comment.