Esempio n. 1
0
 private static void annotateWithNullableStuff(
     final PsiModifierListOwner field,
     final PsiElementFactory factory,
     final PsiModifierListOwner listOwner)
     throws IncorrectOperationException {
   if (AnnotationUtil.isAnnotated(field, AnnotationUtil.NOT_NULL, false)) {
     annotate(factory, listOwner, AnnotationUtil.NOT_NULL);
   } else if (AnnotationUtil.isAnnotated(field, AnnotationUtil.NULLABLE, false)) {
     annotate(factory, listOwner, AnnotationUtil.NULLABLE);
   }
 }
 @Override
 public void visitMethod(@NotNull PsiMethod method) {
   super.visitMethod(method);
   final PsiCodeBlock body = method.getBody();
   if (body == null) {
     return;
   }
   if (method.getNameIdentifier() == null) {
     return;
   }
   final PsiMethod leastConcreteSuperMethod = getLeastConcreteSuperMethod(method);
   if (leastConcreteSuperMethod == null) {
     return;
   }
   final PsiClass objectClass = ClassUtils.findObjectClass(method);
   final PsiMethod[] superMethods = method.findSuperMethods(objectClass);
   if (superMethods.length > 0) {
     return;
   }
   if (ignoreEmptySuperMethods) {
     final PsiMethod superMethod = (PsiMethod) leastConcreteSuperMethod.getNavigationElement();
     if (MethodUtils.isTrivial(superMethod, true)) {
       return;
     }
   }
   if (onlyReportWhenAnnotated) {
     if (!AnnotationUtil.isAnnotated(leastConcreteSuperMethod, annotations)) {
       return;
     }
   }
   if (containsSuperCall(body, leastConcreteSuperMethod)) {
     return;
   }
   registerMethodError(method);
 }
 public static String getConfigAnnotation(PsiMethod method) {
   if (method != null) {
     for (String fqn : CONFIG_ANNOTATIONS_FQN) {
       if (AnnotationUtil.isAnnotated(method, fqn, false)) return fqn;
     }
   }
   return null;
 }
  @Override
  public boolean isAvailable(
      @NotNull Project project,
      @NotNull PsiFile file,
      @NotNull PsiElement startElement,
      @NotNull PsiElement endElement) {
    if (!startElement.isValid()) return false;
    if (!PsiUtil.isLanguageLevel5OrHigher(startElement)) return false;
    final PsiModifierListOwner myModifierListOwner = (PsiModifierListOwner) startElement;

    return !AnnotationUtil.isAnnotated(myModifierListOwner, myAnnotation, false);
  }
  private static boolean isConfigMethod(PsiMethod method, String[] configAnnotationsFqn) {
    for (String fqn : configAnnotationsFqn) {
      if (AnnotationUtil.isAnnotated(method, fqn, false)) return true;
    }

    if (hasDocTagsSupport) {
      final PsiDocComment comment = method.getDocComment();
      if (comment != null) {
        for (String javadocTag : CONFIG_JAVADOC_TAGS) {
          if (comment.findTagByName(javadocTag) != null) return true;
        }
      }
    }
    return false;
  }
  @Override
  public boolean isAvailable(
      @NotNull final Project project, final Editor editor, @NotNull final PsiElement element) {
    if (!super.isAvailable(project, editor, element)) {
      return false;
    }
    PsiModifierListOwner owner = getContainer(element);
    if (owner == null || AnnotationUtil.isAnnotated(owner, getAnnotationsToRemove()[0], false)) {
      return false;
    }
    if (owner instanceof PsiMethod) {
      PsiType returnType = ((PsiMethod) owner).getReturnType();

      return returnType != null && !(returnType instanceof PsiPrimitiveType);
    }
    return true;
  }
Esempio n. 7
0
 public static boolean isJUnitTestMethod(PsiMethod method) {
   //noinspection HardCodedStringLiteral
   if (AnnotationUtil.isAnnotated(method, "org.junit.Test", true)) {
     return true;
   }
   final PsiType returnType = method.getReturnType();
   if (!PsiType.VOID.equals(returnType)) {
     return false;
   }
   if (!method.hasModifierProperty(PsiModifier.PUBLIC)) {
     return false;
   }
   @NonNls final String methodName = method.getName();
   if (!methodName.startsWith("test")) {
     return false;
   }
   final PsiClass containingClass = method.getContainingClass();
   return containingClass != null && isJUnitTestCase(containingClass);
 }
Esempio n. 8
0
 public static boolean isJUnitTestCase(PsiClass aClass) {
   final Project project = aClass.getProject();
   final GlobalSearchScope scope = GlobalSearchScope.allScope(project);
   final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
   final PsiClass testCase = psiFacade.findClass("junit.framework.TestCase", scope);
   if (testCase == null) {
     return false;
   }
   if (aClass.isInheritor(testCase, true)) {
     return true;
   }
   final PsiMethod[] methods = aClass.getMethods();
   for (PsiMethod method : methods) {
     //noinspection HardCodedStringLiteral
     if (AnnotationUtil.isAnnotated(method, "org.junit.Test", true)) {
       return true;
     }
   }
   return false;
 }
 @Override
 public void visitLiteralExpression(@NotNull PsiLiteralExpression value) {
   super.visitLiteralExpression(value);
   final String text = value.getText();
   if (!PsiKeyword.NULL.equals(text)) {
     return;
   }
   PsiElement parent = value.getParent();
   while (parent instanceof PsiParenthesizedExpression
       || parent instanceof PsiConditionalExpression
       || parent instanceof PsiTypeCastExpression) {
     parent = parent.getParent();
   }
   if (parent == null || !(parent instanceof PsiReturnStatement)) {
     return;
   }
   final PsiMethod method = PsiTreeUtil.getParentOfType(value, PsiMethod.class);
   if (method == null) {
     return;
   }
   final PsiType returnType = method.getReturnType();
   if (returnType == null) {
     return;
   }
   final boolean isArray = returnType.getArrayDimensions() > 0;
   if (AnnotationUtil.isAnnotated(method, AnnotationUtil.NULLABLE, false)) {
     return;
   }
   if (m_reportCollectionMethods && CollectionUtils.isCollectionClassOrInterface(returnType)) {
     registerError(value, value);
   } else if (m_reportArrayMethods && isArray) {
     registerError(value, value);
   } else if (m_reportObjectMethods && !isArray) {
     registerError(value, value);
   }
 }
 private static boolean isAnnotatedAsTestOnly(@Nullable PsiMethod m) {
   if (m == null) return false;
   return AnnotationUtil.isAnnotated(m, AnnotationUtil.TEST_ONLY, false, false)
       || AnnotationUtil.isAnnotated(
           m, "com.google.common.annotations.VisibleForTesting", false, false);
 }
Esempio n. 11
0
 public static boolean containsJunitAnnotions(PsiMethod method) {
   return method != null && AnnotationUtil.isAnnotated(method, JUNIT_ANNOTATIONS);
 }
Esempio n. 12
0
  public static boolean hasTest(
      PsiModifierListOwner element,
      boolean checkHierarchy,
      boolean checkDisabled,
      boolean checkJavadoc) {
    // LanguageLevel effectiveLanguageLevel = element.getManager().getEffectiveLanguageLevel();
    // boolean is15 = effectiveLanguageLevel != LanguageLevel.JDK_1_4 && effectiveLanguageLevel !=
    // LanguageLevel.JDK_1_3;
    boolean hasAnnotation =
        AnnotationUtil.isAnnotated(element, TEST_ANNOTATION_FQN, checkHierarchy, true);
    if (hasAnnotation) {
      if (checkDisabled) {
        PsiAnnotation annotation =
            AnnotationUtil.findAnnotation(element, true, TEST_ANNOTATION_FQN);
        if (annotation != null) {
          if (isDisabled(annotation)) return false;
        }
      }
      return true;
    }
    if (element instanceof PsiDocCommentOwner
        && checkJavadoc
        && getTextJavaDoc((PsiDocCommentOwner) element) != null) return true;
    // now we check all methods for the test annotation
    if (element instanceof PsiClass) {
      PsiClass psiClass = (PsiClass) element;
      for (PsiMethod method : psiClass.getAllMethods()) {
        PsiAnnotation annotation = AnnotationUtil.findAnnotation(method, true, TEST_ANNOTATION_FQN);
        if (annotation != null) {
          if (checkDisabled) {
            if (isDisabled(annotation)) continue;
          }
          return true;
        }
        if (AnnotationUtil.isAnnotated(method, FACTORY_ANNOTATION_FQN, false, true)) return true;
        if (checkJavadoc && getTextJavaDoc(method) != null) return true;
      }
      return false;
    } else if (element instanceof PsiMethod) {
      // even if it has a global test, we ignore private and static methods
      if (element.hasModifierProperty(PsiModifier.PRIVATE)
          || element.hasModifierProperty(PsiModifier.STATIC)) {
        return false;
      }

      // if it's a method, we check if the class it's in has a global @Test annotation
      PsiClass psiClass = ((PsiMethod) element).getContainingClass();
      if (psiClass != null) {
        final PsiAnnotation annotation =
            checkHierarchy
                ? AnnotationUtil.findAnnotationInHierarchy(
                    psiClass, Collections.singleton(TEST_ANNOTATION_FQN))
                : AnnotationUtil.findAnnotation(psiClass, true, TEST_ANNOTATION_FQN);
        if (annotation != null) {
          if (checkDisabled && isDisabled(annotation)) return false;
          return !hasConfig(element);
        } else if (checkJavadoc && getTextJavaDoc(psiClass) != null) return true;
      }
    }
    return false;
  }