GWT <replace-with> not working
Asked Answered
J

0

1

I am trying to use a custom implementation of LocaleInfoImpl in GWT. For this I created a class CustomLocaleInfo and in my .gwt.xml file like this:

  <replace-with class="my.package.util.CustomLocaleInfo">
      <when-type-is class="com.google.gwt.i18n.client.impl.LocaleInfoImpl" />
  </replace-with>

My class:

public class CustomLocaleInfo extends LocaleInfoImpl {

     @Override
     public NumberConstants getNumberConstants() {

            Notifier.printDebug(">>> getting number constants");

            final NumberConstants nc = super.getNumberConstants();
            return new NumberConstants() {
                @Override
                public String notANumber() {
                    return nc.notANumber();
                }

                @Override
                public String currencyPattern() {
                    return nc.currencyPattern();
                }

                @Override
                public String decimalPattern() {
                    return nc.decimalPattern();
                }

                @Override
                public String decimalSeparator() {
                    return ".";
                }

                @Override
                public String defCurrencyCode() {
                    return nc.defCurrencyCode();
                }

                @Override
                public String exponentialSymbol() {
                    return nc.exponentialSymbol();
                }

                @Override
                public String globalCurrencyPattern() {
                    return nc.globalCurrencyPattern();
                }

                @Override
                public String groupingSeparator() {
                    return ",";//or any custom separator you desire
                }

                @Override
                public String infinity() {
                    return nc.infinity();
                }

                @Override
                public String minusSign() {
                    return nc.minusSign();
                }

                @Override
                public String monetaryGroupingSeparator() {
                    return nc.monetaryGroupingSeparator();
                }

                @Override
                public String monetarySeparator() {
                    return nc.monetarySeparator();
                }

                @Override
                public String percent() {
                    return nc.percent();
                }

                @Override
                public String percentPattern() {
                    return nc.percentPattern();
                }

                @Override
                public String perMill() {
                    return nc.perMill();
                }

                @Override
                public String plusSign() {
                    return nc.plusSign();
                }

                @Override
                public String scientificPattern() {
                    return nc.scientificPattern();
                }

                @Override
                public String simpleCurrencyPattern() {
                    return nc.simpleCurrencyPattern();
                }

                @Override
                public String zeroDigit() {
                    return nc.zeroDigit();
                }
            };
        }

}

Even if I use random names, the compiler does not throw an error.

I am using GWT 2.8.1. Please advise!

EDIT:

My main app .gwt.xml file:

  <inherits name="com.sksamuel.gwt.GwtWebsockets" />
  <inherits name='com.google.gwt.appengine.channel.Channel'/>
  <inherits name='org.adamtacy.GWTEffects'/> 
  <inherits name='com.gwtsandbox.colorpicker.ColorPicker'/>
  <inherits name="com.googlecode.gwt.charts.Charts"/> 
  <inherits name='com.googlecode.gchart.GChart'/>
  <inherits name='com.allen_sauer.gwt.dnd.gwt-dnd'/>
  <inherits name='org.vaadin.gwtgraphics.GWTGraphics'/>
  <inherits name="com.google.gwt.json.JSON" />
  <inherits name="com.google.web.bindery.autobean.AutoBean"/>
  <inherits name="org.moxieapps.gwt.uploader.Uploader"/>

  <inherits name='...MyBaseModule'/>



  <set-configuration-property name='xsiframe.failIfScriptTag' value='FALSE'/>

  <stylesheet src="res/spi.css?v4"/>
  <stylesheet src="app.css?v=180321"/>

  <entry-point class='.....ApplicationEntryPoint'/>

  <source path='client'/>
  <source path='shared'/>

   <replace-with class="biz.ebas.platform.generic.client.visual.advanced.util.CustomLocaleInfo">
      <when-type-is class="com.google.gwt.i18n.client.impl.LocaleInfoImpl" />
  </replace-with>

  <extend-property name="locale" values="ro,en,ru"/>

  <collapse-property name="locale" values="*"/>
  <set-property-fallback name="locale" value="en"/> 


  <set-property name="user.agent" value="safari,gecko1_8,ie9,ie10" />  

        <!-- enable the SuperDevMode book marklets  -->
<add-linker name="xsiframe"/>

The MyBaseModule lookes like this:

<inherits name="com.google.gwt.user.User" />
<inherits name="com.google.gwt.json.JSON" /> 
<inherits name="com.google.gwt.resources.Resources" />

<stylesheet src="font-awesome-4.7.0/css/font-awesome.min.css"/>
<stylesheet src="res/lib.css?v6"/>
<stylesheet src="res/libcomp.css?v6"/>

<source path="client"/>
<source path="shared"/>
<super-source path="jre"/>
Justification answered 13/8, 2018 at 9:30 Comment(13)
Can you show the context where you put that xml in your .gwt.xml? Both (if necessary) the file it is in, and how that .gwt.xml is included in the .gwt.xml that you are actually compiling? (With regard to "random names", if you try to match something that doesnt exist, it will simply never match, so that isn't an error!)Boyfriend
Hi. I tried different inclusions. The .gwt.xml files are pretty large, but I first tried to put the replace-with tag in my main app .gwt.xml file that includes all needed modules.Justification
So my question is: in what places and how do I actually need to put it?Justification
The short answer is "somewhere so that it will be seen by the compiler, and not replaced by another analogous rule". The long answer probably needs a bit more detail about your setup, because there are several ways to "do it wrong", so without sharing some aspects of your code, it is hard to be specific. At least share the skeleton of the files, with any other inherit statements?Boyfriend
Ok. I have edited my post.Justification
One more check, since it doesn't seem to be in the post yet: How are you requesting the number constants in your own code - how do you know it isn't getting replaced correctly?Boyfriend
I am not requesting that method, GWT framework should do it at runtime. I am just printing some debug info with my method: Notifier.printDebug(">>> getting number constants"); But it doesnt show anythingJustification
Any thoughts now?Justification
I just tried to create a new TestGWT project from scratch based on the web wizzard. I created 2 classes TestReplace and TestReplace2 (that extends TestReplace and overrides a method getString()). I instantiate it using GWT.create() and I added the replace-with tag in the TestGWT.gwt.xml file and still not working... I don't understand what am I missing?Justification
The best way to know if the LocaleInfoImpl is being replaced correctly is by doing a compilation with logLevel=DEBUG (or in maven, the build plugin with the configuration parameter <loglevel>DEBUG</loglevel>)Exploit
This is what I can see in the log file: Computing all possible rebind results for 'com.google.gwt.i18n.client.impl.LocaleInfoImpl' Rebinding com.google.gwt.i18n.client.impl.LocaleInfoImpl Checking rule <replace-with class='com.etcetera.util.PitLocaleInfoImpl'/> Checking if all subconditions are true (<all>) <when-type-is class='com.google.gwt.i18n.client.impl.LocaleInfoImpl'/> Yes, the requested type was an exact match Yes: All subconditions were true Rule was a match and will be used Rebind result was com.etcetera.util.PitLocaleInfoImplExploit
Ok, thank you... I will tryJustification
And also a last point: I use to put the replace-with at the end of the gwt.xml fileExploit

© 2022 - 2024 — McMap. All rights reserved.