Java Code Examples for com.sun.tools.javac.file.JavacFileManager#preRegister()

The following examples show how to use com.sun.tools.javac.file.JavacFileManager#preRegister() . 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 File: MakeLiteralTest.java    From hottub with 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 2
Source File: StarImportTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Setup env by creating pseudo-random collection of names, packages and classes.
 */
void setup() {
    log ("setup");
    context = new Context();
    JavacFileManager.preRegister(context); // required by ClassReader which is required by Symtab
    make = TreeMaker.instance(context);
    names = Names.instance(context);       // Name.Table impls tied to an instance of Names
    symtab = Symtab.instance(context);
    types = Types.instance(context);
    int setupCount = rgen.nextInt(MAX_SETUP_COUNT);
    for (int i = 0; i < setupCount; i++) {
        switch (random(SetupKind.values())) {
            case NAMES:
                setupNames();
                break;
            case PACKAGE:
                setupPackage();
                break;
            case CLASS:
                setupClass();
                break;
        }
    }
}
 
Example 3
Source File: MakeLiteralTest.java    From openjdk-jdk9 with 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 4
Source File: MakeLiteralTest.java    From jdk8u60 with 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 5
Source File: TypeHarness.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
protected TypeHarness() {
    Context ctx = new Context();
    JavacFileManager.preRegister(ctx);
    types = Types.instance(ctx);
    chk = Check.instance(ctx);
    predef = Symtab.instance(ctx);
    names = Names.instance(ctx);
    fac = new Factory();
}
 
Example 6
Source File: Example.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
boolean run(PrintWriter out, Set<String> keys, boolean raw, List<String> opts, List<File> files) {
    if (out != null && keys != null)
        throw new IllegalArgumentException();

    if (verbose)
        System.err.println("run_simple: " + opts + " " + files);

    List<String> args = new ArrayList<String>();

    if (out != null && raw)
        args.add("-XDrawDiagnostics");

    args.addAll(opts);
    for (File f: files)
        args.add(f.getPath());

    StringWriter sw = null;
    PrintWriter pw;
    if (keys != null) {
        sw = new StringWriter();
        pw = new PrintWriter(sw);
    } else
        pw = out;

    Context c = new Context();
    JavacFileManager.preRegister(c); // can't create it until Log has been set up
    MessageTracker.preRegister(c, keys);
    Main m = new Main("javac", pw);
    Main.Result rc = m.compile(args.toArray(new String[args.size()]), c);

    if (keys != null) {
        pw.close();
    }

    return rc.isOK();
}
 
Example 7
Source File: TypeHarness.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
protected TypeHarness() {
    Context ctx = new Context();
    JavacFileManager.preRegister(ctx);
    types = Types.instance(ctx);
    chk = Check.instance(ctx);
    predef = Symtab.instance(ctx);
    names = Names.instance(ctx);
    fac = new Factory();
}
 
Example 8
Source File: TestLog.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
static void test(boolean genEndPos) throws IOException {
    Context context = new Context();

    Options options = Options.instance(context);
    options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");

    Log log = Log.instance(context);
    log.multipleErrors = true;

    JavacFileManager.preRegister(context);
    ParserFactory pfac = ParserFactory.instance(context);

    final String text =
          "public class Foo {\n"
        + "  public static void main(String[] args) {\n"
        + "    if (args.length == 0)\n"
        + "      System.out.println(\"no args\");\n"
        + "    else\n"
        + "      System.out.println(args.length + \" args\");\n"
        + "  }\n"
        + "}\n";
    JavaFileObject fo = new StringJavaFileObject("Foo", text);
    log.useSource(fo);

    CharSequence cs = fo.getCharContent(true);
    Parser parser = pfac.newParser(cs, false, genEndPos, false);
    JCTree.JCCompilationUnit tree = parser.parseCompilationUnit();
    log.setEndPosTable(fo, tree.endPositions);

    TreeScanner ts = new LogTester(log, tree.endPositions);
    ts.scan(tree);

    check(log.nerrors, 4, "errors");
    check(log.nwarnings, 4, "warnings");
}
 
Example 9
Source File: TestLog.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static void test(boolean genEndPos) throws IOException {
    Context context = new Context();

    Options options = Options.instance(context);
    options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");

    Log log = Log.instance(context);
    log.multipleErrors = true;

    JavacFileManager.preRegister(context);
    ParserFactory pfac = ParserFactory.instance(context);

    final String text =
          "public class Foo {\n"
        + "  public static void main(String[] args) {\n"
        + "    if (args.length == 0)\n"
        + "      System.out.println(\"no args\");\n"
        + "    else\n"
        + "      System.out.println(args.length + \" args\");\n"
        + "  }\n"
        + "}\n";
    JavaFileObject fo = new StringJavaFileObject("Foo", text);
    log.useSource(fo);

    CharSequence cs = fo.getCharContent(true);
    Parser parser = pfac.newParser(cs, false, genEndPos, false);
    JCTree.JCCompilationUnit tree = parser.parseCompilationUnit();
    log.setEndPosTable(fo, tree.endPositions);

    TreeScanner ts = new LogTester(log, tree.endPositions);
    ts.scan(tree);

    check(log.nerrors, 4, "errors");
    check(log.nwarnings, 4, "warnings");
}
 
Example 10
Source File: TestSymtabItems.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
void run() throws Exception {
        Context c = new Context();
        JavacFileManager.preRegister(c);
        Symtab syms = Symtab.instance(c);
        JavacTypes types = JavacTypes.instance(c);
        JavaCompiler.instance(c);  // will init ClassReader.sourceCompleter

//        print("noSymbol", syms.noSymbol);
//        print("errSymbol", syms.errSymbol);
//        print("unknownSymbol", syms.unknownSymbol);
//        print("botType", syms.botType);
//        print("errType", syms.errType);
//        print("unknownType", syms.unknownType);

        for (Field f: Symtab.class.getDeclaredFields()) {
//            System.err.println(f.getType() + " " + f.getName());

            // Temporarily ignore methodHandle and transientMethodHandle
            // during API evolution
            if (f.getName().toLowerCase().contains("methodhandle"))
                continue;

            Class<?> ft = f.getType();
            if (TypeMirror.class.isAssignableFrom(ft))
                print(f.getName(), (TypeMirror) f.get(syms), types);
            else if(Element.class.isAssignableFrom(ft))
                print(f.getName(), (Element) f.get(syms));
        }

        if (errors > 0)
            throw new Exception(errors + " errors occurred");
    }
 
Example 11
Source File: T8024415.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public T8024415() {
    Context ctx = new Context();
    JavacFileManager.preRegister(ctx);
    maker = TreeMaker.instance(ctx);
    Names names = Names.instance(ctx);
    x = maker.Ident(names.fromString("x"));
}
 
Example 12
Source File: ImplementationCacheTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    List<? extends JavaFileObject> files = Arrays.asList(new SourceFile());
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, files);
    Context ctx = new Context();
    JavacFileManager.preRegister(ctx);
    checkImplementationCache(ct.analyze(), Types.instance(ctx));
}
 
Example 13
Source File: TestSymtabItems.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
void run() throws Exception {
        Context c = new Context();
        JavacFileManager.preRegister(c);
        Symtab syms = Symtab.instance(c);
        JavacTypes types = JavacTypes.instance(c);
        JavaCompiler.instance(c);  // will init ClassReader.sourceCompleter

//        print("noSymbol", syms.noSymbol);
//        print("errSymbol", syms.errSymbol);
//        print("unknownSymbol", syms.unknownSymbol);
//        print("botType", syms.botType);
//        print("errType", syms.errType);
//        print("unknownType", syms.unknownType);

        for (Field f: Symtab.class.getDeclaredFields()) {
//            System.err.println(f.getType() + " " + f.getName());

            // Temporarily ignore methodHandle and transientMethodHandle
            // during API evolution
            if (f.getName().toLowerCase().contains("methodhandle"))
                continue;

            Class<?> ft = f.getType();
            if (TypeMirror.class.isAssignableFrom(ft))
                print(f.getName(), (TypeMirror) f.get(syms), types);
            else if(Element.class.isAssignableFrom(ft))
                print(f.getName(), (Element) f.get(syms));
        }

        if (errors > 0)
            throw new Exception(errors + " errors occurred");
    }
 
Example 14
Source File: Main.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Programmatic interface for main function.
 * @param args  the command line parameters
 * @return the result of the compilation
 */
public Result compile(String[] args) {
    Context context = new Context();
    JavacFileManager.preRegister(context); // can't create it until Log has been set up
    Result result = compile(args, context);
    if (fileManager instanceof JavacFileManager) {
        try {
            // A fresh context was created above, so jfm must be a JavacFileManager
            ((JavacFileManager)fileManager).close();
        } catch (IOException ex) {
            bugMessage(ex);
        }
    }
    return result;
}
 
Example 15
Source File: ImplementationCacheTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    List<? extends JavaFileObject> files = Arrays.asList(new SourceFile());
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, files);
    Context ctx = new Context();
    JavacFileManager.preRegister(ctx);
    checkImplementationCache(ct.analyze(), Types.instance(ctx));
}
 
Example 16
Source File: TestLog.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static void test(boolean genEndPos) throws IOException {
    Context context = new Context();

    Options options = Options.instance(context);
    options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");

    Log log = Log.instance(context);
    log.multipleErrors = true;

    JavacFileManager.preRegister(context);
    ParserFactory pfac = ParserFactory.instance(context);

    final String text =
          "public class Foo {\n"
        + "  public static void main(String[] args) {\n"
        + "    if (args.length == 0)\n"
        + "      System.out.println(\"no args\");\n"
        + "    else\n"
        + "      System.out.println(args.length + \" args\");\n"
        + "  }\n"
        + "}\n";
    JavaFileObject fo = new StringJavaFileObject("Foo", text);
    log.useSource(fo);

    CharSequence cs = fo.getCharContent(true);
    Parser parser = pfac.newParser(cs, false, genEndPos, false);
    JCTree.JCCompilationUnit tree = parser.parseCompilationUnit();
    log.setEndPosTable(fo, tree.endPositions);

    TreeScanner ts = new LogTester(log, tree.endPositions);
    ts.scan(tree);

    check(log.nerrors, 4, "errors");
    check(log.nwarnings, 4, "warnings");
}
 
Example 17
Source File: TypeHarness.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
protected TypeHarness() {
    Context ctx = new Context();
    JavacFileManager.preRegister(ctx);
    types = Types.instance(ctx);
    chk = Check.instance(ctx);
    predef = Symtab.instance(ctx);
    names = Names.instance(ctx);
    fac = new Factory();
}
 
Example 18
Source File: MethodParametersTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
void inspectBaz() throws Exception {
    final File Qux_java =
        writeFile(classesdir, Qux_name + ".java", Qux_contents);
    final String[] args = { "-XDsave-parameter-names", "-d",
                            classesdir.getPath(),
                            "-cp", classesdir.getPath(),
                            Qux_java.getPath() };
    final StringWriter sw = new StringWriter();
    final PrintWriter pw = new PrintWriter(sw);

    // We need to be able to crack open javac and look at its data
    // structures.  We'll rig up a compiler instance, but keep its
    // Context, thus allowing us to get at the ClassReader.
    Context context = new Context();
    Main comp =  new Main("javac", pw);
    JavacFileManager.preRegister(context);

    // Compile Qux, which uses Baz.
    comp.compile(args, context);
    pw.close();
    final String out = sw.toString();
    if (out.length() > 0)
        System.err.println(out);

    // Now get the class reader, construct a name for Baz, and load it.
    com.sun.tools.javac.jvm.ClassReader cr =
        com.sun.tools.javac.jvm.ClassReader.instance(context);
    Name name = Names.instance(context).fromString(Baz_name);

    // Now walk down the language model and check the name of the
    // parameter.
    final Element baz = cr.loadClass(name);
    for (Element e : baz.getEnclosedElements()) {
        if (e instanceof ExecutableElement) {
            final ExecutableElement ee = (ExecutableElement) e;
            final List<? extends VariableElement> params =
                ee.getParameters();
            if (1 != params.size())
                throw new Exception("Classfile Baz badly formed: wrong number of methods");
            final VariableElement param = params.get(0);
            if (!param.getSimpleName().contentEquals("baz")) {
                errors++;
                System.err.println("javac did not correctly resolve the metadata conflict, parameter's name reads as " + param.getSimpleName());
            } else
                System.err.println("javac did correctly resolve the metadata conflict");
        }
    }
}
 
Example 19
Source File: MissingLNTEntryForFinalizerTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
MissingLNTEntryForFinalizerTest() {
    context = new Context();
    JavacFileManager.preRegister(context);
    MyAttr.preRegister(context);
    tool = new ReusableJavaCompiler(context);
}
 
Example 20
Source File: MethodParametersTest.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
void inspectBaz() throws Exception {
    final File Qux_java =
        writeFile(classesdir, Qux_name + ".java", Qux_contents);
    final String[] args = { "-XDsave-parameter-names", "-d",
                            classesdir.getPath(),
                            "-cp", classesdir.getPath(),
                            Qux_java.getPath() };
    final StringWriter sw = new StringWriter();
    final PrintWriter pw = new PrintWriter(sw);

    // We need to be able to crack open javac and look at its data
    // structures.  We'll rig up a compiler instance, but keep its
    // Context, thus allowing us to get at the ClassReader.
    Context context = new Context();
    Main comp =  new Main("javac", pw);
    JavacFileManager.preRegister(context);

    // Compile Qux, which uses Baz.
    comp.compile(args, context);
    pw.close();
    final String out = sw.toString();
    if (out.length() > 0)
        System.err.println(out);

    // Now get the class reader, construct a name for Baz, and load it.
    com.sun.tools.javac.jvm.ClassReader cr =
        com.sun.tools.javac.jvm.ClassReader.instance(context);
    Name name = Names.instance(context).fromString(Baz_name);

    // Now walk down the language model and check the name of the
    // parameter.
    final Element baz = cr.loadClass(name);
    for (Element e : baz.getEnclosedElements()) {
        if (e instanceof ExecutableElement) {
            final ExecutableElement ee = (ExecutableElement) e;
            final List<? extends VariableElement> params =
                ee.getParameters();
            if (1 != params.size())
                throw new Exception("Classfile Baz badly formed: wrong number of methods");
            final VariableElement param = params.get(0);
            if (!param.getSimpleName().contentEquals("baz")) {
                errors++;
                System.err.println("javac did not correctly resolve the metadata conflict, parameter's name reads as " + param.getSimpleName());
            } else
                System.err.println("javac did correctly resolve the metadata conflict");
        }
    }
}