Search in sources :

Example 11 with CompilationResult

use of org.eclipse.jdt.internal.compiler.CompilationResult in project Japid by branaway.

the class RendererCompiler method compile.

/**
 * Please compile this className and set the bytecode to the class holder
 */
@SuppressWarnings("deprecation")
public void compile(String[] classNames) {
    ICompilationUnit[] compilationUnits = new CompilationUnit[classNames.length];
    for (int i = 0; i < classNames.length; i++) {
        compilationUnits[i] = new CompilationUnit(this, classNames[i]);
    }
    IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.exitOnFirstError();
    IProblemFactory problemFactory = new DefaultProblemFactory(Locale.ENGLISH);
    /**
     * To find types ...
     */
    INameEnvironment nameEnvironment = new NameEnv(this);
    /**
     * Compilation result
     */
    ICompilerRequestor compilerRequestor = new CompilerRequestor(this);
    /**
     * The JDT compiler
     */
    Compiler jdtCompiler = new Compiler(nameEnvironment, policy, settings, compilerRequestor, problemFactory) {

        @Override
        protected void handleInternalException(Throwable e, CompilationUnitDeclaration ud, CompilationResult result) {
        }
    };
    // Go !
    jdtCompiler.compile(compilationUnits);
}
Also used : ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) IErrorHandlingPolicy(org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy) ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) Compiler(org.eclipse.jdt.internal.compiler.Compiler) ICompilerRequestor(org.eclipse.jdt.internal.compiler.ICompilerRequestor) ICompilerRequestor(org.eclipse.jdt.internal.compiler.ICompilerRequestor) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) INameEnvironment(org.eclipse.jdt.internal.compiler.env.INameEnvironment) DefaultProblemFactory(org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory) CompilationResult(org.eclipse.jdt.internal.compiler.CompilationResult) IProblemFactory(org.eclipse.jdt.internal.compiler.IProblemFactory)

Example 12 with CompilationResult

use of org.eclipse.jdt.internal.compiler.CompilationResult 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, int minVersion, boolean checkPositions) 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);
    char[] sourceArray = source.toCharArray();
    final ICompilationUnit sourceUnit;
    try {
        sourceUnit = getSourceUnit(file, source);
    } catch (Throwable t) {
        t.printStackTrace();
        return false;
    }
    Compiler ecjCompiler = new Compiler(createFileSystem(file, minVersion), 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);
        }
    };
    // initializeEclipseBundles();
    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 {
        String output = cud.toString();
        // starting somewhere around ecj16, the print code is a bit too cavalier with printing modifiers.
        output = output.replace("non-sealed @val", "@val");
        result.append(output);
    }
    if (eclipseAvailable()) {
        EclipseDomConversion.toDomAst(cud, sourceArray);
    }
    return true;
}
Also used : ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) Compiler(org.eclipse.jdt.internal.compiler.Compiler) 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

CompilationResult (org.eclipse.jdt.internal.compiler.CompilationResult)12 DefaultProblemFactory (org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory)10 ICompilationUnit (org.eclipse.jdt.internal.compiler.env.ICompilationUnit)9 ICompilerRequestor (org.eclipse.jdt.internal.compiler.ICompilerRequestor)8 Compiler (org.eclipse.jdt.internal.compiler.Compiler)7 IErrorHandlingPolicy (org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy)6 INameEnvironment (org.eclipse.jdt.internal.compiler.env.INameEnvironment)6 CompilerOptions (org.eclipse.jdt.internal.compiler.impl.CompilerOptions)6 ClassFile (org.eclipse.jdt.internal.compiler.ClassFile)5 IProblemFactory (org.eclipse.jdt.internal.compiler.IProblemFactory)5 CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 CategorizedProblem (org.eclipse.jdt.core.compiler.CategorizedProblem)4 IProblem (org.eclipse.jdt.core.compiler.IProblem)4 ClassFileReader (org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader)4 NameEnvironmentAnswer (org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer)4 BufferedOutputStream (java.io.BufferedOutputStream)3