Java Code Examples for com.sun.tools.javac.main.Main#compile()

The following examples show how to use com.sun.tools.javac.main.Main#compile() . 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: StdoutCloseTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public PrintWriter compileClass(String src, String out) {
    List<String> options = new ArrayList<>();
    options.add("-Xstdout");
    options.add(out);
    options.add(src);

    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    Main compiler = new Main("javac", pw);
    compiler.compile(options.toArray(new String[options.size()]));
    pw.flush();
    if (sw.getBuffer().length() > 0) {
        System.err.println(sw.toString());
    }
    return compiler.log.getWriter(Log.WriterKind.NOTICE);
}
 
Example 2
Source File: Example.java    From TencentKona-8 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 3
Source File: T7021650.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
void compile(Context context, String... args) throws Exception {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    Main m = new Main("javac", pw);
    Main.Result res = m.compile(args, context);
    pw.close();
    String out = sw.toString();
    if (!out.isEmpty())
        System.err.println(out);
    if (!res.isOK())
        throw new Exception("compilation failed unexpectedly: result=" + res);
}
 
Example 4
Source File: Example.java    From jdk8u60 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 5
Source File: T7021650.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
void compile(Context context, String... args) throws Exception {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    Main m = new Main("javac", pw);
    Main.Result res = m.compile(args, context);
    pw.close();
    String out = sw.toString();
    if (!out.isEmpty())
        System.err.println(out);
    if (!res.isOK())
        throw new Exception("compilation failed unexpectedly: result=" + res);
}
 
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: T7021650.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
void compile(Context context, String... args) throws Exception {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    Main m = new Main("javac", pw);
    Main.Result res = m.compile(args, context);
    pw.close();
    String out = sw.toString();
    if (!out.isEmpty())
        System.err.println(out);
    if (!res.isOK())
        throw new Exception("compilation failed unexpectedly: result=" + res);
}
 
Example 8
Source File: T7021650.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
void compile(Context context, String... args) throws Exception {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    Main m = new Main("javac", pw);
    Main.Result res = m.compile(args, context);
    pw.close();
    String out = sw.toString();
    if (!out.isEmpty())
        System.err.println(out);
    if (!res.isOK())
        throw new Exception("compilation failed unexpectedly: result=" + res);
}
 
Example 9
Source File: Example.java    From openjdk-jdk8u-backup 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 10
Source File: T7021650.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
void compile(Context context, String... args) throws Exception {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    Main m = new Main("javac", pw);
    Main.Result res = m.compile(args, context);
    pw.close();
    String out = sw.toString();
    if (!out.isEmpty())
        System.err.println(out);
    if (!res.isOK())
        throw new Exception("compilation failed unexpectedly: result=" + res);
}
 
Example 11
Source File: T7021650.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
void compile(Context context, String... args) throws Exception {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    Main m = new Main("javac", pw);
    Main.Result res = m.compile(args, context);
    pw.close();
    String out = sw.toString();
    if (!out.isEmpty())
        System.err.println(out);
    if (!res.isOK())
        throw new Exception("compilation failed unexpectedly: result=" + res);
}
 
Example 12
Source File: Example.java    From openjdk-8 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 13
Source File: Example.java    From hottub 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 14
Source File: MethodParametersTest.java    From openjdk-8-source 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 15
Source File: MethodParametersTest.java    From openjdk-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 16
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");
        }
    }
}
 
Example 17
Source File: Example.java    From openjdk-jdk9 with GNU General Public License v2.0 4 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);

    try {
        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();
    } finally {
        close(c.get(JavaFileManager.class));
    }
}
 
Example 18
Source File: MethodParametersTest.java    From openjdk-jdk8u 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: MethodParametersTest.java    From jdk8u60 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 20
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");
        }
    }
}