[英]Get method parameters with specific annotation in aspect in scala using java reflection
我正在使用aspectj
在scala
使用aop。 我有辦法
def delete(@Id id:Long, name:String)
如何獲取我的外觀文件中的id
值。
@Around("execution (* com.myapp.Employee.delete(..))")
def message(joinPoint: ProceedingJoinPoint): Object = {
val methodSignature =joinPoint.getSignature.asInstanceOf[MethodSignature]
//get the value of the field id
joinPoint.proceed
}
我無法獲取這些值。
如果我嘗試
val res = methodSignature.getMethod.getParameterAnnotations
res.map(annotations => println("size = "+annotations.length))
始終將尺寸打印為0。
編輯:現在我正確獲取大小。 該方法是object
。 但是我認為java反射來讀取object
存在一些問題。 我改為class
,現在可以獲取注釋。 但是,如何獲取帶有該注釋的參數?
這是一些Java示例代碼,顯示了如何獲取方法注釋以及參數注釋以及參數類型(如已提及的cchantep,參數名稱不可用)。 我想您可以輕松地將其自己轉換為Scala。
package de.scrum_master.aspect;
import java.lang.annotation.Annotation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
@Aspect
public class MyAspect {
@Before("execution (* com.myapp.Employee.delete(..))")
public void myAdvice(JoinPoint joinPoint) throws Throwable {
System.out.println(joinPoint);
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String methodName = signature.getMethod().getName();
Class<?>[] parameterTypes = signature.getMethod().getParameterTypes();
System.out.println("Method annotations:");
Annotation[] methodAnnotations = joinPoint.getTarget().getClass().getMethod(methodName, parameterTypes).getAnnotations();
for (Annotation annotation : methodAnnotations)
System.out.println(" " + annotation);
System.out.println("Parameter annotations:");
Annotation[][] parameterAnnotations = joinPoint.getTarget().getClass().getMethod(methodName, parameterTypes).getParameterAnnotations();
int parameterIndex = 0;
for (Annotation[] annotationsPerParameter : parameterAnnotations) {
System.out.println(" Parameter of type " + parameterTypes[parameterIndex++].getName() + ":");
for (Annotation annotation : annotationsPerParameter)
System.out.println(" " + annotation);
}
}
}
更新:
好吧,在Java 8中,如果使用-parameters
選項編譯類,則可以確定參數名稱 。 如果您使用Java 8 JRE / JDK,則Eclipse中還有一個針對此的編譯器開關:
您還應該使用兼容Java 8的AspectJ版本,例如當前的1.8.5。 那么您的方面可以如下所示:
package de.scrum_master.aspect;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
@Aspect
public class MyAspect {
@Before("execution(!static * *..Application.*(..))")
public void myAdvice(JoinPoint joinPoint) throws Throwable {
System.out.println(joinPoint);
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Parameter[] parameters = method.getParameters();
System.out.println("Method annotations:");
Annotation[] methodAnnotations = method.getAnnotations();
for (Annotation annotation : methodAnnotations)
System.out.println(" " + annotation);
System.out.println("Parameter annotations:");
for (Parameter parameter : parameters) {
System.out.println(" " + parameter.getType().getName() + " " + parameter.getName());
for (Annotation annotation : parameter.getAnnotations())
System.out.println(" " + annotation);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.