I was playing around with anonymous subclasses and I found a problem that I can't get around.
EDIT: problem is solved thanks to thomas, the complete working code is on pastebin (Foo.java; FooTest.java)
2 Classes; Foo and FooTest... Code first:
class Foo {
public Foo () {}
public void whichClassAmI () {
System.out.println(this.getClass());
}
public void say () {
System.out.println("Foo says: nothing");
}
public <T extends Foo> T getAnotherFoo () {
try {
Class<? extends Foo> c = this.getClass();
T r = (T)(c.getConstructor().newInstance());
return r;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
_
class FooTest {
public static String stuff = "";
public static void main (String[] args) {
Foo f1 = new Foo();
// instance of anon subclass
Foo f2 = new Foo(){
public void say () {
System.out.println("Modded Foo says: " + stuff);
}
};
f1.whichClassAmI();
f2.whichClassAmI();
stuff = "stuff";
f1.say();
f2.say();
Foo f3 = f1.getAnotherFoo();
f3.say();
Foo f4 = f2.getAnotherFoo(); // <-- exception here
}
}
So this code compiles with an unsafe operation warning, runs and throws an exception; output is:
Note: Foo.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
class Foo
class FooTest$1
Foo says: nothing
Modded Foo says: stuff
Foo says: nothing
Exception in thread "main" java.lang.RuntimeException: java.lang.NoSuchMethodException: FooTest$1.<init>()
at Foo.getAnotherFoo(Foo.java:20)
at FooTest.main(FooTest.java:23)
Caused by: java.lang.NoSuchMethodException: FooTest$1.<init>()
at java.lang.Class.getConstructor0(Class.java:2723)
at java.lang.Class.getConstructor(Class.java:1674)
at Foo.getAnotherFoo(Foo.java:17)
... 1 more
what I don't understand is:
f2 class is FooTest$1 and this class seems to be not extending Foo
if (1) is true why does " Class c = [...] " can be set with FooTest$1
if (1) is false and (2) works correct, why doesn't it find the method?