public static @Nullable PsiType getRelatedType(PsiAnnotation annotation) {
    PsiAnnotationOwner owner = annotation.getOwner();
    if (owner instanceof PsiType) {
      return (PsiType)owner;
    }
    PsiType type = null;
    if (owner instanceof PsiModifierList) {
      PsiElement parent = ((PsiModifierList)owner).getParent();
      PsiTypeElement typeElement = null;
      if (parent instanceof PsiVariable) {
        type = ((PsiVariable)parent).getType();
        typeElement = ((PsiVariable)parent).getTypeElement();
      }
      if (parent instanceof PsiMethod) {
        type = ((PsiMethod)parent).getReturnType();
        typeElement = ((PsiMethod)parent).getReturnTypeElement();
      }
      if (type != null) {
        PsiClass annoClass = annotation.resolveAnnotationType();
        if (annoClass != null) {
          Set<PsiAnnotation.TargetType> targets = AnnotationTargetUtil.getAnnotationTargets(annoClass);
          if (targets != null && targets.contains(PsiAnnotation.TargetType.TYPE_USE) &&
              PsiUtil.isAvailable(JavaFeature.TYPE_ANNOTATIONS, parent)) {
            if (typeElement != null && targets.size() == 1) {
              // For ambiguous annotations, we assume that annotation on the outer type relates to the inner type
              // E.g. @Nullable Outer.Inner is equivalent to Outer.@Nullable Inner (if @Nullable is not type-use only)
              PsiJavaCodeReferenceElement ref = typeElement.getInnermostComponentReferenceElement();
              // See com.intellij.psi.impl.source.tree.java.PsiAnnotationImpl.getOwner
              while (ref != null && ref.isQualified()) {
                ref = ObjectUtils.tryCast(ref.getQualifier(), PsiJavaCodeReferenceElement.class);
              }
              if (ref != null) {
                return JavaPsiFacade.getElementFactory(annotation.getProject()).createType(ref);
              }
            }
            return type.getDeepComponentType();
          }
        }
        return type;
      }
    }
    return null;
  }
public static @Nullable PsiType getRelatedType(PsiAnnotation annotation)