PsiElement (PsiModifierList modifierList) { if (modifierList == null) { return null; } final Deque<PsiElement> modifiers = new ArrayDeque<>(); PsiAnnotation typeAnnotation = null; for (final PsiElement child : modifierList.getChildren()) { if (child instanceof PsiJavaToken) { if (typeAnnotation != null) return typeAnnotation; final String text = child.getText(); if (!modifiers.isEmpty() && modifierComparator.compare(text, modifiers.getLast().getText()) < 0) { while (!modifiers.isEmpty()) { final PsiElement first = modifiers.pollFirst(); if (modifierComparator.compare(text, first.getText()) < 0) { return first; } } } modifiers.add(child); } if (child instanceof PsiAnnotation annotation) { if (m_requireAnnotationsFirst) { if (AnnotationTargetUtil.isTypeAnnotation(annotation) && !isMethodWithVoidReturnType(modifierList.getParent())) { // type annotations go next to the type // see e.g. https://www.oracle.com/technical-resources/articles/java/ma14-architect-annotations.html if (typeUseWithType || !modifiers.isEmpty()) { typeAnnotation = annotation; } final PsiAnnotation.TargetType[] targets = AnnotationTargetUtil.getTargetsForLocation(annotation.getOwner()); if (targets.length > 0 && AnnotationTargetUtil.findAnnotationTarget(annotation, targets[0]) == PsiAnnotation.TargetType.UNKNOWN) { typeAnnotation = annotation; } continue; } if (m_requireAnnotationsFirst && !modifiers.isEmpty()) { //things aren't in order, since annotations come first return modifiers.getFirst(); } } else if (!modifiers.isEmpty()) { typeAnnotation = annotation; } } } return null; }