Annotate类
在Annotate类中有个Annotator接口,定义如下:
/** A client that has annotations to add registers an annotator, * the method it will use to add the annotation. There are no * parameters; any needed data should be captured by the * Annotator. */ public interface Annotator { void enterAnnotation(); String toString(); }
其中的实现分部在MemberEnter与ClassReader类中,如下:
方法1:
/** Queue processing of an attribute default value. */ void annotateDefaultValueLater(final JCExpression defaultValue, final Env<AttrContext> localEnv, final MethodSymbol m) { annotate.later(new Annotate.Annotator() { public String toString() { return "annotate " + m.owner + "." + m + " default " + defaultValue; } public void enterAnnotation() { JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile); try { enterDefaultValue(defaultValue, localEnv, m); } finally { log.useSource(prev); } } }); } /** Enter a default value for an attribute method. */ private void enterDefaultValue(final JCExpression defaultValue, final Env<AttrContext> localEnv, final MethodSymbol m) { m.defaultValue = annotate.enterAttributeValue(m.type.getReturnType(),defaultValue,localEnv); }
方法2:
/** Queue annotations for later processing. */ void annotateLater(final List<JCAnnotation> annotations, final Env<AttrContext> localEnv, final Symbol s) { if (annotations.isEmpty()) { return; } if (s.kind != PCK) { s.attributes_field = null; // mark it incomplete for now } annotate.later(new Annotate.Annotator() { public String toString() { return "annotate " + annotations + " onto " + s + " in " + s.owner; } public void enterAnnotation() { Assert.check(s.kind == PCK || s.attributes_field == null); JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile); try { if (s.attributes_field != null && s.attributes_field.nonEmpty() && annotations.nonEmpty()) { log.error(annotations.head.pos, "already.annotated", kindName(s), s); } enterAnnotations(annotations, localEnv, s); } finally { log.useSource(prev); } } }); }
其中调用了enterAnnotations()方法,代码如下:
/** Enter a set of annotations. */ private void enterAnnotations(List<JCAnnotation> annotations,Env<AttrContext> env,Symbol s) { ListBuffer<Attribute.Compound> buf = new ListBuffer<Attribute.Compound>(); Set<TypeSymbol> annotated = new HashSet<TypeSymbol>(); if (!skipAnnotations) { for (List<JCAnnotation> al = annotations; al.nonEmpty(); al = al.tail) { JCAnnotation a = al.head; Attribute.Compound c = annotate.enterAnnotation(a, syms.annotationType, env); if (c == null) { continue; } buf.append(c); // Note: @Deprecated has no effect on local variables and parameters if (!c.type.isErroneous() && s.owner.kind != MTH && types.isSameType(c.type, syms.deprecatedType)) { s.flags_field |= Flags.DEPRECATED; } // Internally to java.lang.invoke, a @PolymorphicSignature annotation // acts like a classfile attribute. if (!c.type.isErroneous() && types.isSameType(c.type, syms.polymorphicSignatureType)) { if (!target.hasMethodHandles()) { // Somebody is compiling JDK7 source code to a JDK6 target. // Make it an error, since it is unlikely but important. log.error(env.tree.pos(), "wrong.target.for.polymorphic.signature.definition", target.name); } // Pull the flag through for better diagnostics, even on a bad target. s.flags_field |= Flags.POLYMORPHIC_SIGNATURE; } if (!annotated.add(a.type.tsym)) { log.error(a.pos, "duplicate.annotation"); } } } s.attributes_field = buf.toList(); }
方法3:
/** Import statics types of a given name. Non-types are handled in Attr. * @param pos Position to be used for error reporting. * @param tsym The class from which the name is imported. * @param name The (simple) name being imported. * @param env The environment containing the named import * scope to add to. */ private void importNamedStatic(final DiagnosticPosition pos, final TypeSymbol tsym, final Name name, final Env<AttrContext> env) { if (tsym.kind != TYP) { log.error(DiagnosticFlag.RECOVERABLE, pos, "static.imp.only.classes.and.interfaces"); return; } final Scope toScope = env.toplevel.namedImportScope; final PackageSymbol packge = env.toplevel.packge; final TypeSymbol origin = tsym; // enter imported types immediately new Object() { Set<Symbol> processed = new HashSet<Symbol>(); void importFrom(TypeSymbol tsym) { if (tsym == null || !processed.add(tsym)) { return; } // also import inherited names importFrom(types.supertype(tsym.type).tsym); for (Type t : types.interfaces(tsym.type)) { importFrom(t.tsym); } for (Scope.Entry e = tsym.members().lookup(name); e.scope != null; e = e.next()) { Symbol sym = e.sym; if (sym.isStatic() && sym.kind == TYP && staticImportAccessible(sym, packge) && sym.isMemberOf(origin, types) && chk.checkUniqueStaticImport(pos, sym, toScope)) { toScope.enter(sym, sym.owner.members(), origin.members()); } } } }.importFrom(tsym); // enter non-types before annotations that might use them annotate.earlier(new Annotate.Annotator() { Set<Symbol> processed = new HashSet<Symbol>(); boolean found = false; public String toString() { return "import static " + tsym + "." + name; } void importFrom(TypeSymbol tsym) { if (tsym == null || !processed.add(tsym)) { return; } // also import inherited names importFrom(types.supertype(tsym.type).tsym); for (Type t : types.interfaces(tsym.type)) importFrom(t.tsym); for (Scope.Entry e = tsym.members().lookup(name);e.scope != null;e = e.next()) { Symbol sym = e.sym; if (sym.isStatic() && staticImportAccessible(sym, packge) && sym.isMemberOf(origin, types)) { found = true; if (sym.kind == MTH || sym.kind != TYP && chk.checkUniqueStaticImport(pos, sym, toScope)) { toScope.enter(sym, sym.owner.members(), origin.members()); } } } } public void enterAnnotation() { JavaFileObject prev = log.useSource(env.toplevel.sourcefile); try { importFrom(tsym); if (!found) { log.error(pos, "cant.resolve.location", KindName.STATIC, name, List.<Type>nil(), List.<Type>nil(), Kinds.typeKindName(tsym.type), tsym.type); } } finally { log.useSource(prev); } } }); }
方法4:
/** Import all static members of a class or package on demand. * @param pos Position to be used for error reporting. * @param tsym The class or package the members of which are imported. * @param toScope The (import) scope in which imported classes are entered. */ private void importStaticAll(int pos, final TypeSymbol tsym, Env<AttrContext> env) { final JavaFileObject sourcefile = env.toplevel.sourcefile; final Scope toScope = env.toplevel.starImportScope; final PackageSymbol packge = env.toplevel.packge; final TypeSymbol origin = tsym; // enter imported types immediately new Object() { Set<Symbol> processed = new HashSet<Symbol>(); void importFrom(TypeSymbol tsym) { if (tsym == null || !processed.add(tsym)) { return; } // also import inherited names importFrom(types.supertype(tsym.type).tsym); for (Type t : types.interfaces(tsym.type)) { importFrom(t.tsym); } final Scope fromScope = tsym.members(); for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) { Symbol sym = e.sym; if (sym.kind == TYP && (sym.flags() & STATIC) != 0 && staticImportAccessible(sym, packge) && sym.isMemberOf(origin, types) && !toScope.includes(sym) ){ toScope.enter(sym, fromScope, origin.members()); } } } }.importFrom(tsym); // enter non-types before annotations that might use them annotate.earlier(new Annotate.Annotator() { Set<Symbol> processed = new HashSet<Symbol>(); public String toString() { return "import static " + tsym + ".*" + " in " + sourcefile; } void importFrom(TypeSymbol tsym) { if (tsym == null || !processed.add(tsym)) { return; } // also import inherited names importFrom(types.supertype(tsym.type).tsym); for (Type t : types.interfaces(tsym.type)) { importFrom(t.tsym); } final Scope fromScope = tsym.members(); for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) { Symbol sym = e.sym; if (sym.isStatic() && sym.kind != TYP && staticImportAccessible(sym, packge) && !toScope.includes(sym) && sym.isMemberOf(origin, types)) { toScope.enter(sym, fromScope, origin.members()); } } } public void enterAnnotation() { importFrom(tsym); } }); }
继承体系如下图:
类1:
class AnnotationCompleter extends AnnotationDeproxy implements Annotate.Annotator { final Symbol sym; final List<CompoundAnnotationProxy> l; final JavaFileObject classFile; @Override public String toString() { return " ClassReader annotate " + sym.owner + "." + sym + " with " + l; } AnnotationCompleter(Symbol sym, List<CompoundAnnotationProxy> l) { this.sym = sym; this.l = l; this.classFile = currentClassFile; } // implement Annotate.Annotator.enterAnnotation() public void enterAnnotation() { JavaFileObject previousClassFile = currentClassFile; try { currentClassFile = classFile; List<Attribute.Compound> newList = deproxyCompoundList(l); sym.attributes_field = ((sym.attributes_field == null) ? newList : newList.prependList(sym.attributes_field)); } finally { currentClassFile = previousClassFile; } } }
类2:
class AnnotationDefaultCompleter extends AnnotationDeproxy implements Annotate.Annotator { final MethodSymbol sym; final Attribute value; final JavaFileObject classFile = currentClassFile; @Override public String toString() { return " ClassReader store default for " + sym.owner + "." + sym + " is " + value; } AnnotationDefaultCompleter(MethodSymbol sym, Attribute value) { this.sym = sym; this.value = value; } // implement Annotate.Annotator.enterAnnotation() public void enterAnnotation() { JavaFileObject previousClassFile = currentClassFile; try { currentClassFile = classFile; sym.defaultValue = deproxy(sym.type.getReturnType(), value); } finally { currentClassFile = previousClassFile; } } }
Annotate类的更多相关文章
- Java类的继承与多态特性-入门笔记
相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...
- Matplotlib外观和基本配置笔记
title: matplotlib 外观和基本配置笔记 notebook: Python tags:matplotlib --- 参考资料,如何使用matplotlib绘制出数据图形,参考另一篇mat ...
- Swift 3.0 令人兴奋,但Objective-C也有小改进--Objective-C的类属性
由于Swift 3.0 出了太多令人兴奋的新特性,人们很容易忽略 Objective-C中的小改动.或许你会觉得苹果提及Objective-C 很可能是为了提高和Swift互操作性(译者注:互操作性主 ...
- 分享一个关于jackson的Json工具类
直接贴代码: import org.codehaus.jackson.map.DeserializationConfig.Feature; import org.codehaus.jackson.ma ...
- MyBatis里json型字段到Java类的映射
一.简介 我们在用MyBatis里,很多时间有这样一个需求:bean里有个属性是非基本数据类型,在DB存储时我们想存的是json格式的字符串,从DB拿出来时想直接映射成目标类型,也即json格式的字符 ...
- Java 最常用类(前1000名) 来自GitHub 3000个项目
这篇文章主要介绍了最常用的1000个Java类(附代码示例),需要的朋友可以参考下 分析Github 3000个开源项目,粗略统计如下.括号内的数字是使用频率 0-3000. 下面的列表显示不全,完整 ...
- (译)Objective-C 类属性
翻译自:Objective-C Class Properties 译者:Haley_Wong 由于Swift 3.0 出了太多令人兴奋的新特性,人们很容易忽略 Objective-C中的小改动.苹果展 ...
- Django 数据聚合函数 annotate
统计各个分类下的文章数 2 周,3 日前 字数 3818 阅读 546 评论 21 在我们的博客侧边栏有分类列表,显示博客已有的全部文章分类.现在想在分类名后显示该分类下有多少篇文章,该怎么做呢?最优 ...
- jackson工具类 对象转字符串 -- 字符串转对象
这个一个json的工具类.用的是jackson,当然还有谷歌的gosn,阿里的fastjson ,但是jackson的感觉还是最成熟(网上大神说的...) 实现的功能很简单,对象转字符串 字符串转简 ...
随机推荐
- webuploader 上传传自定义参数
全局设置 // 初始化的时候直接添加 var uploader = new WebUploader.Uploader({ ... formData: { uid: 12 ...
- cmake检测g++编译器是否支持c++11
# 相关链接:https://cmake.org/cmake/help/v3.0/module/CheckCXXCompilerFlag.htmlinclude(CheckCXXCompilerF ...
- /usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory
https://stackoverflow.com/questions/39111930/usr-include-boost-python-detail-wrap-python-hpp5023-fat ...
- (KMP 暴力)Corporate Identity -- hdu -- 2328
http://acm.hdu.edu.cn/showproblem.php?pid=2328 Corporate Identity Time Limit: 9000/3000 MS (Java/Oth ...
- C++互斥器:Mutex
互斥器的功能是,使多个线程和谐工作.同一时间内,只能有一个线程得到互斥对象,并获得资源操作权限,那么如果同一时间其他线程也想去操作资源,此时就会因为Mutex未处于激发状态,而无奈的等待…这时候,线程 ...
- web api 多版本控制重要的两个类
1.版本路径替换 public class ReplaceVersionWithExactValueInPath : IDocumentFilter { public void ...
- SQL Server OS 调度
--SQL SERVER OS 采用合作模式的线程调度模式,即除非Worker主动放弃CPU,否则SQL OS 不会强制剥夺其CPU,从而减少Context Switch --默认设置下,SQL SE ...
- JQuery fullcalender文档
转载: http://blog.csdn.net/lgg2011. 使用方式, 引入相关js, css后, $(‘#div_name’).fullCalendar({//options}); 接受的 ...
- Net系列框架-Dapper+简单三层架构
Net系列框架-Dapper+简单三层架构 工作将近6年多了,工作中也陆陆续续学习和搭建了不少的框架,后续将按由浅入深的方式,整理出一些框架源码,所有框架源码本人都亲自调试通过,如果有问题,欢迎联系我 ...
- 爬虫浅谈一:一个简单c#爬虫程序
这篇文章只是简单展示一个基于HTTP请求如何抓取数据的文章,如觉得简单的朋友,后续我们再慢慢深入研究探讨. 图1: 如图1,我们工作过程中,无论平台网站还是企业官网,总少不了新闻展示.如某天产品经理跟 ...