Example usage for org.eclipse.jdt.internal.compiler.problem DefaultProblem errorReportSource

List of usage examples for org.eclipse.jdt.internal.compiler.problem DefaultProblem errorReportSource

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.problem DefaultProblem errorReportSource.

Prototype

public String errorReportSource(char[] unitSource) 

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  . ja  v  a2s .  co 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();
}