?? javacompiler.java
字號(hào):
protected boolean keepComments() { return sourceOutput; } /** * Parse contents of file. * @param filename The name of the file to be parsed. */ public Tree.TopLevel parse(String filename) { return parse(filename, openSource(filename)); } /** * Emit plain Java source for a class. * @param env The attribution environment of the outermost class * containing this class. * @param cdef The class definition to be printed. */ void printSource(Env env, ClassDef cdef) throws IOException { File outFile = writer.outputFile(cdef.sym, ".java"); if (inputFiles.contains(outFile)) { log.error(cdef.pos, "source.cant.overwrite.input.file", outFile.toString()); } else { PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(new FileOutputStream(outFile)))); try { new Pretty(out, true).printUnit(env.toplevel, cdef); if (verbose) printVerbose("wrote.file", outFile.getPath()); } finally { out.close(); } } } /** * Generate code and emit a class file for a given class * @param env The attribution environment of the outermost class * containing this class. * @param cdef The class definition from which code is generated. */ void genCode(Env env, ClassDef cdef) throws IOException { try { if (gen.genClass(env, cdef)) writer.writeClass(cdef.sym); } catch (ClassWriter.PoolOverflow ex) { log.error(cdef.pos, "limit.pool"); } catch (ClassWriter.StringOverflow ex) { log.error(cdef.pos, "limit.string.overflow", ex.value.substring(0, 20)); } catch (CompletionFailure ex) { log.error(Position.NOPOS, ex.getMessage()); } } /** * Complete compiling a source file that has been accessed * by the class file reader. * @param c The class the source file of which needs to be compiled. * @param filename The name of the source file. * @param f An input stream that reads the source file. */ public void complete(ClassSymbol c, String filename, InputStream f) throws CompletionFailure { Tree tree = parse(filename, f); enter.complete(List.make(tree), c); if (enter.getEnv(c) == null) { throw new ClassReader.BadClassFile(c, filename, log.getLocalizedString("file.doesnt.contain.class", c.fullname.toJava())); } } /** * Track when the JavaCompiler has been used to compile something. */ private boolean hasBeenUsed = false; /** * Main method: compile a list of files, return all compiled classes * @param filenames The names of all files to be compiled. */ public List compile(List filenames) throws Throwable { assert ! hasBeenUsed : "attempt to reuse JavaCompiler"; hasBeenUsed = true; long msec = System.currentTimeMillis(); ListBuffer classes = new ListBuffer(); try { ListBuffer trees = new ListBuffer(); for (List l = filenames; l.nonEmpty(); l = l.tail) trees.append(parse((String) l.head)); List roots = trees.toList(); if (errorCount() == 0) enter.main(roots); List rootClasses = null; if (sourceOutput) { ListBuffer cdefs = new ListBuffer(); for (List l = roots; l.nonEmpty(); l = l.tail) { for (List defs = ((TopLevel) l.head).defs; defs.nonEmpty(); defs = defs.tail) { if (defs.head instanceof ClassDef) cdefs.append((ClassDef) defs.head); } } rootClasses = cdefs.toList(); } Attr attr = Attr.instance(context); this.gen = Gen.instance(context); Flow flow = Flow.instance(context); TransTypes transTypes = TransTypes.instance(context); TransInner transInner = TransInner.instance(context); while (todo.nonEmpty()) { Env env = (Env) todo.next(); Tree untranslated = env.tree; if (verbose) printVerbose("checking.attribution", env.enclClass.sym.toJava()); Name prev = log.useSource(env.enclClass.sym.sourcefile); attr.attribClass(env.tree.pos, env.enclClass.sym); if (attrParseOnly) continue; make.at(Position.FIRSTPOS); TreeMaker localMake = new TreeMaker(env.toplevel); if (errorCount() == 0) { flow.analyzeTree(env.tree, localMake); } if (errorCount() == 0) { env.tree = transTypes.translateTopLevelClass(env.tree, localMake); } if (errorCount() == 0) { ClassDef cdef = null; try { if (sourceOutput) { cdef = (ClassDef) env.tree; if (untranslated instanceof ClassDef && rootClasses.contains((ClassDef) untranslated)) { printSource(env, cdef); } } else { List cdefs = transInner.translateTopLevelClass(env, env.tree, localMake); if (errorCount() == 0) for (List l = cdefs; errorCount() == 0 && l.nonEmpty(); l = l.tail) { cdef = (ClassDef) l.head; if (printFlat) printSource(env, cdef); else if (classOutput) genCode(env, cdef); classes.append(cdef.sym); } } } catch (IOException ex) { log.error(cdef.pos, "class.cant.write", cdef.sym.toJava(), ex.getMessage()); } } log.useSource(prev); } } catch (Abort ex) { } Check chk = Check.instance(context); if (verbose) printVerbose("total", Long.toString(System.currentTimeMillis() - msec)); if (chk.deprecatedSource != null && !deprecation) noteDeprecated(chk.deprecatedSource.toString()); if (chk.uncheckedSource != null && !warnunchecked) noteUnchecked(chk.uncheckedSource.toString()); int errCount = errorCount(); if (errCount == 1) printCount("error", errCount); else printCount("error.plural", errCount); if (log.nwarnings == 1) printCount("warn", log.nwarnings); else printCount("warn.plural", log.nwarnings); return classes.toList(); } /** * Close the compiler, flushing the logs */ public void close() { log.flush(); reader.close(); names.dispose(); } /** * Output for "-verbose" option. * @param key The key to look up the correct internationalized string. * @param arg An argument for substitution into the output string. */ private void printVerbose(String key, String arg) { Log.printLines(log.noticeWriter, log.getLocalizedString("verbose." + key, arg)); } /** * Print note that deprecated API's are used. */ private void noteDeprecated(String input) { if (input.equals("*")) log.note("deprecated.plural"); else log.note("deprecated.filename", input); log.note("deprecated.recompile"); } /** * Print note that unchecked operations are used. */ void noteUnchecked(String input) { if (input.equals("*")) log.note("unchecked.plural"); else log.note("unchecked.filename", input); log.note("unchecked.recompile"); } /** * Print numbers of errors and warnings. */ void printCount(String kind, int count) { if (count != 0) { Log.printLines(log.errWriter, log.getLocalizedString("count." + kind, Integer.toString(count))); log.errWriter.flush(); } }}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -