Coverage Report - com.puppycrawl.tools.checkstyle.checks.design.FinalClassCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
FinalClassCheck
100%
23/23
80%
21/26
2.091
FinalClassCheck$ClassDesc
92%
12/13
N/A
2.091
 
 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.design;
 20  
 
 21  
 import com.puppycrawl.tools.checkstyle.api.Check;
 22  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 23  
 import com.puppycrawl.tools.checkstyle.api.FastStack;
 24  
 import com.puppycrawl.tools.checkstyle.api.ScopeUtils;
 25  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 26  
 
 27  
 /**
 28  
  * <p>
 29  
  * Checks that class which has only private ctors
 30  
  * is declared as final.
 31  
  * </p>
 32  
  * <p>
 33  
  * An example of how to configure the check is:
 34  
  * </p>
 35  
  * <pre>
 36  
  * &lt;module name="FinalClass"/&gt;
 37  
  * </pre>
 38  
  * @author o_sukhodolsky
 39  
  */
 40  1
 public class FinalClassCheck
 41  
     extends Check
 42  
 {
 43  
     /** Keeps ClassDesc objects for stack of declared classes. */
 44  1
     private final FastStack<ClassDesc> mClasses = FastStack.newInstance();
 45  
 
 46  
     @Override
 47  
     public int[] getDefaultTokens()
 48  
     {
 49  1
         return new int[]{TokenTypes.CLASS_DEF, TokenTypes.CTOR_DEF};
 50  
     }
 51  
 
 52  
     @Override
 53  
     public void visitToken(DetailAST aAST)
 54  
     {
 55  18
         final DetailAST modifiers = aAST.findFirstToken(TokenTypes.MODIFIERS);
 56  
 
 57  18
         if (aAST.getType() == TokenTypes.CLASS_DEF) {
 58  9
             final boolean isFinal = (modifiers != null)
 59  
                     && modifiers.branchContains(TokenTypes.FINAL);
 60  9
             final boolean isAbstract = (modifiers != null)
 61  
                     && modifiers.branchContains(TokenTypes.ABSTRACT);
 62  9
             mClasses.push(new ClassDesc(isFinal, isAbstract));
 63  9
         }
 64  9
         else if (!ScopeUtils.inEnumBlock(aAST)) { //ctors in enums don't matter
 65  8
             final ClassDesc desc = mClasses.peek();
 66  8
             if ((modifiers != null)
 67  
                 && modifiers.branchContains(TokenTypes.LITERAL_PRIVATE))
 68  
             {
 69  6
                 desc.reportPrivateCtor();
 70  
             }
 71  
             else {
 72  2
                 desc.reportNonPrivateCtor();
 73  
             }
 74  
         }
 75  18
     }
 76  
 
 77  
     @Override
 78  
     public void leaveToken(DetailAST aAST)
 79  
     {
 80  18
         if (aAST.getType() != TokenTypes.CLASS_DEF) {
 81  9
             return;
 82  
         }
 83  
 
 84  9
         final ClassDesc desc = mClasses.pop();
 85  9
         if (!desc.isDeclaredAsFinal()
 86  
             && !desc.isDeclaredAsAbstract()
 87  
             && desc.hasPrivateCtor()
 88  
             && !desc.hasNonPrivateCtor())
 89  
         {
 90  3
             final String className =
 91  
                 aAST.findFirstToken(TokenTypes.IDENT).getText();
 92  3
             log(aAST.getLineNo(), "final.class", className);
 93  
         }
 94  9
     }
 95  
 
 96  
     /** maintains information about class' ctors */
 97  1
     private static final class ClassDesc
 98  
     {
 99  
         /** is class declared as final */
 100  
         private final boolean mDeclaredAsFinal;
 101  
 
 102  
         /** is class declared as abstract */
 103  
         private final boolean mDeclaredAsAbstract;
 104  
 
 105  
         /** does class have non-provate ctors */
 106  
         private boolean mHasNonPrivateCtor;
 107  
 
 108  
         /** does class have private ctors */
 109  
         private boolean mHasPrivateCtor;
 110  
 
 111  
         /**
 112  
          *  create a new ClassDesc instance.
 113  
          *  @param aDeclaredAsFinal indicates if the
 114  
          *         class declared as final
 115  
          *  @param aDeclaredAsAbstract indicates if the
 116  
          *         class declared as abstract
 117  
          */
 118  
         ClassDesc(boolean aDeclaredAsFinal, boolean aDeclaredAsAbstract)
 119  9
         {
 120  9
             mDeclaredAsFinal = aDeclaredAsFinal;
 121  9
             mDeclaredAsAbstract = aDeclaredAsAbstract;
 122  9
         }
 123  
 
 124  
         /** adds private ctor. */
 125  
         void reportPrivateCtor()
 126  
         {
 127  6
             mHasPrivateCtor = true;
 128  6
         }
 129  
 
 130  
         /** adds non-private ctor. */
 131  
         void reportNonPrivateCtor()
 132  
         {
 133  2
             mHasNonPrivateCtor = true;
 134  2
         }
 135  
 
 136  
         /**
 137  
          *  does class have private ctors.
 138  
          *  @return true if class has private ctors
 139  
          */
 140  
         boolean hasPrivateCtor()
 141  
         {
 142  7
             return mHasPrivateCtor;
 143  
         }
 144  
 
 145  
         /**
 146  
          *  does class have non-private ctors.
 147  
          *  @return true if class has non-private ctors
 148  
          */
 149  
         boolean hasNonPrivateCtor()
 150  
         {
 151  4
             return mHasNonPrivateCtor;
 152  
         }
 153  
 
 154  
         /**
 155  
          *  is class declared as final.
 156  
          *  @return true if class is declared as final
 157  
          */
 158  
         boolean isDeclaredAsFinal()
 159  
         {
 160  9
             return mDeclaredAsFinal;
 161  
         }
 162  
 
 163  
         /**
 164  
          *  is class declared as abstract.
 165  
          *  @return true if class is declared as final
 166  
          */
 167  
         boolean isDeclaredAsAbstract()
 168  
         {
 169  9
             return mDeclaredAsAbstract;
 170  
         }
 171  
 
 172  
         @Override
 173  
         public String toString()
 174  
         {
 175  0
             return this.getClass().getName()
 176  
                 + "["
 177  
                 + "final=" + mDeclaredAsFinal
 178  
                 + " abstract=" + mDeclaredAsAbstract
 179  
                 + " pctor=" + mHasPrivateCtor
 180  
                 + " ctor=" + mHasNonPrivateCtor
 181  
                 + "]";
 182  
         }
 183  
     }
 184  
 }