Java 如果存在参数注释,则获取参数值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18129300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:35:08  来源:igfitidea点击:

Get parameter value if parameter annotation exists

javareflectionannotationsejbinterceptor

提问by user2664820

Is it possible to get the value of a parameter if an annotation is present on that parameter?

如果该参数上存在注释,是否可以获取该参数的值?

Given EJB with parameter-level annotations:

给定带有参数级注释的 EJB:

public void fooBar(@Foo String a, String b, @Foo String c) {...}

And an interceptor:

还有一个拦截器:

@AroundInvoke
public Object doIntercept(InvocationContext context) throws Exception {
    // Get value of parameters that have annotation @Foo
}

采纳答案by Sotirios Delimanolis

In your doIntercept()you can retrieve the method being called from the InvocationContextand get the parameter annotations.

在您中,doIntercept()您可以从 检索被调用的方法InvocationContext并获取参数注释

Method method = context.getMethod();
Annotation[][] annotations = method.getParameterAnnotations();
// iterate through annotations and check 
Object[] parameterValues = context.getParameters();

// check if annotation exists at each index
if (annotation[0].length > 0 /* and if the annotation is the type you want */ ) 
    // get the value of the parameter
    System.out.println(parameterValues[0]);

Because the Annotation[][]returns an empty 2nd dimension array if there are no Annotations, you know which parameter positions have the annotations. You can then call InvocationContext#getParameters()to get a Object[]with the values of all the parameters passed. The size of this array and the Annotation[][]will be the same. Just return the value of the indices where there are no annotations.

因为Annotation[][]如果没有注释,则返回一个空的第二维数组,所以您知道哪些参数位置有注释。然后您可以调用InvocationContext#getParameters()获取Object[]所有传递参数的值。这个数组的大小和Annotation[][]将是一样的。只需返回没有注释的索引值。

回答by Evgeniy Dorofeev

You can try something like this

你可以试试这样的

    Method m = context.getMethod();
    Object[] params = context.getParameters();
    Annotation[][] a = m.getParameterAnnotations();
    for(int i = 0; i < a.length; i++) {
        if (a[i].length > 0) {
            // this param has annotation(s)
        }
    }

回答by franlisa

You can try something like this,I define a Param annotation named MyAnnotation and I get the Param annotation in this way. It works.

你可以尝试这样的事情,我定义了一个名为 MyAnnotation 的 Param 注释,我以这种方式获得了 Param 注释。有用。

Annotation[][] parameterAnnotations = method.getParameterAnnotations();
Class[] parameterTypes = method.getParameterTypes();

int i=0;
for(Annotation[] annotations : parameterAnnotations){
  Class parameterType = parameterTypes[i++];

  for(Annotation annotation : annotations){
    if(annotation instanceof MyAnnotation){
        MyAnnotation myAnnotation = (MyAnnotation) annotation;
        System.out.println("param: " + parameterType.getName());
        System.out.println("value: " + myAnnotation.value());
    }
  }
}