This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 20th, 2012  |  syntax: Java  |  size: 1.00 KB  |  views: 38  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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.     }
clone this paste RAW Paste Data