use of javax.tools.JavaFileObject.Kind in project auto by google.
the class CompilationErrorsTest method assertCompilationResultIs.
/**
* Assert that the result of compiling the source file whose lines are {@code testSourceCode}
* corresponds to the diagnostics in {@code expectedDiagnostics}. Each row of
* {@expectedDiagnostics} specifies a diagnostic kind (such as warning or error), a line number
* on which the diagnostic is expected, and a Pattern that is expected to match the diagnostic
* text. If the line number is 0 it is not checked.
*/
private void assertCompilationResultIs(Table<Diagnostic.Kind, Integer, Pattern> expectedDiagnostics, List<String> testSourceCode) throws IOException {
assertFalse(testSourceCode.isEmpty());
StringWriter compilerOut = new StringWriter();
List<String> options = ImmutableList.of("-sourcepath", tmpDir.getPath(), "-d", tmpDir.getPath(), "-processor", AutoValueProcessor.class.getName(), "-Xlint");
javac.getTask(compilerOut, fileManager, diagnosticCollector, options, null, null);
// This doesn't compile anything but communicates the paths to the JavaFileManager.
// Convert the strings containing the source code of the test classes into files that we
// can feed to the compiler.
List<String> classNames = Lists.newArrayList();
List<JavaFileObject> sourceFiles = Lists.newArrayList();
for (String source : testSourceCode) {
ClassName className = ClassName.extractFromSource(source);
File dir = new File(tmpDir, className.sourceDirectoryName());
dir.mkdirs();
// True if we just made it, or it was already there.
assertTrue(dir.isDirectory());
String sourceName = className.simpleName + ".java";
Files.write(source, new File(dir, sourceName), Charset.forName("UTF-8"));
classNames.add(className.fullName());
JavaFileObject sourceFile = fileManager.getJavaFileForInput(StandardLocation.SOURCE_PATH, className.fullName(), Kind.SOURCE);
sourceFiles.add(sourceFile);
}
assertEquals(classNames.size(), sourceFiles.size());
// Compile the classes.
JavaCompiler.CompilationTask javacTask = javac.getTask(compilerOut, fileManager, diagnosticCollector, options, classNames, sourceFiles);
boolean compiledOk = javacTask.call();
// Check that there were no compilation errors unless we were expecting there to be.
// We ignore "notes", typically debugging output from the annotation processor
// when that is enabled.
Table<Diagnostic.Kind, Integer, String> diagnostics = HashBasedTable.create();
for (Diagnostic<?> diagnostic : diagnosticCollector.getDiagnostics()) {
boolean ignore = (diagnostic.getKind() == Diagnostic.Kind.NOTE || (diagnostic.getKind() == Diagnostic.Kind.WARNING && diagnostic.getMessage(null).contains("No processor claimed any of these annotations")));
if (!ignore) {
diagnostics.put(diagnostic.getKind(), (int) diagnostic.getLineNumber(), diagnostic.getMessage(null));
}
}
assertEquals(diagnostics.containsRow(Diagnostic.Kind.ERROR), !compiledOk);
assertEquals("Diagnostic kinds should match: " + diagnostics, expectedDiagnostics.rowKeySet(), diagnostics.rowKeySet());
for (Table.Cell<Diagnostic.Kind, Integer, Pattern> expectedDiagnostic : expectedDiagnostics.cellSet()) {
boolean match = false;
for (Table.Cell<Diagnostic.Kind, Integer, String> diagnostic : diagnostics.cellSet()) {
if (expectedDiagnostic.getValue().matcher(diagnostic.getValue()).find()) {
int expectedLine = expectedDiagnostic.getColumnKey();
if (expectedLine != 0) {
int actualLine = diagnostic.getColumnKey();
if (actualLine != expectedLine) {
fail("Diagnostic matched pattern but on line " + actualLine + " not line " + expectedLine + ": " + diagnostic.getValue());
}
}
match = true;
break;
}
}
assertTrue("Diagnostics should contain " + expectedDiagnostic + ": " + diagnostics, match);
}
}
use of javax.tools.JavaFileObject.Kind in project querydsl by querydsl.
the class MemFileManager method list.
@Override
public Iterable<JavaFileObject> list(Location location, String pkg, Set<Kind> kinds, boolean recurse) throws IOException {
List<JavaFileObject> result = new ArrayList<JavaFileObject>();
for (JavaFileObject f : super.list(location, pkg, kinds, recurse)) {
result.add(f);
}
if (location == StandardLocation.CLASS_PATH) {
location = StandardLocation.CLASS_OUTPUT;
}
for (Kind kind : kinds) {
LocationAndKind key = new LocationAndKind(location, kind);
if (ramFileSystem.containsKey(key)) {
Map<String, JavaFileObject> locatedFiles = ramFileSystem.get(key);
for (Map.Entry<String, JavaFileObject> entry : locatedFiles.entrySet()) {
String name = entry.getKey();
String packageName = "";
if (name.indexOf('.') > -1) {
packageName = name.substring(0, name.lastIndexOf('.'));
}
if (recurse ? packageName.startsWith(pkg) : packageName.equals(pkg)) {
JavaFileObject candidate = entry.getValue();
if (kinds.contains(candidate.getKind())) {
result.add(candidate);
}
}
}
}
}
return result;
}
use of javax.tools.JavaFileObject.Kind in project enumerable by hraberg.
the class InMemoryCompiler method compile.
public Class<?> compile(String className, String source) throws IOException {
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
JavaFileManager manager = new ForwardingJavaFileManager<StandardJavaFileManager>(compiler.getStandardFileManager(null, null, null)) {
public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
return new ByteArrayFileObject(className.replace('/', '.'));
}
};
JavaFileObject file = new JavaSourceFromString(className, source);
List<String> options = new ArrayList<String>(asList("-source", "1.5", "-target", "1.5"));
if (debugInfo) {
options.add("-g");
}
if (useECJ) {
options.add("-warn:-raw");
options.add("-warn:-deadCode");
options.add("-warn:-serial");
}
CompilationTask task = compiler.getTask(null, manager, diagnostics, options, null, Arrays.asList(file));
boolean success = task.call();
if (success) {
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
throw uncheck(e);
}
} else {
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));
}
return null;
}
}
Aggregations