Search in sources :

Example 76 with IMethod

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);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) IMethod(org.eclipse.jdt.core.IMethod) PartInitException(org.eclipse.ui.PartInitException)

Example 77 with IMethod

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;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IMethod(org.eclipse.jdt.core.IMethod)

Example 78 with IMethod

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;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) ArrayList(java.util.ArrayList) IMethod(org.eclipse.jdt.core.IMethod)

Example 79 with IMethod

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);
                }
            }
        }
    }
}
Also used : InternalCompletionProposal(org.eclipse.jdt.internal.codeassist.InternalCompletionProposal) CompletionProposal(org.eclipse.jdt.core.CompletionProposal) IMethod(org.eclipse.jdt.core.IMethod) IField(org.eclipse.jdt.core.IField)

Example 80 with IMethod

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;
}
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) IMethod(org.eclipse.jdt.core.IMethod) SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter) JavaModelException(org.eclipse.jdt.core.JavaModelException) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) IOException(java.io.IOException) 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