<legend id='ymxdI'><style id='ymxdI'><dir id='ymxdI'><q id='ymxdI'></q></dir></style></legend>

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

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

    1. <tfoot id='ymxdI'></tfoot>

      • <bdo id='ymxdI'></bdo><ul id='ymxdI'></ul>
    2. Java 寻找具有特定注解的方法及其注解元素

      时间:2023-07-26

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

          <tfoot id='zdh55'></tfoot>

                  <tbody id='zdh55'></tbody>
                <legend id='zdh55'><style id='zdh55'><dir id='zdh55'><q id='zdh55'></q></dir></style></legend>

                本文介绍了Java 寻找具有特定注解的方法及其注解元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                Suppose I have this annotation class

                
                @Retention(RetentionPolicy.RUNTIME)
                @Target(ElementType.METHOD)
                public @interface MethodXY {
                    public int x();
                    public int y();
                }
                
                public class AnnotationTest {
                    @MethodXY(x=5, y=5)
                    public void myMethodA(){ ... }
                
                    @MethodXY(x=3, y=2)
                    public void myMethodB(){ ... }
                }
                

                So is there a way to look into an object, "seek" out the method with the @MethodXY annotation, where its element x = 3, y = 2, and invoke it?

                Thanks

                解决方案

                Here is a method, which returns methods with specific annotations:

                public static List<Method> getMethodsAnnotatedWith(final Class<?> type, final Class<? extends Annotation> annotation) {
                    final List<Method> methods = new ArrayList<Method>();
                    Class<?> klass = type;
                    while (klass != Object.class) { // need to iterated thought hierarchy in order to retrieve methods from above the current instance
                        // iterate though the list of methods declared in the class represented by klass variable, and add those annotated with the specified annotation
                        for (final Method method : klass.getDeclaredMethods()) {
                            if (method.isAnnotationPresent(annotation)) {
                                Annotation annotInstance = method.getAnnotation(annotation);
                                // TODO process annotInstance
                                methods.add(method);
                            }
                        }
                        // move to the upper class in the hierarchy in search for more methods
                        klass = klass.getSuperclass();
                    }
                    return methods;
                }
                

                It can be easily modified to your specific needs. Pls note that the provided method traverses class hierarchy in order to find methods with required annotations.

                Here is a method for your specific needs:

                public static List<Method> getMethodsAnnotatedWithMethodXY(final Class<?> type) {
                    final List<Method> methods = new ArrayList<Method>();
                    Class<?> klass = type;
                    while (klass != Object.class) { // need to iterated thought hierarchy in order to retrieve methods from above the current instance
                        // iterate though the list of methods declared in the class represented by klass variable, and add those annotated with the specified annotation
                        for (final Method method : klass.getDeclaredMethods()) {
                            if (method.isAnnotationPresent(MethodXY.class)) {
                                MethodXY annotInstance = method.getAnnotation(MethodXY.class);
                                if (annotInstance.x() == 3 && annotInstance.y() == 2) {         
                                    methods.add(method);
                                }
                            }
                        }
                        // move to the upper class in the hierarchy in search for more methods
                        klass = klass.getSuperclass();
                    }
                    return methods;
                }
                

                For invocation of the found method(s) pls refer a tutorial. One of the potential difficulties here is the number of method arguments, which could vary between found methods and thus requiring some additional processing.

                这篇关于Java 寻找具有特定注解的方法及其注解元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:javax.validation.constraints 中的注释不起作用 下一篇:使用 Enum 类型作为 @RolesAllowed-Annotation 的值参数

                相关文章

                最新文章

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

                <legend id='0s2e9'><style id='0s2e9'><dir id='0s2e9'><q id='0s2e9'></q></dir></style></legend>
                <tfoot id='0s2e9'></tfoot>
                  • <bdo id='0s2e9'></bdo><ul id='0s2e9'></ul>
                  1. <small id='0s2e9'></small><noframes id='0s2e9'>