com.sun.tools.javac.main.JavaCompiler.instance()方法的使用及代码示例

x33g5p2x  于2022-01-22 转载在 其他  
字(10.1k)|赞(0)|评价(0)|浏览(106)

本文整理了Java中com.sun.tools.javac.main.JavaCompiler.instance()方法的一些代码示例,展示了JavaCompiler.instance()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JavaCompiler.instance()方法的具体详情如下:
包路径:com.sun.tools.javac.main.JavaCompiler
类名称:JavaCompiler
方法名:instance

JavaCompiler.instance介绍

[英]Get the JavaCompiler instance for this context.
[中]获取此上下文的JavaCompiler实例。

代码示例

代码示例来源:origin: google/error-prone

public ClassSymbol resolveClass(CharSequence qualifiedClass)
  throws CouldNotResolveImportException {
 try {
  Symbol symbol =
    JavaCompiler.instance(context)
      .resolveIdent(symtab().java_base, qualifiedClass.toString());
  if (symbol.equals(symtab().errSymbol) || !(symbol instanceof ClassSymbol)) {
   throw new CouldNotResolveImportException(qualifiedClass);
  } else {
   return (ClassSymbol) symbol;
  }
 } catch (NullPointerException e) {
  throw new CouldNotResolveImportException(qualifiedClass);
 }
}

代码示例来源:origin: google/error-prone

return;
if (JavaCompiler.instance(context).errorCount() > 0) {
 return;

代码示例来源:origin: google/error-prone

return;
if (JavaCompiler.instance(context).errorCount() > errorProneErrors) {
 return;

代码示例来源:origin: cincheo/jsweet

JavacFileManager.preRegister(context);
fileManager = context.get(JavaFileManager.class);
compiler = JavaCompiler.instance(context);
compiler.attrParseOnly = true;
compiler.verbose = false;

代码示例来源:origin: light/JOps

private ReplaceOnAnalyzeTaskListener( Context context ) {
  this.compiler = JavaCompiler.instance( context );
}

代码示例来源:origin: cincheo/jsweet

Arrays.asList(SourceFile.toFiles(sourceFiles)));
JavaCompiler compiler = JavaCompiler.instance(context);
compiler.attrParseOnly = true;
compiler.verbose = true;

代码示例来源:origin: cincheo/jsweet

/**
 * Creates a new compilation environment with the given options and
 * classpath.
 */
public static JavaCompilationEnvironment create(JSweetOptions jsweetOptions, String classPath) {
  JSweetContext context = new JSweetContext(jsweetOptions);
  Options options = Options.instance(context);
  options.put(Option.CLASSPATH, classPath);
  options.put(Option.XLINT, "path");
  context.put(Log.outKey, new PrintWriter(System.out));
  // options.put(Option.XLINT_CUSTOM.text + "-" +
  // LintCategory.OPTIONS.option, "true");
  // options.remove(Option.XLINT_CUSTOM.text +
  // LintCategory.OPTIONS.option);
  options.put(Option.XLINT_CUSTOM.text + "-" + LintCategory.OVERRIDES.option, "true");
  JavacFileManager.preRegister(context);
  JavaFileManager fileManager = context.get(JavaFileManager.class);
  Log log = Log.instance(context);
  log.emitWarnings = false;
  log.suppressNotes = true;
  Types javacTypes = Types.instance(context);
  JavaCompiler compiler = JavaCompiler.instance(context);
  compiler.attrParseOnly = true;
  compiler.verbose = false;
  compiler.genEndPos = true;
  compiler.keepComments = true;
  Names names = Names.instance(context);
  Symtab symtab = Symtab.instance(context);
  return new JavaCompilationEnvironment(fileManager, compiler, options, context, log, javacTypes, names, symtab);
}

代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac

/** Create the compiler to be used for the final compilation. */
JavaCompiler finalCompiler() {
  try {
    Context nextCtx = nextContext();
    JavacProcessingEnvironment.this.context = nextCtx;
    JavaCompiler c = JavaCompiler.instance(nextCtx);
    c.log.initRound(compiler.log);
    return c;
  } finally {
    compiler.close(false);
  }
}

代码示例来源:origin: konsoletyper/teavm-javac

/** Create the compiler to be used for the final compilation. */
JavaCompiler finalCompiler() {
  try {
    Context nextCtx = nextContext();
    JavacProcessingEnvironment.this.context = nextCtx;
    JavaCompiler c = JavaCompiler.instance(nextCtx);
    c.log.initRound(compiler.log);
    return c;
  } finally {
    compiler.close(false);
  }
}

代码示例来源:origin: com.google.errorprone/error_prone_core

public ClassSymbol resolveClass(CharSequence qualifiedClass)
  throws CouldNotResolveImportException {
 try {
  Symbol symbol =
    JavaCompiler.instance(context)
      .resolveIdent(symtab().java_base, qualifiedClass.toString());
  if (symbol.equals(symtab().errSymbol) || !(symbol instanceof ClassSymbol)) {
   throw new CouldNotResolveImportException(qualifiedClass);
  } else {
   return (ClassSymbol) symbol;
  }
 } catch (NullPointerException e) {
  throw new CouldNotResolveImportException(qualifiedClass);
 }
}

代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac

/**
 * For internal use only.  This method will be
 * removed without warning.
 */
public Type parseType(String expr, TypeElement scope) {
  if (expr == null || expr.equals(""))
    throw new IllegalArgumentException();
  compiler = JavaCompiler.instance(context);
  JavaFileObject prev = compiler.log.useSource(null);
  ParserFactory parserFactory = ParserFactory.instance(context);
  Attr attr = Attr.instance(context);
  try {
    CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length());
    Parser parser = parserFactory.newParser(buf, false, false, false);
    JCTree tree = parser.parseType();
    return attr.attribType(tree, (Symbol.TypeSymbol)scope);
  } finally {
    compiler.log.useSource(prev);
  }
}

代码示例来源:origin: sc.fiji/javac

/**
 * For internal use only.  This method will be
 * removed without warning.
 */
public Type parseType(String expr, TypeElement scope) {
  if (expr == null || expr.equals(""))
    throw new IllegalArgumentException();
  compiler = JavaCompiler.instance(context);
  JavaFileObject prev = compiler.log.useSource(null);
  Scanner.Factory scannerFactory = Scanner.Factory.instance(context);
  Parser.Factory parserFactory = Parser.Factory.instance(context);
  Attr attr = Attr.instance(context);
  try {
    Scanner scanner = scannerFactory.newScanner((expr+"\u0000").toCharArray(),
                          expr.length());
    Parser parser = parserFactory.newParser(scanner, false, false);
    JCTree tree = parser.type();
    return attr.attribType(tree, (Symbol.TypeSymbol)scope);
  } finally {
    compiler.log.useSource(prev);
  }
}

代码示例来源:origin: konsoletyper/teavm-javac

/**
 * Use a new context.  May be called from outside to update
 * internal state for a new annotation-processing round.
 */
public void setContext(Context context) {
  context.put(JavacElements.class, this);
  javaCompiler = JavaCompiler.instance(context);
  syms = Symtab.instance(context);
  names = Names.instance(context);
  types = Types.instance(context);
  enter = Enter.instance(context);
}

代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac

/**
 * Use a new context.  May be called from outside to update
 * internal state for a new annotation-processing round.
 */
public void setContext(Context context) {
  context.put(JavacElements.class, this);
  javaCompiler = JavaCompiler.instance(context);
  syms = Symtab.instance(context);
  names = Names.instance(context);
  types = Types.instance(context);
  enter = Enter.instance(context);
}

代码示例来源:origin: org.jvnet.sorcerer/sorcerer-javac

/**
 * Use a new context.  May be called from outside to update
 * internal state for a new annotation-processing round.
 * This instance is *not* then registered with the new context.
 */
public void setContext(Context context) {
javaCompiler = JavaCompiler.instance(context);
  syms = Symtab.instance(context);
  names = Name.Table.instance(context);
types = Types.instance(context);
enter = Enter.instance(context);
  reader = ClassReader.instance(context);
}

代码示例来源:origin: sc.fiji/javac

/**
 * Use a new context.  May be called from outside to update
 * internal state for a new annotation-processing round.
 * This instance is *not* then registered with the new context.
 */
public void setContext(Context context) {
  javaCompiler = JavaCompiler.instance(context);
  syms = Symtab.instance(context);
  names = Name.Table.instance(context);
  types = Types.instance(context);
  enter = Enter.instance(context);
  reader = ClassReader.instance(context);
}

代码示例来源:origin: konsoletyper/teavm-javac

private void initProcessorClassLoader() {
  JavaFileManager fileManager = context.get(JavaFileManager.class);
  try {
    // If processorpath is not explicitly set, use the classpath.
    processorClassLoader = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
      ? fileManager.getClassLoader(ANNOTATION_PROCESSOR_PATH)
      : fileManager.getClassLoader(CLASS_PATH);
    if (processorClassLoader != null && processorClassLoader instanceof Closeable) {
      JavaCompiler compiler = JavaCompiler.instance(context);
      compiler.closeables = compiler.closeables.prepend((Closeable) processorClassLoader);
    }
  } catch (SecurityException e) {
    processorClassLoaderException = e;
  }
}

代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac

private void initProcessorClassLoader() {
  JavaFileManager fileManager = context.get(JavaFileManager.class);
  try {
    // If processorpath is not explicitly set, use the classpath.
    processorClassLoader = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH)
      ? fileManager.getClassLoader(ANNOTATION_PROCESSOR_PATH)
      : fileManager.getClassLoader(CLASS_PATH);
    if (processorClassLoader != null && processorClassLoader instanceof Closeable) {
      JavaCompiler compiler = JavaCompiler.instance(context);
      compiler.closeables = compiler.closeables.prepend((Closeable) processorClassLoader);
    }
  } catch (SecurityException e) {
    processorClassLoaderException = e;
  }
}

代码示例来源:origin: org.jvnet.sorcerer/sorcerer-javac

private void prepareCompiler() throws IOException {
  if (!used.getAndSet(true)) {
    beginContext();
    compilerMain.setOptions(Options.instance(context));
    compilerMain.filenames = new ListBuffer<File>();
    List<File> filenames = compilerMain.processArgs(CommandLine.parse(args));
    if (!filenames.isEmpty())
      throw new IllegalArgumentException("Malformed arguments " + filenames.toString(" "));
    compiler = JavaCompiler.instance(context);
    // force the use of the scanner that captures Javadoc comments
    com.sun.tools.javac.parser.DocCommentScanner.Factory.preRegister(context);
    compiler.keepComments = true;
    compiler.genEndPos = true;
    // NOTE: this value will be updated after annotation processing
    compiler.initProcessAnnotations(processors);
    notYetEntered = new HashMap<JavaFileObject, JCCompilationUnit>();
    for (JavaFileObject file: fileObjects)
      notYetEntered.put(file, null);
    genList = new ListBuffer<Env<AttrContext>>();
    // endContext will be called when all classes have been generated
    // TODO: should handle the case after each phase if errors have occurred
    args = null;
  }
}

代码示例来源:origin: org.kohsuke.sorcerer/sorcerer-javac

/** Create a round (common code). */
private Round(Context context, int number, int priorErrors, int priorWarnings,
    Log.DeferredDiagnosticHandler deferredDiagnosticHandler) {
  this.context = context;
  this.number = number;
  compiler = JavaCompiler.instance(context);
  log = Log.instance(context);
  log.nerrors = priorErrors;
  log.nwarnings = priorWarnings;
  if (number == 1) {
    Assert.checkNonNull(deferredDiagnosticHandler);
    this.deferredDiagnosticHandler = deferredDiagnosticHandler;
  } else {
    this.deferredDiagnosticHandler = new Log.DeferredDiagnosticHandler(log);
  }
  // the following is for the benefit of JavacProcessingEnvironment.getContext()
  JavacProcessingEnvironment.this.context = context;
  // the following will be populated as needed
  topLevelClasses  = List.nil();
  packageInfoFiles = List.nil();
}

相关文章