PsiAnnotation (@NotNull @NonNls String qualifiedName) { PsiAnnotation annotation = JavaPsiFacade.getElementFactory(getProject()).createAnnotationFromText('@' + qualifiedName, this); PsiElement firstChild = getFirstChild(); for (PsiElement child = getLastChild(); child != firstChild; child = child.getPrevSibling()) { if (PsiUtil.isJavaToken(child, JavaTokenType.LBRACKET) || PsiUtil.isJavaToken(child, JavaTokenType.ELLIPSIS)) { return (PsiAnnotation)addBefore(annotation, child); } } if (firstChild instanceof PsiJavaCodeReferenceElement) { PsiIdentifier identifier = PsiTreeUtil.getChildOfType(firstChild, PsiIdentifier.class); if (identifier != null && identifier != firstChild.getFirstChild()) { // qualified reference return (PsiAnnotation)firstChild.addBefore(annotation, identifier); } } PsiElement parent = getParent(); while (parent instanceof PsiTypeElement && ((PsiTypeElement)parent).getType() instanceof PsiArrayType) { parent = parent.getParent(); } if (parent instanceof PsiModifierListOwner) { PsiModifierList modifierList = ((PsiModifierListOwner)parent).getModifierList(); if (modifierList != null) { PsiTypeParameterList list = parent instanceof PsiTypeParameterListOwner ? ((PsiTypeParameterListOwner)parent).getTypeParameterList() : null; if (list == null || list.textMatches("")) { return (PsiAnnotation)modifierList.add(annotation); } } } return (PsiAnnotation)addBefore(annotation, firstChild); }