javax.lang.model Implementation Backed by Core Reflection
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的更多相关文章
- springmvc java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonFactory.requiresPropertyOrdering()Z
在hibernate spring springMVC整合的时候出现下面的情况: WARNING: Exception encountered during context initializatio ...
- 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 ...
- java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException
我从0手动搭建框架,启动tomcat,遇到这个错:java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingEx ...
- java.lang.ClassNotFoundException: org.hibernate.annotations.common.reflection.MetadataProvider
Caused by: java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/MetadataProvi ...
- java.lang.NoSuchMethodError: net.sf.cglib.core.Signature
今天二次开发Dubbo-admin的管理平台,开启tomcat直接报错,错误关键字为“ java.lang.NoSuchMethodError: net.sf.cglib.core.Signature ...
- spring boot 启动报 java.lang.NoClassDefFoundError: ch/qos/logback/core/spi/LifeCycle 错误
Failed to instantiate SLF4J LoggerFactory Reported exception: java.lang.NoClassDefFoundError: ch/qos ...
- 使用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 ...
- java.lang.NoClassDefFoundError: com.nostra13.universalimageloader.core.DisplayImageOptions$Builder
今天在使用Universal-image-loader开源插件的时候,一直出现了这么个错误.原因是在ADT22版本中导入jar包的方式不对. 正确的导入jar包方式: 在adt17的版本之后,导入第三 ...
- java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/ClassLoadingException
下载高版本的: hibernate-commons-annotations-5.0.1.Final.jar 低版本缺包
随机推荐
- spring入门常见的问题及解决办法
在学习spring过程中遇见了种种不同的异常错误,这里做了一下总结,希望遇见类似错误的同学们共勉一下. 1. 错误一 Error creating bean with name 'helloServi ...
- Ubuntu16.04安装xgboost
1.Python下安装方法 git clone --recursive https://github.com/dmlc/xgboost cd xgboost make -j4 cd python-pa ...
- Java中浮点数的处理
import java.text.DecimalFormat; String addGold = String.valueOf(new DecimalFormat("0").for ...
- 【python】并查集
转自:http://blog.csdn.net/rav009/article/details/12781899 # -*- coding: UTF-8 -*- class unionfind: def ...
- 解决在html中引入font-awesome的css文件后, 图标显示不出来
今天小颖在做项目时,需要在html文件中引入font-awesome.min.css,但是引入后: 以前小颖在用font-awesome库里的图标时,都是直接从node中下包,然后在main.js中引 ...
- QT textBrowser 使用
使用环境: VS2010 & QT Designer5 1. 在QT Designer 拖入 textBrowser,点击右键属性获得 name值 2. 在程序使用的地方 加入 ui.te ...
- vagrant up connection time out
vagrant up connection time out 在一台重装系统的机子上装vagrant后 vagrant up 无法启系统 D:\work\vagrant>vagrant up B ...
- 剖析Elasticsearch集群系列之三:近实时搜索、深层分页问题和搜索相关性权衡之道
转载:http://www.infoq.com/cn/articles/anatomy-of-an-elasticsearch-cluster-part03 近实时搜索 虽然Elasticsearch ...
- js 使用a标签 下载资源
文档 let data = new Blob(['hello ajanuw'], { type: 'application/text' }) let src = window.URL.createOb ...
- 怎样将M4A音频格式转换成MP3格式
因为MP3音频格式应用的广泛性,所以很多时候我们都需要将不同的音频格式转换成MP3格式的,那么如果我们需要将M4A音频格式转换成MP3格式,我们应该怎样进行实现呢?下面我们就一起来看一下吧. 操作步骤 ...