guice and groovy without groovy-guice

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

guice and groovy without groovy-guice

Christopher Piggott
I discovered groovy-guice but also discovered that it requires OSGi,
log4j, and a bunch of other things I don't want.  I'm not entirely
sure why that is.

In trying to solve my problem, I tried using reflection to learn about
what was going on with my script:

    final Class groovyClass = gcl.parseClass(f);

then reflect through it.  I did indeed find the private static field I
wanted injected:

    Field private static java.lang.Class
com.libaninc.test.test.$class$com$libaninc$installer$screen$Screen

but reflection shows no annotations attached to it, which is
perplexing.  I expected to see a com.google.inject.Inject on the
field.

I have worked around my prob lem with:

    Script script = childInjector.getInstance(Script.class);
    script.setProperty("injector", childInjector);

which gives the groovy script an injector to work with.  Injecting the
injector is generally not a good thing, though, so I still would like
to make this work.

Things I have tried that got nowhere:

    childInjector.injectMembers(script);  /* try to inject members
manually - doesn't work on statics */
    requestStaticInjection(groovyClass)  /* doesn't work for reasons I
can't yet explain */

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: guice and groovy without groovy-guice

Peter Niederwieser
Christopher Piggott wrote
Field private static java.lang.Class com.libaninc.test.test.$class$com$libaninc$installer$screen$Screen
That's definitely not the field you want to have injected.

Christopher Piggott wrote
Things I have tried that got nowhere:

    childInjector.injectMembers(script);  /* try to inject members manually - doesn't work on statics */
    requestStaticInjection(groovyClass)  /* doesn't work for reasons I can't yet explain */
When a script gets run, there's always an instance created, so you don't need static injection. The reason why your injection doesn't work is that a variable declaration at the top level of a script becomes a local variable of the script's run() method, not a field of the class. In order to get a field, you need to annotate the declaration with @Field (new in Groovy 1.8).

To get an idea of what Groovy code gets translated to under the covers, use Groovy Console's AST browser.

Cheers,
Peter
Reply | Threaded
Open this post in threaded view
|

Re: guice and groovy without groovy-guice

Christopher Piggott
Peter, thank you so much.  My most important goal wasn't to solve this
specific problem, but to learn enough of what's going on under the
hood to really understand it.  You helped a lot!

On Sun, Feb 20, 2011 at 9:32 AM, Peter Niederwieser <[hidden email]> wrote:

>
>
> Christopher Piggott wrote:
>>
>> Field private static java.lang.Class
>> com.libaninc.test.test.$class$com$libaninc$installer$screen$Screen
>>
>
> That's definitely not the field you want to have injected.
>
>
> Christopher Piggott wrote:
>>
>> Things I have tried that got nowhere:
>>
>>     childInjector.injectMembers(script);  /* try to inject members
>> manually - doesn't work on statics */
>>     requestStaticInjection(groovyClass)  /* doesn't work for reasons I
>> can't yet explain */
>>
>
> When a script gets run, there's always an instance created, so you don't
> need static injection. The reason why your injection doesn't work is that a
> variable declaration at the top level of a script becomes a local variable
> of the script's run() method, not a field of the class. In order to get a
> field, you need to annotate the declaration with @Field (new in Groovy 1.8).
>
> To get an idea of what Groovy code gets translated to under the covers, use
> Groovy Console's AST browser.
>
> Cheers,
> Peter
> --
> View this message in context: http://groovy.329449.n5.nabble.com/guice-and-groovy-without-groovy-guice-tp3392860p3393144.html
> Sent from the groovy - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email