Coverage Report - com.puppycrawl.tools.checkstyle.checks.imports.AvoidStaticImportCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
AvoidStaticImportCheck
100%
20/20
85%
12/14
3.25
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2014  Oliver Burn
 4  
 //
 5  
 // This library is free software; you can redistribute it and/or
 6  
 // modify it under the terms of the GNU Lesser General Public
 7  
 // License as published by the Free Software Foundation; either
 8  
 // version 2.1 of the License, or (at your option) any later version.
 9  
 //
 10  
 // This library is distributed in the hope that it will be useful,
 11  
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 // Lesser General Public License for more details.
 14  
 //
 15  
 // You should have received a copy of the GNU Lesser General Public
 16  
 // License along with this library; if not, write to the Free Software
 17  
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  
 ////////////////////////////////////////////////////////////////////////////////
 19  
 package com.puppycrawl.tools.checkstyle.checks.imports;
 20  
 
 21  
 import com.puppycrawl.tools.checkstyle.api.Check;
 22  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 23  
 import com.puppycrawl.tools.checkstyle.api.FullIdent;
 24  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 25  
 
 26  
 /**
 27  
  * <p>
 28  
  * Check that finds static imports.
 29  
  * </p>
 30  
  * <p>
 31  
  * Rationale: Importing static members can lead to naming conflicts
 32  
  * between class' members. It may lead to poor code readability since it
 33  
  * may no longer be clear what class a member resides (without looking
 34  
  * at the import statement).
 35  
  * </p>
 36  
  * <p>
 37  
  * An example of how to configure the check is:
 38  
  * </p>
 39  
  * <pre>
 40  
  * &lt;module name="AvoidStaticImport"&gt;
 41  
  *   &lt;property name="excludes"
 42  
  *       value="java.lang.System.out,java.lang.Math.*"/&gt;
 43  
  * &lt;/module&gt;
 44  
  * </pre>
 45  
  *
 46  
  * The optional "excludes" property allows for certain classes via a star
 47  
  * notation to be excluded such as java.lang.Math.* or specific
 48  
  * static members to be excluded like java.lang.System.out for a variable
 49  
  * or java.lang.Math.random for a method.
 50  
  *
 51  
  * <p>
 52  
  * If you exclude a starred import on a class this automatically
 53  
  * excludes each member individually.
 54  
  * </p>
 55  
  *
 56  
  * <p>
 57  
  * For example:
 58  
  * Excluding java.lang.Math.* will allow the import of
 59  
  * each static member in the Math class individually like
 60  
  * java.lang.Math.PI
 61  
  * </p>
 62  
  * @author Travis Schneeberger
 63  
  * @version 1.0
 64  
  */
 65  4
 public class AvoidStaticImportCheck
 66  
     extends Check
 67  
 {
 68  
     /** the classes/static members to exempt from this check. */
 69  4
     private String[] mExcludes = new String[0];
 70  
 
 71  
     @Override
 72  
     public int[] getDefaultTokens()
 73  
     {
 74  4
         return new int[] {TokenTypes.STATIC_IMPORT};
 75  
     }
 76  
 
 77  
     /**
 78  
      * Sets the list of classes or static members to be exempt from the check.
 79  
      * @param aExcludes a list of fully-qualified class names/specific
 80  
      * static members where static imports are ok
 81  
      */
 82  
     public void setExcludes(String[] aExcludes)
 83  
     {
 84  3
         mExcludes = aExcludes.clone();
 85  3
     }
 86  
 
 87  
     @Override
 88  
     public void visitToken(final DetailAST aAST)
 89  
     {
 90  20
         final DetailAST startingDot =
 91  
             aAST.getFirstChild().getNextSibling();
 92  20
         final FullIdent name = FullIdent.createFullIdent(startingDot);
 93  
 
 94  20
         if ((null != name) && !isExempt(name.getText())) {
 95  16
             log(startingDot.getLineNo(), "import.avoidStatic", name.getText());
 96  
         }
 97  20
     }
 98  
 
 99  
     /**
 100  
      * Checks if a class or static member is exempt from known excludes.
 101  
      *
 102  
      * @param aClassOrStaticMember
 103  
      *                the class or static member
 104  
      * @return true if except false if not
 105  
      */
 106  
     private boolean isExempt(String aClassOrStaticMember)
 107  
     {
 108  54
         for (String exclude : mExcludes) {
 109  38
             if (aClassOrStaticMember.equals(exclude)) {
 110  2
                 return true;
 111  
             }
 112  36
             else if (exclude.endsWith(".*")) {
 113  
                 //this section allows explicit imports
 114  
                 //to be exempt when configured using
 115  
                 //a starred import
 116  7
                 final String excludeMinusDotStar =
 117  
                     exclude.substring(0, exclude.length() - 2);
 118  7
                 if (aClassOrStaticMember.startsWith(excludeMinusDotStar)) {
 119  2
                     final String member =
 120  
                         aClassOrStaticMember.substring(
 121  
                             excludeMinusDotStar.length() + 1);
 122  
                     //if it contains a dot then it is not a member but a package
 123  2
                     if (member.indexOf('.') == -1) {
 124  2
                         return true;
 125  
                     }
 126  
                 }
 127  
             }
 128  
         }
 129  16
         return false;
 130  
     }
 131  
 }