Source:
<code>private void warnAboutTypeAnnotationsTooEarly(Tree node, ModifiersTree modifiersTree) { if (node.getKind() == Tree.Kind.VARIABLE) { if (TreeUtils.elementFromDeclaration((VariableTree) node).getKind() == ElementKind.ENUM_CONSTANT) { return; } <START> Tree parent = getCurrentPath().getParentPath().getLeaf(); <END> if (parent.getKind() == Tree.Kind.TRY) { return; } } Set<Modifier> modifierSet = modifiersTree.getFlags(); List<? extends AnnotationTree> annotations = modifiersTree.getAnnotations(); if (annotations.isEmpty()) { return; } int lastDeclAnnoIndex = -1; for (int i = annotations.size() - 1; i > 0; i--) { if (!isTypeAnnotation(annotations.get(i))) { lastDeclAnnoIndex = i; break; } } if (lastDeclAnnoIndex != -1) { List<AnnotationTree> badTypeAnnos = new ArrayList<>(); for (int i = 0; i < lastDeclAnnoIndex; i++) { AnnotationTree anno = annotations.get(i); if (isTypeAnnotation(anno)) { badTypeAnnos.add(anno); } } if (!badTypeAnnos.isEmpty()) { checker.reportWarning( node, "type.anno.before.decl.anno", badTypeAnnos, annotations.get(lastDeclAnnoIndex)); } } AnnotationTree firstAnno = annotations.get(0); if (!modifierSet.isEmpty() && isTypeAnnotation(firstAnno)) { int precedingTextLength = 0; for (Modifier m : modifierSet) { precedingTextLength += m.toString().length() + 1; } int annoStartPos = ((JCTree) firstAnno).getStartPosition(); int varStartPos = ((JCTree) node).getStartPosition(); if (annoStartPos < varStartPos + precedingTextLength) { checker.reportWarning(node, "type.anno.before.modifier", firstAnno, modifierSet); } } }</code><technical_language>a bit worried Paths, costly. I simplify this significantly: - determine variable kind kind = TreeUtils.elementFromDeclaration((VariableTree) node).getKind() - if kind is ENUM_CONSTANT enum constant - if kind is RESOURCE_VARIABLE a try resource need a path checks more symmetric</technical_language>

Target:
private void warnAboutTypeAnnotationsTooEarly(Tree node, ModifiersTree modifiersTree) { if (node.getKind() == Tree.Kind.VARIABLE) { ElementKind varKind = TreeUtils.elementFromDeclaration((VariableTree) node).getKind(); switch (varKind) { case ENUM_CONSTANT: return; case RESOURCE_VARIABLE: return; default: } } Set<Modifier> modifierSet = modifiersTree.getFlags(); List<? extends AnnotationTree> annotations = modifiersTree.getAnnotations(); if (annotations.isEmpty()) { return; } int lastDeclAnnoIndex = -1; for (int i = annotations.size() - 1; i > 0; i--) { if (!isTypeAnnotation(annotations.get(i))) { lastDeclAnnoIndex = i; break; } } if (lastDeclAnnoIndex != -1) { List<AnnotationTree> badTypeAnnos = new ArrayList<>(); for (int i = 0; i < lastDeclAnnoIndex; i++) { AnnotationTree anno = annotations.get(i); if (isTypeAnnotation(anno)) { badTypeAnnos.add(anno); } } if (!badTypeAnnos.isEmpty()) { checker.reportWarning( node, "type.anno.before.decl.anno", badTypeAnnos, annotations.get(lastDeclAnnoIndex)); } } AnnotationTree firstAnno = annotations.get(0); if (!modifierSet.isEmpty() && isTypeAnnotation(firstAnno)) { int precedingTextLength = 0; for (Modifier m : modifierSet) { precedingTextLength += m.toString().length() + 1; } int annoStartPos = ((JCTree) firstAnno).getStartPosition(); int varStartPos = ((JCTree) node).getStartPosition(); if (annoStartPos < varStartPos + precedingTextLength) { checker.reportWarning(node, "type.anno.before.modifier", firstAnno, modifierSet); } } }