使用java反射在scala中获取带有特定注释的方法参数 [英] Get method parameters with specific annotation in aspect in scala using java reflection

查看:36
本文介绍了使用java反射在scala中获取带有特定注释的方法参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 aspectjscala 中使用 aop.我有一个方法

I am using aop in scala using aspectj. I have a method

def delete(@Id id:Long, name:String)

如何在我的方面文件中获取 id 的值.

How can I get the value of id in my aspect file.

@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 并且现在能够获得注释.但是我怎样才能得到用那个注释注释的参数呢?

Now I am getting the size correctly. The method was in object. But I think there are some problems with java reflection to read object. I changed to class and now able to get the annotations. But how can I get the parameter which is annotated with that annotation?

推荐答案

以下是一些 Java 示例代码,展示了如何获取方法注释以及参数注释以及参数类型(参数名称不可用 cchantep 已经提到).我想你可以很容易地自己把它转换成 Scala.

Here is some Java sample code showing how to get method annotations and also parameter annotations along with paramater types (parameter names are unavailable as cchantep already mentioned). I guess you can easily convert it into Scala by yourself.

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 中还有一个编译器开关:

Well, in Java 8 there is a way to determine parameter names if you compile your classes with the -parameters option. There is also a compiler switch in Eclipse for that if you use a Java 8 JRE/JDK:

您还应该使用符合 Java 8 的 AspectJ 版本,例如当前的 1.8.5.那么您的方面可以如下所示:

You should also use a Java 8-compliant AspectJ version such as the current 1.8.5. Then your aspect can look as follows:

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);
        }
    }
}

这篇关于使用java反射在scala中获取带有特定注释的方法参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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