Search in sources :

Example 1 with IProblemRequestor

use of org.eclipse.jdt.core.IProblemRequestor in project che by eclipse.

the class JavaReconciler method reconcile.

public ReconcileResult reconcile(IJavaProject javaProject, String fqn) throws JavaModelException {
    final ProblemRequestor requestor = new ProblemRequestor();
    WorkingCopyOwner wcOwner = new WorkingCopyOwner() {

        public IProblemRequestor getProblemRequestor(ICompilationUnit unit) {
            return requestor;
        }

        @Override
        public IBuffer createBuffer(ICompilationUnit workingCopy) {
            //                ?????
            return new org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter(workingCopy, (IFile) workingCopy.getResource());
        }
    };
    List<HighlightedPosition> positions = null;
    ICompilationUnit compilationUnit = null;
    try {
        IType type = javaProject.findType(fqn);
        if (type == null) {
            return null;
        }
        if (type.isBinary()) {
            throw new IllegalArgumentException("Can't reconcile binary type: " + fqn);
        } else {
            compilationUnit = type.getCompilationUnit().getWorkingCopy(wcOwner, null);
        }
        requestor.reset();
        CompilationUnit unit = compilationUnit.reconcile(AST.JLS8, true, wcOwner, null);
        positions = semanticHighlighting.reconcileSemanticHighlight(unit);
        if (compilationUnit instanceof ClassFileWorkingCopy) {
            //we don't wont to show any errors from ".class" files
            requestor.reset();
        }
    } catch (JavaModelException e) {
        LOG.error("Can't reconcile class: " + fqn + " in project:" + javaProject.getPath().toOSString(), e);
        throw e;
    } finally {
        if (compilationUnit != null && compilationUnit.isWorkingCopy()) {
            try {
                //todo close buffer
                compilationUnit.getBuffer().close();
                compilationUnit.discardWorkingCopy();
            } catch (JavaModelException e) {
            //ignore
            }
        }
    }
    ReconcileResult result = DtoFactory.getInstance().createDto(ReconcileResult.class);
    result.setProblems(convertProblems(requestor.problems));
    result.setHighlightedPositions(positions);
    return result;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) IProblemRequestor(org.eclipse.jdt.core.IProblemRequestor) IType(org.eclipse.jdt.core.IType) ClassFileWorkingCopy(org.eclipse.jdt.internal.core.ClassFileWorkingCopy) HighlightedPosition(org.eclipse.che.ide.ext.java.shared.dto.HighlightedPosition) ReconcileResult(org.eclipse.che.ide.ext.java.shared.dto.ReconcileResult) WorkingCopyOwner(org.eclipse.jdt.core.WorkingCopyOwner)

Example 2 with IProblemRequestor

use of org.eclipse.jdt.core.IProblemRequestor in project che by eclipse.

the class ReconcileWorkingCopyOperation method executeOperation.

/**
     * @throws org.eclipse.jdt.core.JavaModelException
     *         if setting the source
     *         of the original compilation unit fails
     */
protected void executeOperation() throws JavaModelException {
    checkCanceled();
    try {
        beginTask(Messages.element_reconciling, 2);
        CompilationUnit workingCopy = getWorkingCopy();
        boolean wasConsistent = workingCopy.isConsistent();
        // check is problem requestor is active
        IProblemRequestor problemRequestor = workingCopy.getPerWorkingCopyInfo();
        if (problemRequestor != null)
            problemRequestor = ((JavaModelManager.PerWorkingCopyInfo) problemRequestor).getProblemRequestor();
        boolean defaultRequestorIsActive = problemRequestor != null && problemRequestor.isActive();
        IProblemRequestor ownerProblemRequestor = this.workingCopyOwner.getProblemRequestor(workingCopy);
        boolean ownerRequestorIsActive = ownerProblemRequestor != null && ownerProblemRequestor != problemRequestor && ownerProblemRequestor.isActive();
        this.requestorIsActive = defaultRequestorIsActive || ownerRequestorIsActive;
        // create the delta builder (this remembers the current content of the cu)
        this.deltaBuilder = new JavaElementDeltaBuilder(workingCopy);
        // make working copy consistent if needed and compute AST if needed
        makeConsistent(workingCopy);
        // (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=177319)
        if (!wasConsistent || ((this.reconcileFlags & ICompilationUnit.FORCE_PROBLEM_DETECTION) != 0)) {
            notifyParticipants(workingCopy);
            // recreate ast if one participant reset it
            if (this.ast == null)
                makeConsistent(workingCopy);
        }
        // report problems
        if (this.problems != null && (((this.reconcileFlags & ICompilationUnit.FORCE_PROBLEM_DETECTION) != 0) || !wasConsistent)) {
            if (defaultRequestorIsActive) {
                reportProblems(workingCopy, problemRequestor);
            }
            if (ownerRequestorIsActive) {
                reportProblems(workingCopy, ownerProblemRequestor);
            }
        }
        // report delta
        JavaElementDelta delta = this.deltaBuilder.delta;
        if (delta != null) {
            addReconcileDelta(workingCopy, delta);
        }
    } finally {
        done();
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IProblemRequestor(org.eclipse.jdt.core.IProblemRequestor)

Example 3 with IProblemRequestor

use of org.eclipse.jdt.core.IProblemRequestor in project che by eclipse.

the class QuickFixTest method collectCorrections2.

protected static final ArrayList collectCorrections2(ICompilationUnit cu, int nProblems) throws CoreException {
    final ArrayList problemsList = new ArrayList();
    final IProblemRequestor requestor = new IProblemRequestor() {

        public void acceptProblem(IProblem problem) {
            problemsList.add(problem);
        }

        public void beginReporting() {
            problemsList.clear();
        }

        public void endReporting() {
        }

        public boolean isActive() {
            return true;
        }
    };
    WorkingCopyOwner workingCopyOwner = new WorkingCopyOwner() {

        public IProblemRequestor getProblemRequestor(ICompilationUnit workingCopy) {
            return requestor;
        }
    };
    ICompilationUnit wc = cu.getWorkingCopy(workingCopyOwner, null);
    try {
        wc.reconcile(ICompilationUnit.NO_AST, true, true, wc.getOwner(), null);
    } finally {
        wc.discardWorkingCopy();
    }
    IProblem[] problems = (IProblem[]) problemsList.toArray(new IProblem[problemsList.size()]);
    assertNumberOfProblems(nProblems, problems);
    return collectCorrections(cu, problems[0], null);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IProblemRequestor(org.eclipse.jdt.core.IProblemRequestor) WorkingCopyOwner(org.eclipse.jdt.core.WorkingCopyOwner) DefaultWorkingCopyOwner(org.eclipse.jdt.internal.core.DefaultWorkingCopyOwner) ArrayList(java.util.ArrayList) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Aggregations

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 IProblemRequestor (org.eclipse.jdt.core.IProblemRequestor)3 WorkingCopyOwner (org.eclipse.jdt.core.WorkingCopyOwner)2 ArrayList (java.util.ArrayList)1 HighlightedPosition (org.eclipse.che.ide.ext.java.shared.dto.HighlightedPosition)1 ReconcileResult (org.eclipse.che.ide.ext.java.shared.dto.ReconcileResult)1 IType (org.eclipse.jdt.core.IType)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 IProblem (org.eclipse.jdt.core.compiler.IProblem)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 ClassFileWorkingCopy (org.eclipse.jdt.internal.core.ClassFileWorkingCopy)1 DefaultWorkingCopyOwner (org.eclipse.jdt.internal.core.DefaultWorkingCopyOwner)1