AspectJ constructor force factory pattern

纵饮孤独 提交于 2019-12-21 21:33:45

问题


I want to change the object return from call to a constuctor

FROM

public class A  {

   public A(){
   }

   public String sayHello() {
    return "hello";
   }

   public String foo() {
      return "foo";
   }

}

TO

public class AWrapped extends A {

     private A wrapped;

     public AWrapped() {
       super();
     }


     public AWrapped(A pWrapped) {
        wrapped=pWrapped;
     }

     public String foo() {
       return wrapped.foo();
     }

     public String sayHello {
        return "gday mate";
     }

}

What i want to do is to change the object that is returned from a call

A a = new A();
a.sayHello() returns "gday mate"

a is an instaceof AWrapped

I understand that this would usually be done with a factory pattern but I dont have access to the code of A or the code that makes new A's. And there are 1000s of places that A can be created.

It seems that Aspectj might do the trick, but i dont know much about it, If AspectJ would do the trick how to I get around the infinite wrapping i need to know that its being consturcted from within and aspect so it doesnt wrapp it again.

Thanks for the help Jon


回答1:


If I understand you right you could do the following:

I've created three packages:

  • aspectj for the aspect and AWrapped.java
  • unknown for A.java (could also be Bytecode but then you have to use Load Time Weaving)
  • main to test A a = new A();

MyAspect to return the AWrapped object if a new() call is made on class A:

package aspectj;

import unknown.A;

@Aspect
public class MyAspect {

    @Pointcut("call(unknown.A.new(..)) && !within(aspectj..*)")
    public static void init(ProceedingJoinPoint pjp) {
    }

    @Around("init(pjp)")
    public Object initAdvice(ProceedingJoinPoint pjp) throws Throwable{
        Object ret = pjp.proceed();
        return new AWrapped((A) ret);
        }

}

For testing:

package main;

import unknown.A;

public class Main {

    public static void main(String[] args) {
        A a = new A();
        System.out.println(a.sayHello());
    }
}

This outputs:

gday mate


来源:https://stackoverflow.com/questions/10291053/aspectj-constructor-force-factory-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!