diff options
Diffstat (limited to 'src/main/java/com/puppycrawl/tools/checkstyle/utils/BlockCommentPosition.java')
-rw-r--r-- | src/main/java/com/puppycrawl/tools/checkstyle/utils/BlockCommentPosition.java | 50 |
1 files changed, 46 insertions, 4 deletions
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/utils/BlockCommentPosition.java b/src/main/java/com/puppycrawl/tools/checkstyle/utils/BlockCommentPosition.java index f35404e7e..5d4934261 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/utils/BlockCommentPosition.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/utils/BlockCommentPosition.java @@ -36,6 +36,18 @@ public final class BlockCommentPosition { } /** + * Node is on type definition. + * @param blockComment DetailAST + * @return true if node is before class, interface, enum or annotation. + */ + public static boolean isOnType(DetailAST blockComment) { + return isOnClass(blockComment) + || isOnInterface(blockComment) + || isOnEnum(blockComment) + || isOnAnnotationDef(blockComment); + } + + /** * Node is on class definition. * @param blockComment DetailAST * @return true if node is before class @@ -80,6 +92,20 @@ public final class BlockCommentPosition { } /** + * Node is on type member declaration. + * @param blockComment DetailAST + * @return true if node is before method, field, constructor, enum constant + * or annotation field + */ + public static boolean isOnMember(DetailAST blockComment) { + return isOnMethod(blockComment) + || isOnField(blockComment) + || isOnConstructor(blockComment) + || isOnEnumConstant(blockComment) + || isOnAnnotationField(blockComment); + } + + /** * Node is on method declaration. * @param blockComment DetailAST * @return true if node is before method @@ -130,6 +156,17 @@ public final class BlockCommentPosition { } /** + * Node is on annotation field declaration. + * @param blockComment DetailAST + * @return true if node is before annotation field + */ + public static boolean isOnAnnotationField(DetailAST blockComment) { + return isOnPlainClassMember(blockComment, TokenTypes.ANNOTATION_FIELD_DEF) + || isOnTokenWithModifiers(blockComment, TokenTypes.ANNOTATION_FIELD_DEF) + || isOnTokenWithAnnotation(blockComment, TokenTypes.ANNOTATION_FIELD_DEF); + } + + /** * Checks that block comment is on specified token without any modifiers. * @param blockComment block comment start DetailAST * @param parentTokenType parent token type @@ -179,11 +216,16 @@ public final class BlockCommentPosition { * @return true if block comment is on specified token without modifiers */ private static boolean isOnPlainClassMember(DetailAST blockComment, int memberType) { - return blockComment.getParent() != null - && blockComment.getParent().getType() == TokenTypes.TYPE - && blockComment.getParent().getParent().getType() == memberType + DetailAST parent = blockComment.getParent(); + // type could be in fully qualified form, so we go up to Type token + while (parent != null && parent.getType() == TokenTypes.DOT) { + parent = parent.getParent(); + } + return parent != null + && parent.getType() == TokenTypes.TYPE + && parent.getParent().getType() == memberType // previous parent sibling is always TokenTypes.MODIFIERS - && blockComment.getParent().getPreviousSibling().getChildCount() == 0; + && parent.getPreviousSibling().getChildCount() == 0; } /** |