Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mstr2 committed Oct 17, 2024
1 parent 7438963 commit 2b303ff
Show file tree
Hide file tree
Showing 16 changed files with 1,246 additions and 236 deletions.
41 changes: 41 additions & 0 deletions modules/javafx.base/src/test/java/test/util/ReflectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@
package test.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.function.Function;

public final class ReflectionUtils {

private ReflectionUtils() {}

/**
* Returns the value of a potentially private field of the specified object.
* The field can be declared on any of the object's inherited classes.
Expand Down Expand Up @@ -61,4 +65,41 @@ public static Object getFieldValue(Object object, String fieldName) {

throw new AssertionError("Field not found: " + fieldName);
}

/**
* Invokes the specified method on the object, and returns a value.
* The method can be declared on any of the object's inherited classes.
*
* @param object the object on which the method will be invoked
* @param methodName the method name
* @param args the arguments
* @return the return value
*/
public static Object invokeMethod(Object object, String methodName, Class<?>[] parameterTypes, Object... args) {
Function<Class<?>, Method> getMethod = cls -> {
try {
var method = cls.getDeclaredMethod(methodName, parameterTypes);
method.setAccessible(true);
return method;
} catch (NoSuchMethodException e) {
return null;
}
};

Class<?> cls = object.getClass();
while (cls != null) {
Method method = getMethod.apply(cls);
if (method != null) {
try {
return method.invoke(object, args);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new AssertionError(e);
}
}

cls = cls.getSuperclass();
}

throw new AssertionError("Method not found: " + methodName);
}
}
Loading

0 comments on commit 2b303ff

Please sign in to comment.