什么是背后的概念:类型 - 元 - 镜子 [英] What's the conception behind: Type - Element - Mirror

查看:192
本文介绍了什么是背后的概念:类型 - 元 - 镜子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与Java 6的注解处理,即什么可以在 javax.annotation.processing中找到工作(而不是Java 5的APT)。

I'm working with Java 6's annotation processing, i.e. what can be found within javax.annotation.processing (not Java 5's APT).

我不知道不同的元素键入镜之间的概念性差异类是。正如我真的不明白这一点,就很难有效地编写注释处理器。有迹象表明,这些概念之间的转换的各种方法,但我真的不知道,使用的时候我在做什么。

I wonder what the conceptional difference between the various Element, Type, and Mirror classes is. As I don't really understand this, it's hard to efficiently program an annotation processor. There are various methods that 'convert' between these notions but I'm not really sure what I'm doing when using them.

因此​​,举例来说,让我有一个实例 AnnotationMirror 。结果
当我打电话 getAnnotationType()我得到的一个实例的declaredType (实现 TypeMirror的无论何种原因)。结果
然后,我可以叫 asElement()就这一个,并获得元素的一个实例。结果
发生什么事了?

So, for example, let me have an instance of AnnotationMirror.
When I call getAnnotationType() I get an instance of DeclaredType (which implements TypeMirror for whatever reason).
Then I can call asElement() on this one and obtain an instance of Element.
What has happened?

推荐答案

类型的对象 javax.lang.model.element.AnnotationMirror 重新presents注解在code。

The object of type javax.lang.model.element.AnnotationMirror represents an annotation in your code.

声明类型重新presents注释类。

The declared type represents the annotation class.

它的元素是一般类(见的 http://java.sun.com/javase/6/docs/api/javax/lang/model/element/TypeElement.html 就此事的更多信息)。元素可能是一个类的通用版本,如列表,其中作为申报类型是参数化的版本,例如列表<弦乐> 。不过,我不知道它可能有注解类使用泛型从而区分可能是在这方面无关。

Its element is the generic class (see http://java.sun.com/javase/6/docs/api/javax/lang/model/element/TypeElement.html for more information on that matter). The element might be the generic version of a class, like List, where as the declared type is the parametrized version, for instance List<String>. However I'm not sure it is possible to have annotations classes use generics and thus the distinction might be irrelevant in that context.

比如可以说你有以下JUnit4方式:

For instance lets say you have the following JUnit4 method:

@Test(expected = MyException.class)
public void myTest() {  
     // do some tests on some class...
}

该AnnotationMirror重新presents @Test(预期= NullPointerException.class)。声明的类型是 org.junit.Test 类。因为没有参与仿制元件或多或少相同。

The AnnotationMirror represents @Test(expected = NullPointerException.class). The declared type is the org.junit.Test class. The element is more or less the same as there are no generics involved.

这篇关于什么是背后的概念:类型 - 元 - 镜子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆