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. [Installing Metasploit Framework on CentOS_RHEL 6]在CentOS_RHEL 6上安装Metasploit的框架【翻译】

    [Installing Metasploit Framework on CentOS_RHEL 6]在CentOS_RHEL 6上安装Metasploit的框架[翻译] 标记声明:蓝色汉子为翻译上段英 ...

  2. Windows平台使用RMAN命令自动删除Oracle过期归档日志的方法

    自动删除Oracle过期归档日志的思路如下: 1.编写自动执行的bat脚本文件: 2.Windows设置定期执行计划 OracleArchLogClear.bat D:/Oracle/product/ ...

  3. http 请求参数之Query String Parameters、Form Data、Request Payload

    Query String Parameters 当发起一次GET请求时,参数会以url string的形式进行传递.即?后的字符串则为其请求参数,并以&作为分隔符. 如下http请求报文头: ...

  4. android 手机 多分辨率适配

    近来在做android屏幕适配这方面的工作, 今天总算有点眉目.  小记一下 基础知识就不科普了, 网上一大堆. 作为一个刚接触这方面人, 最先进入我脑子的, 是从小到大的各种屏, 小到手表, 大到街 ...

  5. U3D对齐功能

    1,按快捷键Shift + v 的用法:    [1]先选中你要对齐的模型A,[将物体A对齐到物体B].    [2]按快捷键Shift + v ,此时将鼠标移到模型A的各个顶点处,可发现各个顶点上会 ...

  6. Math.ceil()、Math.floor()和Math.round()

    下面来介绍将小数值舍入为整数的几个方法:Math.ceil().Math.floor()和Math.round(). 这三个方法分别遵循下列舍入规则: Math.ceil()执行向上舍入,即它总是将数 ...

  7. [原]openstack-kilo--issue(十六) instance can't get ip 虚拟机不能得到ip(1)

    =====问题点:vm instance不能正常获取ip地址(此时用户是:admin) =======不一样的点:如果使用用户demo用户,启动一个vm,同样的image这个时候就能正确获取ip == ...

  8. 重装win7,win10,Ubuntu

    使用的软件多了,大了,感觉用起来很卡,快就是生产力,一定要重视.就去维修电安装了120G的固态硬盘,以前所安装的软件都没有了,距上一次重装快一年了,上次修了风扇,加了4G内存条.现在很多东西也一并删除 ...

  9. 新装的MySQL没有密码怎么办

    新装的mysql没有密码怎么办 [root@localhost log]# mysql -u root -p Enter password: Welcome to the MySQL monitor. ...

  10. uni-app,wex5,APPcan,ApiCloud几款国内webapp开发框架的选型对比

    框架列表. https://www.cnblogs.com/xiaxiaxia/articles/5705557.html 前言 近期,要开一个新的项目,APP类型.最重要的需求就是能够随时调整APP ...