
Untitled
By: a guest on
Jan 20th, 2012 | syntax:
Java | size: 1.00 KB | hits: 30 | expires: Never
class Foo {
public Foo () {}
public Foo (FooTest f) {}
public void whichClassAmI () {
System.out.println(this.getClass());
}
public void say () {
System.out.println("Foo says: nothing");
}
public <T extends Foo> T getAnotherFoo () {
Class<? extends Foo> c = this.getClass();
Class<?> enc = c.getEnclosingClass();
if (enc == null) {
// for the f3 clone which has no enclosing class
return (T)new Foo();
} else {
// for the f4 clone of the anon subclass instance
try {
T r = (T)(c.getDeclaredConstructor(enc)
.newInstance(enc.newInstance()));
return r;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}