Search in sources :

Example 6 with WorkingCopyOwner

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

the class RenameFieldProcessor method analyzeRenameChanges.

//----------------
private RefactoringStatus analyzeRenameChanges(IProgressMonitor pm) throws CoreException {
    ICompilationUnit[] newWorkingCopies = null;
    WorkingCopyOwner newWCOwner = new WorkingCopyOwner() {
    };
    try {
        //$NON-NLS-1$
        pm.beginTask("", 2);
        RefactoringStatus result = new RefactoringStatus();
        SearchResultGroup[] oldReferences = fReferences;
        List<ICompilationUnit> compilationUnitsToModify = new ArrayList<ICompilationUnit>();
        if (fIsComposite) {
            // limited change set, no accessors.
            for (int i = 0; i < oldReferences.length; i++) compilationUnitsToModify.add(oldReferences[i].getCompilationUnit());
            compilationUnitsToModify.add(fField.getCompilationUnit());
        } else {
            // include all cus, including accessors
            compilationUnitsToModify.addAll(Arrays.asList(fChangeManager.getAllCompilationUnits()));
        }
        newWorkingCopies = RenameAnalyzeUtil.createNewWorkingCopies(compilationUnitsToModify.toArray(new ICompilationUnit[compilationUnitsToModify.size()]), fChangeManager, newWCOwner, new SubProgressMonitor(pm, 1));
        SearchResultGroup[] newReferences = getNewReferences(new SubProgressMonitor(pm, 1), result, newWCOwner, newWorkingCopies);
        result.merge(RenameAnalyzeUtil.analyzeRenameChanges2(fChangeManager, oldReferences, newReferences, getNewElementName()));
        return result;
    } finally {
        pm.done();
        if (newWorkingCopies != null) {
            for (int i = 0; i < newWorkingCopies.length; i++) {
                newWorkingCopies[i].discardWorkingCopy();
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) WorkingCopyOwner(org.eclipse.jdt.core.WorkingCopyOwner) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 7 with WorkingCopyOwner

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

the class JavaModelManager method discardPerWorkingCopyInfo.

/*
	 * Discards the per working copy info for the given working copy (making it a compilation unit)
	 * if its use count was 1. Otherwise, just decrement the use count.
	 * If the working copy is primary, computes the delta between its state and the original compilation unit
	 * and register it.
	 * Close the working copy, its buffer and remove it from the shared working copy table.
	 * Ignore if no per-working copy info existed.
	 * NOTE: it must NOT be synchronized as it may interact with the element info cache (if useCount is decremented to 0), see bug 50667.
	 * Returns the new use count (or -1 if it didn't exist).
	 */
public int discardPerWorkingCopyInfo(CompilationUnit workingCopy) throws JavaModelException {
    // create the delta builder (this remembers the current content of the working copy)
    // outside the perWorkingCopyInfos lock (see bug 50667)
    JavaElementDeltaBuilder deltaBuilder = null;
    if (workingCopy.isPrimary() && workingCopy.hasUnsavedChanges()) {
        deltaBuilder = new JavaElementDeltaBuilder(workingCopy);
    }
    PerWorkingCopyInfo info = null;
    synchronized (this.perWorkingCopyInfos) {
        WorkingCopyOwner owner = workingCopy.owner;
        Map workingCopyToInfos = (Map) this.perWorkingCopyInfos.get(owner);
        if (workingCopyToInfos == null)
            return -1;
        info = (PerWorkingCopyInfo) workingCopyToInfos.get(workingCopy);
        if (info == null)
            return -1;
        if (--info.useCount == 0) {
            // remove per working copy info
            workingCopyToInfos.remove(workingCopy);
            if (workingCopyToInfos.isEmpty()) {
                this.perWorkingCopyInfos.remove(owner);
            }
        }
    }
    if (info.useCount == 0) {
        // info cannot be null here (check was done above)
        // remove infos + close buffer (since no longer working copy)
        // outside the perWorkingCopyInfos lock (see bug 50667)
        removeInfoAndChildren(workingCopy);
        workingCopy.closeBuffer();
        // compute the delta if needed and register it if there are changes
        if (deltaBuilder != null) {
            deltaBuilder.buildDeltas();
            if (deltaBuilder.delta != null) {
                getDeltaProcessor().registerJavaModelDelta(deltaBuilder.delta);
            }
        }
    }
    return info.useCount;
}
Also used : WorkingCopyOwner(org.eclipse.jdt.core.WorkingCopyOwner) Map(java.util.Map) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap)

Example 8 with WorkingCopyOwner

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

the class CheASTParser method internalCreateAST.

private ASTNode internalCreateAST(IProgressMonitor monitor) {
    boolean needToResolveBindings = (this.bits & CompilationUnitResolver.RESOLVE_BINDING) != 0;
    switch(this.astKind) {
        case K_CLASS_BODY_DECLARATIONS:
        case K_EXPRESSION:
        case K_STATEMENTS:
            if (this.rawSource == null) {
                if (this.typeRoot != null) {
                    // get the source from the type root
                    if (this.typeRoot instanceof ICompilationUnit) {
                        org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit = (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) this.typeRoot;
                        this.rawSource = sourceUnit.getContents();
                    } else if (this.typeRoot instanceof IClassFile) {
                        try {
                            String sourceString = this.typeRoot.getSource();
                            if (sourceString != null) {
                                this.rawSource = sourceString.toCharArray();
                            }
                        } catch (JavaModelException e) {
                            // an error occured accessing the java element
                            StringWriter stringWriter = new StringWriter();
                            PrintWriter writer = null;
                            try {
                                writer = new PrintWriter(stringWriter);
                                e.printStackTrace(writer);
                            } finally {
                                if (writer != null)
                                    writer.close();
                            }
                            throw new IllegalStateException(String.valueOf(stringWriter.getBuffer()));
                        }
                    }
                }
            }
            if (this.rawSource != null) {
                if (this.sourceOffset + this.sourceLength > this.rawSource.length) {
                    throw new IllegalStateException();
                }
                return internalCreateASTForKind();
            }
            break;
        case K_COMPILATION_UNIT:
            CompilationUnitDeclaration compilationUnitDeclaration = null;
            try {
                NodeSearcher searcher = null;
                org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit = null;
                WorkingCopyOwner wcOwner = this.workingCopyOwner;
                if (this.typeRoot instanceof ICompilationUnit) {
                    /*
							 * this.compilationUnitSource is an instance of org.eclipse.jdt.internal.core.CompilationUnit that implements
							 * both org.eclipse.jdt.core.ICompilationUnit and org.eclipse.jdt.internal.compiler.env.ICompilationUnit
							 */
                    sourceUnit = (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) this.typeRoot;
                    /*
							 * use a BasicCompilation that caches the source instead of using the compilationUnitSource directly
							 * (if it is a working copy, the source can change between the parse and the AST convertion)
							 * (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=75632)
							 */
                    sourceUnit = new BasicCompilationUnit(sourceUnit.getContents(), sourceUnit.getPackageName(), new String(sourceUnit.getFileName()), this.project);
                    wcOwner = ((ICompilationUnit) this.typeRoot).getOwner();
                } else if (this.typeRoot instanceof IClassFile) {
                    try {
                        String sourceString = this.typeRoot.getSource();
                        if (sourceString == null) {
                            throw new IllegalStateException();
                        }
                        PackageFragment packageFragment = (PackageFragment) this.typeRoot.getParent();
                        BinaryType type = (BinaryType) this.typeRoot.findPrimaryType();
                        IBinaryType binaryType = (IBinaryType) type.getElementInfo();
                        // file name is used to recreate the Java element, so it has to be the toplevel .class file name
                        char[] fileName = binaryType.getFileName();
                        int firstDollar = CharOperation.indexOf('$', fileName);
                        if (firstDollar != -1) {
                            char[] suffix = SuffixConstants.SUFFIX_class;
                            int suffixLength = suffix.length;
                            char[] newFileName = new char[firstDollar + suffixLength];
                            System.arraycopy(fileName, 0, newFileName, 0, firstDollar);
                            System.arraycopy(suffix, 0, newFileName, firstDollar, suffixLength);
                            fileName = newFileName;
                        }
                        sourceUnit = new BasicCompilationUnit(sourceString.toCharArray(), Util.toCharArrays(packageFragment.names), new String(fileName), this.project);
                    } catch (JavaModelException e) {
                        // an error occured accessing the java element
                        StringWriter stringWriter = new StringWriter();
                        PrintWriter writer = null;
                        try {
                            writer = new PrintWriter(stringWriter);
                            e.printStackTrace(writer);
                        } finally {
                            if (writer != null)
                                writer.close();
                        }
                        throw new IllegalStateException(String.valueOf(stringWriter.getBuffer()));
                    }
                } else if (this.rawSource != null) {
                    needToResolveBindings = ((this.bits & CompilationUnitResolver.RESOLVE_BINDING) != 0) && this.unitName != null && (this.project != null || this.classpaths != null || this.sourcepaths != null || ((this.bits & CompilationUnitResolver.INCLUDE_RUNNING_VM_BOOTCLASSPATH) != 0)) && this.compilerOptions != null;
                    //$NON-NLS-1$
                    sourceUnit = new BasicCompilationUnit(this.rawSource, null, this.unitName == null ? "" : this.unitName, this.project);
                } else {
                    throw new IllegalStateException();
                }
                if ((this.bits & CompilationUnitResolver.PARTIAL) != 0) {
                    searcher = new NodeSearcher(this.focalPointPosition);
                }
                int flags = 0;
                if ((this.bits & CompilationUnitResolver.STATEMENT_RECOVERY) != 0) {
                    flags |= ICompilationUnit.ENABLE_STATEMENTS_RECOVERY;
                }
                if (searcher == null && ((this.bits & CompilationUnitResolver.IGNORE_METHOD_BODIES) != 0)) {
                    flags |= ICompilationUnit.IGNORE_METHOD_BODIES;
                }
                if (needToResolveBindings) {
                    if ((this.bits & CompilationUnitResolver.BINDING_RECOVERY) != 0) {
                        flags |= ICompilationUnit.ENABLE_BINDINGS_RECOVERY;
                    }
                    try {
                        // parse and resolve
                        compilationUnitDeclaration = CheCompilationUnitResolver.resolve(sourceUnit, this.project, getClasspath(), searcher, this.compilerOptions, this.workingCopyOwner, flags, monitor);
                    } catch (JavaModelException e) {
                        flags &= ~ICompilationUnit.ENABLE_BINDINGS_RECOVERY;
                        compilationUnitDeclaration = CompilationUnitResolver.parse(sourceUnit, searcher, this.compilerOptions, flags);
                        needToResolveBindings = false;
                    }
                } else {
                    compilationUnitDeclaration = CompilationUnitResolver.parse(sourceUnit, searcher, this.compilerOptions, flags);
                    needToResolveBindings = false;
                }
                CompilationUnit result = CompilationUnitResolver.convert(compilationUnitDeclaration, sourceUnit.getContents(), this.apiLevel, this.compilerOptions, needToResolveBindings, wcOwner, needToResolveBindings ? new DefaultBindingResolver.BindingTables() : null, flags, monitor, this.project != null);
                result.setTypeRoot(this.typeRoot);
                return result;
            } finally {
                if (compilationUnitDeclaration != null && ((this.bits & CompilationUnitResolver.RESOLVE_BINDING) != 0)) {
                    compilationUnitDeclaration.cleanUp();
                }
            }
    }
    throw new IllegalStateException();
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IClassFile(org.eclipse.jdt.core.IClassFile) IBinaryType(org.eclipse.jdt.internal.compiler.env.IBinaryType) StringWriter(java.io.StringWriter) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) WorkingCopyOwner(org.eclipse.jdt.core.WorkingCopyOwner) DefaultWorkingCopyOwner(org.eclipse.jdt.internal.core.DefaultWorkingCopyOwner) PrintWriter(java.io.PrintWriter) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) BasicCompilationUnit(org.eclipse.jdt.internal.core.BasicCompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) PackageFragment(org.eclipse.jdt.internal.core.PackageFragment) BinaryType(org.eclipse.jdt.internal.core.BinaryType) IBinaryType(org.eclipse.jdt.internal.compiler.env.IBinaryType) BasicCompilationUnit(org.eclipse.jdt.internal.core.BasicCompilationUnit)

Example 9 with WorkingCopyOwner

use of org.eclipse.jdt.core.WorkingCopyOwner 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 10 with WorkingCopyOwner

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

the class CodeAssist method computeProposals.

public Proposals computeProposals(IJavaProject project, String fqn, int offset, final String content) throws JavaModelException {
    WorkingCopyOwner copyOwner = new WorkingCopyOwner() {

        @Override
        public IBuffer createBuffer(ICompilationUnit workingCopy) {
            return new org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter(workingCopy, workingCopy.getPath(), content);
        }
    };
    ICompilationUnit compilationUnit;
    IType type = project.findType(fqn);
    if (type == null) {
        return null;
    }
    if (type.isBinary()) {
        compilationUnit = type.getClassFile().getWorkingCopy(copyOwner, null);
    } else {
        compilationUnit = type.getCompilationUnit().getWorkingCopy(copyOwner, null);
    }
    IBuffer buffer = compilationUnit.getBuffer();
    IDocument document;
    if (buffer instanceof org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) {
        document = ((org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) buffer).getDocument();
    } else {
        document = new DocumentAdapter(buffer);
    }
    TextViewer viewer = new TextViewer(document, new Point(offset, 0));
    JavaContentAssistInvocationContext context = new JavaContentAssistInvocationContext(viewer, offset, compilationUnit);
    List<ICompletionProposal> proposals = new ArrayList<>();
    proposals.addAll(new JavaAllCompletionProposalComputer().computeCompletionProposals(context, null));
    proposals.addAll(new TemplateCompletionProposalComputer().computeCompletionProposals(context, null));
    Collections.sort(proposals, new RelevanceSorter());
    return convertProposals(offset, compilationUnit, viewer, proposals);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaContentAssistInvocationContext(org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext) TemplateCompletionProposalComputer(org.eclipse.jdt.internal.ui.text.java.TemplateCompletionProposalComputer) ArrayList(java.util.ArrayList) DocumentAdapter(org.eclipse.jdt.internal.core.DocumentAdapter) Point(org.eclipse.swt.graphics.Point) RelevanceSorter(org.eclipse.jdt.internal.ui.text.java.RelevanceSorter) IBuffer(org.eclipse.jdt.core.IBuffer) IType(org.eclipse.jdt.core.IType) TextViewer(org.eclipse.che.jdt.javaeditor.TextViewer) WorkingCopyOwner(org.eclipse.jdt.core.WorkingCopyOwner) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) JavaAllCompletionProposalComputer(org.eclipse.jdt.internal.ui.text.java.JavaAllCompletionProposalComputer) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

WorkingCopyOwner (org.eclipse.jdt.core.WorkingCopyOwner)11 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)9 JavaModelException (org.eclipse.jdt.core.JavaModelException)6 ArrayList (java.util.ArrayList)4 IType (org.eclipse.jdt.core.IType)4 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 WeakHashMap (java.util.WeakHashMap)2 IProblemRequestor (org.eclipse.jdt.core.IProblemRequestor)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 ArrayType (org.eclipse.jdt.core.dom.ArrayType)2 Name (org.eclipse.jdt.core.dom.Name)2 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)2 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)2 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)2 QualifiedType (org.eclipse.jdt.core.dom.QualifiedType)2 SimpleName (org.eclipse.jdt.core.dom.SimpleName)2 Type (org.eclipse.jdt.core.dom.Type)2