|
| 1 | +/////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | +// checkstyle: Checks Java source code and other text files for adherence to a set of rules. |
| 3 | +// Copyright (C) 2001-2026 the original author or authors. |
| 4 | +// |
| 5 | +// This library is free software; you can redistribute it and/or |
| 6 | +// modify it under the terms of the GNU Lesser General Public |
| 7 | +// License as published by the Free Software Foundation; either |
| 8 | +// version 2.1 of the License, or (at your option) any later version. |
| 9 | +// |
| 10 | +// This library is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | +// Lesser General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU Lesser General Public |
| 16 | +// License along with this library; if not, write to the Free Software |
| 17 | +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | +/////////////////////////////////////////////////////////////////////////////////////////////// |
| 19 | + |
| 20 | +package com.puppycrawl.tools.checkstyle.checks.naming; |
| 21 | + |
| 22 | +import java.util.regex.Pattern; |
| 23 | + |
| 24 | +import com.puppycrawl.tools.checkstyle.StatelessCheck; |
| 25 | +import com.puppycrawl.tools.checkstyle.api.AbstractCheck; |
| 26 | +import com.puppycrawl.tools.checkstyle.api.DetailAST; |
| 27 | +import com.puppycrawl.tools.checkstyle.api.TokenTypes; |
| 28 | +import com.puppycrawl.tools.checkstyle.utils.NullUtil; |
| 29 | +import com.puppycrawl.tools.checkstyle.utils.ScopeUtil; |
| 30 | + |
| 31 | +/** |
| 32 | + * <div> |
| 33 | + * Checks that member names conform to the |
| 34 | + * <a href= |
| 35 | + * "https://google.github.io/styleguide/javaguide.html#s5.2.5-non-constant-field-names"> |
| 36 | + * Google Java Style Guide</a> for non-constant field naming. |
| 37 | + * </div> |
| 38 | + * |
| 39 | + * <p> |
| 40 | + * This check enforces Google's specific member naming requirements: |
| 41 | + * </p> |
| 42 | + * <ul> |
| 43 | + * <li>Member names must start with a lowercase letter and use uppercase letters |
| 44 | + * for word boundaries.</li> |
| 45 | + * <li>Underscores may be used to separate adjacent numbers (e.g., version |
| 46 | + * numbers like {@code guava33_4_5}), but NOT between letters and digits.</li> |
| 47 | + * </ul> |
| 48 | + * |
| 49 | + * @since 13.1.0 |
| 50 | + */ |
| 51 | +@StatelessCheck |
| 52 | +public class GoogleMemberNameCheck extends AbstractCheck { |
| 53 | + |
| 54 | + /** |
| 55 | + * A key is pointing to the violation message text in "messages.properties" file. |
| 56 | + */ |
| 57 | + public static final String MSG_KEY_INVALID_FORMAT = "google.member.name.format"; |
| 58 | + |
| 59 | + /** |
| 60 | + * A key pointing to the violation message for invalid underscore usage. |
| 61 | + */ |
| 62 | + public static final String MSG_KEY_INVALID_UNDERSCORE = "google.member.name.underscore"; |
| 63 | + |
| 64 | + /** |
| 65 | + * Pattern for valid member names in Google style. |
| 66 | + * Format: lowerCamelCase, optionally followed by numbering suffix. |
| 67 | + * |
| 68 | + * <p> |
| 69 | + * Explanation: |
| 70 | + * <ul> |
| 71 | + * <li>{@code ^(?![a-z]$)} - Negative lookahead: cannot be single lowercase char</li> |
| 72 | + * <li>{@code (?![a-z][A-Z])} - Negative lookahead: cannot be like "fO"</li> |
| 73 | + * <li>{@code [a-z]} - Must start with lowercase</li> |
| 74 | + * <li>{@code [a-z0-9]*} - Followed by lowercase or digits</li> |
| 75 | + * <li>{@code (?:[A-Z][a-z0-9]*)*} - CamelCase humps (uppercase followed by lowercase)</li> |
| 76 | + * <li>{@code $} - End of string (numbering suffix validated separately)</li> |
| 77 | + * </ul> |
| 78 | + */ |
| 79 | + private static final Pattern MEMBER_NAME_PATTERN = Pattern |
| 80 | + .compile("^(?![a-z]$)(?![a-z][A-Z])[a-z][a-z0-9]*(?:[A-Z][a-z0-9]*)*$"); |
| 81 | + |
| 82 | + /** |
| 83 | + * Pattern to strip trailing numbering suffix (underscore followed by digits). |
| 84 | + */ |
| 85 | + private static final Pattern NUMBERING_SUFFIX_PATTERN = Pattern.compile("(?:_[0-9]+)+$"); |
| 86 | + |
| 87 | + /** |
| 88 | + * Pattern to detect invalid underscore usage: leading, trailing, consecutive, |
| 89 | + * or between letter-letter, letter-digit, or digit-letter combinations. |
| 90 | + */ |
| 91 | + private static final Pattern INVALID_UNDERSCORE_PATTERN = |
| 92 | + Pattern.compile("^_|_$|__|[a-zA-Z]_[a-zA-Z]|[a-zA-Z]_\\d|\\d_[a-zA-Z]"); |
| 93 | + |
| 94 | + @Override |
| 95 | + public int[] getDefaultTokens() { |
| 96 | + return getRequiredTokens(); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public int[] getAcceptableTokens() { |
| 101 | + return getRequiredTokens(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public int[] getRequiredTokens() { |
| 106 | + return new int[] {TokenTypes.VARIABLE_DEF}; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void visitToken(DetailAST ast) { |
| 111 | + if (mustCheckName(ast)) { |
| 112 | + final DetailAST nameAst = NullUtil.notNull(ast.findFirstToken(TokenTypes.IDENT)); |
| 113 | + final String memberName = nameAst.getText(); |
| 114 | + |
| 115 | + validateMemberName(nameAst, memberName); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Checks if this field should be validated. Returns true for instance fields |
| 121 | + * and static non-final fields. Constants (static final), local variables, |
| 122 | + * and interface/annotation fields are excluded. |
| 123 | + * |
| 124 | + * @param ast the VARIABLE_DEF AST node |
| 125 | + * @return true if this variable should be checked |
| 126 | + */ |
| 127 | + private static boolean mustCheckName(DetailAST ast) { |
| 128 | + final DetailAST modifiersAST = NullUtil.notNull(ast.findFirstToken(TokenTypes.MODIFIERS)); |
| 129 | + final boolean isStatic = |
| 130 | + modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null; |
| 131 | + final boolean isFinal = |
| 132 | + modifiersAST.findFirstToken(TokenTypes.FINAL) != null; |
| 133 | + |
| 134 | + final boolean isConstant = isStatic && isFinal; |
| 135 | + |
| 136 | + return !isConstant |
| 137 | + && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast) |
| 138 | + && !ScopeUtil.isLocalVariableDef(ast); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Validates a member name according to Google style. |
| 143 | + * |
| 144 | + * @param nameAst the IDENT AST node containing the member name |
| 145 | + * @param memberName the member name string |
| 146 | + */ |
| 147 | + private void validateMemberName(DetailAST nameAst, String memberName) { |
| 148 | + if (INVALID_UNDERSCORE_PATTERN.matcher(memberName).find()) { |
| 149 | + log(nameAst, MSG_KEY_INVALID_UNDERSCORE, memberName); |
| 150 | + } |
| 151 | + else { |
| 152 | + final String nameWithoutNumberingSuffix = NUMBERING_SUFFIX_PATTERN |
| 153 | + .matcher(memberName).replaceAll(""); |
| 154 | + if (!MEMBER_NAME_PATTERN.matcher(nameWithoutNumberingSuffix).matches()) { |
| 155 | + log(nameAst, MSG_KEY_INVALID_FORMAT, memberName); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments