Search in sources :

Example 1 with BaseInspectionVisitor

use of org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor in project intellij-community by JetBrains.

the class JavaStylePropertiesInvocationInspection method buildVisitor.

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

        @Override
        public void visitMethodCallExpression(@NotNull GrMethodCallExpression methodCallExpression) {
            super.visitMethodCallExpression(methodCallExpression);
            visitMethodCall(methodCallExpression);
        }

        @Override
        public void visitApplicationStatement(@NotNull GrApplicationStatement applicationStatement) {
            super.visitApplicationStatement(applicationStatement);
            visitMethodCall(applicationStatement);
        }

        private void visitMethodCall(GrMethodCall methodCall) {
            if (JavaStylePropertiesUtil.isPropertyAccessor(methodCall)) {
                final String message = GroovyInspectionBundle.message("java.style.property.access");
                final GrExpression expression = methodCall.getInvokedExpression();
                if (expression instanceof GrReferenceExpression) {
                    PsiElement referenceNameElement = ((GrReferenceExpression) expression).getReferenceNameElement();
                    registerError(referenceNameElement, message, myFixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
                }
            }
        }
    };
}
Also used : BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) NotNull(org.jetbrains.annotations.NotNull) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with BaseInspectionVisitor

use of org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor 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 3 with BaseInspectionVisitor

use of org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor in project intellij-community by JetBrains.

the class GrDeprecatedAPIUsageInspection method buildVisitor.

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

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression ref) {
            super.visitReferenceExpression(ref);
            checkRef(ref);
        }

        @Override
        public void visitCodeReferenceElement(@NotNull GrCodeReferenceElement ref) {
            super.visitCodeReferenceElement(ref);
            checkRef(ref);
        }

        private void checkRef(GrReferenceElement ref) {
            PsiElement resolved = ref.resolve();
            if (isDeprecated(resolved)) {
                PsiElement toHighlight = getElementToHighlight(ref);
                registerError(toHighlight, GroovyBundle.message("0.is.deprecated", ref.getReferenceName()), LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.LIKE_DEPRECATED);
            }
        }

        @NotNull
        public PsiElement getElementToHighlight(@NotNull GrReferenceElement refElement) {
            final PsiElement refNameElement = refElement.getReferenceNameElement();
            return refNameElement != null ? refNameElement : refElement;
        }

        private boolean isDeprecated(PsiElement resolved) {
            if (resolved instanceof PsiDocCommentOwner) {
                return ((PsiDocCommentOwner) resolved).isDeprecated();
            }
            if (resolved instanceof PsiModifierListOwner && PsiImplUtil.isDeprecatedByAnnotation((PsiModifierListOwner) resolved)) {
                return true;
            }
            return false;
        }
    };
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) PsiDocCommentOwner(com.intellij.psi.PsiDocCommentOwner) PsiModifierListOwner(com.intellij.psi.PsiModifierListOwner) NotNull(org.jetbrains.annotations.NotNull) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with BaseInspectionVisitor

use of org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor in project intellij-community by JetBrains.

the class GrPackageInspection method buildVisitor.

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

        @Override
        public void visitFile(@NotNull GroovyFileBase file) {
            if (!(file instanceof GroovyFile))
                return;
            if (!myCheckScripts && file.isScript())
                return;
            String expectedPackage = ExpectedPackageNameProviderKt.inferExpectedPackageName((GroovyFile) file);
            String actual = file.getPackageName();
            if (!expectedPackage.equals(actual)) {
                PsiElement toHighlight = getElementToHighlight((GroovyFile) file);
                if (toHighlight == null)
                    return;
                registerError(toHighlight, "Package name mismatch. Actual: '" + actual + "', expected: '" + expectedPackage + "'", new LocalQuickFix[] { new ChangePackageQuickFix(expectedPackage), GroovyQuickFixFactory.getInstance().createGrMoveToDirFix(actual) }, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
            }
        }
    };
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) NotNull(org.jetbrains.annotations.NotNull) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with BaseInspectionVisitor

use of org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor in project intellij-community by JetBrains.

the class GrReassignedInClosureLocalVarInspection method buildVisitor.

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

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
            super.visitReferenceExpression(referenceExpression);
            if (!PsiUtil.isLValue(referenceExpression))
                return;
            final PsiElement resolved = referenceExpression.resolve();
            if (!PsiUtil.isLocalVariable(resolved))
                return;
            final PsiType checked = GrReassignedLocalVarsChecker.getReassignedVarType(referenceExpression, false);
            if (checked == null)
                return;
            final GrControlFlowOwner varFlowOwner = ControlFlowUtils.findControlFlowOwner(resolved);
            final GrControlFlowOwner refFlorOwner = ControlFlowUtils.findControlFlowOwner(referenceExpression);
            if (isOtherScopeAndType(referenceExpression, checked, varFlowOwner, refFlorOwner)) {
                String flowDescription = getFlowDescription(refFlorOwner);
                final String message = GroovyInspectionBundle.message("local.var.0.is.reassigned", ((GrNamedElement) resolved).getName(), flowDescription);
                registerError(referenceExpression, message, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
            }
        }
    };
}
Also used : GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) PsiType(com.intellij.psi.PsiType) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

NotNull (org.jetbrains.annotations.NotNull)15 BaseInspectionVisitor (org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor)15 PsiElement (com.intellij.psi.PsiElement)11 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)5 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)4 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)4 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)3 PsiAnnotation (com.intellij.psi.PsiAnnotation)2 PsiType (com.intellij.psi.PsiType)2 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)2 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)2 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)2 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)2 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)2 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 CompilerConfiguration (com.intellij.compiler.CompilerConfiguration)1 HierarchicalMethodSignature (com.intellij.psi.HierarchicalMethodSignature)1 PsiAnnotationMemberValue (com.intellij.psi.PsiAnnotationMemberValue)1 PsiClass (com.intellij.psi.PsiClass)1 PsiClassType (com.intellij.psi.PsiClassType)1