Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement