Search in sources :

Example 1 with HierarchicalMethodSignature

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

the class ClashingTraitMethodsInspectionBase method buildVisitor.

@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
    return new BaseInspectionVisitor() {

        @Override
        public void visitTypeDefinition(@NotNull GrTypeDefinition typeDefinition) {
            super.visitTypeDefinition(typeDefinition);
            List<PsiClass> superTraits = collectImplementedTraits(typeDefinition);
            if (superTraits.size() < 2)
                return;
            List<ClashingMethod> clashingMethods = collectClassingMethods(typeDefinition);
            for (ClashingMethod clashing : clashingMethods) {
                registerError(typeDefinition.getNameIdentifierGroovy(), buildWarning(clashing), new LocalQuickFix[] { getFix() }, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
            }
        }

        @NotNull
        private String buildWarning(@NotNull ClashingMethod entry) {
            return "Traits " + buildTraitString(entry) + " contain clashing methods with signature " + buildSignatureString(entry);
        }

        @NotNull
        private String buildSignatureString(@NotNull ClashingMethod entry) {
            HierarchicalMethodSignature signature = entry.getSignature();
            return PsiFormatUtil.formatMethod(signature.getMethod(), signature.getSubstitutor(), PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_TYPE);
        }

        @NotNull
        private String buildTraitString(@NotNull ClashingMethod entry) {
            return StringUtil.join(entry.getSuperTraits(), tr -> tr.getName(), ", ");
        }
    };
}
Also used : BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiClass(com.intellij.psi.PsiClass) HierarchicalMethodSignature(com.intellij.psi.HierarchicalMethodSignature) NotNull(org.jetbrains.annotations.NotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with HierarchicalMethodSignature

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

the class ClashingTraitMethodsInspectionBase method collectClassingMethods.

@NotNull
protected static List<ClashingMethod> collectClassingMethods(@NotNull GrTypeDefinition typeDefinition) {
    Collection<HierarchicalMethodSignature> visibleSignatures = typeDefinition.getVisibleSignatures();
    List<ClashingMethod> clashingMethods = ContainerUtil.newArrayList();
    for (HierarchicalMethodSignature signature : visibleSignatures) {
        PsiMethod method = signature.getMethod();
        if (method instanceof GrTraitMethod && method.getContainingClass() == typeDefinition) {
            List<HierarchicalMethodSignature> superSignatures = signature.getSuperSignatures();
            if (superSignatures.size() > 1) {
                List<GrTypeDefinition> traits = ContainerUtil.newArrayList();
                for (HierarchicalMethodSignature superSignature : superSignatures) {
                    PsiMethod superMethod = superSignature.getMethod();
                    PsiClass superClass = superMethod.getContainingClass();
                    if (GrTraitUtil.isTrait(superClass) && !superMethod.getModifierList().hasExplicitModifier(PsiModifier.ABSTRACT)) {
                        traits.add((GrTypeDefinition) superClass);
                    }
                }
                if (traits.size() > 1) {
                    clashingMethods.add(new ClashingMethod(signature, traits));
                }
            }
        }
    }
    return clashingMethods;
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) HierarchicalMethodSignature(com.intellij.psi.HierarchicalMethodSignature) GrTraitMethod(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrTraitMethod) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

HierarchicalMethodSignature (com.intellij.psi.HierarchicalMethodSignature)2 PsiClass (com.intellij.psi.PsiClass)2 NotNull (org.jetbrains.annotations.NotNull)2 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)2 PsiMethod (com.intellij.psi.PsiMethod)1 BaseInspectionVisitor (org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor)1 GrTraitMethod (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrTraitMethod)1