AspectJ - 检索带注释的参数列表 [英] AspectJ - Retrieve list of annotated parameters

查看:27
本文介绍了AspectJ - 检索带注释的参数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自以下上一个问题(AspectJ - Presence无法识别连接点表达式中的注释),

From the following previous question (AspectJ - Presence of annotation in join point expression not recognized),

我的目标:在一方面,我希望能够从匹配函数中提取/检索所有带注释的参数,无论有多少.(然后对其进行一些处理,但这不是这个问题的范围)

My goal: In an aspect, i'd like to be able to extract/retrieve all annotated parameters from matching functions, no matter how many there are. (and then apply some treatment on but it's not the scope of this question)

所以目前,这就是我所做的(不工作):

So for the moment, this is what i did (not working):

@Before("execution (* org.xx.xx.xx..*.*(@org.xx.xx.xx.xx.xx.Standardized (*),..))")
public void standardize(JoinPoint jp) throws Throwable {
    Object[] myArgs = jp.getArgs();
    getLogger().info("Here: arg length=" + myArgs.length);
    // Roll on join point arguments
    for (Object myParam : myArgs) {

        getLogger().info(
                    "In argument with " + myParam.getClass().getAnnotations().length
                                + " declaread annotations");
        getLogger().info("Class name is " + myParam.getClass().getName());
        // Get only the one matching the expected @Standardized annotation
        if (myParam.getClass().getAnnotation(Standardized.class) != null) {
            getLogger().info("Found parameter annotated with @Standardized");
            standardizeData(myParam.getClass().getAnnotation(Standardized.class), myParam);
        }
    }
}

这是通知匹配的代码:

public boolean insertLog(@Standardized(type = StandardizedData.CLIPON) CliponStat theStat) {
    // ...
}

以及junit测试生成的痕迹:

And the traces generated by a junit test:

INFO: ICI: arg lenght=1
INFO: In argument with 0 declaread annotations

看起来它没有检测到注释

Looks like it doesn't detect the annotation

所以我的问题是:如何检测具有特定注释的参数?

So my question is: how to detect parameters which have specific annotation(s) ?

有人知道怎么做吗?

预先感谢您的帮助.

问候.

我找到了这个线程带注释参数的切入点匹配方法,讨论同样的事情,并应用了给定的解决方案,但它不起作用..

i found this thread Pointcut matching methods with annotated parameters, discussing of the same thing, and applied the given solution but it doesn't work..

推荐答案

希望我理解你的意思.

myParam.getClass().getAnnotations() 为您提供类的注释.类似的东西:

myParam.getClass().getAnnotations() gives you the annotations on a class. Something like:

@Standardized(type = StandardizedData.CLIPON)
public class Main{...}

也许这个切入点/建议对你有帮助:

Maybe this pointcut/advice helps you:

@Before("execution (* org.xx.xx.xx..*.*(@org.xx.xx.xx.xx.xx.Standardized (*),..))")
public void standardize(JoinPoint jp) throws Throwable {
    Object[] args = jp.getArgs();
    MethodSignature ms = (MethodSignature) jp.getSignature();
    Method m = ms.getMethod();

    Annotation[][] parameterAnnotations = m.getParameterAnnotations();

    for (int i = 0; i < parameterAnnotations.length; i++) {
        Annotation[] annotations = parameterAnnotations[i];
        System.out.println("I am checking parameter: " + args[i]);
        for (Annotation annotation : annotations) {
            System.out.println(annotation);

            if (annotation.annotationType() == Standardized.class) {
                System.out.println("we have a Standardized Parameter with type = "
                        + ((Standardized) annotation).type());
            }
        }
    }
}

这给了我以下输出:

I am checking parameter:  main.CliponStat@331f2ee1 
@annotation.Standardized(type=CLIPON)
we have a Standardized Parameter with type = CLIPON

这篇关于AspectJ - 检索带注释的参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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