Search in sources :

Example 86 with PsiMethod

use of com.intellij.psi.PsiMethod in project intellij-community by JetBrains.

the class SuperMethodWarningUtil method getSuperMethods.

@NotNull
static Collection<PsiMethod> getSuperMethods(@NotNull PsiMethod method, PsiClass aClass, @NotNull Collection<PsiElement> ignore) {
    final Collection<PsiMethod> superMethods = DeepestSuperMethodsSearch.search(method).findAll();
    superMethods.removeAll(ignore);
    if (superMethods.isEmpty()) {
        VirtualFile virtualFile = PsiUtilCore.getVirtualFile(aClass);
        if (virtualFile != null && ProjectRootManager.getInstance(aClass.getProject()).getFileIndex().isInSourceContent(virtualFile)) {
            PsiMethod siblingSuperMethod = FindSuperElementsHelper.getSiblingInheritedViaSubClass(method);
            if (siblingSuperMethod != null) {
                superMethods.add(siblingSuperMethod);
            }
        }
    }
    return superMethods;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiMethod(com.intellij.psi.PsiMethod) NotNull(org.jetbrains.annotations.NotNull)

Example 87 with PsiMethod

use of com.intellij.psi.PsiMethod in project intellij-community by JetBrains.

the class SuperMethodWarningUtil method checkSuperMethods.

@NotNull
public static PsiMethod[] checkSuperMethods(@NotNull PsiMethod method, @NotNull String actionString, @NotNull Collection<PsiElement> ignore) {
    PsiClass aClass = method.getContainingClass();
    if (aClass == null)
        return new PsiMethod[] { method };
    final Collection<PsiMethod> superMethods = getSuperMethods(method, aClass, ignore);
    if (superMethods.isEmpty())
        return new PsiMethod[] { method };
    Set<String> superClasses = new HashSet<>();
    boolean superAbstract = false;
    boolean parentInterface = false;
    for (final PsiMethod superMethod : superMethods) {
        final PsiClass containingClass = superMethod.getContainingClass();
        superClasses.add(containingClass.getQualifiedName());
        final boolean isInterface = containingClass.isInterface();
        superAbstract |= isInterface || superMethod.hasModifierProperty(PsiModifier.ABSTRACT);
        parentInterface |= isInterface;
    }
    SuperMethodWarningDialog dialog = new SuperMethodWarningDialog(method.getProject(), DescriptiveNameUtil.getDescriptiveName(method), actionString, superAbstract, parentInterface, aClass.isInterface(), ArrayUtil.toStringArray(superClasses));
    dialog.show();
    if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
        return superMethods.toArray(new PsiMethod[superMethods.size()]);
    }
    if (dialog.getExitCode() == SuperMethodWarningDialog.NO_EXIT_CODE) {
        return new PsiMethod[] { method };
    }
    return PsiMethod.EMPTY_ARRAY;
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 88 with PsiMethod

use of com.intellij.psi.PsiMethod in project intellij-community by JetBrains.

the class HierarchicalMethodSignatureImpl method addSuperSignature.

public void addSuperSignature(@NotNull HierarchicalMethodSignature superSignatureHierarchical) {
    PsiMethod superMethod = superSignatureHierarchical.getMethod();
    PsiMethod method = getMethod();
    if (PsiUtil.isAccessible(method.getProject(), superMethod, method, null)) {
        if (mySupers == null)
            mySupers = new SmartList<>();
        mySupers.add(superSignatureHierarchical);
    } else {
        if (myInaccessibleSupers == null)
            myInaccessibleSupers = new SmartList<>();
        myInaccessibleSupers.add(superSignatureHierarchical);
    }
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) SmartList(com.intellij.util.SmartList)

Example 89 with PsiMethod

use of com.intellij.psi.PsiMethod in project intellij-community by JetBrains.

the class InsertAfterEqualsHashCodeStrategy method insertNewMethod.

public PsiMethod insertNewMethod(PsiClass clazz, @NotNull PsiMethod newMethod, Editor editor) {
    PsiMethod methodHashCode = PsiAdapter.findHashCodeMethod(clazz);
    PsiMethod methodEquals = PsiAdapter.findEqualsMethod(clazz);
    // if both methods exist determine the last method in the javafile
    PsiMethod method;
    if (methodEquals != null && methodHashCode != null) {
        if (methodEquals.getTextOffset() > methodHashCode.getTextOffset()) {
            method = methodEquals;
        } else {
            method = methodHashCode;
        }
    } else {
        method = methodHashCode != null ? methodHashCode : methodEquals;
    }
    if (method != null) {
        // insert after the equals/hashCode method
        newMethod = (PsiMethod) clazz.addAfter(newMethod, method);
    } else {
        // no equals/hashCode so insert at caret
        newMethod = InsertAtCaretStrategy.getInstance().insertNewMethod(clazz, newMethod, editor);
    }
    return newMethod;
}
Also used : PsiMethod(com.intellij.psi.PsiMethod)

Example 90 with PsiMethod

use of com.intellij.psi.PsiMethod in project intellij-community by JetBrains.

the class ClassAccessVisitor method visitMethodCallExpression.

@Override
public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
    super.visitMethodCallExpression(expression);
    final PsiMethod method = expression.resolveMethod();
    if (method == null) {
        return;
    }
    final PsiClass calledClass = method.getContainingClass();
    if (calledClass == null) {
        return;
    }
    if (currentClass.equals(calledClass)) {
        return;
    }
    final Set<PsiClass> overAccessedClasses = m_overAccessedClasses;
    if (overAccessedClasses.contains(calledClass)) {
        return;
    }
    if (LibraryUtil.classIsInLibrary(calledClass)) {
        return;
    }
    if (PsiTreeUtil.isAncestor(currentClass, calledClass, true)) {
        return;
    }
    if (PsiTreeUtil.isAncestor(calledClass, currentClass, true)) {
        return;
    }
    PsiClass lexicallyEnclosingClass = currentClass;
    while (lexicallyEnclosingClass != null) {
        if (lexicallyEnclosingClass.isInheritor(calledClass, true)) {
            return;
        }
        lexicallyEnclosingClass = ClassUtils.getContainingClass(lexicallyEnclosingClass);
    }
    final Map<PsiClass, Integer> accessCounts = m_accessCounts;
    final Integer count = accessCounts.get(calledClass);
    if (count == null) {
        accessCounts.put(calledClass, 1);
    } else if (count.equals(Integer.valueOf(1))) {
        accessCounts.put(calledClass, 2);
    } else {
        overAccessedClasses.add(calledClass);
    }
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass)

Aggregations

PsiMethod (com.intellij.psi.PsiMethod)229 PsiClass (com.intellij.psi.PsiClass)97 PsiElement (com.intellij.psi.PsiElement)71 ArrayList (java.util.ArrayList)24 NotNull (org.jetbrains.annotations.NotNull)22 Nullable (org.jetbrains.annotations.Nullable)19 Project (com.intellij.openapi.project.Project)16 PsiField (com.intellij.psi.PsiField)13 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)12 Location (com.intellij.execution.Location)11 JavaEvaluator (com.android.tools.klint.client.api.JavaEvaluator)9 PsiReference (com.intellij.psi.PsiReference)9 PsiFile (com.intellij.psi.PsiFile)8 List (java.util.List)7 Nullable (com.android.annotations.Nullable)6 Module (com.intellij.openapi.module.Module)6 PsiType (com.intellij.psi.PsiType)6 SearchScope (com.intellij.psi.search.SearchScope)6 PsiParameter (com.intellij.psi.PsiParameter)5 UExpression (org.jetbrains.uast.UExpression)5