Search in sources :

Example 1 with CompilerMessage

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());
        }
    };
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CompilerMessage(lombok.javac.CapturingDiagnosticListener.CompilerMessage) ConfigurationResolverFactory(lombok.core.configuration.ConfigurationResolverFactory) FileTester(lombok.DirectoryRunner.FileTester) StringWriter(java.io.StringWriter) ConfigurationResolver(lombok.core.configuration.ConfigurationResolver)

Example 2 with CompilerMessage

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();
    }
}
Also used : CompilerMessage(lombok.javac.CapturingDiagnosticListener.CompilerMessage) FileOutputStream(java.io.FileOutputStream)

Example 3 with CompilerMessage

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;
    }
}
Also used : CompilerMessage(lombok.javac.CapturingDiagnosticListener.CompilerMessage) File(java.io.File)

Example 4 with CompilerMessage

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;
}
Also used : CompilationUnit(org.eclipse.jdt.internal.compiler.batch.CompilationUnit) ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) Compiler(org.eclipse.jdt.internal.compiler.Compiler) ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) CompilerMessage(lombok.javac.CapturingDiagnosticListener.CompilerMessage) ICompilerRequestor(org.eclipse.jdt.internal.compiler.ICompilerRequestor) AtomicReference(java.util.concurrent.atomic.AtomicReference) CategorizedProblem(org.eclipse.jdt.core.compiler.CategorizedProblem) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) CompilationResult(org.eclipse.jdt.internal.compiler.CompilationResult) DefaultProblemFactory(org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory)

Example 5 with CompilerMessage

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;
    }
}
Also used : CompilerMessage(lombok.javac.CapturingDiagnosticListener.CompilerMessage)

Aggregations

CompilerMessage (lombok.javac.CapturingDiagnosticListener.CompilerMessage)5 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 StringWriter (java.io.StringWriter)1 LinkedHashSet (java.util.LinkedHashSet)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 FileTester (lombok.DirectoryRunner.FileTester)1 ConfigurationResolver (lombok.core.configuration.ConfigurationResolver)1 ConfigurationResolverFactory (lombok.core.configuration.ConfigurationResolverFactory)1 CategorizedProblem (org.eclipse.jdt.core.compiler.CategorizedProblem)1 CompilationResult (org.eclipse.jdt.internal.compiler.CompilationResult)1 Compiler (org.eclipse.jdt.internal.compiler.Compiler)1 ICompilerRequestor (org.eclipse.jdt.internal.compiler.ICompilerRequestor)1 CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)1 CompilationUnit (org.eclipse.jdt.internal.compiler.batch.CompilationUnit)1 ICompilationUnit (org.eclipse.jdt.internal.compiler.env.ICompilationUnit)1 DefaultProblemFactory (org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory)1