ConstructorInterceptor

140 views
Skip to first unread message

egolan

unread,
Jan 23, 2012, 1:01:07 PM1/23/12
to google-guice
Hi,
I saw many question on how to intercept constructor.
My purpose is just to log something like: initialized with ... (the
parameters).
I saw that the module doesn't allow to intercept constructor, but only
methods.

Is there a reason why Guice won't allow constructor interception?
Just wandering to understand the internals.

And,
I tried many ways of passing around this problem without success, so I
would appreciate some examples if there are any.

Thanks,

Eyal

Sam Berlin

unread,
Jan 23, 2012, 1:06:20 PM1/23/12
to google...@googlegroups.com
The latest version of Guice in git trunk supports provision interception by using:
 
void bindListener(Matcher<? super Key<?>> keyMatcher, ProvisionListener... listeners);

ProvisionListeners are notified when Guice is going to provision anything (including through providers).  You can hook into before & after the provision completes.  It isn't exactly like method interception, because Guice doesn't give you access to parameters to the constructor, but it does give you the dependency graph.  (It doesn't give access to the constructor because the goal isn't to actually let you *change* provision, it's just to let you know when it's happening.)

 sam


--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.


Noctarius

unread,
Jan 23, 2012, 1:06:43 PM1/23/12
to google...@googlegroups.com
Hi Eyal,

with the next Guice release there will be a ProvisionListener which
can be used to "intercept" constructor injection.

Noc

Noctarius

unread,
Jan 23, 2012, 1:09:15 PM1/23/12
to google...@googlegroups.com
Btw:
Here's some proof of concept implementation I'd done which uses such
kind of interception to exchange constructor parameters.
https://bitbucket.org/noctarius/guiceidentityinjection/src/014f6cb8fcc0/src/main/java/com/google/inject/identityinjection

Am 23.01.2012 19:01, schrieb egolan:

egolan

unread,
Jan 24, 2012, 3:44:12 AM1/24/12
to google-guice
Thanks Noc and Sam.
Sounds cool.

We are using guice 3.0 from maven.
Any estimation when the next version comes?

Eyal

On Jan 23, 8:09 pm, Noctarius <noctarius...@googlemail.com> wrote:
> Btw:
> Here's some proof of concept implementation I'd done which uses such
> kind of interception to exchange constructor parameters.https://bitbucket.org/noctarius/guiceidentityinjection/src/014f6cb8fc...

Noctarius

unread,
Jan 24, 2012, 3:51:35 AM1/24/12
to google...@googlegroups.com
Hi Eyal

As far as I know there's no roadmap with release dates. But you can
compile it yourself and publish it to a Maven mirror until it
published at the mainrepo.

Cheers
Chris

Stuart McCulloch

unread,
Jan 24, 2012, 9:14:38 AM1/24/12
to google...@googlegroups.com
On 24 Jan 2012, at 08:51, Noctarius wrote:

> Hi Eyal
>
> As far as I know there's no roadmap with release dates. But you can
> compile it yourself and publish it to a Maven mirror until it
> published at the mainrepo.

FYI, there's also the https://github.com/sonatype/sisu-guice branch which is Guice + https://github.com/sonatype/sisu-guice/blob/master/PATCHES and has regular dot releases deployed to Maven central:

http://repo1.maven.org/maven2/org/sonatype/sisu/sisu-guice/
http://repo1.maven.org/maven2/org/sonatype/sisu/inject/guice-assistedinject/
...etc...

The latest release (3.1.1) is based on trunk as of Sun Jan 15

Note the main difference wrt. trunk is we don't embed Guava in this branch, so you do need it as a dependency - but this isn't usually a problem as a lot of projects already have Guava on the classpath :)

Also by default sisu-guice depends on sisu-guava which we use for similar unofficial dot releases of Guava, but you can always replace this with the official Guava:

http://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html#Dependency_Exclusions

HTH

egolan

unread,
Jan 28, 2012, 4:08:46 PM1/28/12
to google-guice
Hi,
Thanks for the inputs.
Not sure we'll be able to use a snapshot :)
However, these links are great reference.

Eyal

On Jan 24, 4:14 pm, Stuart McCulloch <mccu...@gmail.com> wrote:
> On 24 Jan 2012, at 08:51, Noctarius wrote:
>
> > Hi Eyal
>
> > As far as I know there's no roadmap with release dates. But you can
> > compile it yourself and publish it to a Maven mirror until it
> > published at the mainrepo.
>
> FYI, there's also thehttps://github.com/sonatype/sisu-guicebranch which is Guice +https://github.com/sonatype/sisu-guice/blob/master/PATCHESand has regular dot releases deployed to Maven central:
>
>    http://repo1.maven.org/maven2/org/sonatype/sisu/sisu-guice/
>    http://repo1.maven.org/maven2/org/sonatype/sisu/inject/guice-assisted...
>    ...etc...
>
> The latest release (3.1.1) is based on trunk as of Sun Jan 15
>
> Note the main difference wrt. trunk is we don't embed Guava in this branch, so you do need it as a dependency - but this isn't usually a problem as a lot of projects already have Guava on the classpath :)
>
> Also by default sisu-guice depends on sisu-guava which we use for similar unofficial dot releases of Guava, but you can always replace this with the official Guava:
>
>    http://maven.apache.org/guides/introduction/introduction-to-optional-...
Reply all
Reply to author
Forward
0 new messages