001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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.checks;
021
022import com.puppycrawl.tools.checkstyle.StatelessCheck;
023import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
024import com.puppycrawl.tools.checkstyle.api.DetailAST;
025import com.puppycrawl.tools.checkstyle.api.TokenTypes;
026
027/**
028 * <div> Checks that hexadecimal literals are defined using uppercase letters {@code (A-F)}
029 * rather than lowercase {@code (a-f)}.
030 * This convention follows the
031 * <a href="https://cr.openjdk.org/~alundblad/styleguide/index-v6.html#toc-literals">
032 * OpenJDK Style Guide</a>.
033 * </div>
034 *
035 * @since 12.1.0
036 */
037@StatelessCheck
038public class HexLiteralCaseCheck extends AbstractCheck {
039
040    /**
041     * A key is pointing to the warning message text in "messages.properties" file.
042     */
043    public static final String MSG_KEY = "hex.literal";
044
045    /** ASCII value for lowercase 'a'. */
046    private static final int A_LOWER_ASCII = 97;
047
048    /** ASCII value for lowercase 'f'. */
049    private static final int F_LOWER_ASCII = 102;
050
051    /**
052     * Creates a new {@code HexLiteralCaseCheck} instance.
053     */
054    public HexLiteralCaseCheck() {
055        // no code by default
056    }
057
058    @Override
059    public int[] getDefaultTokens() {
060        return getRequiredTokens();
061    }
062
063    @Override
064    public int[] getAcceptableTokens() {
065        return getRequiredTokens();
066    }
067
068    @Override
069    public int[] getRequiredTokens() {
070        return new int[] {TokenTypes.NUM_LONG, TokenTypes.NUM_INT, TokenTypes.NUM_FLOAT,
071            TokenTypes.NUM_DOUBLE,
072        };
073    }
074
075    @Override
076    public void visitToken(DetailAST ast) {
077        final String text = ast.getText();
078        if (text.startsWith("0x") || text.startsWith("0X")) {
079            final int type = ast.getType();
080            if (type == TokenTypes.NUM_FLOAT || type == TokenTypes.NUM_DOUBLE) {
081                if (containsLowerLetterInHexFloatLiteral(text)) {
082                    log(ast, MSG_KEY);
083                }
084            }
085            else {
086                if (containsLowerLetter(text)) {
087                    log(ast, MSG_KEY);
088                }
089            }
090        }
091    }
092
093    /**
094     * Checks if the given text contains any lowercase hexadecimal letter (a–f).
095     *
096     * @param text the literal text to check
097     * @return true if the literal contains lowercase hex digits; false otherwise
098     */
099    private static boolean containsLowerLetter(final String text) {
100        boolean result = false;
101        final char[] characterList = text.toCharArray();
102        for (char character : characterList) {
103            if (character >= A_LOWER_ASCII && character <= F_LOWER_ASCII) {
104                result = true;
105                break;
106            }
107        }
108        return result;
109    }
110
111    /**
112     * Checks if the given text contains any lowercase hexadecimal letter in
113     * Hex Float Literal (a–f).
114     *
115     * @param text the literal text to check
116     * @return true if the literal contains lowercase hex digits; false otherwise
117     */
118    public static boolean containsLowerLetterInHexFloatLiteral(final String text) {
119        boolean result = false;
120
121        final int index = text.length() - 1;
122        for (char character : text.substring(0, index).toCharArray()) {
123            if (character >= A_LOWER_ASCII && character <= F_LOWER_ASCII) {
124                result = true;
125                break;
126            }
127        }
128
129        return result;
130    }
131
132}