I want to call a method's super method using reflection, but my method doesn't work, and I'm not sure it's possible. Here's my method to call the super method for the equals(Object)
method of class T, which is also called the targetClass here:
private ToBooleanThrowingBiFunction<T> getSuperEquals() {
Class<T> targetClass = getTargetClass();
Class<?> superClass = targetClass.getSuperclass();
while ((superClass != Object.class) && (superClass != null)) {
try {
Method method = superClass.getDeclaredMethod("equals", Object.class);
return (thisOne, thatOne) -> {
try {
return (Boolean) method.invoke(thisOne, thatOne);
} catch (InvocationTargetException e) {
throw new IllegalStateException(e.getCause());
}
};
} catch (NoSuchMethodException e) {
superClass = superClass.getSuperclass();
}
}
throw new IllegalArgumentException(String.format("E10: No superclass of %s has an equals() method.", targetClass));
}
@FunctionalInterface
private interface ToBooleanThrowingBiFunction<T> {
boolean eval(T thisOne, T thatOne) throws IllegalAccessException;
}
This code correctly extracts a Method that holds the super.equals() method. But when I call it, it executes the equals() method of the targetClass
. Is there a way to do this?
Aucun commentaire:
Enregistrer un commentaire