Search in sources :

Example 81 with IMethod

use of org.eclipse.jdt.core.IMethod in project eclipse.jdt.ls by eclipse.

the class MethodsSourcePositionComparator method compareInTheSameType.

private int compareInTheSameType(IMethodBinding firstMethodBinding, IMethodBinding secondMethodBinding) {
    try {
        IMethod firstMethod = (IMethod) firstMethodBinding.getJavaElement();
        IMethod secondMethod = (IMethod) secondMethodBinding.getJavaElement();
        if (firstMethod == null || secondMethod == null) {
            return 0;
        }
        ISourceRange firstSourceRange = firstMethod.getSourceRange();
        ISourceRange secondSourceRange = secondMethod.getSourceRange();
        if (!SourceRange.isAvailable(firstSourceRange) || !SourceRange.isAvailable(secondSourceRange)) {
            return firstMethod.getElementName().compareTo(secondMethod.getElementName());
        } else {
            return firstSourceRange.getOffset() - secondSourceRange.getOffset();
        }
    } catch (JavaModelException e) {
        return 0;
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IMethod(org.eclipse.jdt.core.IMethod) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 82 with IMethod

use of org.eclipse.jdt.core.IMethod in project eclipse.jdt.ls by eclipse.

the class CompletionResolveHandler method resolve.

public CompletionItem resolve(CompletionItem param, IProgressMonitor monitor) {
    @SuppressWarnings("unchecked") Map<String, String> data = JSONUtility.toModel(param.getData(), Map.class);
    // clean resolve data
    param.setData(null);
    if (!data.containsKey(DATA_FIELD_URI) || !data.containsKey(DATA_FIELD_REQUEST_ID) || !data.containsKey(DATA_FIELD_PROPOSAL_ID)) {
        return param;
    }
    int proposalId = Integer.parseInt(data.get(DATA_FIELD_PROPOSAL_ID));
    long requestId = Long.parseLong(data.get(DATA_FIELD_REQUEST_ID));
    CompletionResponse completionResponse = CompletionResponses.get(requestId);
    if (completionResponse == null || completionResponse.getProposals().size() <= proposalId) {
        throw new IllegalStateException("Invalid completion proposal");
    }
    String uri = data.get(DATA_FIELD_URI);
    ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri);
    if (unit == null) {
        throw new IllegalStateException(NLS.bind("Unable to match Compilation Unit from {0} ", uri));
    }
    CompletionProposalReplacementProvider proposalProvider = new CompletionProposalReplacementProvider(unit, completionResponse.getContext(), completionResponse.getOffset(), this.manager.getClientPreferences());
    proposalProvider.updateReplacement(completionResponse.getProposals().get(proposalId), param, '\0');
    if (monitor.isCanceled()) {
        param.setData(null);
        return param;
    }
    if (data.containsKey(DATA_FIELD_DECLARATION_SIGNATURE)) {
        String typeName = stripSignatureToFQN(String.valueOf(data.get(DATA_FIELD_DECLARATION_SIGNATURE)));
        try {
            IMember member = null;
            IType type = unit.getJavaProject().findType(typeName);
            if (type != null && data.containsKey(DATA_FIELD_NAME)) {
                String name = data.get(DATA_FIELD_NAME);
                String[] paramSigs = CharOperation.NO_STRINGS;
                if (data.containsKey(DATA_FIELD_SIGNATURE)) {
                    String[] parameters = Signature.getParameterTypes(String.valueOf(fix83600(data.get(DATA_FIELD_SIGNATURE).toCharArray())));
                    for (int i = 0; i < parameters.length; i++) {
                        parameters[i] = getLowerBound(parameters[i]);
                    }
                    paramSigs = parameters;
                }
                IMethod method = type.getMethod(name, paramSigs);
                if (method.exists()) {
                    member = method;
                } else {
                    IField field = type.getField(name);
                    if (field.exists()) {
                        member = field;
                    }
                }
            } else {
                member = type;
            }
            if (member != null && member.exists() && !monitor.isCanceled()) {
                String javadoc = null;
                try {
                    final IMember curMember = member;
                    javadoc = new SimpleTimeLimiter().callWithTimeout(() -> {
                        Reader reader = JavadocContentAccess.getPlainTextContentReader(curMember);
                        return reader == null ? null : CharStreams.toString(reader);
                    }, 500, TimeUnit.MILLISECONDS, true);
                } catch (UncheckedTimeoutException tooSlow) {
                    // Ignore error for now as it's spamming clients on content assist.
                    // TODO cache javadoc resolution results?
                    // JavaLanguageServerPlugin.logError("Unable to get documentation under 500ms");
                    monitor.setCanceled(true);
                } catch (Exception e) {
                    JavaLanguageServerPlugin.logException("Unable to read documentation", e);
                    monitor.setCanceled(true);
                }
                param.setDocumentation(javadoc);
            }
        } catch (JavaModelException e) {
            JavaLanguageServerPlugin.logException("Unable to resolve compilation", e);
            monitor.setCanceled(true);
        }
    }
    if (monitor.isCanceled()) {
        param.setData(null);
    }
    return param;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) Reader(java.io.Reader) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) IField(org.eclipse.jdt.core.IField) IMember(org.eclipse.jdt.core.IMember) JavaModelException(org.eclipse.jdt.core.JavaModelException) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) IType(org.eclipse.jdt.core.IType) CompletionProposalReplacementProvider(org.eclipse.jdt.ls.core.internal.contentassist.CompletionProposalReplacementProvider) IMethod(org.eclipse.jdt.core.IMethod) SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter)

Example 83 with IMethod

use of org.eclipse.jdt.core.IMethod in project eclipse.jdt.ls by eclipse.

the class JavaRefactoringDescriptorUtil method handleToElement.

/**
 * Converts an input handle back to the corresponding java element.
 *
 * @param owner
 *            the working copy owner
 * @param project
 *            the project, or <code>null</code> for the workspace
 * @param handle
 *            the input handle
 * @param check
 *            <code>true</code> to check for existence of the element,
 *            <code>false</code> otherwise
 * @return the corresponding java element, or <code>null</code> if no such
 *         element exists
 */
public static IJavaElement handleToElement(final WorkingCopyOwner owner, final String project, final String handle, final boolean check) {
    IJavaElement element = null;
    if (owner != null) {
        element = JavaCore.create(handle, owner);
    } else {
        element = JavaCore.create(handle);
    }
    if (element == null && project != null) {
        final IJavaProject javaProject = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProject(project);
        final String identifier = javaProject.getHandleIdentifier();
        if (owner != null) {
            element = JavaCore.create(identifier + handle, owner);
        } else {
            element = JavaCore.create(identifier + handle);
        }
    }
    if (check && element instanceof IMethod) {
        /*
			 * Resolve the method based on simple names of parameter types
			 * (to accommodate for different qualifications when refactoring is e.g.
			 * recorded in source but applied on binary method):
			 */
        final IMethod method = (IMethod) element;
        final IMethod[] methods = method.getDeclaringType().findMethods(method);
        if (methods != null && methods.length > 0) {
            element = methods[0];
        }
    }
    if (element != null && (!check || element.exists())) {
        return element;
    }
    return null;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IJavaProject(org.eclipse.jdt.core.IJavaProject) IMethod(org.eclipse.jdt.core.IMethod)

Example 84 with IMethod

use of org.eclipse.jdt.core.IMethod in project eclipse.jdt.ls by eclipse.

the class RenameProcessor method renameOccurrences.

public void renameOccurrences(WorkspaceEdit edit, String newName, IProgressMonitor monitor) throws CoreException {
    if (fElement == null || !canRename()) {
        return;
    }
    IJavaElement[] elementsToSearch = null;
    if (fElement instanceof IMethod) {
        elementsToSearch = RippleMethodFinder.getRelatedMethods((IMethod) fElement, monitor, null);
    } else {
        elementsToSearch = new IJavaElement[] { fElement };
    }
    SearchPattern pattern = createOccurrenceSearchPattern(elementsToSearch);
    if (pattern == null) {
        return;
    }
    SearchEngine engine = new SearchEngine();
    engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, createSearchScope(), new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            Object o = match.getElement();
            if (o instanceof IJavaElement) {
                IJavaElement element = (IJavaElement) o;
                ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
                if (compilationUnit == null) {
                    return;
                }
                TextEdit replaceEdit = collectMatch(match, element, compilationUnit, newName);
                if (replaceEdit != null) {
                    convert(edit, compilationUnit, replaceEdit);
                }
            }
        }
    }, monitor);
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) TextEdit(org.eclipse.text.edits.TextEdit) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IMethod(org.eclipse.jdt.core.IMethod)

Example 85 with IMethod

use of org.eclipse.jdt.core.IMethod in project eclipse.jdt.ls by eclipse.

the class RenameTypeProcessor method renameOccurrences.

@Override
public void renameOccurrences(WorkspaceEdit edit, String newName, IProgressMonitor monitor) throws CoreException {
    super.renameOccurrences(edit, newName, monitor);
    IType t = (IType) fElement;
    IMethod[] methods = t.getMethods();
    for (IMethod method : methods) {
        if (method.isConstructor()) {
            TextEdit replaceEdit = new ReplaceEdit(method.getNameRange().getOffset(), method.getNameRange().getLength(), newName);
            convert(edit, t.getCompilationUnit(), replaceEdit);
        }
    }
}
Also used : TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IMethod(org.eclipse.jdt.core.IMethod) IType(org.eclipse.jdt.core.IType)

Aggregations

IMethod (org.eclipse.jdt.core.IMethod)217 IType (org.eclipse.jdt.core.IType)112 IJavaElement (org.eclipse.jdt.core.IJavaElement)56 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)37 ArrayList (java.util.ArrayList)35 JavaModelException (org.eclipse.jdt.core.JavaModelException)32 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)29 IField (org.eclipse.jdt.core.IField)27 Test (org.junit.Test)22 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)19 ITypeHierarchy (org.eclipse.jdt.core.ITypeHierarchy)19 IMember (org.eclipse.jdt.core.IMember)15 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)14 RenameJavaElementDescriptor (org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)13 IJavaProject (org.eclipse.jdt.core.IJavaProject)12 HashSet (java.util.HashSet)11 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)10 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)10 ISourceRange (org.eclipse.jdt.core.ISourceRange)9 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)9