Example usage for org.eclipse.jdt.internal.compiler CompilationResult getAllProblems

List of usage examples for org.eclipse.jdt.internal.compiler CompilationResult getAllProblems

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler CompilationResult getAllProblems.

Prototype

public CategorizedProblem[] getAllProblems() 

Source Link

Usage

From source file:de.plugins.eclipse.depclipse.testcommons.Util.java

License:Open Source License

/**
 * Returns the compilation errors / warnings for the given CompilationResult.
 *
 * @param compilationResult the compilation result
 * @param showCategory/*ww w.  j a  v a  2 s .c o  m*/
 * @param showWarningToken
 * @return String the problem log
 */
public static String getProblemLog(CompilationResult compilationResult, boolean showCategory,
        boolean showWarningToken) {
    StringBuffer buffer = new StringBuffer(100);
    if (compilationResult.hasProblems() || compilationResult.hasTasks()) {
        CategorizedProblem[] problems = compilationResult.getAllProblems();
        int count = problems.length;
        int problemCount = 0;
        char[] unitSource = compilationResult.compilationUnit.getContents();
        for (int i = 0; i < count; i++) {
            DefaultProblem problem = (DefaultProblem) problems[i];
            if (problem != null) {
                if (problemCount == 0)
                    buffer.append("----------\n");
                problemCount++;
                buffer.append(problemCount + (problem.isError() ? ". ERROR" : ". WARNING"));
                buffer.append(" in " + new String(problem.getOriginatingFileName()).replace('/', '\\'));
                try {
                    buffer.append(problem.errorReportSource(unitSource));
                    buffer.append("\n");
                    if (showCategory) {
                        String category = problem.getInternalCategoryMessage();
                        if (category != null) {
                            buffer.append("[@cat:").append(category).append("] ");
                        }
                    }
                    if (showWarningToken) {
                        int irritant = ProblemReporter.getIrritant(problem.getID());
                        if (irritant != 0) {
                            String warningToken = CompilerOptions.warningTokenFromIrritant(irritant);
                            if (warningToken != null) {
                                buffer.append("[@sup:").append(warningToken).append("] ");
                            }
                        }
                    }
                    buffer.append(problem.getMessage());
                    buffer.append("\n");
                } catch (Exception e) {
                }
                buffer.append("----------\n");
            }
        }
    }
    return buffer.toString();
}

From source file:fr.inria.astor.core.manipulation.compiler.bytecode.JDTByteCodeCompiler.java

License:Open Source License

/**
 * //from w  ww  .ja  va  2  s. c  o m
 */
@Override
public void acceptResult(CompilationResult result) {
    if (result.hasErrors()) {
        String problems = "";
        for (CategorizedProblem cp : result.getAllProblems()) {
            problems += new String(result.getFileName()) + "-l:(" + cp.getSourceLineNumber() + "):"
                    + cp.toString() + ", ";
        }
        classErrors.add(problems);
    }

    for (ClassFile f : result.getClassFiles()) {
        classFiles.add(f);
    }
}

From source file:lombok.RunTestsViaEcj.java

License:Open Source License

@Override
public void 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/*from   w w w  .j av  a2s . c o m*/
        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());
}

From source file:org.ant4eclipse.lib.jdt.ecj.internal.tools.CompilerRequestorImpl.java

License:Open Source License

/**
 * {@inheritDoc}/*from  w  w w . j  a  v  a 2s .  c  o m*/
 */
public void acceptResult(CompilationResult result) {

    // get the compilation unit...
    CompilationUnitImpl compilationUnitImpl = (CompilationUnitImpl) result.getCompilationUnit();

    // ...and the source file
    SourceFile sourceFile = compilationUnitImpl.getSourceFile();

    // return immediately if the source file is a ReferableSourceFile
    if (sourceFile instanceof ReferableSourceFile) {

        // add the problems...
        if (result.getAllProblems() != null) {
            if (A4ELogging.isTraceingEnabled()) {
                A4ELogging.trace("Could not compile referenced class '%s'. Reason: %s",
                        sourceFile.getSourceFileName(), Arrays.asList(result.getAllProblems()));
            }
        }

        return;
    }

    // get the destination directory
    File destinationDirectory = sourceFile.getDestinationFolder();

    if (!result.hasErrors()) {
        ClassFile[] classFiles = result.getClassFiles();
        for (ClassFile classFile2 : classFiles) {
            char[][] compoundName = classFile2.getCompoundName();
            StringBuffer classFileName = new StringBuffer();
            for (int j = 0; j < compoundName.length; j++) {
                classFileName.append(compoundName[j]);
                if (j < compoundName.length - 1) {
                    classFileName.append('/');
                }
            }
            classFileName.append(".class");
            File classFile = new File(destinationDirectory, classFileName.toString());
            File classDir = classFile.getParentFile();
            if (!classDir.exists()) {
                classDir.mkdirs();
            }
            try {
                A4ELogging.debug("writing class file: '%s'", classFile);
                Utilities.writeFile(classFile, classFile2.getBytes());
                this._compiledClassFiles.put(classFileName.toString(), classFile);
            } catch (Ant4EclipseException ioe) {
                A4ELogging.error("Could not write classfile '%s': %s", classFileName.toString(),
                        ioe.toString());
                ioe.printStackTrace();
                this._compilationSuccessful = false;
            }
        }
    } else {
        this._compilationSuccessful = false;
    }

    // add the problems...
    if (result.getAllProblems() != null) {
        this._categorizedProblems.addAll(Arrays.asList(result.getAllProblems()));
    }
}

From source file:org.codehaus.groovy.eclipse.core.compiler.ErrorRecoveryTests.java

License:Apache License

public void testDotNothing1() {
    CompilationResult result = compileScript("s.");
    assertEquals(1, result.getAllProblems().length);
}

From source file:org.codehaus.groovy.eclipse.core.compiler.ErrorRecoveryTests.java

License:Apache License

public void testDotNothing2() {
    CompilationResult result = compileScript("s.a.");
    assertEquals(1, result.getAllProblems().length);
}

From source file:org.codehaus.groovy.eclipse.core.compiler.ErrorRecoveryTests.java

License:Apache License

public void testDotNothing3() {
    CompilationResult result = compileScript("s[10].");
    assertEquals(1, result.getAllProblems().length);
}

From source file:org.codehaus.groovy.eclipse.core.compiler.ErrorRecoveryTests.java

License:Apache License

public void testDotNothing4() {
    CompilationResult result = compileScript("s().");
    assertEquals(1, result.getAllProblems().length);
}

From source file:org.codehaus.groovy.eclipse.core.compiler.ErrorRecoveryTests.java

License:Apache License

public void testDotNothing5() {
    CompilationResult result = compileScript("s { it }.");
    assertEquals(1, result.getAllProblems().length);
}

From source file:org.codehaus.groovy.eclipse.core.compiler.ErrorRecoveryTests.java

License:Apache License

public void testDotNothing6() {
    CompilationResult result = compileScript("String s = 'hello'; s.");
    assertEquals(1, result.getAllProblems().length);
}