0

リフレクションを使用してメソッドのスーパー メソッドを呼び出したいのですが、メソッドが機能せず、それが可能かどうかわかりません。クラス Tのメソッドのスーパー メソッドを呼び出すメソッドを次に示しますequals(Object)。これは、ここでは targetClass とも呼ばれます。

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;
}

このコードは、super.equals() メソッドを保持するメソッドを正しく抽出します。しかし、それを呼び出すと、 の equals() メソッドが実行されtargetClassます。これを行う方法はありますか?

4

1 に答える 1