<legend id='KaIhH'><style id='KaIhH'><dir id='KaIhH'><q id='KaIhH'></q></dir></style></legend>
  1. <i id='KaIhH'><tr id='KaIhH'><dt id='KaIhH'><q id='KaIhH'><span id='KaIhH'><b id='KaIhH'><form id='KaIhH'><ins id='KaIhH'></ins><ul id='KaIhH'></ul><sub id='KaIhH'></sub></form><legend id='KaIhH'></legend><bdo id='KaIhH'><pre id='KaIhH'><center id='KaIhH'></center></pre></bdo></b><th id='KaIhH'></th></span></q></dt></tr></i><div id='KaIhH'><tfoot id='KaIhH'></tfoot><dl id='KaIhH'><fieldset id='KaIhH'></fieldset></dl></div>
    <tfoot id='KaIhH'></tfoot>

    <small id='KaIhH'></small><noframes id='KaIhH'>

      • <bdo id='KaIhH'></bdo><ul id='KaIhH'></ul>

      类型参数上的注释是否可以在运行时访问?

      时间:2023-07-26

    1. <i id='6UR4t'><tr id='6UR4t'><dt id='6UR4t'><q id='6UR4t'><span id='6UR4t'><b id='6UR4t'><form id='6UR4t'><ins id='6UR4t'></ins><ul id='6UR4t'></ul><sub id='6UR4t'></sub></form><legend id='6UR4t'></legend><bdo id='6UR4t'><pre id='6UR4t'><center id='6UR4t'></center></pre></bdo></b><th id='6UR4t'></th></span></q></dt></tr></i><div id='6UR4t'><tfoot id='6UR4t'></tfoot><dl id='6UR4t'><fieldset id='6UR4t'></fieldset></dl></div>
      • <bdo id='6UR4t'></bdo><ul id='6UR4t'></ul>

        <small id='6UR4t'></small><noframes id='6UR4t'>

            <tbody id='6UR4t'></tbody>

                <legend id='6UR4t'><style id='6UR4t'><dir id='6UR4t'><q id='6UR4t'></q></dir></style></legend><tfoot id='6UR4t'></tfoot>

              • 本文介绍了类型参数上的注释是否可以在运行时访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                Java 8 允许:

                public List<@NonNull String> names;
                

                但是有没有办法在运行时访问这个注解,还是只对编译器插件可用?

                But is there a way to access this annotation in runtime or is it available only to compiler plugins?

                有新的 Method#getAnnotatedReturnType 提供对返回类型注释的访问,所以我希望 ParameterizedType 现在会有类似 getActualAnnotatedTypeArguments 的东西这对泛型类型参数会做同样的事情,但它不存在......

                There's new Method#getAnnotatedReturnType that provides access to annotations on the return type, so I was hoping ParameterizedType would now have something like getActualAnnotatedTypeArguments that would do the same for generic type arguments, but it doesn't exist...

                推荐答案

                新的 API 延续了需要大量 instanceof 和类型转换的传统:

                The new API continues the tradition of requiring lots of instanceofs and type casts:

                import java.lang.annotation.*;
                import java.lang.reflect.*;
                import java.util.*;
                import java.util.stream.*;
                
                public class AnnoTest {
                    @Retention(RetentionPolicy.RUNTIME)
                    @Target(ElementType.TYPE_USE)
                    @interface NonNull {}
                
                    @Retention(RetentionPolicy.RUNTIME)
                    @Target(ElementType.TYPE_USE)
                    @interface NonEmpty {}
                
                    List<@NonNull String> list;
                    Map<@NonNull Integer, @NonNull @NonEmpty Set<String>> map;
                    Object plain;
                
                    public static void main(String[] args) throws ReflectiveOperationException {
                        for(Field field: AnnoTest.class.getDeclaredFields()) {
                            AnnotatedType at = field.getAnnotatedType();
                            System.out.println(formatType(at)+" "+field.getName());
                        }
                    }
                    static CharSequence formatType(AnnotatedType type) {
                        StringBuilder sb=new StringBuilder();
                        for(Annotation a: type.getAnnotations()) sb.append(a).append(' ');
                        if(type instanceof AnnotatedParameterizedType) {
                            AnnotatedParameterizedType apt=(AnnotatedParameterizedType)type;
                            sb.append(((ParameterizedType)type.getType()).getRawType().getTypeName());
                            sb.append(Stream.of(apt.getAnnotatedActualTypeArguments())
                                .map(AnnoTest::formatType).collect(Collectors.joining(",", "<", ">")));
                        }
                        else sb.append(type.getType().getTypeName());
                        return sb;
                    }
                }
                

                另请参阅此答案的末尾以获取处理类型变量、通配符类型和数组等其他场景的示例.

                See also the end of this answer for an example handling the other scenarios like type variables, wild card types and arrays.

                这篇关于类型参数上的注释是否可以在运行时访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:未调用跨域验证的自定义类级别约束 下一篇:自定义注释作为方法记录的拦截器

                相关文章

                最新文章

              • <i id='YEE0L'><tr id='YEE0L'><dt id='YEE0L'><q id='YEE0L'><span id='YEE0L'><b id='YEE0L'><form id='YEE0L'><ins id='YEE0L'></ins><ul id='YEE0L'></ul><sub id='YEE0L'></sub></form><legend id='YEE0L'></legend><bdo id='YEE0L'><pre id='YEE0L'><center id='YEE0L'></center></pre></bdo></b><th id='YEE0L'></th></span></q></dt></tr></i><div id='YEE0L'><tfoot id='YEE0L'></tfoot><dl id='YEE0L'><fieldset id='YEE0L'></fieldset></dl></div>
                • <bdo id='YEE0L'></bdo><ul id='YEE0L'></ul>

                1. <small id='YEE0L'></small><noframes id='YEE0L'>

                2. <tfoot id='YEE0L'></tfoot><legend id='YEE0L'><style id='YEE0L'><dir id='YEE0L'><q id='YEE0L'></q></dir></style></legend>