Explicit base class method invocation

10 views
Skip to first unread message

Nick Hebner

unread,
Mar 9, 2010, 11:12:17 AM3/9/10
to GWT-Ent Developer Forum
Hello,

First, thanks for the excellent library!

I am using it for reflection and have had much success so far. However
I was wondering if there is any way to call an overloaded base class
method, skipping the sub class method. For example:

class A {
void method() { GWT.log("method A"); }
}

class B extends A {
void method() { GWT.log("method B"); }
}

B bInstance = new B();

ClassType aType = TypeOracle.Instance.getClassType(A.class);

aType.invoke(bInstance, "method", null);

Since the method is invoked on the type A, I would like the A method()
to be called, printing out "method A". However, currently it calls
method B through dynamic dispatching, and "method B" is printed
instead. This sort of sub class override skipping works with real java
reflection. Is there a way to explicitly call the base class method
with gwtent?

Thanks,

Nick

James Luo

unread,
Mar 9, 2010, 6:19:56 PM3/9/10
to gwt...@googlegroups.com
Hi, Nick

  Thank you very much to let me know this. Sorry I didn't thought about this before.

  There is no Reflection support in GWT, all I can do is write normal java code to simulate what Java Reflection did.

  I think there is no way to call a super class method and ignore the overrided method from an instance.

  Actually the call:
   ClassType aType = TypeOracle.Instance.getClassType(A.class);
   aType.invoke(bInstance, "method", null);

  In gwtent, it's convert to a call like this: 
   bInstance.method();

  Only normal java code, that's why B.method() been called :(

  Do you know any tricky on this. So that I can call a super class method from instance? Thanks.

class A {
 void method() { GWT.log("method A"); }
}

class B extends A {
 void method() { GWT.log("method B"); }
}

B bInstance = new B();

//Any tricky here. can call A.method() from bInstance?

Regards
James


--
You received this message because you are subscribed to the Google Groups "GWT-Ent Developer Forum" group.
To post to this group, send email to gwt...@googlegroups.com.
To unsubscribe from this group, send email to gwt-ent+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/gwt-ent?hl=en.




--
Regards
James Luo

Nick Hebner

unread,
Mar 9, 2010, 6:57:23 PM3/9/10
to gwt...@googlegroups.com
Thanks for the response.

I spent a couple of hours today learning how the generator of this works, and I now understand how the reflection code is generated. Since overridden base class methods cannot be accessed from outside of the overridding class, it would be up to that class to expose a method that forwards the call to the base class. For example:


class A {
 void method() { GWT.log("method A"); }
}

class B extends A {
 void method() { GWT.log("method B"); }
 void aMethod() { super.method(); }

}

B bInstance = new B();

bInstance.aMethod(); // calls A.method() indirectly

I don't know if it would be possible, but if the generator could insert an aMethod() into class B at compile time, and then call aMethod() when aType.invoke(bInstance, "method", null); is called, this would simulate the behavior I am looking for. Do you think this might work?

Thanks,

Nick

James Luo

unread,
Mar 9, 2010, 7:11:01 PM3/9/10
to gwt...@googlegroups.com
Hi, Nick

  Thanks for that. There is no code analyze in gwtent, so it's always generate all method calls.
  It's possible to generate a new method to only call super methods, but this will generate so many code and makes JavaScript very very big :)

James

Nick Hebner

unread,
Mar 9, 2010, 7:53:30 PM3/9/10
to gwt...@googlegroups.com
Yes, generating pass-through methods for all overridden methods would bloat the javascript. You could require an annotation to flag certain methods for pass-through generation, so that only the methods that you want get generated. Any way, this feature is probably too difficult and obscure to implement, so I will just figure something else out.

Thanks for the help!

Nick

James Luo

unread,
Mar 9, 2010, 8:21:58 PM3/9/10
to gwt...@googlegroups.com
No worries,  the annotation is a good idea, maybe I can support it if more people want it :)

Regards
James
Reply all
Reply to author
Forward
0 new messages