use of lombok.javac.CapturingDiagnosticListener.CompilerMessage in project lombok by rzwitserloot.
the class AbstractRunTests method createTester.
public final FileTester createTester(final DirectoryRunner.TestParams params, final File file, String platform, int version) throws IOException {
ConfigurationKeysLoader.LoaderLoader.loadAllConfigurationKeys();
AssertionError directiveFailure = null;
LombokTestSource sourceDirectives = null;
try {
sourceDirectives = LombokTestSource.readDirectives(file);
if (sourceDirectives.isIgnore())
return null;
if (!sourceDirectives.versionWithinLimit(version))
return null;
if (!sourceDirectives.runOnPlatform(platform))
return null;
} catch (AssertionError ae) {
directiveFailure = ae;
}
String fileName = file.getName();
final LombokTestSource expected = LombokTestSource.read(params.getAfterDirectory(), params.getMessagesDirectory(), fileName);
if (expected.isIgnore())
return null;
if (!expected.versionWithinLimit(params.getVersion()))
return null;
if (!expected.versionWithinLimit(version))
return null;
final LombokTestSource sourceDirectives_ = sourceDirectives;
final AssertionError directiveFailure_ = directiveFailure;
return new FileTester() {
@Override
public void runTest() throws Throwable {
if (directiveFailure_ != null)
throw directiveFailure_;
LinkedHashSet<CompilerMessage> messages = new LinkedHashSet<CompilerMessage>();
StringWriter writer = new StringWriter();
LombokConfiguration.overrideConfigurationResolverFactory(new ConfigurationResolverFactory() {
@Override
public ConfigurationResolver createResolver(AST<?, ?, ?> ast) {
return sourceDirectives_.getConfiguration();
}
});
boolean changed = transformCode(messages, writer, file, sourceDirectives_.getSpecifiedEncoding(), sourceDirectives_.getFormatPreferences());
boolean forceUnchanged = sourceDirectives_.forceUnchanged() || sourceDirectives_.isSkipCompareContent();
if (params.expectChanges() && !forceUnchanged && !changed)
messages.add(new CompilerMessage(-1, -1, true, "not flagged modified"));
if (!params.expectChanges() && changed)
messages.add(new CompilerMessage(-1, -1, true, "unexpected modification"));
compare(file.getName(), expected, writer.toString(), messages, params.printErrors(), sourceDirectives_.isSkipCompareContent() || expected.isSkipCompareContent());
}
};
}
use of lombok.javac.CapturingDiagnosticListener.CompilerMessage in project lombok by rzwitserloot.
the class AbstractRunTests method dumpToFile.
private static void dumpToFile(File file, Collection<CompilerMessage> content) throws IOException {
FileOutputStream fos = new FileOutputStream(file);
try {
for (CompilerMessage message : content) {
fos.write(CompilerMessageMatcher.asCompilerMessageMatcher(message).toString().getBytes("UTF-8"));
fos.write('\n');
}
} finally {
fos.close();
}
}
use of lombok.javac.CapturingDiagnosticListener.CompilerMessage in project lombok by rzwitserloot.
the class AbstractRunTests method compare.
private void compare(String name, LombokTestSource expected, String actualFile, LinkedHashSet<CompilerMessage> actualMessages, boolean printErrors, boolean skipCompareContent) throws Throwable {
if (!skipCompareContent)
try {
compareContent(name, expected.getContent(), actualFile);
} catch (Throwable e) {
if (printErrors) {
System.out.println("***** " + name + " *****");
System.out.println(e.getMessage());
System.out.println("**** Expected ******");
System.out.println(expected.getContent());
System.out.println("**** Actual ******");
System.out.println(actualFile);
if (actualMessages != null && !actualMessages.isEmpty()) {
System.out.println("**** Actual Errors *****");
for (CompilerMessage actualMessage : actualMessages) {
System.out.println(actualMessage);
}
}
System.out.println("*******************");
}
if (dumpActualFilesHere != null) {
dumpToFile(new File(dumpActualFilesHere, name), actualFile);
}
throw e;
}
try {
compareMessages(name, expected.getMessages(), actualMessages);
} catch (Throwable e) {
if (printErrors) {
System.out.println("***** " + name + " *****");
System.out.println(e.getMessage());
System.out.println("**** Expected ******");
for (CompilerMessageMatcher expectedMessage : expected.getMessages()) {
System.out.println(expectedMessage);
}
System.out.println("**** Actual ******");
for (CompilerMessage actualMessage : actualMessages) {
System.out.println(actualMessage);
}
System.out.println("*******************");
}
if (dumpActualFilesHere != null) {
dumpToFile(new File(dumpActualFilesHere, name + ".messages"), actualMessages);
}
throw e;
}
}
use of lombok.javac.CapturingDiagnosticListener.CompilerMessage in project lombok by rzwitserloot.
the class RunTestsViaEcj method transformCode.
@Override
public boolean transformCode(Collection<CompilerMessage> messages, StringWriter result, File file, String encoding, Map<String, String> formatPreferences) throws Throwable {
final AtomicReference<CompilationResult> compilationResult_ = new AtomicReference<CompilationResult>();
final AtomicReference<CompilationUnitDeclaration> compilationUnit_ = new AtomicReference<CompilationUnitDeclaration>();
ICompilerRequestor bitbucketRequestor = new ICompilerRequestor() {
@Override
public void acceptResult(CompilationResult result) {
compilationResult_.set(result);
}
};
String source = readFile(file);
final CompilationUnit sourceUnit = new CompilationUnit(source.toCharArray(), file.getName(), encoding == null ? "UTF-8" : encoding);
Compiler ecjCompiler = new Compiler(createFileSystem(file), ecjErrorHandlingPolicy(), ecjCompilerOptions(), bitbucketRequestor, new DefaultProblemFactory(Locale.ENGLISH)) {
@Override
protected synchronized void addCompilationUnit(ICompilationUnit inUnit, CompilationUnitDeclaration parsedUnit) {
if (inUnit == sourceUnit)
compilationUnit_.set(parsedUnit);
super.addCompilationUnit(inUnit, parsedUnit);
}
};
ecjCompiler.compile(new ICompilationUnit[] { sourceUnit });
CompilationResult compilationResult = compilationResult_.get();
CategorizedProblem[] problems = compilationResult.getAllProblems();
if (problems != null)
for (CategorizedProblem p : problems) {
messages.add(new CompilerMessage(p.getSourceLineNumber(), p.getSourceStart(), p.isError(), p.getMessage()));
}
CompilationUnitDeclaration cud = compilationUnit_.get();
if (cud == null)
result.append("---- NO CompilationUnit provided by ecj ----");
else
result.append(cud.toString());
return true;
}
use of lombok.javac.CapturingDiagnosticListener.CompilerMessage in project lombok by rzwitserloot.
the class AbstractRunTests method compareMessages.
@SuppressWarnings("null")
private static /* eclipse bug; it falsely thinks stuffAc will always be null or some such hogwash. */
void compareMessages(String name, LombokImmutableList<CompilerMessageMatcher> expected, LinkedHashSet<CompilerMessage> actual) {
Iterator<CompilerMessageMatcher> expectedIterator = expected.iterator();
Iterator<CompilerMessage> actualIterator = actual.iterator();
CompilerMessage stuffAc = null;
while (true) {
boolean exHasNext = expectedIterator.hasNext();
boolean acHasNext = stuffAc != null || actualIterator.hasNext();
if (!exHasNext && !acHasNext)
break;
if (exHasNext && acHasNext) {
CompilerMessageMatcher cmm = expectedIterator.next();
CompilerMessage cm = stuffAc == null ? actualIterator.next() : stuffAc;
if (cmm.matches(cm))
continue;
if (cmm.isOptional())
stuffAc = cm;
fail(String.format("[%s] Expected message '%s' but got message '%s'", name, cmm, cm));
throw new AssertionError("fail should have aborted already.");
}
while (expectedIterator.hasNext()) {
CompilerMessageMatcher next = expectedIterator.next();
if (next.isOptional())
continue;
fail(String.format("[%s] Expected message '%s' but ran out of actual messages", name, next));
}
if (acHasNext)
fail(String.format("[%s] Unexpected message: %s", name, actualIterator.next()));
break;
}
}
Aggregations