Javac的语法糖
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Javac的语法糖相关的知识,希望对你有一定的参考价值。
在Javac中解语法糖主要是Lower类来完成,调用这个类的入口函数translateTopLevelClass即可。这个方法只是JavacCompiler类的desugar方法中进行了调用,desugar方法的具体代码如下:
/** * Prepare attributed parse trees, in conjunction with their attribution contexts, * for source or code generation. If the file was not listed on the command line, * the current implicitSourcePolicy is taken into account. * The preparation stops as soon as an error is found. */ protected void desugar(final Env<AttrContext> env, Queue<Pair<Env<AttrContext>, JCClassDecl>> results) { if (shouldStop(CompileState.TRANSTYPES)) return; if (implicitSourcePolicy == ImplicitSourcePolicy.NONE && !inputFiles.contains(env.toplevel.sourcefile)) { return; } if (compileStates.isDone(env, CompileState.LOWER)) { results.addAll(desugaredEnvs.get(env)); return; } /** * Ensure that superclasses of C are desugared before C itself. This is * required for two reasons: (i) as erasure (TransTypes) destroys * information needed in flow analysis and (ii) as some checks carried * out during lowering require that all synthetic fields/methods have * already been added to C and its superclasses. */ class ScanNested extends TreeScanner { Set<Env<AttrContext>> dependencies = new LinkedHashSet<Env<AttrContext>>(); @Override public void visitClassDef(JCClassDecl node) { Type st = types.supertype(node.sym.type); if (st.tag == TypeTags.CLASS) { ClassSymbol c = st.tsym.outermostClass(); Env<AttrContext> stEnv = enter.getEnv(c); if (stEnv != null && env != stEnv) { if (dependencies.add(stEnv)) scan(stEnv.tree); } } super.visitClassDef(node); } } ScanNested scanner = new ScanNested(); scanner.scan(env.tree); for (Env<AttrContext> dep: scanner.dependencies) { if (!compileStates.isDone(dep, CompileState.FLOW)) desugaredEnvs.put(dep, desugar(flow(attribute(dep)))); } //We need to check for error another time as more classes might //have been attributed and analyzed at this stage if (shouldStop(CompileState.TRANSTYPES)) return; if (verboseCompilePolicy) printNote("[desugar " + env.enclClass.sym + "]"); JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ? env.enclClass.sym.sourcefile : env.toplevel.sourcefile); try { //save tree prior to rewriting JCTree untranslated = env.tree; make.at(Position.FIRSTPOS); TreeMaker localMake = make.forToplevel(env.toplevel); if (env.tree instanceof JCCompilationUnit) { if (!(stubOutput || sourceOutput || printFlat)) { if (shouldStop(CompileState.LOWER)) return; List<JCTree> pdef = lower.translateTopLevelClass(env, env.tree, localMake); if (pdef.head != null) { Assert.check(pdef.tail.isEmpty()); results.add(new Pair<Env<AttrContext>, JCClassDecl>(env, (JCClassDecl)pdef.head)); } } return; } if (stubOutput) { //emit stub Java source file, only for compilation //units enumerated explicitly on the command line JCClassDecl cdef = (JCClassDecl)env.tree; if (untranslated instanceof JCClassDecl && rootClasses.contains((JCClassDecl)untranslated) && ((cdef.mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 || cdef.sym.packge().getQualifiedName() == names.java_lang)) { results.add(new Pair<Env<AttrContext>, JCClassDecl>(env, removeMethodBodies(cdef))); } return; } if (shouldStop(CompileState.TRANSTYPES)) return; env.tree = transTypes.translateTopLevelClass(env.tree, localMake); compileStates.put(env, CompileState.TRANSTYPES); if (shouldStop(CompileState.LOWER)) return; if (sourceOutput) { //emit standard Java source file, only for compilation //units enumerated explicitly on the command line JCClassDecl cdef = (JCClassDecl)env.tree; if (untranslated instanceof JCClassDecl && rootClasses.contains((JCClassDecl)untranslated)) { results.add(new Pair<Env<AttrContext>, JCClassDecl>(env, cdef)); } return; } //translate out inner classes List<JCTree> cdefs = lower.translateTopLevelClass(env, env.tree, localMake); compileStates.put(env, CompileState.LOWER); if (shouldStop(CompileState.LOWER)) return; //generate code for each class for (List<JCTree> l = cdefs; l.nonEmpty(); l = l.tail) { JCClassDecl cdef = (JCClassDecl)l.head; results.add(new Pair<Env<AttrContext>, JCClassDecl>(env, cdef)); } } finally { log.useSource(prev); } }
以上是关于Javac的语法糖的主要内容,如果未能解决你的问题,请参考以下文章