问题描述
从下面的previous问题(<一个href=\"http://stackoverflow.com/questions/10554453/aspectj-$p$psence-of-annotation-in-join-point-ex$p$pssion-not-recognized\">AspectJ - 注释presence在连接点前pression无法识别)
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);
}
}
}
这是code。通过意见一致:
This is the code matched by the advice:
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
所以我的问题是:如何检测具有特定注释(S)参数
So my question is: how to detect parameters which have specific annotation(s) ?
是否有人有一个想法,该怎么办呢?
Does somebody have an idea how to do it?
在此先感谢您的帮助。
问候。
编辑:我发现这个线程Pointcut匹配带注释的参数的方法,同样的事情讨论,并应用了给出的解决方案,但它不能正常工作。
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..
推荐答案
我希望我理解你的权利。
I hope I understand you right.
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的 - 检索注解参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!