コード例 #1
0
  @Nullable
  private PsiAnnotation findNullabilityAnnotation(
      @NotNull PsiModifierListOwner owner, boolean checkBases, boolean nullable) {
    Set<String> qNames = ContainerUtil.newHashSet(nullable ? getNullables() : getNotNulls());
    PsiAnnotation annotation =
        checkBases && (owner instanceof PsiClass || owner instanceof PsiMethod)
            ? AnnotationUtil.findAnnotationInHierarchy(owner, qNames)
            : AnnotationUtil.findAnnotation(owner, qNames);
    if (annotation != null) {
      return annotation;
    }

    if (owner instanceof PsiParameter
        && !TypeConversionUtil.isPrimitiveAndNotNull(((PsiParameter) owner).getType())) {
      // even if javax.annotation.Nullable is not configured, it should still take precedence over
      // ByDefault annotations
      if (AnnotationUtil.isAnnotated(
          owner,
          nullable ? Arrays.asList(DEFAULT_NOT_NULLS) : Arrays.asList(DEFAULT_NULLABLES),
          checkBases,
          false)) {
        return null;
      }
      return findContainerAnnotation(
          owner,
          nullable
              ? "javax.annotation.ParametersAreNullableByDefault"
              : "javax.annotation.ParametersAreNonnullByDefault");
    }
    return null;
  }
コード例 #2
0
  @Nullable
  private static PsiAnnotation findContainerAnnotation(
      PsiModifierListOwner owner, String annotationFQN) {
    PsiElement element = owner.getParent();
    while (element != null) {
      if (element instanceof PsiModifierListOwner) {
        PsiAnnotation annotation =
            AnnotationUtil.findAnnotation((PsiModifierListOwner) element, annotationFQN);
        if (annotation != null) {
          return annotation;
        }
      }

      if (element instanceof PsiClassOwner) {
        String packageName = ((PsiClassOwner) element).getPackageName();
        PsiPackage psiPackage =
            JavaPsiFacade.getInstance(element.getProject()).findPackage(packageName);
        return AnnotationUtil.findAnnotation(psiPackage, annotationFQN);
      }

      element = element.getContext();
    }
    return null;
  }