Search in sources :

Example 1 with CompilationUnit

use of org.eclipse.jdt.internal.compiler.batch.CompilationUnit 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)

Aggregations

AtomicReference (java.util.concurrent.atomic.AtomicReference)1 CompilerMessage (lombok.javac.CapturingDiagnosticListener.CompilerMessage)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