001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2019 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.utils; 021 022import com.puppycrawl.tools.checkstyle.api.DetailAST; 023import com.puppycrawl.tools.checkstyle.api.TokenTypes; 024 025/** 026 * Utility class that has methods to check javadoc comment position in java file. 027 * 028 */ 029public final class BlockCommentPosition { 030 031 /** 032 * Forbid new instances. 033 */ 034 private BlockCommentPosition() { 035 } 036 037 /** 038 * Node is on type definition. 039 * @param blockComment DetailAST 040 * @return true if node is before class, interface, enum or annotation. 041 */ 042 public static boolean isOnType(DetailAST blockComment) { 043 return isOnClass(blockComment) 044 || isOnInterface(blockComment) 045 || isOnEnum(blockComment) 046 || isOnAnnotationDef(blockComment); 047 } 048 049 /** 050 * Node is on class definition. 051 * @param blockComment DetailAST 052 * @return true if node is before class 053 */ 054 public static boolean isOnClass(DetailAST blockComment) { 055 return isOnPlainToken(blockComment, TokenTypes.CLASS_DEF, TokenTypes.LITERAL_CLASS) 056 || isOnTokenWithModifiers(blockComment, TokenTypes.CLASS_DEF) 057 || isOnTokenWithAnnotation(blockComment, TokenTypes.CLASS_DEF); 058 } 059 060 /** 061 * Node is on package definition. 062 * @param blockComment DetailAST 063 * @return true if node is before package 064 */ 065 public static boolean isOnPackage(DetailAST blockComment) { 066 final DetailAST nextSibling = blockComment.getNextSibling(); 067 return isOnTokenWithAnnotation(blockComment, TokenTypes.PACKAGE_DEF) 068 || nextSibling != null && nextSibling.getType() == TokenTypes.PACKAGE_DEF; 069 } 070 071 /** 072 * Node is on interface definition. 073 * @param blockComment DetailAST 074 * @return true if node is before interface 075 */ 076 public static boolean isOnInterface(DetailAST blockComment) { 077 return isOnPlainToken(blockComment, TokenTypes.INTERFACE_DEF, TokenTypes.LITERAL_INTERFACE) 078 || isOnTokenWithModifiers(blockComment, TokenTypes.INTERFACE_DEF) 079 || isOnTokenWithAnnotation(blockComment, TokenTypes.INTERFACE_DEF); 080 } 081 082 /** 083 * Node is on enum definition. 084 * @param blockComment DetailAST 085 * @return true if node is before enum 086 */ 087 public static boolean isOnEnum(DetailAST blockComment) { 088 return isOnPlainToken(blockComment, TokenTypes.ENUM_DEF, TokenTypes.ENUM) 089 || isOnTokenWithModifiers(blockComment, TokenTypes.ENUM_DEF) 090 || isOnTokenWithAnnotation(blockComment, TokenTypes.ENUM_DEF); 091 } 092 093 /** 094 * Node is on annotation definition. 095 * @param blockComment DetailAST 096 * @return true if node is before annotation 097 */ 098 public static boolean isOnAnnotationDef(DetailAST blockComment) { 099 return isOnPlainToken(blockComment, TokenTypes.ANNOTATION_DEF, TokenTypes.AT) 100 || isOnTokenWithModifiers(blockComment, TokenTypes.ANNOTATION_DEF) 101 || isOnTokenWithAnnotation(blockComment, TokenTypes.ANNOTATION_DEF); 102 } 103 104 /** 105 * Node is on type member declaration. 106 * @param blockComment DetailAST 107 * @return true if node is before method, field, constructor, enum constant 108 * or annotation field 109 */ 110 public static boolean isOnMember(DetailAST blockComment) { 111 return isOnMethod(blockComment) 112 || isOnField(blockComment) 113 || isOnConstructor(blockComment) 114 || isOnEnumConstant(blockComment) 115 || isOnAnnotationField(blockComment); 116 } 117 118 /** 119 * Node is on method declaration. 120 * @param blockComment DetailAST 121 * @return true if node is before method 122 */ 123 public static boolean isOnMethod(DetailAST blockComment) { 124 return isOnPlainClassMember(blockComment, TokenTypes.METHOD_DEF) 125 || isOnTokenWithModifiers(blockComment, TokenTypes.METHOD_DEF) 126 || isOnTokenWithAnnotation(blockComment, TokenTypes.METHOD_DEF); 127 } 128 129 /** 130 * Node is on field declaration. 131 * @param blockComment DetailAST 132 * @return true if node is before field 133 */ 134 public static boolean isOnField(DetailAST blockComment) { 135 return isOnPlainClassMember(blockComment, TokenTypes.VARIABLE_DEF) 136 || isOnTokenWithModifiers(blockComment, TokenTypes.VARIABLE_DEF) 137 && blockComment.getParent().getParent().getParent() 138 .getType() == TokenTypes.OBJBLOCK 139 || isOnTokenWithAnnotation(blockComment, TokenTypes.VARIABLE_DEF) 140 && blockComment.getParent().getParent().getParent() 141 .getParent().getType() == TokenTypes.OBJBLOCK; 142 } 143 144 /** 145 * Node is on constructor. 146 * @param blockComment DetailAST 147 * @return true if node is before constructor 148 */ 149 public static boolean isOnConstructor(DetailAST blockComment) { 150 return isOnPlainToken(blockComment, TokenTypes.CTOR_DEF, TokenTypes.IDENT) 151 || isOnTokenWithModifiers(blockComment, TokenTypes.CTOR_DEF) 152 || isOnTokenWithAnnotation(blockComment, TokenTypes.CTOR_DEF); 153 } 154 155 /** 156 * Node is on enum constant. 157 * @param blockComment DetailAST 158 * @return true if node is before enum constant 159 */ 160 public static boolean isOnEnumConstant(DetailAST blockComment) { 161 final boolean isOnPlainConst = blockComment.getParent() != null 162 && blockComment.getParent().getType() == TokenTypes.ENUM_CONSTANT_DEF 163 && getPrevSiblingSkipComments(blockComment).getType() == TokenTypes.ANNOTATIONS 164 && getPrevSiblingSkipComments(blockComment).getChildCount() == 0; 165 final boolean isOnConstWithAnnotation = !isOnPlainConst && blockComment.getParent() != null 166 && blockComment.getParent().getType() == TokenTypes.ANNOTATION 167 && blockComment.getParent().getParent().getParent().getType() 168 == TokenTypes.ENUM_CONSTANT_DEF; 169 return isOnPlainConst || isOnConstWithAnnotation; 170 } 171 172 /** 173 * Node is on annotation field declaration. 174 * @param blockComment DetailAST 175 * @return true if node is before annotation field 176 */ 177 public static boolean isOnAnnotationField(DetailAST blockComment) { 178 return isOnPlainClassMember(blockComment, TokenTypes.ANNOTATION_FIELD_DEF) 179 || isOnTokenWithModifiers(blockComment, TokenTypes.ANNOTATION_FIELD_DEF) 180 || isOnTokenWithAnnotation(blockComment, TokenTypes.ANNOTATION_FIELD_DEF); 181 } 182 183 /** 184 * Checks that block comment is on specified token without any modifiers. 185 * @param blockComment block comment start DetailAST 186 * @param parentTokenType parent token type 187 * @param nextTokenType next token type 188 * @return true if block comment is on specified token without modifiers 189 */ 190 private static boolean isOnPlainToken(DetailAST blockComment, 191 int parentTokenType, int nextTokenType) { 192 return blockComment.getParent() != null 193 && blockComment.getParent().getType() == parentTokenType 194 && getPrevSiblingSkipComments(blockComment).getChildCount() == 0 195 && getNextSiblingSkipComments(blockComment).getType() == nextTokenType; 196 } 197 198 /** 199 * Checks that block comment is on specified token with modifiers. 200 * @param blockComment block comment start DetailAST 201 * @param tokenType parent token type 202 * @return true if block comment is on specified token with modifiers 203 */ 204 private static boolean isOnTokenWithModifiers(DetailAST blockComment, int tokenType) { 205 return blockComment.getParent() != null 206 && blockComment.getParent().getType() == TokenTypes.MODIFIERS 207 && blockComment.getParent().getParent().getType() == tokenType 208 && getPrevSiblingSkipComments(blockComment) == null; 209 } 210 211 /** 212 * Checks that block comment is on specified token with annotation. 213 * @param blockComment block comment start DetailAST 214 * @param tokenType parent token type 215 * @return true if block comment is on specified token with annotation 216 */ 217 private static boolean isOnTokenWithAnnotation(DetailAST blockComment, int tokenType) { 218 return blockComment.getParent() != null 219 && blockComment.getParent().getType() == TokenTypes.ANNOTATION 220 && getPrevSiblingSkipComments(blockComment.getParent()) == null 221 && blockComment.getParent().getParent().getParent().getType() == tokenType 222 && getPrevSiblingSkipComments(blockComment) == null; 223 } 224 225 /** 226 * Checks that block comment is on specified class member without any modifiers. 227 * @param blockComment block comment start DetailAST 228 * @param memberType parent token type 229 * @return true if block comment is on specified token without modifiers 230 */ 231 private static boolean isOnPlainClassMember(DetailAST blockComment, int memberType) { 232 DetailAST parent = blockComment.getParent(); 233 // type could be in fully qualified form, so we go up to Type token 234 while (parent != null && (parent.getType() == TokenTypes.DOT 235 || parent.getType() == TokenTypes.ARRAY_DECLARATOR)) { 236 parent = parent.getParent(); 237 } 238 return parent != null 239 && (parent.getType() == TokenTypes.TYPE 240 || parent.getType() == TokenTypes.TYPE_PARAMETERS) 241 && parent.getParent().getType() == memberType 242 // previous parent sibling is always TokenTypes.MODIFIERS 243 && parent.getPreviousSibling().getChildCount() == 0 244 && parent.getParent().getParent().getType() == TokenTypes.OBJBLOCK; 245 } 246 247 /** 248 * Get next sibling node skipping any comment nodes. 249 * @param node current node 250 * @return next sibling 251 */ 252 private static DetailAST getNextSiblingSkipComments(DetailAST node) { 253 DetailAST result = node.getNextSibling(); 254 while (result.getType() == TokenTypes.SINGLE_LINE_COMMENT 255 || result.getType() == TokenTypes.BLOCK_COMMENT_BEGIN) { 256 result = result.getNextSibling(); 257 } 258 return result; 259 } 260 261 /** 262 * Get previous sibling node skipping any comments. 263 * @param node current node 264 * @return previous sibling 265 */ 266 private static DetailAST getPrevSiblingSkipComments(DetailAST node) { 267 DetailAST result = node.getPreviousSibling(); 268 while (result != null 269 && (result.getType() == TokenTypes.SINGLE_LINE_COMMENT 270 || result.getType() == TokenTypes.BLOCK_COMMENT_BEGIN)) { 271 result = result.getPreviousSibling(); 272 } 273 return result; 274 } 275 276}