Search in sources :

Example 1 with MethodSignature

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

the class GroovyOverrideImplementExploreUtil method getMapToOverrideImplement.

@NotNull
public static Map<MethodSignature, CandidateInfo> getMapToOverrideImplement(GrTypeDefinition aClass, boolean toImplement, boolean skipImplemented) {
    Map<MethodSignature, PsiMethod> abstracts = ContainerUtil.newLinkedHashMap();
    Map<MethodSignature, PsiMethod> finals = ContainerUtil.newLinkedHashMap();
    Map<MethodSignature, PsiMethod> concretes = ContainerUtil.newLinkedHashMap();
    PsiUtilCore.ensureValid(aClass);
    Collection<HierarchicalMethodSignature> allMethodSignatures = aClass.getVisibleSignatures();
    PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(aClass.getProject()).getResolveHelper();
    for (HierarchicalMethodSignature signature : allMethodSignatures) {
        PsiMethod method = signature.getMethod();
        if (method instanceof GrTraitMethod) {
            for (HierarchicalMethodSignature superSignature : signature.getSuperSignatures()) {
                processMethod(aClass, skipImplemented, abstracts, finals, concretes, resolveHelper, superSignature, superSignature.getMethod());
            }
        } else {
            processMethod(aClass, skipImplemented, abstracts, finals, concretes, resolveHelper, signature, method);
        }
    }
    final Map<MethodSignature, CandidateInfo> result = new TreeMap<>(new OverrideImplementExploreUtil.MethodSignatureComparator());
    if (toImplement || aClass.isInterface()) {
        collectMethodsToImplement(aClass, abstracts, finals, concretes, result);
    } else {
        for (Map.Entry<MethodSignature, PsiMethod> entry : concretes.entrySet()) {
            MethodSignature signature = entry.getKey();
            PsiMethod concrete = entry.getValue();
            if (finals.get(signature) == null) {
                PsiMethod abstractOne = abstracts.get(signature);
                if (abstractOne == null || !abstractOne.getContainingClass().isInheritor(concrete.getContainingClass(), true) || CommonClassNames.JAVA_LANG_OBJECT.equals(concrete.getContainingClass().getQualifiedName())) {
                    PsiSubstitutor subst = OverrideImplementExploreUtil.correctSubstitutor(concrete, signature.getSubstitutor());
                    CandidateInfo info = new CandidateInfo(concrete, subst);
                    result.put(signature, info);
                }
            }
        }
    }
    return result;
}
Also used : MethodSignature(com.intellij.psi.util.MethodSignature) CandidateInfo(com.intellij.psi.infos.CandidateInfo) TreeMap(java.util.TreeMap) OverrideImplementExploreUtil(com.intellij.codeInsight.generation.OverrideImplementExploreUtil) GrTraitMethod(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrTraitMethod) TreeMap(java.util.TreeMap) Map(java.util.Map) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with MethodSignature

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

the class ConvertClosureToMethodIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrField field;
    if (element.getParent() instanceof GrField) {
        field = (GrField) element.getParent();
    } else {
        final PsiReference ref = element.getReference();
        LOG.assertTrue(ref != null);
        PsiElement resolved = ref.resolve();
        if (resolved instanceof GrAccessorMethod) {
            resolved = ((GrAccessorMethod) resolved).getProperty();
        }
        LOG.assertTrue(resolved instanceof GrField);
        field = (GrField) resolved;
    }
    final HashSet<PsiReference> usages = new HashSet<>();
    usages.addAll(ReferencesSearch.search(field).findAll());
    final GrAccessorMethod[] getters = field.getGetters();
    for (GrAccessorMethod getter : getters) {
        usages.addAll(MethodReferencesSearch.search(getter).findAll());
    }
    final GrAccessorMethod setter = field.getSetter();
    if (setter != null) {
        usages.addAll(MethodReferencesSearch.search(setter).findAll());
    }
    final String fieldName = field.getName();
    LOG.assertTrue(fieldName != null);
    final Collection<PsiElement> fieldUsages = new HashSet<>();
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    for (PsiReference usage : usages) {
        final PsiElement psiElement = usage.getElement();
        if (PsiUtil.isMethodUsage(psiElement))
            continue;
        if (!GroovyLanguage.INSTANCE.equals(psiElement.getLanguage())) {
            conflicts.putValue(psiElement, GroovyIntentionsBundle.message("closure.is.accessed.outside.of.groovy", fieldName));
        } else {
            if (psiElement instanceof GrReferenceExpression) {
                fieldUsages.add(psiElement);
                if (PsiUtil.isAccessedForWriting((GrExpression) psiElement)) {
                    conflicts.putValue(psiElement, GroovyIntentionsBundle.message("write.access.to.closure.variable", fieldName));
                }
            } else if (psiElement instanceof GrArgumentLabel) {
                conflicts.putValue(psiElement, GroovyIntentionsBundle.message("field.is.used.in.argument.label", fieldName));
            }
        }
    }
    final PsiClass containingClass = field.getContainingClass();
    final GrExpression initializer = field.getInitializerGroovy();
    LOG.assertTrue(initializer != null);
    final PsiType type = initializer.getType();
    LOG.assertTrue(type instanceof GrClosureType);
    final GrSignature signature = ((GrClosureType) type).getSignature();
    final List<MethodSignature> signatures = GrClosureSignatureUtil.generateAllMethodSignaturesBySignature(fieldName, signature);
    for (MethodSignature s : signatures) {
        final PsiMethod method = MethodSignatureUtil.findMethodBySignature(containingClass, s, true);
        if (method != null) {
            conflicts.putValue(method, GroovyIntentionsBundle.message("method.with.signature.already.exists", GroovyPresentationUtil.getSignaturePresentation(s)));
        }
    }
    if (!conflicts.isEmpty()) {
        final ConflictsDialog conflictsDialog = new ConflictsDialog(project, conflicts, () -> execute(field, fieldUsages));
        conflictsDialog.show();
        if (conflictsDialog.getExitCode() != DialogWrapper.OK_EXIT_CODE)
            return;
    }
    execute(field, fieldUsages);
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) MethodSignature(com.intellij.psi.util.MethodSignature) GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) MultiMap(com.intellij.util.containers.MultiMap) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) GrSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature) HashSet(com.intellij.util.containers.HashSet) GrClosureType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType)

Example 3 with MethodSignature

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

the class I18nInspection method isInNonNlsEquals.

private static boolean isInNonNlsEquals(PsiExpression expression, final Set<PsiModifierListOwner> nonNlsTargets) {
    if (!(expression.getParent().getParent() instanceof PsiMethodCallExpression)) {
        return false;
    }
    final PsiMethodCallExpression call = (PsiMethodCallExpression) expression.getParent().getParent();
    final PsiReferenceExpression methodExpression = call.getMethodExpression();
    final PsiExpression qualifier = methodExpression.getQualifierExpression();
    if (qualifier != expression) {
        return false;
    }
    if (!"equals".equals(methodExpression.getReferenceName())) {
        return false;
    }
    final PsiElement resolved = methodExpression.resolve();
    if (!(resolved instanceof PsiMethod)) {
        return false;
    }
    PsiType objectType = PsiType.getJavaLangObject(resolved.getManager(), resolved.getResolveScope());
    MethodSignature equalsSignature = MethodSignatureUtil.createMethodSignature("equals", new PsiType[] { objectType }, PsiTypeParameter.EMPTY_ARRAY, PsiSubstitutor.EMPTY);
    if (!equalsSignature.equals(((PsiMethod) resolved).getSignature(PsiSubstitutor.EMPTY))) {
        return false;
    }
    final PsiExpression[] expressions = call.getArgumentList().getExpressions();
    if (expressions.length != 1) {
        return false;
    }
    final PsiExpression arg = expressions[0];
    PsiReferenceExpression ref = null;
    if (arg instanceof PsiReferenceExpression) {
        ref = (PsiReferenceExpression) arg;
    } else if (arg instanceof PsiMethodCallExpression)
        ref = ((PsiMethodCallExpression) arg).getMethodExpression();
    if (ref != null) {
        final PsiElement resolvedEntity = ref.resolve();
        if (resolvedEntity instanceof PsiModifierListOwner) {
            PsiModifierListOwner modifierListOwner = (PsiModifierListOwner) resolvedEntity;
            if (annotatedAsNonNls(modifierListOwner)) {
                return true;
            }
            nonNlsTargets.add(modifierListOwner);
        }
    }
    return false;
}
Also used : MethodSignature(com.intellij.psi.util.MethodSignature)

Example 4 with MethodSignature

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

the class JavaOverridingMethodsSearcher method findOverridingMethod.

@Nullable
static PsiMethod findOverridingMethod(@NotNull Project project, @NotNull PsiClass inheritor, @NotNull PsiMethod method, @NotNull PsiClass methodContainingClass) {
    String name = method.getName();
    if (inheritor.findMethodsByName(name, false).length > 0) {
        PsiMethod found = MethodSignatureUtil.findMethodBySuperSignature(inheritor, getSuperSignature(inheritor, methodContainingClass, method), false);
        if (found != null && isAcceptable(project, found, inheritor, method, methodContainingClass)) {
            return found;
        }
    }
    if (methodContainingClass.isInterface() && !inheritor.isInterface()) {
        //check for sibling implementation
        final PsiClass superClass = inheritor.getSuperClass();
        if (superClass != null && !superClass.isInheritor(methodContainingClass, true) && superClass.findMethodsByName(name, true).length > 0) {
            MethodSignature signature = getSuperSignature(inheritor, methodContainingClass, method);
            PsiMethod derived = MethodSignatureUtil.findMethodInSuperClassBySignatureInDerived(inheritor, superClass, signature, true);
            if (derived != null && isAcceptable(project, derived, inheritor, method, methodContainingClass)) {
                return derived;
            }
        }
    }
    return null;
}
Also used : MethodSignature(com.intellij.psi.util.MethodSignature) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with MethodSignature

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

the class SuspiciousCollectionsMethodCallsInspection method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    final List<PsiMethod> patternMethods = new ArrayList<>();
    final IntArrayList indices = new IntArrayList();
    return new JavaElementVisitor() {

        @Override
        public void visitMethodCallExpression(PsiMethodCallExpression methodCall) {
            final String message = getSuspiciousMethodCallMessage(methodCall, REPORT_CONVERTIBLE_METHOD_CALLS, patternMethods, indices);
            if (message != null) {
                holder.registerProblem(methodCall.getArgumentList().getExpressions()[0], message);
            }
        }

        @Override
        public void visitMethodReferenceExpression(PsiMethodReferenceExpression expression) {
            final PsiType functionalInterfaceType = expression.getFunctionalInterfaceType();
            final PsiClassType.ClassResolveResult functionalInterfaceResolveResult = PsiUtil.resolveGenericsClassInType(functionalInterfaceType);
            final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(functionalInterfaceType);
            if (interfaceMethod != null && interfaceMethod.getParameterList().getParametersCount() == 1) {
                final PsiSubstitutor psiSubstitutor = LambdaUtil.getSubstitutor(interfaceMethod, functionalInterfaceResolveResult);
                final MethodSignature signature = interfaceMethod.getSignature(psiSubstitutor);
                String message = SuspiciousMethodCallUtil.getSuspiciousMethodCallMessage(expression, signature.getParameterTypes()[0], REPORT_CONVERTIBLE_METHOD_CALLS, patternMethods, indices);
                if (message != null) {
                    holder.registerProblem(ObjectUtils.notNull(expression.getReferenceNameElement(), expression), message);
                }
            }
        }
    };
}
Also used : MethodSignature(com.intellij.psi.util.MethodSignature) IntArrayList(com.intellij.util.containers.IntArrayList) ArrayList(java.util.ArrayList) IntArrayList(com.intellij.util.containers.IntArrayList) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

MethodSignature (com.intellij.psi.util.MethodSignature)31 Nullable (org.jetbrains.annotations.Nullable)8 NotNull (org.jetbrains.annotations.NotNull)7 ArrayList (java.util.ArrayList)5 Map (java.util.Map)4 com.intellij.psi (com.intellij.psi)3 List (java.util.List)3 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)3 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 Logger (com.intellij.openapi.diagnostic.Logger)2 TextRange (com.intellij.openapi.util.TextRange)2 CandidateInfo (com.intellij.psi.infos.CandidateInfo)2 MethodCandidateInfo (com.intellij.psi.infos.MethodCandidateInfo)2 Processor (com.intellij.util.Processor)2 GrSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature)2 GrClosureType (org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType)2 OverrideImplementExploreUtil (com.intellij.codeInsight.generation.OverrideImplementExploreUtil)1 RedundantLambdaCodeBlockInspection (com.intellij.codeInspection.RedundantLambdaCodeBlockInspection)1 FindBundle (com.intellij.find.FindBundle)1 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)1