0

There are a couple of cases where I would like to add/modify just one method of GWT's implementation of a JRE class (see Class.isInstance or System.arraycopy, for example).

As GWT is improved, other methods in the same classes might be updated, so I would rather not just take the current implementation of the entire class, modify it, and then stick it in a super-source directory, as I would then have to check for significant changes in these files every time a new version of GWT is released.

I would much prefer to just extend the already existing GWT implementation and only override the one method I would like to change. Is that possible somehow?

Community
  • 1
  • 1
Markus A.
  • 12,349
  • 8
  • 52
  • 116

1 Answers1

0

This might help:

Deferred Binding Using Replacement

The first type of deferred binding uses replacement. Replacement means overriding the implementation of one java class with another that is determined at compile time. For example, this technique is used to conditionalize the implementation of some widgets, such as the PopupPanel. The use of for the PopupPanel class is shown in the previous section describing the deferred binding rules. The actual replacement rules are specified in Popup.gwt.xml, as shown below:

http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsDeferred.html

Zied Hamdi
  • 2,400
  • 1
  • 25
  • 40
  • Does this allow me to replace only one method in a class, though? I think with this I can only replace the class as a whole, correct? I guess I could use it in cases where the class I would like to modify isn't marked as final (by extending it, overriding the required method, and then replacing the original with the extended version), but unfortunately both, `System` and `Class` are marked final... – Markus A. May 18 '14 at 22:08
  • You can still delegate your calls to the final class implementation and have your own implementation for the other methods. At google they naturally had the same issue – Zied Hamdi May 18 '14 at 22:14
  • If I have the GWT compiler replace all references to `System` with references to `MySystem` instead, is there a way in my `MySystem` class to access the original methods in `System`? I.e. can I exclude the `MySystem` class from the replacement? Otherwise, I won't be able to delegate to the original implementation any more, no? – Markus A. May 18 '14 at 22:18
  • 1
    You should access the emulated System class for the methods you don't want to override (from you MySystem class). You can have a regular access "by class name" to the emulated class. You have here a link that explains further the whole process http://concentricsky.com/blog/2011/mar/emulating-jre-classes-gwt – Zied Hamdi May 18 '14 at 23:50
  • This post might also interest you: http://stackoverflow.com/questions/6457047/gwt-java-emulation – Zied Hamdi May 18 '14 at 23:51
  • Thanks for the links. I think I understand how the whole process works in general. What I'm having trouble with (which I guess I could just try out) is the following: Let's say I write the `MySystem` class and it has a method `public static long currentTimeMillis() { return System.currentTimeMillis(); }` and I now tell GWT to replace the `System` class everywhere with `MySystem`, wouldn't it rewrite my method to `public static long currentTimeMillis() { return MySystem.currentTimeMillis(); }` resulting in an infinite loop? – Markus A. May 19 '14 at 02:30
  • That's done at compile time: a java class is generated with MySystem that points to System, then that class is generated, so each time you'll want System, it will go to MySystem that does the job as it wants. But if MySystem calls System, it doesn't go through the compiler again because it doesn't do it in clear Java code but in a template "Generator", so the sompiler didn't see you are referring to System until he generated the code. Don't worry about that if you don't get it clearly, the GWT already thought about it for you to enjoy :) – Zied Hamdi May 19 '14 at 07:03