For GWT client-side code, you can use delayed binding to generate Java code at compile time. You create a subclass of com.google.gwt.core.ext.Generator . Google uses lazy binding, i.e. localization, etc. I used it to implement a simple XML converter for GWT, but it is a closed source.
For example, you create the com.foo.Constants interface:
public interface Constants { int getY(); int getZ(); }
And create a generator for your constants:
public class ConstantsGenerator extends Generator { private static final String PACKAGE_NAME = "com.foo"; private static final String SIMPLE_CLASS_NAME = "GeneratedConstants"; private static final String CLASS_NAME = PACKAGE_NAME + "." + SIMPLE_CLASS_NAME; @Override public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException {
Then you need to configure the GWT module XML file:
<generate-with class="com.foo.ConstantsGenerator"> <when-type-assignable class="com.foo.Constants" /> </generate-with>
Then, in your GWT client code, you can instantiate your generated class as follows:
public Constants getConstants() { return (Constants) GWT.create(Constants.class); }
But this only works for client-side code. And maybe a little too much overhead to just pre-calculate some values.
It would be easier to include the code generation step in your build scripts. Then you can use the created classes both on the server and on the client code.
But for predefined values, this can still be too large. You may not generate code at all if you generate a simple file (for example, a properties file) with your pre-calculated values and load it at run time.
On the server side, this should not be a problem. For the client side, you can generate your GWT homepage using JSP and include your precalculated values as a JavaScript dicationary. In the GWT code, these values are easy to access.