Why does com.google.gwt.i18n.shared.DateTimeFormat call Gwt.create - gwt

In my shared code I replaced com.google.gwt.i18n.client.DateTimeFormat with com.google.gwt.i18n.shared.DateTimeFormat to avoid runtime issues with Gwt.create as proposed here.
My problem is now, that DateTimeFormat.getFormat calls getDefaultDateTimeFormatInfo which calls LocaleInfo.getCurrentLocale().getDateTimeFormatInfo(). LocaleInfo is a singleton which is instanciated via new LocaleInfo((LocaleInfoImpl)GWT.create(LocaleInfoImpl.class), (CldrImpl)GWT.create(CldrImpl.class)).
In my test this ends up in an ExceptionInInitializerError.
java.lang.ExceptionInInitializerError
at com.google.gwt.i18n.shared.DateTimeFormat.getDefaultDateTimeFormatInfo(DateTimeFormat.java:681)
at com.google.gwt.i18n.shared.DateTimeFormat.getFormat(DateTimeFormat.java:665)
Caused by: java.lang.UnsupportedOperationException: ERROR: GWT.create() is only usable in client code! It cannot be called, for example, from server code. If you are running a unit test, check that your test case extends GWTTestCase and that GWT.create() is not called from within an initializer or constructor.
at com.google.gwt.core.shared.GWT.create(GWT.java:66)
at com.google.gwt.core.client.GWT.create(GWT.java:86)
at com.google.gwt.i18n.client.LocaleInfo.<clinit>(LocaleInfo.java:36)
... 27 more
So I'm really surprised that a shared class calls Gwt.create at all. In my understanding of how GWT works, this should not be the case. Correct me if I'm wrong.

At least I think, this is a known issue: https://github.com/gwtproject/gwt/issues/7668
Instead, you can give:
https://github.com/vegegoku/gwt-i18n-apt
a try.
But not sure, if it is already migrated completly. You may ask this here: https://gitter.im/gwtproject/gwt-modules

Related

Scala project-wide static instructions

I'm using a Java lib within my Scala project. I want to call a specific static method adjustLib of that lib as early as possible and for every possible starting point of an execution (e.g. before runing the "main" App and before executing tests) to achieve the desired behaviour.
One solution would be to place this statement at the very top of each class, that might be executed (applies for all classes extending App and for all tests).
However, if someone implements a new, executable class but forgets adjustLib, things might get weird.
Is there any chance to define an object or something similar, that executes this adjustLib statement in a "static" manner every time anything in the given project is executed?

How do I undo a Setup call for a moq Mock?

This might be a special use case that I am dealing with here. Here is what my simple C# NUnit test that uses Moq looks like
Mock<ISomeRepository> mockR = new Mock<ISomeRepository>();
mockR.Setup(x => x.GetSomething).Returns(new Something(a=1,b=2);
--use the mocked repository here
Now later in this same unit test or another test case I want to invoke the real implementation of the method GetSomething() on this mockR object.
Is there a way to do that? My repository is Singleton at its heart. So even if I create a new object, the GetSomething method still returns the same Moq'd object.
That would largely depend on your implementation of that GetSomething, which is something you're not showing here ;). Also, I'm not sure that's even a valid setup, shouldn't there be a .Setup(..).Returns(..) there?
Mocks are used to represent dependencies of a class allowing that class to be tested without using their actual dependencies. Or you can do tests which involve the actual dependencies.
But using a mocked dependency and the real dependency within the same unit test sounds like you're not clear what your test is testing.
If it's another test case, it shouldn't be a problem either. Each test should not impact another, so if you set up the class under test separately that should be fine, even with a singleton.
I'm assuming that you're injecting the singleton dependency. If not, do that.

What causes GWT to output: <SomethingNotNull>.nullMethod()?

I have my own emulation of java.util.Timer (and quite a lot of other stuff missing in GWT). I have even a JUnit test proving it works in the browser.
I've just tried to convert some third-party library to GWT, which needed a Timer, and in some part of it, I call:
SystemUtils.getTimer().scheduleAtFixedRate(timerTask, value, value);
But the GWT compiler turns getTimer().scheduleAtFixedRate() to:
getTimer().nullMethod()
SystemUtils.getTimer() is a static method. I have googled for nullMethod(), but most hits are about:
null.nullMethod();
That doesn't apply to me. What could be going wrong, and what can I do to fix it?
[EDIT] Actually, the java.util.Timer emulation itself works, but it seems that (atm?) SystemUtils.getTimer() returns "undefined". Could that be the reason? Since getTimer() returns an instance created dynamically, how could the GWT compiler possibly make any assumption about the return value of getTimer(), and the presence/usage of the methods of the Timer type?
When I have seen this kind of errors it was caused by unreachable code: GWT had determined that some code was not reachable, turning off compilation for some stuff, but then it still somehow tried to link the unreachable code, showing this kind of errors.
For completeness sake
If this error shows up (which often happens after deploying to App Engine) then compile without obfuscation, turn off super dev mode, restart jetty and refresh the browser. Open the generated javascript and find where the problem occurs by searching for 'nullMethod'. You'll see that the compiler may have removed whole chunks of code that it believes is 'unreachable'.
The code surrounding 'null.nullMethod' is probably very different than what you expected. The simplest way around this is to add a null /undefined check and initializing whatever variable that is generated as 'null'. This forces the compiler to reconsider because now the variable can never be null and the code that follows it must be reachable.
For example, if null.nullMethod is found and 'null' is actually supposed to be var a = ... then add if(a == null) { a = ""; } before it (in Java of course).
For anybody who struggles with this null.nullMethod issue:
It may be possible that your GWT compiler isn't able to find the properties of your JSON bean object if your object variable is declared with its interface type:
MyTypeIF item = ...;
...
item.getStart();
...
In my scenario, GWT compiled that into:
MyTypeIF item = ...;
...
null.nullMethod();
...
Instead, I had to declare and cast it to its real implementation class:
JSMyType item = (JSMyType)...;
...

In MEF how is the dependency chain of exports-imports evaluated?

I'm facing an issue where I have App1.ClassA Importing App2.ClassB which itself needs to import App2.ClassC in a property. App1 and App2 are 2 different xaps
App1.ClassA invokes ComponentInitializer.SatisfyImports(this) in its initializing code. However this chain of satisfying imports does not seem to cascade down across assemblies.
I cannot specify ComponentInitializer on ClassB, since it is exporting itself (and MEF throws an error).However, ClassC is not being imported into the property of ClassB without this invocation.
Is this the expected behaviour or am i seeing some other bug due to which ClassC is not getting loaded ?
I went through this post -http://forums.silverlight.net/forums/t/202811.aspx, but the difference may be that i am crossing over Xaps in my scenario
Thanks in advance
i am crossing over Xaps in my scenario
By default, ComponentInitializer will find only parts in the current XAP. You can override this default host configuration by calling CompositionHost.Initialize.
The chain of resolving imports was being respected even when crossing over xap boundries. I'm guessing that xap boundries are not even an issue for xap, as it works at assembly level and as long as the assembly is available, it will resolve all the imports in the dependency chain.
The mistake i made was not realizing that the imports are not resolved during class instantiation( so the resolved elements are not available in the constructor). I had to hook up for the IPartInitialized Notification, and take action when this event is raised.
I'm marking this as the correct answer not to boost standings but to guide anyone facing the same issue.

OSGi SAT, how should we deal with activation failure?

The eclipse OSGi Service Activator Toolkit provides a framework that simplifies handling the dependencies between budles.
One can derive from org.eclipse.soda.sat.core.framework.BaseBundleActivator and over-ride (for example) the activate() method to do some spefic initialisation work.
The signature is protected void activate(){}
Which leads to the question: "what are we spposed to do if activate() fails?", suppose for some reason we cannot initialise correctly. I can't throw an exception, the method signatiure won't allow that.
Throwing an RuntimeException or an Error in the activate() method does not help you if you are using Equinox (tested with org.eclipse.osgi_3.5.1.R35x_v20090827).
Independent of what you are throwing the bundle will finally end up in state ACTIVE.
I assume that this behavior is specific to Equinox (bug?) because from my understanding this violates the OSGi specs.
It's still possible to throw RuntimeException and Error (And Exceptions that inherit these). (Remember that Error indicates serious problems that a reasonable application should not try to catch.)
It would also seems like a good idea to output something to your logging facility.
The alternative you choose is dependent on the situation; what the root cause of the failed activation is. If the cause is something abnormal, that should not happen during normal circumstances, an Error or RuntimeException (and ofcourse loggin) feels right.