publicvoid foo() { System.out.println("I am A's foo"); }
// We previously used to invoke this method with a Y instance, due // to invoke-super underspecified behavior. publicvoid bar() { System.out.println("I am A's bar"); }
}
class B {
B() { System.out.println("new B"); }
publicvoid foo() { System.out.println("I am B's foo"); }
}
// // Two subclasses X and Y that call foo() on super. //
class X extends A { publicvoid foo() { super.foo(); }
}
class Y extends B { publicvoid foo() { super.foo(); }
}
// // Driver class. //
publicclass Main {
publicstaticvoid main(String[] args) throws Exception { // The normal stuff, X's super goes to A, Y's super goes to B. new X().foo(); new Y().foo();
// And now it gets interesting.
// In bytecode, we define a class Z that is a subclass of A, and we call // invoke-super on an instance of Y. Class<?> z = Class.forName("Z");
Method m = z.getMethod("foo"); try {
m.invoke(z.newInstance()); thrownew Error("Expected InvocationTargetException");
} catch (InvocationTargetException e) { if (!(e.getCause() instanceof NoSuchMethodError)) { thrownew Error("Expected NoSuchMethodError");
}
}
System.out.println("passed");
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-06-29)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.