com.sun.tools.javac.main.Main Java Examples
The following examples show how to use
com.sun.tools.javac.main.Main.
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: CompileHelper.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
public static int compileJava(Context context, JavaProjectFolder projectFile, @Nullable DiagnosticListener listener) { try { String[] args = new String[]{ "-verbose", "-cp", projectFile.getJavaClassPath(context), "-sourcepath", projectFile.getSourcePath(), //sourcepath "-d", projectFile.getDirBuildClasses().getPath(), //output dir projectFile.getMainClass().getPath(projectFile) //main class }; DLog.d(TAG, "compileJava args = " + Arrays.toString(args)); int compileStatus; compileStatus = Javac.compile(args, listener); return compileStatus; } catch (Throwable e) { e.printStackTrace(); } return Main.EXIT_ERROR; }
Example #2
Source File: CompilerTest.java From JQF with BSD 2-Clause "Simplified" License | 6 votes |
@Fuzz public void testWithString(@From(AsciiStringGenerator.class) String classBody) { // Construct test class with provided body String code = String.format("class Test { %s }", classBody); File javaFile; try { javaFile = writeToFile(code); } catch (IOException e) { throw new RuntimeException(e); } // Compile using javac + error-prone Main.Result result = ErrorProneCompiler.compile(new String[]{ javaFile.getAbsolutePath()}, new PrintWriter(new ByteArrayOutputStream( ))); // Ensure that there was no issue with test command or environment Assert.assertFalse(result == CMDERR); Assert.assertFalse(result == SYSERR); // Ignore compilation errors (only semantically valid inputs will pass) Assume.assumeFalse(result == ERROR); // This is the main test criteria -- abnormal exits are bugs Assert.assertFalse( result == ABNORMAL); }
Example #3
Source File: Log.java From lua-for-android with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Creates a log. * @param context the context in which the log should be registered * @param writers a map of writers that can be accessed by the kind of writer required */ private Log(Context context, Map<WriterKind, PrintWriter> writers) { super(JCDiagnostic.Factory.instance(context)); context.put(logKey, this); this.writers = writers; @SuppressWarnings("unchecked") // FIXME DiagnosticListener<? super JavaFileObject> dl = context.get(DiagnosticListener.class); this.diagListener = dl; diagnosticHandler = new DefaultDiagnosticHandler(); messages = JavacMessages.instance(context); messages.add(Main.javacBundleName); final Options options = Options.instance(context); initOptions(options); options.addListener(() -> initOptions(options)); }
Example #4
Source File: Log.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
/** Construct a log with given I/O redirections. */ protected Log(Context context, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) { super(JCDiagnostic.Factory.instance(context)); context.put(logKey, this); this.errWriter = errWriter; this.warnWriter = warnWriter; this.noticeWriter = noticeWriter; @SuppressWarnings("unchecked") // FIXME DiagnosticListener<? super JavaFileObject> dl = context.get(DiagnosticListener.class); this.diagListener = dl; diagnosticHandler = new DefaultDiagnosticHandler(); messages = JavacMessages.instance(context); messages.add(Main.javacBundleName); final Options options = Options.instance(context); initOptions(options); options.addListener(new Runnable() { public void run() { initOptions(options); } }); }
Example #5
Source File: CompileFail.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
public static void main(String... args) { if (args.length < 2) throw new IllegalArgumentException("insufficient args"); int expected_rc = getReturnCode(args[0]); List<String> javacArgs = new ArrayList<>(); javacArgs.addAll(Arrays.asList( "-d", "." )); File testSrc = new File(System.getProperty("test.src")); for (int i = 1; i < args.length; i++) { // skip first arg String arg = args[i]; if (arg.endsWith(".java")) javacArgs.add(new File(testSrc, arg).getPath()); else javacArgs.add(arg); } int rc = com.sun.tools.javac.Main.compile( javacArgs.toArray(new String[javacArgs.size()])); if (rc != expected_rc) throw new Error("unexpected exit code: " + rc + ", expected: " + expected_rc); }
Example #6
Source File: StdoutCloseTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
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 #7
Source File: Log.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
/** Construct a log with given I/O redirections. */ protected Log(Context context, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) { super(JCDiagnostic.Factory.instance(context)); context.put(logKey, this); this.errWriter = errWriter; this.warnWriter = warnWriter; this.noticeWriter = noticeWriter; @SuppressWarnings("unchecked") // FIXME DiagnosticListener<? super JavaFileObject> dl = context.get(DiagnosticListener.class); this.diagListener = dl; diagnosticHandler = new DefaultDiagnosticHandler(); messages = JavacMessages.instance(context); messages.add(Main.javacBundleName); final Options options = Options.instance(context); initOptions(options); options.addListener(new Runnable() { public void run() { initOptions(options); } }); }
Example #8
Source File: ProfileBootClassPathTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@OptionModesTester.Test void testProfileBootClassPath() throws IOException { writeFile("C.java", "class C { }"); String javaHome = System.getProperty("java.home"); String rt_jar = new File(javaHome, "jre/lib/rt.jar").getPath(); String[] opts = { "-profile", "compact1", "-bootclasspath", rt_jar }; String[] files = { "C.java" }; runMain(opts, files) .checkResult(Main.Result.CMDERR.exitCode); // The API tests are disabled (for now) because Args.validate does // not have an easy way to access/validate file manager options, // which are handled directly by the file manager // runCall(opts, files) // .checkIllegalStateException(); // runParse(opts, files) // .checkIllegalStateException(); }
Example #9
Source File: StdOutTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@Test void testXstdout() throws IOException { String[] args = { "-Xstdout", "out.txt" }; String[] files = { "src/Bad.java" }; // verify messages get written to the specified file runMain(args, files) .checkResult(Main.Result.ERROR.exitCode); if (!Files.exists(Paths.get("out.txt"))) { error("expected file not found"); } runCall(args, files) .checkIllegalArgumentException(); runParse(args, files) .checkIllegalArgumentException(); }
Example #10
Source File: ProfileTargetTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@Test void testProfileTarget() throws IOException { writeFile("C.java", "class C { }"); String[] opts = { "-profile", "compact1", "-source", "7", "-target", "7" }; String[] files = { "C.java" }; runMain(opts, files) .checkResult(Main.Result.CMDERR.exitCode); runCall(opts, files) .checkIllegalStateException(); runParse(opts, files) .checkIllegalStateException(); }
Example #11
Source File: DocLintTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@Test void testDocLint() throws IOException { writeFile("src/C.java", "/** & */ class C { }"); mkdirs("classes"); String[] opts = { "-d", "classes", "-Xdoclint" }; String[] files = { "src/C.java" }; runMain(opts, files) .checkResult(Main.Result.ERROR.exitCode); runCall(opts, files) .checkResult(false); // 1. doclint runs at the beginning of analyze // 2. the analyze call itself succeeds, so we check for errors being reported runAnalyze(opts, files) .checkResult(true) .checkLog(Log.DIRECT, "^"); }
Example #12
Source File: OutputDirTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
void testOutputDir(String opt) { String[] opts = { opt, "does-not-exist"}; String[] files = { "src/C.java" }; runMain(opts, files) .checkResult(Main.Result.OK.exitCode); // The API tests are disabled (for now) because Args.validate does // not have an easy way to access/validate file manager options, // which are handled directly by the file manager // runCall(opts, files) // .checkIllegalStateException(); // runParse(opts, files) // .checkIllegalStateException(); }
Example #13
Source File: Log.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
/** Construct a log with given I/O redirections. */ protected Log(Context context, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) { super(JCDiagnostic.Factory.instance(context)); context.put(logKey, this); this.errWriter = errWriter; this.warnWriter = warnWriter; this.noticeWriter = noticeWriter; @SuppressWarnings("unchecked") // FIXME DiagnosticListener<? super JavaFileObject> dl = context.get(DiagnosticListener.class); this.diagListener = dl; diagnosticHandler = new DefaultDiagnosticHandler(); messages = JavacMessages.instance(context); messages.add(Main.javacBundleName); final Options options = Options.instance(context); initOptions(options); options.addListener(new Runnable() { public void run() { initOptions(options); } }); }
Example #14
Source File: DocLintTest.java From hottub with GNU General Public License v2.0 | 5 votes |
void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) { System.err.println("test: " + opts); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); List<JavaFileObject> files = Arrays.asList(file); try { DocumentationTask t = javadoc.getTask(pw, fm, null, null, opts, files); boolean ok = t.call(); pw.close(); String out = sw.toString().replaceAll("[\r\n]+", "\n"); if (!out.isEmpty()) System.err.println(out); if (ok && expectResult != Main.Result.OK) { error("Compilation succeeded unexpectedly"); } else if (!ok && expectResult != Main.Result.ERROR) { error("Compilation failed unexpectedly"); } else check(out, expectMessages); } catch (IllegalArgumentException e) { System.err.println(e); String expectOut = expectMessages.iterator().next().text; if (expectResult != Main.Result.CMDERR) error("unexpected exception caught"); else if (!e.getMessage().equals(expectOut)) { error("unexpected exception message: " + e.getMessage() + " expected: " + expectOut); } } // if (errors > 0) // throw new Error("stop"); }
Example #15
Source File: DocLintTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) { System.err.println("test: " + opts); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); List<JavaFileObject> files = Arrays.asList(file); try { JavacTask t = (JavacTask) javac.getTask(pw, fm, null, opts, null, files); boolean ok = t.call(); pw.close(); String out = sw.toString().replaceAll("[\r\n]+", "\n"); if (!out.isEmpty()) System.err.println(out); if (ok && expectResult != Main.Result.OK) { error("Compilation succeeded unexpectedly"); } else if (!ok && expectResult != Main.Result.ERROR) { error("Compilation failed unexpectedly"); } else check(out, expectMessages); } catch (IllegalArgumentException e) { System.err.println(e); String expectOut = expectMessages.iterator().next().text; if (expectResult != Main.Result.CMDERR) error("unexpected exception caught"); else if (!e.getMessage().equals(expectOut)) { error("unexpected exception message: " + e.getMessage() + " expected: " + expectOut); } } // if (errors > 0) // throw new Error("stop"); }
Example #16
Source File: T7021650.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
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 #17
Source File: JavacTool.java From javaide with GNU General Public License v3.0 | 5 votes |
public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) { if (err == null) err = System.err; for (String argument : arguments) argument.getClass(); // null check return com.sun.tools.javac.Main.compile(arguments, new PrintWriter(err, true)); }
Example #18
Source File: JavacParserTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Test //JDK-8065753 void testWrongFirstToken() throws IOException { String code = "<"; String expectedErrors = "Test.java:1:1: compiler.err.expected3: class, interface, enum\n" + "1 error\n"; StringWriter out = new StringWriter(); JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(out, fm, null, Arrays.asList("-XDrawDiagnostics"), null, Arrays.asList(new MyFileObject(code))); Result errorCode = ct.doCall(); assertEquals("the error code is not correct; actual:" + errorCode, Main.Result.ERROR, errorCode); String actualErrors = normalize(out.toString()); assertEquals("the error message is not correct, actual: " + actualErrors, expectedErrors, actualErrors); }
Example #19
Source File: NoOperandsTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Test void testNoOperands() throws IOException { String[] opts = { "-d", "." }; String[] files = { }; runMain(opts, files) .checkResult(Main.Result.CMDERR.exitCode); runCall(opts, files) .checkIllegalStateException(); runParse(opts, files) .checkIllegalStateException(); }
Example #20
Source File: Example.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@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 (keys != 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; int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw); if (keys != null) { pw.close(); scanForKeys(sw.toString(), keys); } return (rc == 0); }
Example #21
Source File: CompileFail.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
public static void main(String... args) { if (args.length < 2) throw new IllegalArgumentException("insufficient args"); int expected_rc = getReturnCode(args[0]); List<String> javacArgs = new ArrayList<>(); javacArgs.addAll(Arrays.asList( "-bootclasspath", System.getProperty("sun.boot.class.path"), "-d", "." )); File testSrc = new File(System.getProperty("test.src")); for (int i = 1; i < args.length; i++) { // skip first arg String arg = args[i]; if (arg.endsWith(".java")) javacArgs.add(new File(testSrc, arg).getPath()); else javacArgs.add(arg); } int rc = com.sun.tools.javac.Main.compile( javacArgs.toArray(new String[javacArgs.size()])); if (rc != expected_rc) throw new Error("unexpected exit code: " + rc + ", expected: " + expected_rc); }
Example #22
Source File: DocLintTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) { System.err.println("test: " + opts); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); List<JavaFileObject> files = Arrays.asList(file); try { JavacTask t = (JavacTask) javac.getTask(pw, fm, null, opts, null, files); boolean ok = t.call(); pw.close(); String out = sw.toString().replaceAll("[\r\n]+", "\n"); if (!out.isEmpty()) System.err.println(out); if (ok && expectResult != Main.Result.OK) { error("Compilation succeeded unexpectedly"); } else if (!ok && expectResult != Main.Result.ERROR) { error("Compilation failed unexpectedly"); } else check(out, expectMessages); } catch (IllegalArgumentException e) { System.err.println(e); String expectOut = expectMessages.iterator().next().text; if (expectResult != Main.Result.CMDERR) error("unexpected exception caught"); else if (!e.getMessage().equals(expectOut)) { error("unexpected exception message: " + e.getMessage() + " expected: " + expectOut); } } // if (errors > 0) // throw new Error("stop"); }
Example #23
Source File: T7021650.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
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 #24
Source File: CompileFail.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
public static void main(String... args) { if (args.length < 2) throw new IllegalArgumentException("insufficient args"); int expected_rc = getReturnCode(args[0]); List<String> javacArgs = new ArrayList<>(); javacArgs.addAll(Arrays.asList( "-bootclasspath", System.getProperty("sun.boot.class.path"), "-d", "." )); File testSrc = new File(System.getProperty("test.src")); for (int i = 1; i < args.length; i++) { // skip first arg String arg = args[i]; if (arg.endsWith(".java")) javacArgs.add(new File(testSrc, arg).getPath()); else javacArgs.add(arg); } int rc = com.sun.tools.javac.Main.compile( javacArgs.toArray(new String[javacArgs.size()])); if (rc != expected_rc) throw new Error("unexpected exit code: " + rc + ", expected: " + expected_rc); }
Example #25
Source File: MethodParametersTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
String compile(String... args) throws Exception { System.err.println("compile: " + Arrays.asList(args)); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); int rc = com.sun.tools.javac.Main.compile(args, pw); pw.close(); String out = sw.toString(); if (out.length() > 0) System.err.println(out); if (rc != 0) error("compilation failed, rc=" + rc); return out; }
Example #26
Source File: DocLintTest.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) { System.err.println("test: " + opts); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); List<JavaFileObject> files = Arrays.asList(file); try { DocumentationTask t = javadoc.getTask(pw, fm, null, null, opts, files); boolean ok = t.call(); pw.close(); String out = sw.toString().replaceAll("[\r\n]+", "\n"); if (!out.isEmpty()) System.err.println(out); if (ok && expectResult != Main.Result.OK) { error("Compilation succeeded unexpectedly"); } else if (!ok && expectResult != Main.Result.ERROR) { error("Compilation failed unexpectedly"); } else check(out, expectMessages); } catch (IllegalArgumentException e) { System.err.println(e); String expectOut = expectMessages.iterator().next().text; if (expectResult != Main.Result.CMDERR) error("unexpected exception caught"); else if (!e.getMessage().equals(expectOut)) { error("unexpected exception message: " + e.getMessage() + " expected: " + expectOut); } } // if (errors > 0) // throw new Error("stop"); }
Example #27
Source File: Example.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
@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 #28
Source File: MethodParametersTest.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
String compile(String... args) throws Exception { System.err.println("compile: " + Arrays.asList(args)); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); int rc = com.sun.tools.javac.Main.compile(args, pw); pw.close(); String out = sw.toString(); if (out.length() > 0) System.err.println(out); if (rc != 0) error("compilation failed, rc=" + rc); return out; }
Example #29
Source File: JavacTool.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) { if (err == null) err = System.err; for (String argument : arguments) argument.getClass(); // null check return com.sun.tools.javac.Main.compile(arguments, new PrintWriter(err, true)); }
Example #30
Source File: IncludePackagesTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) { System.err.println("test: " + opts); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); try { JavacTask t = (JavacTask) javac.getTask(pw, fm, null, opts, null, files); boolean ok = t.call(); pw.close(); String out = sw.toString().replaceAll("[\r\n]+", "\n"); if (!out.isEmpty()) System.err.println(out); if (ok && expectResult != Main.Result.OK) { error("Compilation succeeded unexpectedly"); } else if (!ok && expectResult != Main.Result.ERROR) { error("Compilation failed unexpectedly"); } else check(out, expectMessages); } catch (IllegalArgumentException e) { System.err.println(e); String expectOut = expectMessages.iterator().next().text; if (expectResult != Main.Result.CMDERR) error("unexpected exception caught"); else if (!e.getMessage().equals(expectOut)) { error("unexpected exception message: " + e.getMessage() + " expected: " + expectOut); } } }