#185 Check integer constants: octal/hexadecimal

release_3.2
closed
Rick Giles
Check (274)
5
2012-10-10
2003-06-07
Tim Tyler
No

Octal:

Leading zeros convert integers into octal in Java.

This is more likely to be an error than by design -
an error Checkstyle could be used to avoid.

Hex:

Java accepts hex in either case. However lower case
is less legible - and is best avoided. Checkstyle could
be used to enforce the use of upper case.

Discussion

  • Dale King
    Dale King
    2003-06-09

    Logged In: YES
    user_id=130378

    On the hexadecimal, I agree because as I see it numerals are
    essentially "upper case" since they are large like capital
    letters, but I prefer to have the x in lower case to make the
    separation more visible. The capitalization for the x should be
    configurable separately from the captialization of the digits.

     
  • Tim Tyler
    Tim Tyler
    2003-06-12

    Logged In: YES
    user_id=796025

    I've implemented these checks - as follows:

        <module name="HexLiteralCaseCheck"/>
        <module name="OctalLiteralCheck"/>
    

    The hex case check ignores the case of any Xs (and Ls) it
    finds.

    I /could/ make the case for the "X" and the "digits"
    configurable - but it hardly seems worth it at this
    stage.

    Hopefully, I'll get a few checks together - and then submit
    them.

     
  • Tim Tyler
    Tim Tyler
    2003-06-12

    Logged In: YES
    user_id=796025

    I've implemented these checks - as follows:

        <module name="HexLiteralCaseCheck"/>
        <module name="OctalLiteralCheck"/>
    

    The hex case check ignores the case of any Xs (and Ls) it
    finds.

    I /could/ make the case for the "X" and the "digits"
    configurable - but it hardly seems worth it at this
    stage.

    Hopefully, I'll get a few checks together - and then submit
    them.

     
  • Tim Tyler
    Tim Tyler
    2003-06-26

    Logged In: YES
    user_id=796025

    This checks should probably eventually be replaced by
    something like a "TextOfTokenMatchesRegexpCheck".

    I reckon that could deal with any case, hex, octal, "E"
    exponents, "F" for float, "D" for double, "L" for long - and
    possibly some other things I haven't thought of yet.

     
  • Rick Giles
    Rick Giles
    2003-07-26

    Logged In: YES
    user_id=539926

    IllegalTokenTextCheck of the 3.2 CVS can be configured to
    perform this check.

     
  • Rick Giles
    Rick Giles
    2003-08-02

    Logged In: YES
    user_id=539926

    Both of these configurations check for leading zeros in
    integer literals, other than zero and a hex literal:

    <module name="IllegalTokenText">
    <property name="tokens" value="NUM_INT,NUM_LONG"/>
    <property name="format" value="^0[^lx]"/>
    <property name="ignoreCase" value="true"/>
    </module>

    <module name="IllegalTokenText">
    <property name="tokens" value="NUM_INT,NUM_LONG"/>
    <property name="format" value="^0[^lLxX]"/>
    </module>