如何发现子类中的方法(参数)是否具有在实现的接口中定义的注释?

发布于 2024-12-05 05:20:59 字数 446 浏览 1 评论 0原文

不幸的是,注释继承似乎受到以下事实的严格限制:只能继承来自类(而不是接口)的类级注释。

考虑这段代码:

interface Foo {
    @A
    void bar(String str, @B int i);
}

class FooImpl implements Foo {
    void bar(String str, @B int i) { ... }
}

如果我有一个 FooImpl 的实例,是否可以发现该方法是否已用 A 注释(在类中(简单)或在实现中)界面)?

那么方法参数呢?是否有可能发现是否以及哪个参数已用 B 注释?

看来这对于 AspectJ 来说是不可能的,我需要使用 Java Reflection。

固体溶液会是什么样子?

Unfortunately it seems that annotation inheritance is severely restricted by the fact that only class-level annotations from classes (and not interfaces) can be inherited.

Consider this code:

interface Foo {
    @A
    void bar(String str, @B int i);
}

class FooImpl implements Foo {
    void bar(String str, @B int i) { ... }
}

If I have an instance of FooImpl is it possible to discover if the method has been annotated with A (either in the class (easy) or in the implemented interface)?

What about the method parameter? Is it possible to dicover if and which parameter has been annotated with B?

It seems that this is not possible with AspectJ and I need to use Java Reflection.

How would a solid solution look like?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隔纱相望 2024-12-12 05:20:59

可以在类对象上使用 getInterfaces() 并查询结果。

参数注释

package mawi12345;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface B {
    int version() default 0;
}

方法注释

package mawi12345;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
public @interface Revision {
    int minor() default 0;
    int major() default 1;
}

接口注释

package mawi12345;
public interface Foo {
    @Revision(minor=1, major=3)
    public void setMark(@B(version=3) int mark);
}

类注释 Foo接口

package mawi12345;
public class Test implements Foo {

    public void setMark(int mark) {

    }

    @Revision(minor=2, major=4)
    public boolean isPassed() {
        return true;
    }
}

测试类

package mawi12345;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class ReflectionTest {

    public static void printAnnotations(Class<?> clazz) {
        // array of methods
        Method[] methods = clazz.getDeclaredMethods();
        System.out.println("found "+methods.length+" methods");
        for (int i=0; i<methods.length; i++) {
            // get the annotations of this method
            Annotation[] methodAnnotations = methods[i].getAnnotations();
            // if you only wont to check for one annotation use getAnnotation(Class<T>)

            for (Annotation methodAnnotation : methodAnnotations) {
                System.out.println(methodAnnotation);
            }
            // get the parameter annotations (2d array) 
            Annotation[][] parameterAnnotations = methods[i].getParameterAnnotations();
            // get an array of parameters
            Class<?>[] parameters = methods[i].getParameterTypes();
            for(int x=0; x<parameterAnnotations.length; x++) {
                Class<?> parameter = parameters[x];
                for(Annotation annotation : parameterAnnotations[x]){
                    // print the parameter name and his annotation
                    System.out.println(parameter.getName() + " " + annotation);
                }
            }
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // create Test object
        Test test = new Test();
        // get the class
        Class<?> clazz = test.getClass();
        System.out.println("Class Test");
        // print annotations
        printAnnotations(clazz);
        System.out.println();

        // get the interfaces of the class
        Class<?>[] interfaces = clazz.getInterfaces();

        System.out.println("found "+interfaces.length+" interfaces");
        // print annotations for each interface
        for (Class<?> type : interfaces) {
            System.out.println(type);
            printAnnotations(type);
        }

    }

}

输出

Class Test
found 2 methods
@mawi12345.Revision(minor=2, major=4)

found 1 interfaces
interface mawi12345.Foo
found 1 methods
@mawi12345.Revision(minor=1, major=3)
int @mawi12345.B(version=3)

Its possible use getInterfaces() on the class object and query the result.

parameter annotation

package mawi12345;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface B {
    int version() default 0;
}

method annotation

package mawi12345;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
public @interface Revision {
    int minor() default 0;
    int major() default 1;
}

interface with annotations

package mawi12345;
public interface Foo {
    @Revision(minor=1, major=3)
    public void setMark(@B(version=3) int mark);
}

class with annotations and the Foo interface

package mawi12345;
public class Test implements Foo {

    public void setMark(int mark) {

    }

    @Revision(minor=2, major=4)
    public boolean isPassed() {
        return true;
    }
}

Test Class

package mawi12345;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class ReflectionTest {

    public static void printAnnotations(Class<?> clazz) {
        // array of methods
        Method[] methods = clazz.getDeclaredMethods();
        System.out.println("found "+methods.length+" methods");
        for (int i=0; i<methods.length; i++) {
            // get the annotations of this method
            Annotation[] methodAnnotations = methods[i].getAnnotations();
            // if you only wont to check for one annotation use getAnnotation(Class<T>)

            for (Annotation methodAnnotation : methodAnnotations) {
                System.out.println(methodAnnotation);
            }
            // get the parameter annotations (2d array) 
            Annotation[][] parameterAnnotations = methods[i].getParameterAnnotations();
            // get an array of parameters
            Class<?>[] parameters = methods[i].getParameterTypes();
            for(int x=0; x<parameterAnnotations.length; x++) {
                Class<?> parameter = parameters[x];
                for(Annotation annotation : parameterAnnotations[x]){
                    // print the parameter name and his annotation
                    System.out.println(parameter.getName() + " " + annotation);
                }
            }
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // create Test object
        Test test = new Test();
        // get the class
        Class<?> clazz = test.getClass();
        System.out.println("Class Test");
        // print annotations
        printAnnotations(clazz);
        System.out.println();

        // get the interfaces of the class
        Class<?>[] interfaces = clazz.getInterfaces();

        System.out.println("found "+interfaces.length+" interfaces");
        // print annotations for each interface
        for (Class<?> type : interfaces) {
            System.out.println(type);
            printAnnotations(type);
        }

    }

}

Output

Class Test
found 2 methods
@mawi12345.Revision(minor=2, major=4)

found 1 interfaces
interface mawi12345.Foo
found 1 methods
@mawi12345.Revision(minor=1, major=3)
int @mawi12345.B(version=3)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文