Is there a way to intercept static methods with Guice?

968 views
Skip to first unread message

pro...@gmail.com

unread,
May 13, 2007, 9:43:44 AM5/13/07
to google-guice
Hello guys!

I want to create an TracingInterceptor. It works fine on normal
methods, but doesn't work with static methods.
I think the Dependency Injection is implemented by using the Proxy-
Pattern. This could be the reason for my Problem, because you can't
build a proxy over a class using static methods. But how can i
intercept static methods?
Is this possible with Guice?

Greetings
Fabian Wohlschläger

Dhanji R. Prasanna

unread,
May 13, 2007, 4:36:35 PM5/13/07
to google...@googlegroups.com
On 5/13/07, pro...@gmail.com <pro...@gmail.com> wrote:
This could be the reason for my Problem, because you can't
build a proxy over a class using static methods. But how can i
intercept static methods?

You will need to use bytecode manipulation tools (such as javassist or asm) directly. You can use AspectJ with compile-time weaving to achieve this too. The pointcut expression would look something like:

pointcut staticAdvisor() : call(static * com.wideplay.ClassName.staticMethod*(...));

Is this possible with Guice?

No, you cannot. Guice's aop is based on cglib and *runtime* weaving. Afaik there is no way to intercept static methods with runtime weaving.

Greetings
Fabian Wohlschläger

Dhanji.

Gregory Kick

unread,
May 13, 2007, 4:43:52 PM5/13/07
to google...@googlegroups.com
hey, just saw the original post, and I made an interceptor for what
you're talking about (i think) that you might want to take a look at.

https://kickstyle.net/svn/main/underpinnings/trunk/src/main/java/net/kickstyle/underpinnings/logging/


--
Gregory Kick
http://kickstyle.net/

Dhanji R. Prasanna

unread,
May 13, 2007, 4:48:23 PM5/13/07
to google...@googlegroups.com
On 5/13/07, Gregory Kick <gk5...@gmail.com> wrote:
hey, just saw the original post, and I made an interceptor for what
you're talking about (i think) that you might want to take a look at.

The interceptors impl MethodInterceptor from aopalliance--they cant be inserted into a static join point with guice. =(

Dhanji.

Gregory Kick

unread,
May 13, 2007, 4:55:09 PM5/13/07
to google...@googlegroups.com
oh yeah, that wasn't solving his static problem, I just thought that
it would save him writing the interceptor all over again.

On 5/14/07, Dhanji R. Prasanna <dha...@gmail.com> wrote:
>
>

pro...@gmail.com

unread,
May 14, 2007, 9:06:32 AM5/14/07
to google-guice
First of all thank you very much for your answers!

I checked your solutions and it seems there is really no choice than
manipulate bytecode or using AspectJ with compile time weaving (as you
said).
In my opinion this is much work to do, just for using aspect-oriented
features. The only economic/fast solution seems to place aspects (like
tracing/logging) directly in static methods. This would be kind of
mix, because the aspects are in static classes and in the
interceptors, but using code weaving + aspectJ or Bytecode
manipulation is rather complex.

Dhanji R. Prasanna

unread,
May 14, 2007, 1:13:34 PM5/14/07
to google...@googlegroups.com
On 5/14/07, pro...@gmail.com <pro...@gmail.com> wrote:

First of all thank you very much for your answers!

I checked your solutions and it seems there is really no choice than
manipulate bytecode or using AspectJ with compile time weaving (as you
said).
 
You *might* be able to do this using Spring AOP (2.0+) and load-time weaving (e.g. spring-tomcat-weaver) which would be a lot easier, however I have never tried it. Let us know if you manage to get it to work =)

Dhanji.

Bob Lee

unread,
May 14, 2007, 1:24:35 PM5/14/07
to google...@googlegroups.com
Can you get rid of the static methods? Chances are it will make your code more flexible and easier to test anyway.

Bob

pro...@gmail.com

unread,
May 16, 2007, 9:32:19 AM5/16/07
to google-guice
Take a look at this DAO:

package de.fabe.guice.impl.dao;


public class ServiceDaoImpl {
private static ServiceDaoImpl mInstance;

private ServiceDaoImpl() {
}

public static ServiceDaoImpl getInstance() {
System.out.println("getInstance of ServiceDao");

if (mInstance == null) {
mInstance = new ServiceDaoImpl();
}

return mInstance;
}
}

How should I get rid of the static method?

pro...@gmail.com

unread,
May 16, 2007, 9:32:30 AM5/16/07
to google-guice
Take a look at this DAO:

package de.fabe.guice.impl.dao;


public class ServiceDaoImpl {
private static ServiceDaoImpl mInstance;

private ServiceDaoImpl() {
}

public static ServiceDaoImpl getInstance() {
System.out.println("getInstance of ServiceDao");

if (mInstance == null) {
mInstance = new ServiceDaoImpl();
}

return mInstance;
}
}

How should I get rid of the static method?


On 14 Mai, 19:24, "Bob Lee" <crazy...@crazybob.org> wrote:

pro...@gmail.com

unread,
May 16, 2007, 9:34:35 AM5/16/07
to google-guice
@Dhanji R. Prasanna

Thanks for your post.
One of my targets is an evaluation of Guice in comparison to Spring.
If I use Spring AOP i clould use Spring DI either ;)

Stuart McCulloch

unread,
May 16, 2007, 9:37:37 AM5/16/07
to google...@googlegroups.com
this code implements a singleton - why not use Guice to inject
ServiceDaoImpl, then you can get rid of the static method and
configure it as a singleton in your Guice bindings?


--
Cheers, Stuart

pro...@gmail.com

unread,
May 16, 2007, 9:48:13 AM5/16/07
to google-guice
@Stuart McCulloch
Brilliant idea!
I'm such a fool ;)

@Dhanji R. Prasanna
Seems that Spring AOP is based on Proxies, too. The only way to fix
this problem is AspectJ.

http://forum.springframework.org/archive/index.php/t-26790.html


pro...@gmail.com

unread,
May 16, 2007, 9:48:23 AM5/16/07
to google-guice

Dhanji R. Prasanna

unread,
May 16, 2007, 2:10:04 PM5/16/07
to google...@googlegroups.com
On 5/16/07, pro...@gmail.com <pro...@gmail.com> wrote:

@Stuart McCulloch
Brilliant idea!
I'm such a fool ;)

@Dhanji R. Prasanna
Seems that Spring AOP is based on Proxies, too. The only way to fix
this problem is AspectJ.

Not entirely correct. Spring AOP 2.0 is based on AspectJ under the hood. If you used *load-time weaving* it is possible that the AspectJ engine would generate the new classes for you (which would then be proxied). Anyway it was a stab in the dark--you should do what Bob Lee and Stuart McCulloch suggested.

Dhanji.

http://forum.springframework.org/archive/index.php/t-26790.html






pro...@gmail.com

unread,
May 17, 2007, 10:26:41 PM5/17/07
to google-guice
I solved the problem, by using the suggestions of Bob Lee and Stuart
McCulloch.
But thx for your information that spring aop 2.0 uses aspect/j under
the hood. This could be useful ;)

On 16 Mai, 20:10, "Dhanji R. Prasanna" <dha...@gmail.com> wrote:

Reply all
Reply to author
Forward
0 new messages