List<PsiAnnotation[]> (@NotNull PsiElement anchor, @Nullable PsiAnnotation stopAt) { List<PsiAnnotation[]> annotations = Collections.emptyList(); List<PsiAnnotation> current = null; boolean found = stopAt == null; boolean stop = false; for (PsiElement child = anchor.getNextSibling(); child != null; child = child.getNextSibling()) { if (child instanceof PsiComment || child instanceof PsiWhiteSpace) continue; if (child instanceof PsiAnnotation) { if (current == null) current = new SmartList<>(); current.add((PsiAnnotation)child); if (child == stopAt) found = stop = true; continue; } if (PsiUtil.isJavaToken(child, JavaTokenType.LBRACKET)) { if (annotations == Collections.EMPTY_LIST) annotations = new SmartList<>(); annotations.add(0, current == null ? PsiAnnotation.EMPTY_ARRAY : ContainerUtil.toArray(current, PsiAnnotation.ARRAY_FACTORY)); current = null; if (stop) return annotations; } else if (!PsiUtil.isJavaToken(child, JavaTokenType.RBRACKET)) { break; } } // annotation is misplaced (either located before the anchor or has no following brackets) return !found || stop ? null : annotations; }