java - How can I replace GWT.create with an Annotation Processor? -
i want create annotation processor replaces call gwt.create
.
with annotation processor, you'd have generate both classes , dynamically (at runtime) select among them, depending on context (you generate factory doing that, you'd still have somehow feed factory current context, e.g. current locale).
– source: https://stackoverflow.com/a/29915793/116472
i got annotation processor running generated classes well. part not know runtime selection part.
how can runtime selection?
i'll assume you've got code generation side covered, , focus on how might pick correct implementation in gwt:
but you'd still have somehow feed factory current context, e.g. current locale).
we @ runtime, suggest, of added support system.getproperty
@ compile time.
step one, of course, generate code each implementation might want have access to. taking locales example, might have foo_en.java
, foo_es.java
, foo_de.java
, etc.
next, need consistent way 1 implementation - perhaps generated foofactory
, method this:
public static foo getfoo(string locale) { if ("en".equals(locale)) { return new foo_en(); } else if /*... ...*/ throw new illegalargumentexception("locale " + locale + " not supported"); }
if ask user @ runtime locale wanted, pass value in factory method implementation wanted. likewise, if read value @ runtime something, again right instance , proceed forward.
but if want make compiler pick you? lets keep code generated annotation processor, move phase of selecting locale compiler , permutations.
as in existing gwt code, specify property locale, , several values. then, instead of asking user or deciding @ runtime in own java code locale want, use same selection script wiring gwt typically uses (checks url, cookie, meta tags, user agent itself, etc) - can construct own property-provider
if wanted.
as before, can use getfoo(locale)
, use system.getproperty
read out property created in our .gwt.xml file. statically compiled out correct constant each permutation. rather calling foofactory.getfoo(system.getproperty("locale"))
each time need instance, lets make yet method in our generated foofactory:
public static foo getfoo() { return getfoo(system.getproperty("locale")); }
now can call foofactory.getfoo()
, , correct class our current permutation.
dagger question: thomas far better suited address this, no, dagger2 doesn't bind(foo).to(bar).in(scope)
guice does, since require running code resolve bindings, whereas dagger operates can see reflecting on types, , not running code. means end lot of these @provides methods or annotating actual types details of should used implement what.
fwiw far haven't adopted dagger due few quirks require me rethink few bits, , haven't taken time rethinking yet:
Comments
Post a Comment