的AspectJ / Java的仪器截取的annoted参数调用/使用 [英] AspectJ/Java instrumentation to intercept an annoted parameter call/usage

查看:294
本文介绍了的AspectJ / Java的仪器截取的annoted参数调用/使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何当使用param1(参数定义,并在TestClassGeneralMeasuraments类的方法使用)doMonitorization方法可以被称为具有具有拦截的AspectJ定义为波纹管$ C $正确标注C显示。

 包monitorization;进口org.aspectj.lang.JoinPoint;
进口org.aspectj.lang.annotation.After;
进口org.aspectj.lang.annotation.Aspect;
进口org.aspectj.lang.annotation.Pointcut;@方面
公共类AspectJInterceptor {
    @Pointcut(值=@annotation(monitorme),argNames =monitorme)
    公共无效monitorActivity(Monitorme monitorme){}    @After(monitorActivity(monitorme))
    公共无效doMonitorization(JP的连接点,Monitorme monitorme){
        MetricsDataStructure.staticInstance.addOperation(jp.getSignature()toLongString(),monitorme.value());
    }
}

/////////////////////////////////////////////// //

 包monitorization;进口java.lang.annotation.Retention;
进口java.lang.annotation.Target;
进口java.lang.annotation.ElementType;
进口java.lang.annotation.RetentionPolicy;@Target({ElementType.METHOD,ElementType.FIELD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
公共@interface Monitorme {
    int值();
}

/////////////////////////////////////////////// //

 包monitorization;公共类TestClassGeneralMeasuraments {
    @Monitorme(20)
    INT场;
    @Monitorme(50)
    诠释域2;    @Monitorme(5)
    公共无效simpleTestMethod(@Monitorme(10)字符串参数1){
        this.field = 1;        this.field = 3;        this.field = 4;        参数1 =;
    }    @Monitorme(30)
    公共无效simpleTestMethod2(@Monitorme(10)字符串参数1){
        this.field2 = 1;
        this.field2 = 1;
        参数1 =;
    }    公共静态无效的主要(最终字串[] args){
        长intialTimeStamp = System.currentTimeMillis的();
        的System.out.println(启动);
        TestClassGeneralMeasuraments的TestObject =新TestClassGeneralMeasuraments();
        为(长I = 0; I< 50;我++)
        {
            testObject.simpleTestMethod(嗨);
            testObject.simpleTestMethod(嗨);
            testObject.simpleTestMethod2();
        }
    }
}


解决方案

AspectJ的 @annotation 切入点无法比拟只能在参数类型的参数标注,即你可以不匹配

公共富(@MyAnnotation的MyType富)

但你只能匹配

公共富(的MyType富)

如果类的MyType @MyAnnotation 注释。

这已经AspectJ的邮件列表上讨论是没有到目前为止实施的愿望清单上的功能。

I would like to know how the doMonitorization method can be called when the param1 is used (param defined and used on the methods of the TestClassGeneralMeasuraments Class) which has the correct annotation that has a interception AspectJ definition as the bellow code shows.

package monitorization;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class AspectJInterceptor {
    @Pointcut(value = "@annotation(monitorme)", argNames = "monitorme")
    public void monitorActivity(Monitorme monitorme) {}

    @After("monitorActivity(monitorme)")
    public void doMonitorization(JoinPoint jp, Monitorme monitorme) {
        MetricsDataStructure.staticInstance.addOperation(jp.getSignature().toLongString(), monitorme.value());
    }
}

/////////////////////////////////////////////////

package monitorization;

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

@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Monitorme {
    int value();
}

/////////////////////////////////////////////////

package monitorization;

public class TestClassGeneralMeasuraments{
    @Monitorme(20)
    int field;
    @Monitorme(50)
    int field2;

    @Monitorme(5)
    public void simpleTestMethod(@Monitorme(10) String param1){
        this.field = 1;

        this.field = 3;

        this.field = 4;

        param1 = "";
    }

    @Monitorme(30)
    public void simpleTestMethod2(@Monitorme(10) String param1){
        this.field2 = 1;
        this.field2 = 1;
        param1 = "";
    }

    public static void main(final String[] args){
        long intialTimeStamp = System.currentTimeMillis();
        System.out.println("Starting up");
        TestClassGeneralMeasuraments testObject = new TestClassGeneralMeasuraments();
        for(long i=0; i<50; i++)
        {
            testObject.simpleTestMethod("Hey");
            testObject.simpleTestMethod("Hey");
            testObject.simpleTestMethod2("");
        }
    }
}

解决方案

AspectJ @annotation pointcuts cannot match annotations on parameters, only on parameter types, i.e. you cannot match on

public foo(@MyAnnotation MyType foo)

but you can only match on

public foo(MyType foo)

if class MyType is annotated by @MyAnnotation.

This has been discussed on the AspectJ mailing list and is a feature on the wish list which has not been implemented so far.

这篇关于的AspectJ / Java的仪器截取的annoted参数调用/使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆