Search in sources :

Example 1 with BaseInspectionVisitor

use of com.siyeh.ig.BaseInspectionVisitor in project intellij-community by JetBrains.

the class JUnitRuleInspection method buildVisitor.

@Override
public BaseInspectionVisitor buildVisitor() {
    return new BaseInspectionVisitor() {

        @Override
        public void visitField(PsiField field) {
            final boolean ruleAnnotated = REPORT_RULE_PROBLEMS && AnnotationUtil.isAnnotated(field, RULE_FQN, false);
            final boolean classRuleAnnotated = REPORT_CLASS_RULE_PROBLEMS && AnnotationUtil.isAnnotated(field, CLASS_RULE_FQN, false);
            if (ruleAnnotated || classRuleAnnotated) {
                String annotation = ruleAnnotated ? RULE_FQN : CLASS_RULE_FQN;
                String errorMessage = getPublicStaticErrorMessage(field, ruleAnnotated, classRuleAnnotated);
                if (errorMessage != null) {
                    registerError(field.getNameIdentifier(), InspectionGadgetsBundle.message("junit.rule.problem.descriptor", annotation, errorMessage), "Make field " + errorMessage, annotation);
                }
                final PsiClass aClass = PsiUtil.resolveClassInClassTypeOnly(field.getType());
                if (!InheritanceUtil.isInheritor(aClass, false, "org.junit.rules.TestRule") && !InheritanceUtil.isInheritor(aClass, false, "org.junit.rules.MethodRule")) {
                    registerError(field.getNameIdentifier(), InspectionGadgetsBundle.message("junit.rule.type.problem.descriptor"));
                }
            }
        }
    };
}
Also used : BaseInspectionVisitor(com.siyeh.ig.BaseInspectionVisitor) PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass)

Example 2 with BaseInspectionVisitor

use of com.siyeh.ig.BaseInspectionVisitor in project intellij-community by JetBrains.

the class ParameterizedParametersStaticCollectionInspectionBase method buildVisitor.

@Override
public BaseInspectionVisitor buildVisitor() {
    return new BaseInspectionVisitor() {

        @Override
        public void visitClass(PsiClass aClass) {
            final PsiAnnotation annotation = AnnotationUtil.findAnnotation(aClass, "org.junit.runner.RunWith");
            if (annotation != null) {
                for (PsiNameValuePair pair : annotation.getParameterList().getAttributes()) {
                    final PsiAnnotationMemberValue value = pair.getValue();
                    if (value instanceof PsiClassObjectAccessExpression) {
                        final PsiTypeElement typeElement = ((PsiClassObjectAccessExpression) value).getOperand();
                        if (typeElement.getType().getCanonicalText().equals(PARAMETERIZED_FQN)) {
                            List<MethodCandidate> candidates = new ArrayList<>();
                            for (PsiMethod method : aClass.getMethods()) {
                                PsiType returnType = method.getReturnType();
                                final PsiClass returnTypeClass = PsiUtil.resolveClassInType(returnType);
                                final Project project = aClass.getProject();
                                final PsiClass collectionsClass = JavaPsiFacade.getInstance(project).findClass(Collection.class.getName(), GlobalSearchScope.allScope(project));
                                if (AnnotationUtil.isAnnotated(method, PARAMETERS_FQN, false)) {
                                    final PsiModifierList modifierList = method.getModifierList();
                                    boolean hasToFixSignature = false;
                                    String message = "Make method \'" + method.getName() + "\' ";
                                    String errorString = "Method \'#ref()\' should be ";
                                    if (!modifierList.hasModifierProperty(PsiModifier.PUBLIC)) {
                                        message += PsiModifier.PUBLIC + " ";
                                        errorString += PsiModifier.PUBLIC + " ";
                                        hasToFixSignature = true;
                                    }
                                    if (!modifierList.hasModifierProperty(PsiModifier.STATIC)) {
                                        message += PsiModifier.STATIC;
                                        errorString += PsiModifier.STATIC;
                                        hasToFixSignature = true;
                                    }
                                    if (collectionsClass != null && (returnTypeClass == null || !InheritanceUtil.isInheritorOrSelf(returnTypeClass, collectionsClass, true))) {
                                        message += (hasToFixSignature ? " and" : "") + " return Collection";
                                        errorString += (hasToFixSignature ? " and" : "") + " return Collection";
                                        returnType = JavaPsiFacade.getElementFactory(project).createType(collectionsClass);
                                        hasToFixSignature = true;
                                    }
                                    if (hasToFixSignature) {
                                        candidates.add(new MethodCandidate(method, message, errorString, returnType));
                                        continue;
                                    }
                                    return;
                                }
                            }
                            if (candidates.isEmpty()) {
                                registerClassError(aClass);
                            } else {
                                for (MethodCandidate candidate : candidates) {
                                    registerMethodError(candidate.myMethod, candidate.myProblem, candidate.myErrorString, candidate.myReturnType);
                                }
                            }
                        }
                    }
                }
            }
        }
    };
}
Also used : BaseInspectionVisitor(com.siyeh.ig.BaseInspectionVisitor) ArrayList(java.util.ArrayList) Project(com.intellij.openapi.project.Project) Collection(java.util.Collection)

Example 3 with BaseInspectionVisitor

use of com.siyeh.ig.BaseInspectionVisitor in project intellij-community by JetBrains.

the class JUnit5ConverterInspection method buildVisitor.

@Override
public BaseInspectionVisitor buildVisitor() {
    return new BaseInspectionVisitor() {

        @Override
        public void visitClass(PsiClass aClass) {
            TestFramework framework = TestFrameworks.detectFramework(aClass);
            if (framework == null || !"JUnit4".equals(framework.getName())) {
                return;
            }
            if (!canBeConvertedToJUnit5(aClass))
                return;
            registerClassError(aClass);
        }
    };
}
Also used : BaseInspectionVisitor(com.siyeh.ig.BaseInspectionVisitor) TestFramework(com.intellij.testIntegration.TestFramework)

Aggregations

BaseInspectionVisitor (com.siyeh.ig.BaseInspectionVisitor)3 Project (com.intellij.openapi.project.Project)1 PsiClass (com.intellij.psi.PsiClass)1 PsiField (com.intellij.psi.PsiField)1 TestFramework (com.intellij.testIntegration.TestFramework)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1