Java - 如何在单个参数中获取多个注释? [英] Java - How to get multiple annotations in a single parameter?

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

问题描述

如果我有这样的方法:

   testLink(@LinkLength(min=0, max=5) List<@LinkRange(min=5, max=9) Integer> link) { ... }

如何同时获得@LinkLength 和@LinkRange 注释?

How do I get both @LinkLength and @LinkRange annotation?

推荐答案

我假设您想反射性地访问这些注释.举个例子:

I'm assuming you want to reflectively access these annotations. Here's an example:

package com.example;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedParameterizedType;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.List;

public class Main {

  @Retention(RetentionPolicy.RUNTIME)
  @Target({ElementType.PARAMETER, ElementType.TYPE_USE})
  public @interface Foo {

    String value();
  }

  public static void bar(@Foo("parameter") List<@Foo("type_use") Integer> list) {}

  public static void main(String[] args) throws NoSuchMethodException {
    Method method = Main.class.getMethod("bar", List.class);

    // get annotation from parameter
    Parameter parameter = method.getParameters()[0];
    System.out.println("PARAMETER ANNOTATION = " + parameter.getAnnotation(Foo.class));

    // get annotation from type argument used in parameter
    AnnotatedParameterizedType annotatedType =
        (AnnotatedParameterizedType) parameter.getAnnotatedType();
    AnnotatedType typeArgument = annotatedType.getAnnotatedActualTypeArguments()[0];
    System.out.println("TYPE_USE ANNOTATION  = " + typeArgument.getAnnotation(Foo.class));
  }
}

输出以下内容:

PARAMETER ANNOTATION = @com.example.Main$Foo(value="parameter")
TYPE_USE ANNOTATION  = @com.example.Main$Foo(value="type_use")

以下是使用的方法:

Executable#getParamaeters()

  • Method 继承自 Executable.
  • Method extends from Executable.

Parameter#getAnnotatedType()

AnnotatedParameterizedType#getAnnotatedActualTypeArguments()

AnnotatedElement#getAnnotation(Class)

  • MethodParameterAnnotatedType(以及其他类型)都继承了 AnnotatedElement.
  • Method, Parameter, and AnnotatedType (and other types) all inherit AnnotatedElement.

上面的例子充分利用了 bar 方法的知识.换句话说,我知道有一个参数,我知道 Parameter#getAnnotatedType() 会返回 AnnotatedParameterizedType,并且我知道参数化类型有一个类型参数.在反射扫描任意类时,不会提前知道此信息,这意味着您必须添加适当的检查并仅在适当时执行某些操作.例如:

The above example makes use of perfect knowledge about the bar method. In other words, I knew there was one parameter, I knew that Parameter#getAnnotatedType() would return AnnotatedParameterizedType, and I knew that the parameterized type had one type argument. This information won't be known ahead-of-time when reflectively scanning an arbitrary class, meaning you'll have to add appropriate checks and only execute certain actions when appropriate. For example:

AnnotatedType type = parameter.getAnnotatedType();
if (type instanceof AnnotatedParameterizedType) {
  // do something...
}

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

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