Files
processing4/java/test/processing/mode/java/UTCompiler.java
A Samuel Pottinger ee299ef935 Move to ANTLR 4 with Java 11 lang features and localization. (#5)
* Move to ANTLR 4 with Java 11 lang features and localization.

Introduces ANTLR4 and Java 8 language feature support within IDE while also adding additional hooks for localization of syntax error messages, addressing https://github.com/processing/processing/issues/3054 and https://github.com/processing/processing/issues/3055.

The PR is broadly a continuation of https://github.com/processing/processing/issues/3055, bringing it up to speed with the latest Processing master plus the changes introduced at https://github.com/processing/processing/pull/5753. **Requires https://github.com/processing/processing/pull/5753 as pre-requisite.** This introduces a number of edits beyond https://github.com/processing/processing/issues/3055 beyond compatibility with current Processing master and https://github.com/processing/processing/pull/5753 including:

 - Update to the grammar itself
 - Change ANTLR listeners to emit `TextTransform.Edit` to unify JDT-based `PreprocessingService` and `JavaBuild`, removing code with duplicate purpose.
 - Introduction of syntax error rewriting with support for localization.
 - Addition of complete localized strings set for English and Spanish.
 - Addition of partial localized strings set for other languages.
 - Refactor of ANTLR-related code for testability and readability
 - Expansion of tests including full parse tests for new Java features (type inference, lambdas).

* Ask travis for ant upgrade prior to run.

* Ask Travis for java11 update.

* Add openjdk ppa

* Travis no confirmation on add ppa.

* Force newer ant on travis.

* Swtich ant download to www-us mirror.

* Switch ant to 1.10.7

* Start x for unit tests in travis.

* More complete start x in travis.

* Revert x in travis.

* Try x in services.
2019-10-05 23:34:38 -07:00

61 lines
1.9 KiB
Java

package processing.mode.java;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import processing.app.Platform;
import processing.app.exec.ProcessHelper;
import processing.app.exec.ProcessResult;
/**
* Utility class for compiling single compilationUnits.
*
* @author Jonathan Feinberg <jdf@pobox.com>
*
*/
class UTCompiler {
private final String classpath;
UTCompiler(File... classpath) throws IOException {
final StringBuilder sb = new StringBuilder();
for (final File f : classpath) {
if (sb.length() > 0)
sb.append(File.pathSeparatorChar);
sb.append(f.getAbsolutePath());
}
this.classpath = sb.toString();
}
ProcessResult compile(final String name, final String program)
throws IOException {
final File tmpdir = File.createTempFile("utcompiler", ".tmp");
if (!tmpdir.delete())
throw new IOException("Cannot delete " + tmpdir);
if (!tmpdir.mkdir())
throw new IOException("Cannot create " + tmpdir);
final File javaFile = new File(tmpdir, name + ".java");
final FileWriter java = new FileWriter(javaFile);
try {
java.write(program);
} finally {
java.close();
}
try {
return new ProcessHelper("javac",
"-sourcepath", tmpdir.getAbsolutePath(),
"-cp", classpath,
"-nowarn",
"-d", tmpdir.getAbsolutePath(),
javaFile.getAbsolutePath()).execute();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
for (final File f: tmpdir.listFiles())
if (!f.getName().startsWith("."))if (!f.delete())
throw new IOException("Can't delete " + f);
if (!tmpdir.delete())
throw new IOException("Can't delete " + tmpdir);
}
}
}