javax.lang.model Implementation Backed by Core Reflection

1.javax.lang.model: How do I get the type of a field?

TypeElement : Represents a class or interface program element. Provides access to information about the type and its members. Note that an enum type is a

kind of class and an annotation type is a kind of interface.

VariableElement: Represents a field, enum constant, method or constructor parameter, local variable, resource variable, or exception parameter.

In java.lang.reflect, one would do:

Field someField = ...;
Class<?> fieldType = someField.getType();

But what do I do with javax.lang.model's VariableElement (which may or may not represent a field)? A corresponding return value would be (I guess) TypeElement.

VariableElement someField = ...;
TypeElement fieldType = someField.???;

So, in javax.lang.model, how do I get the type (or TypeElement) of a field, represented by VariableElement?

public class SomeClass {
private final ProcessingEnvironment pe = /* get it somewhere */;
private final Types typeUtils = pe.getTypeUtils(); public TypeElement getExtracted(VariableElement ve) {
TypeMirror typeMirror = ve.asType();
Element element = typeUtils.asElement(typeMirror); // instanceof implies null-ckeck
return (element instanceof TypeElement)
? (TypeElement)element : null;
}
}

It seems that the class Types has to be got from current ProcessingEnvironment because some of its internals depend on it, so it's not a usual utility class.

二。process

            for (Element annotatedElement : roundEnv.getElementsAnnotatedWith(Factory.class)) {

                // Check if a class has been annotated with @Factory
/**
* 我们为什么不这样判断呢if (! (annotatedElement instanceof TypeElement) )?
* 这是错误的,因为接口(interface)类型也是TypeElement。
* 所以在注解处理器中,我们要避免使用instanceof,而是配合TypeMirror使用EmentKind或者TypeKind。
*/
if (annotatedElement.getKind() != ElementKind.CLASS) {
throw new ProcessingException(annotatedElement, "Only classes can be annotated with @%s",
Factory.class.getSimpleName());
} // We can cast it, because we know that it of ElementKind.CLASS
//因为我们已经知道它是ElementKind.CLASS类型,所以可以直接强制转换
TypeElement typeElement = (TypeElement) annotatedElement; //......
}

javax.lang.model Implementation Backed by Core Reflection的更多相关文章

  1. springmvc java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonFactory.requiresPropertyOrdering()Z

    在hibernate spring springMVC整合的时候出现下面的情况: WARNING: Exception encountered during context initializatio ...

  2. Caused by: java.lang.ClassNotFoundException: org.hibernate.annotations.common.reflection.MetadataPro

    1.错误描述 信息: MLog clients using java 1.4+ standard logging. 2014-7-12 19:29:20 com.mchange.v2.c3p0.C3P ...

  3. java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException

    我从0手动搭建框架,启动tomcat,遇到这个错:java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingEx ...

  4. java.lang.ClassNotFoundException: org.hibernate.annotations.common.reflection.MetadataProvider

    Caused by: java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/MetadataProvi ...

  5. java.lang.NoSuchMethodError: net.sf.cglib.core.Signature

    今天二次开发Dubbo-admin的管理平台,开启tomcat直接报错,错误关键字为“ java.lang.NoSuchMethodError: net.sf.cglib.core.Signature ...

  6. spring boot 启动报 java.lang.NoClassDefFoundError: ch/qos/logback/core/spi/LifeCycle 错误

    Failed to instantiate SLF4J LoggerFactory Reported exception: java.lang.NoClassDefFoundError: ch/qos ...

  7. 使用Grizzy+Jersey搭建一个RESTful框架()报错Exception in thread "main" java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder;

    报错的类涉及UriBuilder,我搜索类发现, 这个类存在于两个包中,我在baidu的时候,也有人提到是jar包冲突,我就删除了 这个依赖,问题解决了. 环境搭建过程请见地址https://blog ...

  8. java.lang.NoClassDefFoundError: com.nostra13.universalimageloader.core.DisplayImageOptions$Builder

    今天在使用Universal-image-loader开源插件的时候,一直出现了这么个错误.原因是在ADT22版本中导入jar包的方式不对. 正确的导入jar包方式: 在adt17的版本之后,导入第三 ...

  9. java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/ClassLoadingException

    下载高版本的: hibernate-commons-annotations-5.0.1.Final.jar 低版本缺包

随机推荐

  1. PDF Transformer+与Transformer3.0功能对比

    ABBYY PDF Transformer+是一个新的.全面的巧妙解决PDF文档的工具,它将泰比的光学字符识别(OCR)技术和Adobe®PDF技术完美结合,以确保实现便捷地处理任何类型的PDF文件, ...

  2. ios 消除 字符串 首尾空格

    本文转载至 http://blog.csdn.net/reylen/article/details/8233353 (1)系统去首尾空格方法,使用NSString中的str = [str string ...

  3. ImportError: libmysqlclient_r.so.16: cannot open shared object file: No such file or directory

    在开发一个python项目是,需要用到mysql,但是, 安装完mysql-python后import加载模块提示以下错误: ImportError: libmysqlclient_r.so.16: ...

  4. sencha touch Model validations 自定义验证 二选一输入验证、重复验证、时间验证、比较验证、条件验证(2015-1-14)

    项目初始化时执行以下代码 //重写模型,方便进行自定义验证 Ext.define("Ext.zh.data.Model", { override: "Ext.data.M ...

  5. [原]jenkins(五)---jenkins添加项目

    /** * lihaibo * 文章内容都是根据自己工作情况实践得出. http://www.cnblogs.com/horizonli/p/5332258.html 版权声明:本博客欢迎转发,但请保 ...

  6. hadoop源码学习(-)

    这是从hadoop源码中间截取的一个片段,你看看你能不能看懂:如果不能看懂,说明你的javase基础很差. private static class GroupFactory extends Coun ...

  7. Python处理数据

    由于找实习,要学习python处理数据,python连接mysql,python读写文件,python读写xlsx文件,这些只要引入了相关的包,就非常容易,处理过程非常清晰.模块如果封装的好,没怎么学 ...

  8. vscode 的 vue模板

    12 { "Print to console": { "prefix": "vue", "body": [ " ...

  9. Python数据结构———队列

    队列(Queue) 队列也是一系列有顺序的元素的集合,新元素的加入在队列的一端,叫做“队尾”(rear),已有元素的移除发生在队列的另一端,叫做“队首”(front),和栈不同的是,队列只能在队尾插入 ...

  10. Python数据结构———栈

    线性数据结构 当添加一个项目时,它就被放在这样一个位置:在之前存在的项与后来要加入的项之间.像这样的数据集合常被称为线性数据结构. 栈 栈是一个项的有序集合.添加项和移除项都发生在同一“端”,这一端通 ...