I am pretty certain I am right, inner classes initialize their pointer to the 
outer field before calling their own super. EG:

  | abstract class Base {
  |     Base() {
  |         System.out.println( getField() );
  |     }
  | 
  |     abstract int getField();
  | }
  | 
The constructor calls the abstract method getField and if this abstract method 
is implemented with an instance inner class then all is OK, e.g.:

  | public class InnerTest {
  |     int field = 1;
  | 
  |     Base base() {
  |         return new Base() {
  |             int getField() {
  |                 return field;
  |             }
  |         };
  |     }
  | 
  |     public static void main( final String[] notUsed ) {
  |         new InnerTest().base();
  |     }
  | }
  | 
This prints 1 as you would expect. But if you hand code the inner class then it 
won't work, e.g.:

  | class Derived extends Base {
  |     final ExternalTest outer;
  | 
  |     Derived( final ExternalTest outer ) {
  |         this.outer = outer;
  |     }
  | 
  |     int getField() {
  |         return outer.field;
  |     }
  | }
  | 

  | public class ExternalTest {
  |     int field = 1;
  | 
  |     Base base() {
  |         return new Derived( this );
  |     }
  | 
  |     public static void main( final String[] notUsed ) {
  |         new ExternalTest().base();
  |     }
  | }
  | 
This gives a NullPointerException since the field outer is not initialized when 
 constructor Derived calls its super constructor, Base. Also: you can see if 
you dissassemble the code for InternalTest$1 that it initializes its pointer to 
the outer class, this$0, before calling its super.

What the inner class does is equivalent to:

  |     Derived( final ExternalTest outer ) {
  |         this.outer = outer;
  |         super();
  |     }
  | }
  | 
Which is illegal in Java because super is always called first.

My question is how can I do this in Javassist which also requires the call to 
super to be the first line.


View the original post : 
http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3882299#3882299

Reply to the post : 
http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=3882299


-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
JBoss-user mailing list
JBoss-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to