0

I'm currently building a library right now for other libraries to use. My library uses AspectJ to intercept common function calls and does some modifications. Therefore, other libraries just need to import my package and they're set. In other words, external libraries do not need to instantiate anything from my package.

My package currently uses a Constants class to keep track of constants, but I wish to use guice to do this for me. Here's an example of what I had in mind:

// pseudoish-code
@Aspect
public class MyAspect {
   @Inject
   @Named("myConstant")
   private static String myConstant;
   // currently I'm doing:
   // private static String myConstant = Constants.myConstant;

   /*
   * This method handles intercepting calls to foo(). External libraries using my
   * package will have their foo() method intercepted and modified
   */
   @Around("execution(* ...foo(..))")
   public Object handleFooIntercept(ProceedingJoinpoint jp) {
      // do stuff with myConstant
   }
}

If I understand guice correctly, I'm going to need to make an injector to actually inject these values:

Injector injector = createInjector(new MyModule());   // binds and provides constants
MyAspect myAspect = injector.getInstance(MyAspect.class);

The problem is that I do not know where to put this. My package does not have an entry point; it simply gets used when other libraries call foo(). Is my design idea critically flawed? How should I approach this?

Kiwi breeder
  • 459
  • 4
  • 11
  • Just FYI: ["Whatever happened to Aspect Oriented Programming?"](https://stackoverflow.com/questions/316132/what-ever-happened-to-aspect-oriented-programming). This was asked back in 08, *twelve years ago.* Even a comment says its lead author basically gave up on it. I think you'd need a very good reason to start a new project using it. It's a very dead horse. – markspace Sep 18 '20 at 19:56
  • Unfortunately, using aop wasn't really my choice and I can not give up on that part – Kiwi breeder Sep 18 '20 at 20:09
  • I am an AspectJ expert, but not a Guice user. If you were able to provide an [MCVE](https://stackoverflow.com/help/mcve) for me, I would agree to take a look. I am not sure, though, that it is a good idea to tightly couple an AspectJ aspect to Guice just because you want to inject a constant. BTW, does Guice not have its own "AOP lite" solution? I vaguely remember something like that, even though I exclusively use full-blown AspectJ in my projects. – kriegaex Sep 22 '20 at 01:58

0 Answers0