#587 ConstantName ignores applyToXYZ

release_5.0
closed
nobody
None
5
2014-10-22
2009-07-23
Ziad Mansouri
No

Create a check with these two rules:

<module name="ConstantName">
  <metadata name="net.sf.eclipsecs.core.comment" value="non-public constant names should start with an underscore"/>
  <property name="applyToPublic" value="false"/>
  <property name="format" value="^_[a-zA-Z][a-zA-Z0-9]*(_[A-Z0-9]+)*$"/>
</module>
<module name="ConstantName">
  <metadata name="net.sf.eclipsecs.core.comment" value="public constant names should not start with an underscore"/>
  <property name="applyToProtected" value="false"/>
  <property name="applyToPackage" value="false"/>
  <property name="applyToPrivate" value="false"/>
  <property name="format" value="^[a-zA-Z][a-zA-Z0-9]*(_[a-zA-Z0-9]+)*$"/>
</module>

Use this to test:

public class CsTest
{
private static final int _privateStaticFinal = 7;
public static final int publicStaticFinal = 7;
}

Both _privateStaticFinal and publicStaticFinal are flagged with warnings.

Discussion

  • Ziad Mansouri
    Ziad Mansouri
    2009-07-23

    Same is true for StaticVariableName. Add these:

    <module name="StaticVariableName">
      <metadata name="net.sf.eclipsecs.core.comment" value="non-public static variable names should start with an underscore"/>
      <property name="applyToPublic" value="false"/>
      <property name="format" value="^_[a-z][a-zA-Z0-9]*$"/>
    </module>
    <module name="StaticVariableName">
      <metadata name="net.sf.eclipsecs.core.comment" value="public static variable names should not start with an underscore"/>
      <property name="applyToProtected" value="false"/>
      <property name="applyToPackage" value="false"/>
      <property name="applyToPrivate" value="false"/>
    </module>
    

    and use these to test:

    private static int _privateStatic;
    private static int publicStatic;

     
  • Oliver Burn
    Oliver Burn
    2009-07-30

    Can you supply a full working example.

    I suspect your problem is that you have TWO <module name="ConstantName"> definitions. This means that either one can fire.

     
  • Ziad Mansouri
    Ziad Mansouri
    2009-07-31

    eclipse 3.5 project with checkstyle.xml and CsTest.java

     
    Attachments
  • Ziad Mansouri
    Ziad Mansouri
    2009-07-31

    SF is playing tricks on me. There was a comment from oburn asking for an update, to which I replied twice, all of which (comment and replies) are now gone.

    I've attached cs-test.zip. It's a complete Eclipse 3.5 with the lates Checkstyle plugin, a sample checkstyle.xml and a test class, CsTest.java. The only thing to note is that I added two MethodName checks despite the fact that MethodName doesn't currently support applyToXYZ(). Hopefully it will soon.

    Cheers,

    Z.

     
  • Ziad Mansouri
    Ziad Mansouri
    2009-07-31

    And as an answer to your suspicion, yes I do have two ConstantName checks, one for public constant names and one for non-public constant names. Isn't that how it should be?

    Cheers,

    Z.

     
  • Roman Ivanov
    Roman Ivanov
    2013-11-12

    • status: open --> closed