Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: Foo.java
- ===================================================================
- --- Foo.java (revision 57)
- +++ Foo.java (working copy)
- @@ -1,6 +1,10 @@
- + import java.lang.reflect.Constructor;
- +
- class Foo {
- public Foo () {}
- +
- + public Foo (FooTest f) {}
- public void whichClassAmI () {
- System.out.println(this.getClass());
- @@ -14,7 +18,8 @@
- try {
- Class<? extends Foo> c = this.getClass();
- - T r = (T)(c.getConstructor().newInstance());
- + Constructor cst = c.getConstructor(c.getEnclosingClass());
- + T r = (T)(cst.newInstance(c.getEnclosingClass().newInstance()));
- return r;
- } catch (Exception e) {
- throw new RuntimeException(e);
- Index: FooTest.java
- ===================================================================
- --- FooTest.java (revision 57)
- +++ FooTest.java (working copy)
- @@ -2,8 +2,15 @@
- public static String stuff = "";
- +
- +
- public static void main (String[] args) {
- + FooTest t = new FooTest();
- + t.go();
- + }
- + public void go() {
- +
- Foo f1 = new Foo();
- // instance of anon subclass
- @@ -20,8 +27,8 @@
- f1.say();
- f2.say();
- - Foo f3 = f1.getAnotherFoo();
- - f3.say();
- + //Foo f3 = f1.getAnotherFoo(); // <-- doesnt work anymore cuz enc-class is null
- + //f3.say();
- Foo f4 = f2.getAnotherFoo(); // <-- exception here
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement