Search code examples
javaemailguicefinal

Is there a way to inject final class with guice?


I have provider which should inject javax.mail.Session (provider looks it up in env. context of tomcat) as singleton. There is a problem when I use field injection somewhere in code:

java.lang.IllegalArgumentException: Cannot subclass final class class javax.mail.Session

Is there a way to go round this? Only clues that I've found point to Spring AOP/Proxies.

Thanks && regards


Solution

  • Look at this http://code.google.com/p/google-guice/wiki/AOP. It seems that there is some limitations:

    Limitations

    Behind the scenes, method interception is implemented by generating bytecode at runtime. Guice dynamically creates a subclass that applies interceptors by overriding methods. If you are on a platform that doesn't support bytecode generation (such as Android), you should use Guice without AOP support.

    This approach imposes limits on what classes and methods can be intercepted:

    Classes must be public or package-private. Classes must be non-final Methods must be public, package-private or protected Methods must be non-final Instances must be created by Guice by an @Inject-annotated or no-argument constructor