简体   繁体   English

子类中带有注释的Aspectj切入点方法

[英]Aspectj pointcut method with annotation in subclass

I have 2 classes 我有2节课

class Fragment1{
   createView(SomeObject p1, AnoterObject p2)
}

@AutoLayout(String annotationParam)
class Fragment2 extends Fragment1{
}

How i can do @Around createView on Fragment2.createView call and get annotationParam? 我如何在Fragment2.createView调用上执行@Around createView并获取AnnotationParam? Thanks 谢谢

ADD: If i add method stub into Fragment2, it is start be possible to use next annotation @Around("createMethod(inflater, group, savedState) && @within(autoInflate)") , but it is a very ugly solution 添加:如果我将方法存根添加到Fragment2中,则可以开始使用下一个注释@Around("createMethod(inflater, group, savedState) && @within(autoInflate)") ,但这是一个非常丑陋的解决方案

SOLUTION: Thanks to @kriegaex i found solution: 解决方案:感谢@kriegaex,我找到了解决方案:

@Around("execution(* com.github.a_kokulyuk.kotakt.ui.BaseFragment+.*(*, *, *)) && args(inflater, parent, savedState) && @this(an)")
    public Object doLayout(ProceedingJoinPoint jo, LayoutInflater inflater, ViewGroup parent, Bundle savedState, AutoLayout an) throws Throwable {
        return inflater.inflate(an.value(), parent, false);
    }

Given this annotation: 给定此注释:

package de.scrum_master.app;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {}

Let us assume we have three classes: 让我们假设我们有三个类:

  • a parent class without annotation, 没有注释的父类,
  • a plain child class without annotation and 一个没有注释的普通子类,并且
  • an annotated child class: 带注释的子类:
package de.scrum_master.app;

public class Parent {
    public void doSomething() {}
}
package de.scrum_master.app;

public class PlainChild extends Parent {
    int doSomethingElse() { return 11; }
}
package de.scrum_master.app;

@MyAnnotation
public class AnnotatedChild extends Parent {
    String doSomethingSpecial(int number) { return ""; }
}

Here is a little driver application instantiating all three classes, calling all available methods on them, inherited or not, with different signatures and return types: 这是一个小小的驱动程序应用程序,实例化所有三个类,并使用不同的签名和返回类型调用它们上的所有可用方法(是否继承):

package de.scrum_master.app;

public class Application {
    public static void main(String[] args) {
        new Parent().doSomething();
        new PlainChild().doSomething();
        new PlainChild().doSomethingElse();
        new AnnotatedChild().doSomething();
        new AnnotatedChild().doSomethingSpecial(123);
    }
}

Finally, here is the aspect doing what was asked in the question: It intercepts all method executions in Parent or any of its subclasses (thus the + ), but only if the class of the current instance this bears @MyAnnotation : 最后,这里是做什么用问问题的方面:它截取所有方法执行Parent或任何其子类(因而+ ),但前提是类当前实例的this@MyAnnotation

package de.scrum_master.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class MyAspect {
    @Around(
        "execution(* de.scrum_master.app.Parent+.*(..)) && " +
        "@this(de.scrum_master.app.MyAnnotation)"
    )
    public Object myAdvice(ProceedingJoinPoint thisJoinPoint) {
        System.out.println(thisJoinPoint);
        System.out.println("  " + thisJoinPoint.getThis());
        return thisJoinPoint.proceed();
    }
}

The console log: 控制台日志:

execution(void de.scrum_master.app.Parent.doSomething())
  de.scrum_master.app.AnnotatedChild@681a9515
execution(String de.scrum_master.app.AnnotatedChild.doSomethingSpecial(int))
  de.scrum_master.app.AnnotatedChild@13221655

As you can see, doSomething() is called three times, but only intercepted once. 如您所见, doSomething()被调用了三次,但仅被拦截了一次。 You can also see from the printed getThis() object, that really the right execution is intercepted. 您还可以从打印的getThis()对象中看到,确实正确的执行被拦截了。

Here is the example 这是例子

public @interface customAnnotation {
    String value() default "someValue";
}


@Aspect
public class customAspect {

    @Around(value="@annotation(customAnnotation)")
    public Object customAdvice(ProceedingJoinPoint joinPoint, CustomAnnotation customAnnotation ) throws Throwable {
    // ...
     }
}

See below as above snippet is inspired by these resources 如下所示,摘录摘录受这些资源的启发

Accessing Annotation-value in advice 访问通知中的注释值

Spring AOP: Getting parameters of the pointcut annotation Spring AOP:获取切入点注释的参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM