Skip to content

new Check: NumericalPrefixesInfixesSuffixesCharacterCaseCheck numerical prefixes, infixes, and suffixes should be lowercase, except for "L" #16678

@mohitsatr

Description

@mohitsatr

I have executed the cli and showed it below, as cli describes the problem better than 1,000 words

According to openjdk's updated java-style guidelines on literals:

  • long literals should use the upper case letter L suffix.
  • All other (other than long and hexadecimal) numerical prefixes, infixes, and suffixes should use lowercase letters.
    Motivation
    Lower case L resembles a 1 in many monospace fonts which means that the literal 5432l can be confused with 54321. Using upper case L suffix avoids this. The lowercase 0x, 0b, e, p, f, and d characters are easier to spot in a long sequence of digits, and they avoid confusion with the hexadecimal digits A-F.

Image

numerical prefixes: "0x", "0b"
numerical suffixes: f, and d
numerical infixes: e, p

$ cat sun/Literals.java

package sun;
/* somejavadoc. */
public class Literals {

    /** some javadoc.
     *
     * @param args param.
     */
    public static void main(final String[] args) {
            long l = 5432L;
            int i = 0x123 + 0xABC;
            byte b = 0b1010;
            float f1 = 1 / 5432f;
            float f2 = 0.123e4f;
            double d1 = 1 / 5432d;  // or 1 / 5432.0
            double d2 = 0x1.3p2;

            long l = 5432l;        // violation from UpperEll Check
            int i = 0X123 + 0xabc; // violation 'Literal '0X123' should have lowercase characters in prefix'
            byte b = 0B1010;       // violation from HexLiteralCase Check
            float f1 = 1 / 5432F;  // violation 'Literal '5432F' should have lowercase characters in suffix'
            float f2 = 0.123E4f;   // violation, "E" is not ok, 'Literal '0.123E4f' should have lowercase characters in infix'
            double d1 = 1 / 5432D; // violation 'Literal '5432D' should have lowercase characters in suffix'
            double d2 = 0x1.3P2;   // violation 'Literal ' 0x1.3P2' should have lowercase characters in infix'
    }
}


$ java -jar testing/checkstyle-10.21.4-all.jar -c sun/sun_checks.xml sun/Literals.java
Starting audit...
Audit done.

Is your feature request related to a problem? Please describe.
currently, two rules are not being followed. we should have had violations in all the cases.
based on discussion,
we need to create new checks for this. they will be similar to UpperEllCheck which checks if long datatype's Literals have uppercase L suffix.


<module name="Checker">
  <module name="TreeWalker">
    <module name="NumericalPrefixesInfixesSuffixesCharacterCaseCheck"/> 
  </module> 
</module>

  • Hexadecimal literals should use upper case letters A-F.

will be covered by HexLiteralCase - #16836

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions