Advertisement
Guest User

Untitled

a guest
Jan 20th, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1.     class Foo {
  2.  
  3.         public Foo () {}
  4.        
  5.         public Foo (FooTest f) {}
  6.  
  7.         public void whichClassAmI () {
  8.             System.out.println(this.getClass());
  9.         }
  10.  
  11.         public void say () {
  12.             System.out.println("Foo says: nothing");
  13.         }
  14.  
  15.         public <T extends Foo> T getAnotherFoo () {
  16.            
  17.             Class<? extends Foo> c = this.getClass();
  18.             Class<?> enc = c.getEnclosingClass();
  19.             if (enc == null) {
  20.                 // for the f3 clone which has no enclosing class
  21.                 return (T)new Foo();
  22.             } else {
  23.                 // for the f4 clone of the anon subclass instance
  24.                 try {
  25.                     T r = (T)(c.getDeclaredConstructor(enc)
  26.                                 .newInstance(enc.newInstance()));
  27.                     return r;
  28.                 } catch (Exception e) {
  29.                     throw new RuntimeException(e);
  30.                 }
  31.             }
  32.         }
  33.  
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement