Java Code Examples for com.sun.tools.javac.util.Context

The following examples show how to use com.sun.tools.javac.util.Context. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: openjdk-jdk8u   Source File: T7018098.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
    FSInfo fsInfo = context.get(FSInfo.class);

    round++;
    if (round == 1) {
        boolean expect = Boolean.valueOf(options.get("expect"));
        checkEqual("cache result", fsInfo.isDirectory(testDir), expect);
        initialFSInfo = fsInfo;
    } else {
        checkEqual("fsInfo", fsInfo, initialFSInfo);
    }

    return true;
}
 
Example 2
private LambdaToMethod(Context context) {
    context.put(unlambdaKey, this);
    diags = JCDiagnostic.Factory.instance(context);
    log = Log.instance(context);
    lower = Lower.instance(context);
    names = Names.instance(context);
    syms = Symtab.instance(context);
    rs = Resolve.instance(context);
    operators = Operators.instance(context);
    make = TreeMaker.instance(context);
    types = Types.instance(context);
    transTypes = TransTypes.instance(context);
    analyzer = new LambdaAnalyzerPreprocessor();
    Options options = Options.instance(context);
    dumpLambdaToMethodStats = options.isSet("debug.dumpLambdaToMethodStats");
    attr = Attr.instance(context);
    forceSerializable = options.isSet("forceSerializable");
}
 
Example 3
/**
 * Verify that an alternate file manager can be specified:
 * in this case, a PathFileManager.
 */
@Test
public void testFileManager() throws Exception {
    JavaFileObject srcFile = createSimpleJavaFileObject();
    DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
    PathFileManager fm = new JavacPathFileManager(new Context(), false, null);
    Path outDir = getOutDir().toPath();
    fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
    Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
    DocumentationTask t = tool.getTask(null, fm, null, null, null, files);
    if (t.call()) {
        System.err.println("task succeeded");
        checkFiles(outDir, standardExpectFiles);
    } else {
        throw new Exception("task failed");
    }
}
 
Example 4
Source Project: openjdk-8-source   Source File: DocEnv.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructor
 *
 * @param context      Context for this javadoc instance.
 */
protected DocEnv(Context context) {
    context.put(docEnvKey, this);
    this.context = context;

    messager = Messager.instance0(context);
    syms = Symtab.instance(context);
    reader = JavadocClassReader.instance0(context);
    enter = JavadocEnter.instance0(context);
    names = Names.instance(context);
    externalizableSym = reader.enterClass(names.fromString("java.io.Externalizable"));
    chk = Check.instance(context);
    types = Types.instance(context);
    fileManager = context.get(JavaFileManager.class);
    if (fileManager instanceof JavacFileManager) {
        ((JavacFileManager)fileManager).setSymbolFileEnabled(false);
    }

    // Default.  Should normally be reset with setLocale.
    this.doclocale = new DocLocale(this, "", breakiterator);
    source = Source.instance(context);
}
 
Example 5
Source Project: openjdk-jdk9   Source File: T7018098.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
    FSInfo fsInfo = context.get(FSInfo.class);

    round++;
    if (round == 1) {
        boolean expect = Boolean.valueOf(options.get("expect"));
        checkEqual("cache result", fsInfo.isDirectory(testDir.toPath()), expect);
        initialFSInfo = fsInfo;
    } else {
        checkEqual("fsInfo", fsInfo, initialFSInfo);
    }

    return true;
}
 
Example 6
Source Project: openjdk-8   Source File: Start.java    License: GNU General Public License v2.0 6 votes vote down vote up
public Start(Context context) {
    context.getClass(); // null check
    this.context = context;
    apiMode = true;
    defaultDocletClassName = standardDocletClassName;
    docletParentClassLoader = null;

    Log log = context.get(Log.logKey);
    if (log instanceof Messager)
        messager = (Messager) log;
    else {
        PrintWriter out = context.get(Log.outKey);
        messager = (out == null) ? new Messager(context, javadocName)
                : new Messager(context, javadocName, out, out, out);
    }
}
 
Example 7
Source Project: openjdk-jdk9   Source File: MakeLiteralTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
void run() throws Exception {
    Context context = new Context();
    JavacFileManager.preRegister(context);
    Symtab syms = Symtab.instance(context);
    maker = TreeMaker.instance(context);
    types = Types.instance(context);

    test("abc",                     CLASS,      syms.stringType,    "abc");
    test(Boolean.FALSE,             BOOLEAN,    syms.booleanType,   Integer.valueOf(0));
    test(Boolean.TRUE,              BOOLEAN,    syms.booleanType,   Integer.valueOf(1));
    test(Byte.valueOf((byte) 1),    BYTE,       syms.byteType,      Byte.valueOf((byte) 1));
    test(Character.valueOf('a'),    CHAR,       syms.charType,      Integer.valueOf('a'));
    test(Double.valueOf(1d),        DOUBLE,     syms.doubleType,    Double.valueOf(1d));
    test(Float.valueOf(1f),         FLOAT,      syms.floatType,     Float.valueOf(1f));
    test(Integer.valueOf(1),        INT,        syms.intType,       Integer.valueOf(1));
    test(Long.valueOf(1),           LONG,       syms.longType,      Long.valueOf(1));
    test(Short.valueOf((short) 1),  SHORT,      syms.shortType,     Short.valueOf((short) 1));

    if (errors > 0)
        throw new Exception(errors + " errors found");
}
 
Example 8
/** Create the first round. */
Round(Context context, List<JCCompilationUnit> roots, List<ClassSymbol> classSymbols,
        Log.DeferredDiagnosticHandler deferredDiagnosticHandler) {
    this(context, 1, 0, 0, deferredDiagnosticHandler);
    this.roots = roots;
    genClassFiles = new HashMap<String,JavaFileObject>();

    compiler.todo.clear(); // free the compiler's resources

    // The reverse() in the following line is to maintain behavioural
    // compatibility with the previous revision of the code. Strictly speaking,
    // it should not be necessary, but a javah golden file test fails without it.
    topLevelClasses =
        getTopLevelClasses(roots).prependList(classSymbols.reverse());

    packageInfoFiles = getPackageInfoFiles(roots);

    findAnnotationsPresent();
}
 
Example 9
/** 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();
}
 
Example 10
Source Project: openjdk-jdk8u   Source File: Start.java    License: GNU General Public License v2.0 6 votes vote down vote up
public Start(Context context) {
    context.getClass(); // null check
    this.context = context;
    apiMode = true;
    defaultDocletClassName = standardDocletClassName;
    docletParentClassLoader = null;

    Log log = context.get(Log.logKey);
    if (log instanceof Messager)
        messager = (Messager) log;
    else {
        PrintWriter out = context.get(Log.outKey);
        messager = (out == null) ? new Messager(context, javadocName)
                : new Messager(context, javadocName, out, out, out);
    }
}
 
Example 11
/**
 *  Construct a new javadoc tool.
 */
public static JavadocTool make0(Context context) {
    Messager messager = null;
    try {
        // force the use of Javadoc's class reader
        JavadocClassReader.preRegister(context);

        // force the use of Javadoc's own enter phase
        JavadocEnter.preRegister(context);

        // force the use of Javadoc's own member enter phase
        JavadocMemberEnter.preRegister(context);

        // force the use of Javadoc's own todo phase
        JavadocTodo.preRegister(context);

        // force the use of Messager as a Log
        messager = Messager.instance0(context);

        return new JavadocTool(context);
    } catch (CompletionFailure ex) {
        messager.error(Position.NOPOS, ex.getMessage());
        return null;
    }
}
 
Example 12
/**
 * Set the context for JavacFileManager.
 */
@Override
public void setContext(Context context) {
    super.setContext(context);

    fsInfo = FSInfo.instance(context);

    contextUseOptimizedZip = options.getBoolean("useOptimizedZip", true);
    if (contextUseOptimizedZip)
        zipFileIndexCache = ZipFileIndexCache.getSharedInstance();

    mmappedIO = options.isSet("mmappedIO");
    symbolFileEnabled = !options.isSet("ignore.symbol.file");

    String sf = options.get("sortFiles");
    if (sf != null) {
        sortFiles = (sf.equals("reverse") ? SortFiles.REVERSE : SortFiles.FORWARD);
    }
}
 
Example 13
Source Project: hottub   Source File: DPrinter.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static DPrinter instance(Context context) {
    DPrinter dp = context.get(DPrinter.class);
    if (dp == null) {
        dp = new DPrinter(context);
    }
    return dp;

}
 
Example 14
Source Project: openjdk-8   Source File: JavacTool.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public JavacTask getTask(Writer out,
                         JavaFileManager fileManager,
                         DiagnosticListener<? super JavaFileObject> diagnosticListener,
                         Iterable<String> options,
                         Iterable<String> classes,
                         Iterable<? extends JavaFileObject> compilationUnits) {
    Context context = new Context();
    return getTask(out, fileManager, diagnosticListener,
            options, classes, compilationUnits,
            context);
}
 
Example 15
Source Project: jdk8u60   Source File: ScannerFactory.java    License: GNU General Public License v2.0 5 votes vote down vote up
/** Create a new scanner factory. */
protected ScannerFactory(Context context) {
    context.put(scannerFactoryKey, this);
    this.log = Log.instance(context);
    this.names = Names.instance(context);
    this.source = Source.instance(context);
    this.tokens = Tokens.instance(context);
}
 
Example 16
/**
 * Verify that exceptions from a bad file manager are thrown as expected.
 */
@Test
public void testBadFileManager() throws Exception {
    JavaFileObject srcFile = createSimpleJavaFileObject();
    DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
    PathFileManager fm = new JavacPathFileManager(new Context(), false, null) {
        @Override
        public Iterable<JavaFileObject> list(Location location,
                String packageName,
                Set<Kind> kinds,
                boolean recurse)
                throws IOException {
            throw new UnexpectedError();
        }
    };
    Path outDir = getOutDir().toPath();
    fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
    Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
    DocumentationTask t = tool.getTask(null, fm, null, null, null, files);
    try {
        t.call();
        error("call completed without exception");
    } catch (RuntimeException e) {
        Throwable c = e.getCause();
        if (c.getClass() == UnexpectedError.class)
            System.err.println("exception caught as expected: " + c);
        else
            throw e;
    }
}
 
Example 17
Source Project: openjdk-jdk8u   Source File: Messager.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static void preRegister(Context context,
                               final String programName,
                               final PrintWriter errWriter,
                               final PrintWriter warnWriter,
                               final PrintWriter noticeWriter) {
    context.put(logKey, new Context.Factory<Log>() {
        public Log make(Context c) {
            return new Messager(c,
                                programName,
                                errWriter,
                                warnWriter,
                                noticeWriter);
        }
    });
}
 
Example 18
Source Project: openjdk-8-source   Source File: JNIWriter.java    License: GNU General Public License v2.0 5 votes vote down vote up
/** Construct a class writer, given an options table.
 */
private JNIWriter(Context context) {
    context.put(jniWriterKey, this);
    fileManager = context.get(JavaFileManager.class);
    log = Log.instance(context);

    Options options = Options.instance(context);
    verbose = options.isSet(VERBOSE);
    checkAll = options.isSet("javah:full");

    this.context = context; // for lazyInit()
    syms = Symtab.instance(context);

    lineSep = System.getProperty("line.separator");
}
 
Example 19
Source Project: hottub   Source File: Start.java    License: GNU General Public License v2.0 5 votes vote down vote up
Start(String programName, String defaultDocletClassName,
      ClassLoader docletParentClassLoader) {
    context = new Context();
    messager = new Messager(context, programName);
    this.defaultDocletClassName = defaultDocletClassName;
    this.docletParentClassLoader = docletParentClassLoader;
}
 
Example 20
Source Project: manifold   Source File: ManTypes.java    License: Apache License 2.0 5 votes vote down vote up
public static Types instance( Context ctx )
{
  Types types = ctx.get( typesKey );
  if( !(types instanceof ManTypes) )
  {
    ctx.put( typesKey, (Types)null );
    types = new ManTypes( ctx );
  }

  return types;
}
 
Example 21
Source Project: hottub   Source File: Messager.java    License: GNU General Public License v2.0 5 votes vote down vote up
/** Get the current messager, which is also the compiler log. */
public static Messager instance0(Context context) {
    Log instance = context.get(logKey);
    if (instance == null || !(instance instanceof Messager))
        throw new InternalError("no messager instance!");
    return (Messager)instance;
}
 
Example 22
Source Project: hottub   Source File: T6877206.java    License: GNU General Public License v2.0 5 votes vote down vote up
JavacFileManager createFileManager(boolean useOptimizedZip, boolean useSymbolFile) {
    Context ctx = new Context();
    Options options = Options.instance(ctx);
    options.put("useOptimizedZip", Boolean.toString(useOptimizedZip));
    if (!useSymbolFile) {
        options.put("ignore.symbol.file", "true");
    }
    return new JavacFileManager(ctx, false, null);
}
 
Example 23
Source Project: hottub   Source File: JavadocTool.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public DocumentationTask getTask(
        Writer out,
        JavaFileManager fileManager,
        DiagnosticListener<? super JavaFileObject> diagnosticListener,
        Class<?> docletClass,
        Iterable<String> options,
        Iterable<? extends JavaFileObject> compilationUnits) {
    Context context = new Context();
    return getTask(out, fileManager, diagnosticListener,
            docletClass, options, compilationUnits, context);
}
 
Example 24
@Override
public final void init(ProcessingEnvironment env) {
    super.init(env);
    JavacTask.instance(env).addTaskListener(listener);
    Context ctx = ((JavacProcessingEnvironment)processingEnv).getContext();
    JavaCompiler compiler = JavaCompiler.instance(ctx);
    compiler.shouldStopPolicyIfNoError = CompileState.max(compiler.shouldStopPolicyIfNoError,
            CompileState.FLOW);
}
 
Example 25
Source Project: openjdk-jdk9   Source File: Operators.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected Operators(Context context) {
    context.put(operatorsKey, this);
    syms = Symtab.instance(context);
    names = Names.instance(context);
    log = Log.instance(context);
    types = Types.instance(context);
    noOpSymbol = new OperatorSymbol(names.empty, Type.noType, -1, syms.noSymbol);
    initOperatorNames();
    initUnaryOperators();
    initBinaryOperators();
}
 
Example 26
Source Project: jdk8u60   Source File: CompileTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
void test(String[] opts, String className) throws Exception {
    count++;
    System.err.println("Test " + count + " " + Arrays.asList(opts) + " " + className);
    Path testSrcDir = Paths.get(System.getProperty("test.src"));
    Path testClassesDir = Paths.get(System.getProperty("test.classes"));
    Path classes = Files.createDirectory(Paths.get("classes." + count));

    Context ctx = new Context();
    PathFileManager fm = new JavacPathFileManager(ctx, true, null);
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    List<String> options = new ArrayList<String>();
    options.addAll(Arrays.asList(opts));
    options.addAll(Arrays.asList(
            "-verbose", "-XDverboseCompilePolicy",
            "-d", classes.toString(),
            "-g"
    ));
    Iterable<? extends JavaFileObject> compilationUnits =
            fm.getJavaFileObjects(testSrcDir.resolve(className + ".java"));
    StringWriter sw = new StringWriter();
    PrintWriter out = new PrintWriter(sw);
    JavaCompiler.CompilationTask t =
            compiler.getTask(out, fm, null, options, null, compilationUnits);
    boolean ok = t.call();
    System.err.println(sw.toString());
    if (!ok) {
        throw new Exception("compilation failed");
    }

    File expect = new File("classes." + count + "/" + className + ".class");
    if (!expect.exists())
        throw new Exception("expected file not found: " + expect);
    // Note that we explicitly specify -g for compiling both the actual class and the expected class.
    // This isolates the expected class from javac options that might be given to jtreg.
    long expectedSize = new File(testClassesDir.toString(), className + ".class").length();
    long actualSize = expect.length();
    if (expectedSize != actualSize)
        throw new Exception("wrong size found: " + actualSize + "; expected: " + expectedSize);
}
 
Example 27
Source Project: openjdk-8-source   Source File: T6877206.java    License: GNU General Public License v2.0 5 votes vote down vote up
JavacFileManager createFileManager(boolean useOptimizedZip, boolean useSymbolFile) {
    Context ctx = new Context();
    Options options = Options.instance(ctx);
    options.put("useOptimizedZip", Boolean.toString(useOptimizedZip));
    if (!useSymbolFile) {
        options.put("ignore.symbol.file", "true");
    }
    return new JavacFileManager(ctx, false, null);
}
 
Example 28
Source Project: java-n-IDE-for-Android   Source File: JavacFileManager.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create a JavacFileManager using a given context, optionally registering
 * it as the JavaFileManager for that context.
 */
public JavacFileManager(Context context, boolean register, Charset charset) {
    super(charset);
    if (register)
        context.put(JavaFileManager.class, this);
    setContext(context);
}
 
Example 29
Source Project: hottub   Source File: JavadocEnter.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static void preRegister(Context context) {
    context.put(enterKey, new Context.Factory<Enter>() {
           public Enter make(Context c) {
               return new JavadocEnter(c);
           }
    });
}
 
Example 30
Source Project: jdk8u60   Source File: Lint.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected Lint(Context context) {
    // initialize values according to the lint options
    Options options = Options.instance(context);
    values = EnumSet.noneOf(LintCategory.class);
    for (Map.Entry<String, LintCategory> e: map.entrySet()) {
        if (options.lint(e.getKey()))
            values.add(e.getValue());
    }

    suppressedValues = EnumSet.noneOf(LintCategory.class);

    context.put(lintKey, this);
    augmentor = new AugmentVisitor(context);
}