use of javax.tools.DiagnosticCollector in project Gargoyle by callakrsos.
the class JavaSourceFromString method main.
public static void main(String[] args) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
StringWriter writer = new StringWriter();
PrintWriter out = new PrintWriter(writer);
// out.println("package pak;");
out.println("public class HelloWorld {");
out.println(" public static void main(String args[]) {");
out.println(" System.out.println(\"This is in another java file\");");
out.println(" }");
out.println("}");
out.close();
JavaFileObject file = new JavaSourceFromString("pak/HelloWorld", writer.toString());
Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(file);
CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, compilationUnits);
boolean success = task.call();
for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
System.out.println(diagnostic.getCode());
System.out.println(diagnostic.getKind());
System.out.println(diagnostic.getPosition());
System.out.println(diagnostic.getStartPosition());
System.out.println(diagnostic.getEndPosition());
System.out.println(diagnostic.getSource());
System.out.println(diagnostic.getMessage(null));
}
System.out.println("Success: " + success);
if (success) {
try {
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { new File("").toURI().toURL() });
Class.forName("HelloWorld", true, classLoader).getDeclaredMethod("main", new Class[] { String[].class }).invoke(null, new Object[] { null });
} catch (ClassNotFoundException e) {
System.err.println("Class not found: " + e);
} catch (NoSuchMethodException e) {
System.err.println("No such method: " + e);
} catch (IllegalAccessException e) {
System.err.println("Illegal access: " + e);
} catch (InvocationTargetException e) {
System.err.println("Invocation target: " + e);
}
}
}
use of javax.tools.DiagnosticCollector in project ceylon-compiler by ceylon.
the class T6458823 method main.
public static void main(String[] args) throws Exception {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
throw new RuntimeException("can't get javax.tools.JavaCompiler!");
}
DiagnosticCollector<JavaFileObject> diagColl = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
List<String> options = new ArrayList<String>();
options.add("-processor");
options.add("MyProcessor");
options.add("-proc:only");
List<File> files = new ArrayList<File>();
files.add(new File(T6458823.class.getResource("TestClass.java").toURI()));
final CompilationTask task = compiler.getTask(null, fm, diagColl, options, null, fm.getJavaFileObjectsFromFiles(files));
task.call();
int diagCount = 0;
for (Diagnostic<? extends JavaFileObject> diag : diagColl.getDiagnostics()) {
if (diag.getKind() != Diagnostic.Kind.WARNING) {
throw new AssertionError("Only warnings expected");
}
System.out.println(diag);
if (diag.getPosition() == Diagnostic.NOPOS) {
throw new AssertionError("No position info in message");
}
if (diag.getSource() == null) {
throw new AssertionError("No source info in message");
}
diagCount++;
}
if (diagCount != 2) {
throw new AssertionError("unexpected number of warnings: " + diagCount + ", expected: 2");
}
}
use of javax.tools.DiagnosticCollector in project ceylon-compiler by ceylon.
the class T6956638 method test.
void test(File... sourceFiles) throws Exception {
System.err.println("Test " + (++count) + ": " + Arrays.asList(sourceFiles));
File classesDir = new File("classes" + count);
classesDir.mkdirs();
StringWriter compilerOutputStream = new StringWriter();
List<String> compileOptions = Arrays.asList("-d", classesDir.getPath());
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector, null, null);
Iterable<? extends JavaFileObject> sourceFileObjects = fileManager.getJavaFileObjects(sourceFiles);
System.err.println("1- javac given java source JavaFileObjects " + sourceFileObjects);
JavaCompiler.CompilationTask task = compiler.getTask(compilerOutputStream, fileManager, null, compileOptions, null, sourceFileObjects);
JavacTask javacTask = (JavacTask) task;
Iterable<? extends CompilationUnitTree> parsedTrees = javacTask.parse();
Iterable<? extends Element> analyzedTrees = javacTask.analyze();
Iterable<? extends JavaFileObject> generatedFiles = javacTask.generate();
System.err.println("2- parsed:" + size(parsedTrees) + " analysed:" + size(analyzedTrees) + " generated:" + size(generatedFiles));
System.err.print("3-");
for (JavaFileObject f : generatedFiles) System.err.print(" " + f);
System.err.println("");
System.err.print("5-");
for (File f : classesDir.listFiles()) System.err.print(" " + f);
System.err.println("");
System.err.println("----");
System.err.println(compilerOutputStream.toString());
if (size(generatedFiles) != size(parsedTrees)) {
throw new Exception("wrong number of files generated: " + size(generatedFiles) + " expected: " + size(parsedTrees));
}
}
Aggregations