use of org.eclipse.jdt.core.IMethod in project liferay-ide by liferay.
the class AddJSRPortletActionMethodMarkerResolution method resolve.
@Override
protected void resolve(IMarker marker) {
try {
IProgressMonitor npm = new NullProgressMonitor();
IMethod newMethod = this.type.createMethod(MessageFormat.format(getCode(), getTextContent(marker)), null, true, npm);
for (String importName : getImports()) {
type.getCompilationUnit().createImport(importName, null, npm);
}
type.getCompilationUnit().save(npm, false);
try {
JavaUI.revealInEditor(JavaUI.openInEditor(newMethod), (IJavaElement) newMethod);
} catch (PartInitException pie) {
LiferayXMLSearchUI.logError("Unable to open java editor on action method", pie);
}
if (marker.getResource() instanceof IFile) {
ComponentUtil.validateFile((IFile) marker.getResource(), npm);
}
} catch (JavaModelException jme) {
LiferayXMLSearchUI.logError("Unable to add JSR process action method", jme);
}
}
use of org.eclipse.jdt.core.IMethod in project eclipse.jdt.ls by eclipse.
the class StubUtility method getBaseNameFromLocationInParent.
private static String getBaseNameFromLocationInParent(Expression assignedExpression, List<Expression> arguments, IMethodBinding binding) {
if (binding == null) {
return null;
}
ITypeBinding[] parameterTypes = binding.getParameterTypes();
if (parameterTypes.length != arguments.size()) {
return null;
}
int index = arguments.indexOf(assignedExpression);
if (index == -1) {
return null;
}
ITypeBinding expressionBinding = assignedExpression.resolveTypeBinding();
if (expressionBinding != null && !expressionBinding.isAssignmentCompatible(parameterTypes[index])) {
return null;
}
try {
IJavaElement javaElement = binding.getJavaElement();
if (javaElement instanceof IMethod) {
IMethod method = (IMethod) javaElement;
if (method.getOpenable().getBuffer() != null) {
// avoid dummy names and lookup from Javadoc
String[] parameterNames = method.getParameterNames();
if (index < parameterNames.length) {
return NamingConventions.getBaseName(NamingConventions.VK_PARAMETER, parameterNames[index], method.getJavaProject());
}
}
}
} catch (JavaModelException e) {
// ignore
}
return null;
}
use of org.eclipse.jdt.core.IMethod in project eclipse.jdt.ls by eclipse.
the class StubUtility method suggestArgumentNames.
// public static String[][] suggestArgumentNamesWithProposals(IJavaProject project, String[] paramNames) {
// String[][] newNames = new String[paramNames.length][];
// ArrayList<String> takenNames = new ArrayList<>();
//
// // Ensure that the code generation preferences are respected
// for (int i = 0; i < paramNames.length; i++) {
// String curr = paramNames[i];
// String baseName = NamingConventions.getBaseName(NamingConventions.VK_PARAMETER, curr, project);
//
// String[] proposedNames = getVariableNameSuggestions(NamingConventions.VK_PARAMETER, project, curr, 0,
// takenNames, true);
// if (!curr.equals(baseName)) {
// // make the existing name to favorite
// LinkedHashSet<String> updatedNames = new LinkedHashSet<>();
// updatedNames.add(curr);
// for (int k = 0; k < proposedNames.length; k++) {
// updatedNames.add(proposedNames[k]);
// }
// proposedNames = updatedNames.toArray(new String[updatedNames.size()]);
// }
// newNames[i] = proposedNames;
// takenNames.add(proposedNames[0]);
// }
// return newNames;
// }
// public static String[][] suggestArgumentNamesWithProposals(IJavaProject project, IMethodBinding binding) {
// int nParams = binding.getParameterTypes().length;
// if (nParams > 0) {
// try {
// IMethod method = (IMethod) binding.getMethodDeclaration().getJavaElement();
// if (method != null) {
// String[] parameterNames = method.getParameterNames();
// if (parameterNames.length == nParams) {
// return suggestArgumentNamesWithProposals(project, parameterNames);
// }
// }
// } catch (JavaModelException e) {
// // ignore
// }
// }
// String[][] names = new String[nParams][];
// for (int i = 0; i < names.length; i++) {
// names[i] = new String[] { "arg" + i }; //$NON-NLS-1$
// }
// return names;
// }
public static String[] suggestArgumentNames(IJavaProject project, IMethodBinding binding) {
int nParams = binding.getParameterTypes().length;
if (nParams > 0) {
try {
IMethod method = (IMethod) binding.getMethodDeclaration().getJavaElement();
if (method != null) {
String[] paramNames = method.getParameterNames();
if (paramNames.length == nParams) {
String[] namesArray = EMPTY;
ArrayList<String> newNames = new ArrayList<>(paramNames.length);
// Ensure that the code generation preferences are respected
for (int i = 0; i < paramNames.length; i++) {
String curr = paramNames[i];
String baseName = NamingConventions.getBaseName(NamingConventions.VK_PARAMETER, curr, method.getJavaProject());
if (!curr.equals(baseName)) {
// make the existing name the favorite
newNames.add(curr);
} else {
newNames.add(suggestArgumentName(project, curr, namesArray));
}
namesArray = newNames.toArray(new String[newNames.size()]);
}
return namesArray;
}
}
} catch (JavaModelException e) {
// ignore
}
}
String[] names = new String[nParams];
for (int i = 0; i < names.length; i++) {
// $NON-NLS-1$
names[i] = "arg" + i;
}
return names;
}
use of org.eclipse.jdt.core.IMethod in project eclipse.jdt.ls by eclipse.
the class GetterSetterCompletionProposal method evaluateProposals.
public static void evaluateProposals(IType type, String prefix, int offset, int length, int relevance, Collection<CompletionProposal> result) throws JavaModelException {
if (prefix.length() == 0) {
relevance--;
}
IField[] fields = type.getFields();
IMethod[] methods = type.getMethods();
for (IField curr : fields) {
if (!JdtFlags.isEnum(curr)) {
String getterName = GetterSetterUtil.getGetterName(curr, null);
if (Strings.startsWithIgnoreCase(getterName, prefix) && !hasMethod(methods, getterName)) {
int getterRelevance = relevance;
if (JdtFlags.isStatic(curr) && JdtFlags.isFinal(curr)) {
getterRelevance = relevance - 1;
}
CompletionProposal proposal = new GetterSetterCompletionProposal(curr, true, offset);
proposal.setName(getterName.toCharArray());
String signature = Signature.createMethodSignature(new String[] {}, curr.getTypeSignature());
proposal.setReplaceRange(offset, offset + prefix.length());
proposal.setSignature(signature.toCharArray());
proposal.setCompletion(getterName.toCharArray());
proposal.setDeclarationSignature(curr.getTypeSignature().toCharArray());
result.add(proposal);
}
if (!JdtFlags.isFinal(curr)) {
String setterName = GetterSetterUtil.getSetterName(curr, null);
if (Strings.startsWithIgnoreCase(setterName, prefix) && !hasMethod(methods, setterName)) {
CompletionProposal proposal = new GetterSetterCompletionProposal(curr, false, offset);
proposal.setName(setterName.toCharArray());
String signature = Signature.createMethodSignature(new String[] { curr.getTypeSignature() }, Signature.SIG_VOID);
proposal.setReplaceRange(offset, offset + prefix.length());
proposal.setSignature(signature.toCharArray());
proposal.setParameterNames(new char[][] { curr.getElementName().toCharArray() });
proposal.setCompletion(getterName.toCharArray());
proposal.setDeclarationSignature(curr.getTypeSignature().toCharArray());
result.add(proposal);
}
}
}
}
}
use of org.eclipse.jdt.core.IMethod in project eclipse.jdt.ls by eclipse.
the class SignatureHelpRequestor method computeJavaDoc.
public String computeJavaDoc(CompletionProposal proposal) {
try {
IType type = unit.getJavaProject().findType(SignatureUtil.stripSignatureToFQN(String.valueOf(proposal.getDeclarationSignature())));
if (type != null) {
String[] parameters = Signature.getParameterTypes(String.valueOf(SignatureUtil.fix83600(proposal.getSignature())));
for (int i = 0; i < parameters.length; i++) {
parameters[i] = getLowerBound(parameters[i]);
}
IMethod method = JavaModelUtil.findMethod(String.valueOf(proposal.getName()), parameters, proposal.isConstructor(), type);
if (method != null && method.exists()) {
ICompilationUnit unit = type.getCompilationUnit();
if (unit != null) {
unit.reconcile(ICompilationUnit.NO_AST, false, null, null);
}
String javadoc = null;
try {
javadoc = new SimpleTimeLimiter().callWithTimeout(() -> {
Reader reader = JavadocContentAccess.getPlainTextContentReader(method);
return reader == null ? null : CharStreams.toString(reader);
}, 500, TimeUnit.MILLISECONDS, true);
} catch (UncheckedTimeoutException tooSlow) {
} catch (Exception e) {
JavaLanguageServerPlugin.logException("Unable to read documentation", e);
}
return javadoc;
}
}
} catch (JavaModelException e) {
JavaLanguageServerPlugin.logException("Unable to resolve signaturehelp javadoc", e);
}
return null;
}
Aggregations