Search in sources :

Example 11 with BaseInspectionVisitor

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

the class ClashingGettersInspection method buildVisitor.

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

        @Override
        public void visitTypeDefinition(@NotNull GrTypeDefinition typeDefinition) {
            super.visitTypeDefinition(typeDefinition);
            Map<String, PsiMethod> getters = new HashMap<>();
            for (PsiMethod method : typeDefinition.getMethods()) {
                final String methodName = method.getName();
                if (!GroovyPropertyUtils.isSimplePropertyGetter(method))
                    continue;
                final String propertyName = GroovyPropertyUtils.getPropertyNameByGetterName(methodName, true);
                final PsiMethod otherGetter = getters.get(propertyName);
                if (otherGetter != null && !methodName.equals(otherGetter.getName())) {
                    final Pair<PsiElement, String> description = getGetterDescription(method);
                    final Pair<PsiElement, String> otherDescription = getGetterDescription(otherGetter);
                    if (description.first != null) {
                        registerError(description.first, description.second, otherDescription.second);
                    }
                    if (otherDescription.first != null) {
                        registerError(otherDescription.first, otherDescription.second, description.second);
                    }
                } else {
                    getters.put(propertyName, method);
                }
            }
        }
    };
}
Also used : BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) HashMap(java.util.HashMap) PsiMethod(com.intellij.psi.PsiMethod) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with BaseInspectionVisitor

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

the class ChangeToMethodInspection method buildVisitor.

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

        @Override
        public void visitExpression(@NotNull GrExpression expression) {
            Transformation<?> transformation = getTransformation(expression);
            if (transformation == null)
                return;
            PsiElement highlightingElement = getHighlightingElement(expression);
            if (highlightingElement == null)
                return;
            if (transformation.couldApplyRow(expression)) {
                registerError(highlightingElement, message("replace.with.method.message", transformation.getMethod()), new LocalQuickFix[] { getFix(transformation) }, GENERIC_ERROR_OR_WARNING);
            }
        }
    };
}
Also used : BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with BaseInspectionVisitor

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

the class GroovyDocCheckInspection method buildVisitor.

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

        @Override
        public void visitDocMethodReference(@NotNull GrDocMethodReference reference) {
            checkGrDocMemberReference(reference);
        }

        @Override
        public void visitDocFieldReference(@NotNull GrDocFieldReference reference) {
            checkGrDocMemberReference(reference);
        }

        @Override
        public void visitCodeReferenceElement(@NotNull GrCodeReferenceElement refElement) {
            GroovyResolveResult resolveResult = refElement.advancedResolve();
            if (refElement.getReferenceName() == null)
                return;
            if (PsiTreeUtil.getParentOfType(refElement, GroovyDocPsiElement.class, true, GrMember.class, GrCodeBlock.class) == null)
                return;
            final PsiElement resolved = resolveResult.getElement();
            if (resolved != null)
                return;
            final PsiElement toHighlight = refElement.getReferenceNameElement();
            registerError(toHighlight, GroovyBundle.message("cannot.resolve", refElement.getReferenceName()));
        }

        private void checkGrDocMemberReference(final GrDocMemberReference reference) {
            if (reference.resolve() != null)
                return;
            registerError(reference.getReferenceNameElement(), GroovyBundle.message("cannot.resolve", reference.getReferenceName()));
        }
    };
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GroovyDocPsiElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GroovyDocPsiElement) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrDocFieldReference(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocFieldReference) GrDocMemberReference(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMemberReference) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) GroovyDocPsiElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GroovyDocPsiElement) GrDocMethodReference(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMethodReference) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with BaseInspectionVisitor

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

the class NewInstanceOfSingletonInspection method buildVisitor.

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

        @Override
        public void visitNewExpression(@NotNull GrNewExpression newExpression) {
            if (newExpression.getArrayDeclaration() != null)
                return;
            GrCodeReferenceElement refElement = newExpression.getReferenceElement();
            if (refElement == null)
                return;
            PsiElement resolved = refElement.resolve();
            if (!(resolved instanceof GrTypeDefinition))
                return;
            PsiAnnotation annotation = AnnotationUtil.findAnnotation((GrTypeDefinition) resolved, GROOVY_LANG_SINGLETON);
            if (annotation == null)
                return;
            registerError(newExpression, GroovyInspectionBundle.message("new.instance.of.singleton"), ContainerUtil.ar(new ReplaceWithInstanceAccessFix()), ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
        }
    };
}
Also used : GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiAnnotation(com.intellij.psi.PsiAnnotation) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with BaseInspectionVisitor

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

the class TypeCustomizerInspection method buildVisitor.

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

        @Override
        public void visitFile(@NotNull GroovyFileBase file) {
            CompilerConfiguration configuration = CompilerConfiguration.getInstance(file.getProject());
            if (configuration != null && !configuration.isResourceFile(file.getVirtualFile()) && fileSeemsToBeTypeCustomizer(file)) {
                final LocalQuickFix[] fixes = { new AddToResourceFix(file) };
                final String message = GroovyInspectionBundle.message("type.customizer.is.not.marked.as.a.resource.file");
                registerError(file, message, fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
            }
        }
    };
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) CompilerConfiguration(com.intellij.compiler.CompilerConfiguration) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) NotNull(org.jetbrains.annotations.NotNull) 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