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;
21
22 import com.puppycrawl.tools.checkstyle.StatelessCheck;
23 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26
27 /**
28 * <div> Checks that hexadecimal literals are defined using uppercase letters {@code (A-F)}
29 * rather than lowercase {@code (a-f)}.
30 * This convention follows the
31 * <a href="https://cr.openjdk.org/~alundblad/styleguide/index-v6.html#toc-literals">
32 * OpenJDK Style Guide</a>.
33 * </div>
34 *
35 * @since 12.1.0
36 */
37 @StatelessCheck
38 public class HexLiteralCaseCheck extends AbstractCheck {
39
40 /**
41 * A key is pointing to the warning message text in "messages.properties"
42 * file.
43 */
44 public static final String MSG_KEY = "hex.literal";
45
46 /** ASCII value for lowercase 'a'. */
47 private static final int A_LOWER_ASCII = 97;
48
49 /** ASCII value for lowercase 'f'. */
50 private static final int F_LOWER_ASCII = 102;
51
52 @Override
53 public int[] getDefaultTokens() {
54 return getRequiredTokens();
55 }
56
57 @Override
58 public int[] getAcceptableTokens() {
59 return getRequiredTokens();
60 }
61
62 @Override
63 public int[] getRequiredTokens() {
64 return new int[] {TokenTypes.NUM_LONG, TokenTypes.NUM_INT, TokenTypes.NUM_FLOAT,
65 TokenTypes.NUM_DOUBLE,
66 };
67 }
68
69 @Override
70 public void visitToken(DetailAST ast) {
71 final String text = ast.getText();
72 if (text.startsWith("0x") || text.startsWith("0X")) {
73 final int type = ast.getType();
74 if (type == TokenTypes.NUM_FLOAT || type == TokenTypes.NUM_DOUBLE) {
75 if (containsLowerLetterInHexFloatLiteral(text)) {
76 log(ast, MSG_KEY);
77 }
78 }
79 else {
80 if (containsLowerLetter(text)) {
81 log(ast, MSG_KEY);
82 }
83 }
84 }
85 }
86
87 /**
88 * Checks if the given text contains any lowercase hexadecimal letter (a–f).
89 *
90 * @param text the literal text to check
91 * @return true if the literal contains lowercase hex digits; false otherwise
92 */
93 private static boolean containsLowerLetter(final String text) {
94 boolean result = false;
95 final char[] characterList = text.toCharArray();
96 for (char character : characterList) {
97 if (character >= A_LOWER_ASCII && character <= F_LOWER_ASCII) {
98 result = true;
99 break;
100 }
101 }
102 return result;
103 }
104
105 /**
106 * Checks if the given text contains any lowercase hexadecimal letter in
107 * Hex Float Literal (a–f).
108 *
109 * @param text the literal text to check
110 * @return true if the literal contains lowercase hex digits; false otherwise
111 */
112 public static boolean containsLowerLetterInHexFloatLiteral(final String text) {
113 boolean result = false;
114
115 final int index = text.length() - 1;
116 for (char character : text.substring(0, index).toCharArray()) {
117 if (character >= A_LOWER_ASCII && character <= F_LOWER_ASCII) {
118 result = true;
119 break;
120 }
121 }
122
123 return result;
124 }
125 }