Search in sources :

Example 6 with DefaultProblemFactory

use of org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory in project che by eclipse.

the class IndexManager method getSourceElementParser.

public SourceElementParser getSourceElementParser(IJavaProject project, ISourceElementRequestor requestor) {
    // disable task tags to speed up parsing
    Map options = project.getOptions(true);
    //$NON-NLS-1$
    options.put(JavaCore.COMPILER_TASK_TAGS, "");
    try {
        SourceElementParser parser = new IndexingParser(requestor, new DefaultProblemFactory(Locale.getDefault()), new CompilerOptions(options), // index local declarations
        true, // optimize string literals
        true, // do not use source javadoc parser to speed up parsing
        false);
        parser.reportOnlyOneSyntaxError = true;
        // Always check javadoc while indexing
        parser.javadocParser.checkDocComment = true;
        parser.javadocParser.reportProblems = false;
        return parser;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Also used : SourceElementParser(org.eclipse.jdt.internal.compiler.SourceElementParser) CompilerOptions(org.eclipse.jdt.internal.compiler.impl.CompilerOptions) DefaultProblemFactory(org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory) Map(java.util.Map) JavaModelException(org.eclipse.jdt.core.JavaModelException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IOException(java.io.IOException)

Example 7 with DefaultProblemFactory

use of org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory in project jetbrick-template-1x by subchen.

the class JdtCompiler method generateJavaClass.

@Override
protected void generateJavaClass(JavaSource source) throws IOException {
    INameEnvironment env = new NameEnvironment(source);
    IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.proceedWithAllProblems();
    CompilerOptions options = getCompilerOptions();
    CompilerRequestor requestor = new CompilerRequestor();
    IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault());
    Compiler compiler = new Compiler(env, policy, options, requestor, problemFactory);
    compiler.compile(new ICompilationUnit[] { new CompilationUnit(source) });
    if (requestor.hasErrors()) {
        String sourceCode = source.getSourceCode();
        String[] sourceCodeLines = sourceCode.split("(\r\n|\r|\n)", -1);
        StringBuilder sb = new StringBuilder();
        sb.append("Compilation failed.");
        sb.append('\n');
        for (IProblem p : requestor.getErrors()) {
            sb.append(p.getMessage()).append('\n');
            int start = p.getSourceStart();
            // default
            int column = start;
            for (int i = start; i >= 0; i--) {
                char c = sourceCode.charAt(i);
                if (c == '\n' || c == '\r') {
                    column = start - i;
                    break;
                }
            }
            sb.append(StringUtils.getPrettyError(sourceCodeLines, p.getSourceLineNumber(), column, p.getSourceStart(), p.getSourceEnd(), 3));
        }
        sb.append(requestor.getErrors().length);
        sb.append(" error(s)\n");
        throw new CompileErrorException(sb.toString());
    }
    requestor.save(source.getOutputdir());
}
Also used : Compiler(org.eclipse.jdt.internal.compiler.Compiler) IProblem(org.eclipse.jdt.core.compiler.IProblem) CompilerOptions(org.eclipse.jdt.internal.compiler.impl.CompilerOptions) DefaultProblemFactory(org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory)

Example 8 with DefaultProblemFactory

use of org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory in project opennms by OpenNMS.

the class CustomJRJdtCompiler method compileUnits.

@Override
protected String compileUnits(final JRCompilationUnit[] units, String classpath, File tempDirFile) {
    final INameEnvironment env = getNameEnvironment(units);
    final IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.proceedWithAllProblems();
    final CompilerOptions options = getJdtSettings();
    final IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault());
    final CompilerRequestor requestor = getCompilerRequestor(units);
    final Compiler compiler = new Compiler(env, policy, options, requestor, problemFactory);
    do {
        CompilationUnit[] compilationUnits = requestor.processCompilationUnits();
        compiler.compile(compilationUnits);
    } while (requestor.hasMissingMethods());
    requestor.processProblems();
    return requestor.getFormattedProblems();
}
Also used : IErrorHandlingPolicy(org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy) JRCompilationUnit(net.sf.jasperreports.engine.design.JRCompilationUnit) ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) JRAbstractJavaCompiler(net.sf.jasperreports.engine.design.JRAbstractJavaCompiler) Compiler(org.eclipse.jdt.internal.compiler.Compiler) ICompilerRequestor(org.eclipse.jdt.internal.compiler.ICompilerRequestor) CompilerOptions(org.eclipse.jdt.internal.compiler.impl.CompilerOptions) INameEnvironment(org.eclipse.jdt.internal.compiler.env.INameEnvironment) DefaultProblemFactory(org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory) IProblemFactory(org.eclipse.jdt.internal.compiler.IProblemFactory)

Example 9 with DefaultProblemFactory

use of org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory in project webpieces by deanhiller.

the class CompilerWrapper method compile.

/**
     * Please compile this className
     */
@SuppressWarnings("deprecation")
public void compile(String[] classNames, ClassDefinitionLoader loader) {
    ICompilationUnit[] compilationUnits = new CompilationUnit[classNames.length];
    for (int i = 0; i < classNames.length; i++) {
        compilationUnits[i] = new CompilationUnit(classNames[i]);
    }
    IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.exitOnFirstError();
    IProblemFactory problemFactory = new DefaultProblemFactory(Locale.ENGLISH);
    /**
         * To find types ...
         */
    INameEnvironment nameEnvironment = new INameEnvironmentImpl(loader);
    /**
         * Compilation result
         */
    ICompilerRequestor compilerRequestor = new ICompilerRequestorImpl();
    /**
         * 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) 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)

Aggregations

DefaultProblemFactory (org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory)9 Compiler (org.eclipse.jdt.internal.compiler.Compiler)6 CompilerOptions (org.eclipse.jdt.internal.compiler.impl.CompilerOptions)6 CompilationResult (org.eclipse.jdt.internal.compiler.CompilationResult)5 ICompilerRequestor (org.eclipse.jdt.internal.compiler.ICompilerRequestor)5 IProblemFactory (org.eclipse.jdt.internal.compiler.IProblemFactory)5 ICompilationUnit (org.eclipse.jdt.internal.compiler.env.ICompilationUnit)5 IErrorHandlingPolicy (org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy)4 CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)4 INameEnvironment (org.eclipse.jdt.internal.compiler.env.INameEnvironment)4 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 IProblem (org.eclipse.jdt.core.compiler.IProblem)2 SourceElementParser (org.eclipse.jdt.internal.compiler.SourceElementParser)2 Nullable (com.android.annotations.Nullable)1 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedReader (java.io.BufferedReader)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1