Java8:注释数据类型的类
在Java8中,Java8:注释数据类型的类,java,java-8,Java,Java 8,在Java8中,Class似乎已经获得了获取其超类及其超接口的AnnotatedType视图的方法 如何将类转换为自己的注释类型?这个问题有意义吗 据我所知,AnnotatedType具有-aType,而不是is-aType。不过,这是一个注释删除;一切都很混乱 到目前为止,我已经搜索了Javadocs,但没有找到任何结果。因此我最终对AnnotatedType接口有了一个可接受的理解。下面是一个运行中的Java8示例来说明它的一个用途 public static void main(Strin
Class
似乎已经获得了获取其超类及其超接口的AnnotatedType
视图的方法
如何将类
转换为自己的注释类型
?这个问题有意义吗
据我所知,AnnotatedType
具有-aType
,而不是is-aType
。不过,这是一个注释删除
;一切都很混乱
到目前为止,我已经搜索了Javadocs,但没有找到任何结果。因此我最终对
AnnotatedType
接口有了一个可接受的理解。下面是一个运行中的Java8示例来说明它的一个用途
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 {
}
界面状态
AnnotatedType
表示在
当前在此VM中运行的程序
和javadoc状态
返回一个AnnotatedType
对象,该对象表示要使用的类型
指定此类
对象表示的实体的超类
我在AnnotatedType
javadoc中潜在地使用了粗体,因为它清楚地表明不必对类型用法进行注释。如果你有
public static class Bar {}
...
Bar.class.getAnnotatedSuperclass(); // returns Class instance for java.lang.Object
这是一个在Java7和更低版本中不可能实现的用例,因为您不能注释类型用法()。但是,在Java8中,您可以
public static class Foo extends @Custom Bar {
其中类型条
用作一个超类,其用法用@Custom
注释。因此,它是一个注释数据类型
。因此,Foo.class.getAnnotatedSuperClass()
将为该用法返回一个AnnotatedType
实例
如何将类
转换为自己的注释类型
?是吗
这个问题有意义吗
这个问题没有意义。这是因为类
对象包含关于类的自包含元数据。所谓自包含,我指的是可以从类“.class
文件(或实际声明)中推断出的所有内容。您无法在其他任何地方推断该类型的任何用法,因此无法将其自身转换为任何AnnotatedType
你可以
public static class Foo extends @Custom Bar {}
public static class Zoom extends @Custom Bar {}
public static class Doing extends @Custom Bar {}
对于上述使用的
Bar
都有一个AnnotatedType
实例,但是您会选择哪一个来将[Bar]类转换为自己的AnnotatedType
?下面是一个简单的示例,说明getAnnotatedSuperclass()
的用法:
该程序的输出为:
There are 2 annotations on B's use of its superclass.
AnnotationTest$First
AnnotationTest$Second
注意注释在B的超类(即A)的使用处与A本身的声明处发生的区别。+1个有趣的问题。我想我已经发现这是毫无意义的:类型的用法可以注释,但类型本身不能是。我仍在试图弄清楚类型的注释用法是什么,以及它如何应用于getAnnotatedSuperclass()
Gack!你比我先发布了一个这样的例子+1.:-)请注意,@Retention(RetentionPolicy.RUNTIME)也很重要。如果省略它,这个例子就行不通了。
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());
}
}
There are 2 annotations on B's use of its superclass.
AnnotationTest$First
AnnotationTest$Second