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. Ruby学习小记

    ruby安装 方法一:使用apt-get安装 可以直接使用两个命令完成Ruby的安装. # sudo apt-get update # sudo apt-get install ruby 或者 # s ...

  2. 0x800f0845 更新1803报错

    Windows 10累积更新KB4056892可能并不兼容AMD处理器,采用AMD Athlon 64 X2处理器的设备至少存在两起报告.

  3. ABBYY FineReader Pro for Mac有哪些特性(下)

    使用ABBYY FineReader Pro for Mac轻松转换纸质文档.PDF文件和数字文本照片为可编辑和可搜索的文件,再也不需要手动重新输入或格式化了,相反,可以编辑.搜索.共享.归档和复制文 ...

  4. android bionic c 对比 gnu c

    Bionic 是一个BSD标准的C库,用在android平台上面的. Android 是一个不完全开源的系统. android的kernel使用的是基于linux的,linux使用的是GPL2的开源标 ...

  5. 《精通Python网络爬虫》

    抓包工具 Fiddler 爬虫的浏览器伪装技术 Python Scrapy 框架

  6. HandlerSocket介绍

    HandlerSocket的原理 HandlerSocket的应用场景: MySQL自身的局限性,很多站点都采用了MySQL+Memcached的经典架构,甚至一些网站放弃MySQL而采用NoSQL产 ...

  7. js的with语句,和debugger语句

    减少操作,速度及慢, 严格模式无法使用 debugger 在程序中打断点 'use strict' var name = 'global'; var obj = { name: "ajanu ...

  8. node_readline (逐行读取)

    node官方文档 用于逐行读取文件流, 和终端交互 读取文件流 const fs = require('fs'); const readline = require('readline'); var ...

  9. ThinkPHP框架 自定义 Empty 方法保护本地信息不被暴露!!!

    在使用ThinkPHP框架开发过程中,在每个Controller文件夹里面都要设置一个空控制器,用来保护本地信息不被泄露(EmptyController.class.php) 此方法很有效的隐藏系统内 ...

  10. VBS数组导出到Excel

    <script language="vbscript"> dim arr(9999,4) for i=0 to 9999 for j = 0 to 4 arr(i,j) ...