#479 ImportControl flags pkg.Outer.Inner with exact-match

_release_4.3
open
nobody
None
5
2014-01-03
2007-05-01
No

The ImportControl(http://checkstyle.sourceforge.net/config_imports.html#ImportControl) test flags an import of an inner class like pkg.Outer.Inner as invalid when it is given

  <allow pkg="pkg" exact-match="true"/>

I think it should allow the import.

Discussion

  • Logged In: YES
    user_id=1534936
    Originator: YES

    I think the following patch would fix the problem:

    Index: src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/Guard.java

    RCS file: /cvsroot/checkstyle/checkstyle/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/Guard.java,v
    retrieving revision 1.4
    diff -u -r1.4 Guard.java
    --- src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/Guard.java 27 Jan 2007 14:51:19 -0000 1.4
    +++ src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/Guard.java 1 May 2007 13:06:55 -0000
    @@ -93,12 +93,32 @@
    //boolean pkgMatch = aForImport.startsWith(mPkgName + ".");
    boolean pkgMatch = aForImport.startsWith(mPkgName + ".");
    if (pkgMatch && mExactMatch) {
    - pkgMatch = (aForImport.indexOf('.', (mPkgName.length() + 1)) == -1);
    + pkgMatch = isExact(aForImport);
    }
    return calculateResult(pkgMatch);
    }

     /**
    
      • Verifies whether an import satisfies an exact match. Uses the simple heuristic
      • that any import starting with neither * nor a non-capital letter is a
      • subpackage and thus forbidden. If you use classes with lower-case names
      • or package names with an initial capital, then this will not work.
      • @param aForImport the import to check.
      • @return true if is a match, false otherwise.
    • */
    • private boolean isExact(final String aForImport)
    • {
    • final int pkgLen = mPkgName.length();
    • if (aForImport.length() >= pkgLen + 2) {
    • if (aForImport.charAt(pkgLen) == '.') {
    • final char next = aForImport.charAt(pkgLen + 1);
    • return next == '*' || Character.isUpperCase(next);
    • }
    • }
    • return false;
    • }
      +
    • /**
      • @return returns whether the guard is to only be applied locally.
        */
        boolean isLocalOnly()
     
  • Roman Ivanov
    Roman Ivanov
    2014-01-03

    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,5 +1,7 @@
     The ImportControl test flags an import of an inner class like pkg.Outer.Inner as invalid when it is given
    
    +~~~~~~
       <allow pkg="pkg" exact-match="true"/>
    +~~~~~~
    
     I think it should allow the import.
    
     
  • Roman Ivanov
    Roman Ivanov
    2014-01-03

    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,4 +1,4 @@
    -The ImportControl test flags an import of an inner class like pkg.Outer.Inner as invalid when it is given
    +The ImportControl(http://checkstyle.sourceforge.net/config_imports.html#ImportControl) test flags an import of an inner class like pkg.Outer.Inner as invalid when it is given
    

    <allow pkg="pkg" exact-match="true"/>