@Override
  public @Nullable PsiAnnotationOwner getOwner() {
    PsiElement parent = getParent();

    if (parent instanceof PsiTypeElementImpl) {
      PsiType type = ((PsiTypeElement)parent).getType();
      if (type instanceof PsiClassType) {
        // If we have a type element like @Anno Outer.Inner then the annotation belongs to the Outer type
        // which doesn't have a corresponding type element at all. We create this type here.
        PsiJavaCodeReferenceElement origRef = ((PsiTypeElement)parent).getInnermostComponentReferenceElement();
        PsiJavaCodeReferenceElement ref = origRef;
        while (ref != null && ref.isQualified()) {
          ref = ObjectUtils.tryCast(ref.getQualifier(), PsiJavaCodeReferenceElement.class);
        }
        if (ref != null && ref != origRef) {
          return new PsiClassReferenceType(ref, null).annotate(type.getAnnotationProvider());
        }
      }
      else if (type instanceof PsiArrayType) {
        for (PsiElement sibling = getPrevSibling(); sibling != null; sibling = sibling.getPrevSibling()) {
          if (PsiUtil.isJavaToken(sibling, JavaTokenType.LBRACKET)) {
            type = ((PsiArrayType)type).getComponentType();
          }
        }
      }
      return type;
    }
    
    if (parent instanceof PsiAnnotationOwner) {
      return (PsiAnnotationOwner)parent;
    }

    if (parent instanceof PsiNewExpression) {
      return ((PsiNewExpression)parent).getOwner(this);
    }

    if (parent instanceof PsiReferenceExpression) {
      PsiElement ctx = parent.getParent();
      if (ctx instanceof PsiMethodReferenceExpression) {
        return new PsiClassReferenceType((PsiJavaCodeReferenceElement)parent, null);
      }
    }
    else if (parent instanceof PsiJavaCodeReferenceElement) {
      PsiElement ctx = PsiTreeUtil.skipParentsOfType(parent, PsiJavaCodeReferenceElement.class);
      if (ctx instanceof PsiReferenceList ||
          ctx instanceof PsiNewExpression ||
          ctx instanceof PsiTypeElement ||
          ctx instanceof PsiAnonymousClass) {
        return new PsiClassReferenceType((PsiJavaCodeReferenceElement)parent, null);
      }
    }

    PsiTypeElement typeElement = null;
    PsiElement anchor = null;
    if (parent instanceof PsiMethod) {
      typeElement = ((PsiMethod)parent).getReturnTypeElement();
      anchor = ((PsiMethod)parent).getParameterList();
    }
    else if (parent instanceof PsiField || parent instanceof PsiParameter || parent instanceof PsiLocalVariable) {
      typeElement = ((PsiVariable)parent).getTypeElement();
      anchor = ((PsiVariable)parent).getNameIdentifier();
    }
    if (typeElement != null && anchor != null) {
      return JavaSharedImplUtil.getType(typeElement, anchor, this);
    }

    return null;
  }
@Override
  public @Nullable PsiAnnotationOwner getOwner()