A common situation using generators is write instantiation code to
"inject" instances of non-generated classes. For example:

String newFooExpr(String name){
  return "new " + Foo.class.getName() + "(" + name + ")";
}

String initializationExpr(Class<?> cls, String varName, String expr){
  return cls.getName() + " " + varName + " = " + expr;
}

String fooReferenceExpr = "foo";

String fooInitializacionExpr = initializationExpr(Foo.class,
fooReferenceExpr, newFooExpr("hello"));

Later, fooInitializationExpr is injected in the generated source code.
This approach is easy to write for light object trees, but becomes
complex with deep trees.
Since serialization is a way to freeze an object tree for further
consumption, such mechanism could be used to inject instances from
generators to generated code. For example, being Foo serializable, and
having an InstancePool in GeneratorContext:

Foo foo = new Foo("hello");

String fooReferenceExpr = generatorContext.getInstancePool().add(foo);

InstancePool.add() adds an instance to the pool and returns a
generated global reference name. When all the generators were invoked,
the InstancePool is serialized in a static context, where the
instances can be referenced by generated reference names.

This approach could allow frameworks like GIN to be fully compatible
with Guice Modules, enabling injection of instances, scopes, etc..

How silly is this idea?

Thanks in advance.

- Andrés

--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/Google-Web-Toolkit-Contributors
-~----------~----~----~----~------~----~------~--~---

Reply via email to