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;
}
}
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;
}
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;
}
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);
}
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);
}
}
}
Aggregations