首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java 8:类到AnnotatedType

Java 8:类到AnnotatedType
EN

Stack Overflow用户
提问于 2013-12-03 23:59:18
回答 2查看 3.2K关注 0票数 15

在Java8中,Class似乎获得了获取其超类及其超级接口的AnnotatedType视图的方法。

如何将Class转换为自己的AnnotatedType?这个问题有意义吗?

据我所知,AnnotatedType有-- Type而不是is-- Type。不过,这是一个AnnotatedElement;一切都很混乱。

到目前为止,我已经搜索了Javadocs,但没有结果。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-04 02:42:42

因此,我终于对AnnotatedType接口有了一个可接受的理解。下面是一个实用的Java 8示例,以说明它的一种用法

代码语言:javascript
复制
public static void main(String[] args) {
    Class<?> fooClass = Foo.class;
    AnnotatedType type = fooClass.getAnnotatedSuperclass();
    System.out.println(type);
    System.out.println(Bar.class == type.getType());
    System.out.println(Arrays.toString(type.getAnnotations()));
    System.out.println(Arrays.toString(type.getDeclaredAnnotations()));
}

public static class Bar {
}

public static class Foo extends @Custom Bar {
}

// So that annotation metadata is available at run time
@Retention(RetentionPolicy.RUNTIME)
// TYPE_USE being the important one
@Target(value = {ANNOTATION_TYPE, CONSTRUCTOR, FIELD, LOCAL_VARIABLE,
        METHOD, PACKAGE, PARAMETER, TYPE, TYPE_PARAMETER, TYPE_USE}) 
public @interface Custom {
}

这个指纹

代码语言:javascript
复制
sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@1d44bcfa
true
[@com.testing.Test$Custom()]
[@com.testing.Test$Custom()]

AnnotatedType接口状态

AnnotatedType表示在当前运行于此VM中的程序中,可能使用注释的类型。

Class#getAnnotatedSuperclass() javadoc状态

返回一个AnnotatedType对象,该对象表示使用类型指定由此Class对象表示的实体的超类。

我在AnnotatedType javadoc中使用了可能的粗体,因为它清楚地表明类型用法不需要注释。如果你有

代码语言:javascript
复制
public static class Bar {}
...
Bar.class.getAnnotatedSuperclass(); // returns Class instance for java.lang.Object

这是一个在Java7和更低版本中不可能的用例,因为您不能注释类型使用(see some examples here)。但是,在Java 8中,您可以

代码语言:javascript
复制
public static class Foo extends @Custom Bar {

其中,Bar类型作为超类使用,并使用@Custom对其使用进行注释。因此,它是一个AnnotatedType。因此,Foo.class.getAnnotatedSuperClass()将为该用法返回一个AnnotatedType实例。

如何将Class转换为自己的AnnotatedType?这个问题有意义吗?

这个问题没有道理。这是因为Class对象保存关于类的自包含元数据。所谓自包含,我指的是从类的.class文件(或实际声明)中可以推断出的所有内容。您不能在任何其他地方推断该类型的任何用法,因此它本身不能转换为任何AnnotatedType

你可以

代码语言:javascript
复制
public static class Foo extends @Custom Bar {}

public static class Zoom extends @Custom Bar {}

public static class Doing extends @Custom Bar {}

上面的每一种Bar用法都有一个Bar实例,但是您会选择哪一个将条形Class转换为自己的AnnotatedType

票数 11
EN

Stack Overflow用户

发布于 2013-12-04 02:45:33

下面是一个简单的示例,展示了getAnnotatedSuperclass()的用法

代码语言:javascript
复制
import java.lang.annotation.*;

public class AnnotationTest {

    @Target(ElementType.TYPE_USE)
    @Retention(RetentionPolicy.RUNTIME)
    @interface First { }

    @Target(ElementType.TYPE_USE)
    @Retention(RetentionPolicy.RUNTIME)
    @interface Second { }

    class A { }

    class B extends @First @Second A { }

    public static void main(String[] args) {
        Annotation[] anns = B.class.getAnnotatedSuperclass().getAnnotations();
        System.out.printf("There are %d annotations on B's use of its superclass.%n", anns.length);
        for (Annotation a : anns)
            System.out.println(a.annotationType().getName());
    }
}

该程序的输出是:

代码语言:javascript
复制
There are 2 annotations on B's use of its superclass.
AnnotationTest$First
AnnotationTest$Second

注意在中出现的注释的区别是使用B的超类的(即A),而不是A本身的声明

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20364236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档