001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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.api.AbstractCheck;
023import com.puppycrawl.tools.checkstyle.api.DetailAST;
024import com.puppycrawl.tools.checkstyle.api.TokenTypes;
025import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
026
027/**
028 * <p>Checks that long constants are defined with an upper ell.
029 * That is <span class="code">'L'</span> and not
030 * <span class="code">'l'</span>. This is in accordance to the Java Language
031 * Specification, <a href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.1">
032 * Section 3.10.1</a>.
033 * </p>
034 * <p>
035 * Rationale: The letter <span class="code">l</span> looks a lot
036 * like the number <span class="code">1</span>.
037 * </p>
038 *
039 * <p>Examples
040 * <p class="body">
041 * To configure the check:
042 *
043 * </p>
044 * <pre class="body">
045 * &lt;module name=&quot;UpperEll&quot;/&gt;
046 * </pre>
047 *
048 * @author Oliver Burn
049 */
050public class UpperEllCheck extends AbstractCheck {
051
052    /**
053     * A key is pointing to the warning message text in "messages.properties"
054     * file.
055     */
056    public static final String MSG_KEY = "upperEll";
057
058    @Override
059    public int[] getDefaultTokens() {
060        return getAcceptableTokens();
061    }
062
063    @Override
064    public int[] getAcceptableTokens() {
065        return new int[] {TokenTypes.NUM_LONG};
066    }
067
068    @Override
069    public int[] getRequiredTokens() {
070        return getAcceptableTokens();
071    }
072
073    @Override
074    public void visitToken(DetailAST ast) {
075        if (CommonUtils.endsWithChar(ast.getText(), 'l')) {
076            log(ast.getLineNo(),
077                ast.getColumnNo() + ast.getText().length() - 1,
078                MSG_KEY);
079        }
080    }
081}