如何在java中获取参数的注释? [英] how to get parameter's annotation in java?

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

问题描述

我是一名新的 Java 程序员.以下是我的代码:

I'm a new Java programmer. Following is my code:

    public void testSimple1(String lotteryName,
                        int useFrequence,
                        Date validityBegin,
                        Date validityEnd,
                        LotteryPasswdEnum lotteryPasswd,
                        LotteryExamineEnum lotteryExamine,
                        LotteryCarriageEnum lotteryCarriage,
                        @TestMapping(key = "id", csvFile = "lottyScope.csv") xxxxxxxx lotteryScope,
                        @TestMapping(key = "id", csvFile = "lotteryUseCondition.csv") xxxxxxxx lotteryUseCondition,
                        @TestMapping(key = "id", csvFile = "lotteryFee.csv") xxxxxxxx lotteryFee)

我想获取所有已归档的注释.有些字段有注释,有些没有.

I want to get all filed's annotations. Some fields are annotated and some ain't.

我知道如何使用 method.getParameterAnnotations() 函数,但它只返回三个注释.

I know how to use method.getParameterAnnotations() function, but it just returns three annotations.

我不知道如何对应它们.

I don't know how to correspond them.

我希望得到以下结果:

lotteryName - none
useFrequence- none
validityBegin -none
validityEnd -none
lotteryPasswd -none
lotteryExamine-none
lotteryCarriage-none
lotteryScope - @TestMapping(key = "id", csvFile = "lottyScope.csv")
lotteryUseCondition - @TestMapping(key = "id", csvFile = "lotteryUseCondition.csv")
lotteryFee - @TestMapping(key = "id", csvFile = "lotteryFee.csv")

推荐答案

getParameterAnnotations 为每个参数返回一个数组,对于没有任何注释的任何参数使用一个空数组.例如:

getParameterAnnotations returns one array per parameter, using an empty array for any parameter which doesn't have any annotations. For example:

import java.lang.annotation.*;
import java.lang.reflect.*;

@Retention(RetentionPolicy.RUNTIME)
@interface TestMapping {
}

public class Test {

    public void testMethod(String noAnnotation,
        @TestMapping String withAnnotation)
    {
    }

    public static void main(String[] args) throws Exception {
        Method method = Test.class.getDeclaredMethod
            ("testMethod", String.class, String.class);
        Annotation[][] annotations = method.getParameterAnnotations();
        for (Annotation[] ann : annotations) {
            System.out.printf("%d annotatations", ann.length);
            System.out.println();
        }
    }
}

这给出了输出:

0 annotatations
1 annotatations

那说明第一个参数没有注解,第二个参数有一个注解.(当然,注释本身将在第二个数组中.)

That shows that the first parameter has no annotations, and the second parameter has one annotation. (The annotation itself would be in the second array, of course.)

这看起来正是您想要的,所以我对您声称 getParameterAnnotations 仅返回 3 个注释" - 它将返回一个数组数组感到困惑.也许您以某种方式压平了返回的数组?

That looks like exactly what you want, so I'm confused by your claim that getParameterAnnotations "only returns 3 annotations" - it will return an array of arrays. Perhaps you're somehow flattening the returned array?

这篇关于如何在java中获取参数的注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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