Coverage Report - com.puppycrawl.tools.checkstyle.api.Check
 
Classes in this File Line Coverage Branch Coverage Complexity
Check
97%
34/35
100%
2/2
1.048
 
 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.api;
 20  
 
 21  
 import com.google.common.collect.Sets;
 22  
 import java.util.Set;
 23  
 
 24  
 /**
 25  
  * The base class for checks.
 26  
  *
 27  
  * @author Oliver Burn
 28  
  * @version 1.0
 29  
  * @see <a href="./{@docRoot}/../writingchecks.html" target="_top">Writing
 30  
  * your own checks</a>
 31  
  */
 32  611
 public abstract class Check extends AbstractViolationReporter
 33  
 {
 34  
     /** default tab width for column reporting */
 35  
     private static final int DEFAULT_TAB_WIDTH = 8;
 36  
 
 37  
     /** the current file contents */
 38  
     private FileContents mFileContents;
 39  
 
 40  
     /** the tokens the check is interested in */
 41  611
     private final Set<String> mTokens = Sets.newHashSet();
 42  
 
 43  
     /** the object for collecting messages. */
 44  
     private LocalizedMessages mMessages;
 45  
 
 46  
     /** the tab width for column reporting */
 47  611
     private int mTabWidth = DEFAULT_TAB_WIDTH; // meaningful default
 48  
 
 49  
     /**
 50  
      * The class loader to load external classes. Not initialised as this must
 51  
      * be set by my creator.
 52  
      */
 53  
     private ClassLoader mLoader;
 54  
 
 55  
     /**
 56  
      * Returns the default token a check is interested in. Only used if the
 57  
      * configuration for a check does not define the tokens.
 58  
      * @return the default tokens
 59  
      * @see TokenTypes
 60  
      */
 61  
     public abstract int[] getDefaultTokens();
 62  
 
 63  
     /**
 64  
      * The configurable token set.
 65  
      * Used to protect Checks against malicious users who specify an
 66  
      * unacceptable token set in the configuration file.
 67  
      * The default implementation returns the check's default tokens.
 68  
      * @return the token set this check is designed for.
 69  
      * @see TokenTypes
 70  
      */
 71  
     public int[] getAcceptableTokens()
 72  
     {
 73  10
         final int[] defaultTokens = getDefaultTokens();
 74  10
         final int[] copy = new int[defaultTokens.length];
 75  10
         System.arraycopy(defaultTokens, 0, copy, 0, defaultTokens.length);
 76  10
         return copy;
 77  
     }
 78  
 
 79  
     /**
 80  
      * The tokens that this check must be registered for.
 81  
      * @return the token set this must be registered for.
 82  
      * @see TokenTypes
 83  
      */
 84  
     public int[] getRequiredTokens()
 85  
     {
 86  36
         return new int[] {};
 87  
     }
 88  
 
 89  
     /**
 90  
      * Adds a set of tokens the check is interested in.
 91  
      * @param aStrRep the string representation of the tokens interested in
 92  
      */
 93  
     public final void setTokens(String[] aStrRep)
 94  
     {
 95  105
         for (final String s : aStrRep) {
 96  61
             mTokens.add(s);
 97  
         }
 98  44
     }
 99  
 
 100  
     /**
 101  
      * Returns the tokens registered for the check.
 102  
      * @return the set of token names
 103  
      */
 104  
     public final Set<String> getTokenNames()
 105  
     {
 106  620
         return mTokens;
 107  
     }
 108  
 
 109  
     /**
 110  
      * Set the global object used to collect messages.
 111  
      * @param aMessages the messages to log with
 112  
      */
 113  
     public final void setMessages(LocalizedMessages aMessages)
 114  
     {
 115  608
         mMessages = aMessages;
 116  608
     }
 117  
 
 118  
     /**
 119  
      * Initialise the check. This is the time to verify that the check has
 120  
      * everything required to perform it job.
 121  
      */
 122  
     public void init()
 123  
     {
 124  598
     }
 125  
 
 126  
     /**
 127  
      * Destroy the check. It is being retired from service.
 128  
      */
 129  
     public void destroy()
 130  
     {
 131  585
     }
 132  
 
 133  
     /**
 134  
      * Called before the starting to process a tree. Ideal place to initialise
 135  
      * information that is to be collected whilst processing a tree.
 136  
      * @param aRootAST the root of the tree
 137  
      */
 138  
     public void beginTree(DetailAST aRootAST)
 139  
     {
 140  396
     }
 141  
 
 142  
     /**
 143  
      * Called after finished processing a tree. Ideal place to report on
 144  
      * information collected whilst processing a tree.
 145  
      * @param aRootAST the root of the tree
 146  
      */
 147  
     public void finishTree(DetailAST aRootAST)
 148  
     {
 149  585
     }
 150  
 
 151  
     /**
 152  
      * Called to process a token.
 153  
      * @param aAST the token to process
 154  
      */
 155  
     public void visitToken(DetailAST aAST)
 156  
     {
 157  0
     }
 158  
 
 159  
     /**
 160  
      * Called after all the child nodes have been process.
 161  
      * @param aAST the token leaving
 162  
      */
 163  
     public void leaveToken(DetailAST aAST)
 164  
     {
 165  7997
     }
 166  
 
 167  
     /**
 168  
      * Returns the lines associated with the tree.
 169  
      * @return the file contents
 170  
      */
 171  
     public final String[] getLines()
 172  
     {
 173  21335
         return getFileContents().getLines();
 174  
     }
 175  
 
 176  
     /**
 177  
      * Set the file contents associated with the tree.
 178  
      * @param aContents the manager
 179  
      */
 180  
     public final void setFileContents(FileContents aContents)
 181  
     {
 182  601
         mFileContents = aContents;
 183  601
     }
 184  
 
 185  
     /**
 186  
      * Returns the file contents associated with the tree.
 187  
      * @return the file contents
 188  
      */
 189  
     public final FileContents getFileContents()
 190  
     {
 191  22442
         return mFileContents;
 192  
     }
 193  
 
 194  
     /**
 195  
      * Set the class loader associated with the tree.
 196  
      * @param aLoader the class loader
 197  
      */
 198  
     public final void setClassLoader(ClassLoader aLoader)
 199  
     {
 200  605
         mLoader = aLoader;
 201  605
     }
 202  
 
 203  
     /**
 204  
      * Returns the class loader associated with the tree.
 205  
      * @return the class loader
 206  
      */
 207  
     public final ClassLoader getClassLoader()
 208  
     {
 209  19
         return mLoader;
 210  
     }
 211  
 
 212  
     /** @return the tab width to report errors with */
 213  
     protected final int getTabWidth()
 214  
     {
 215  20022
         return mTabWidth;
 216  
     }
 217  
 
 218  
     /**
 219  
      * Set the tab width to report errors with.
 220  
      * @param aTabWidth an <code>int</code> value
 221  
      */
 222  
     public final void setTabWidth(int aTabWidth)
 223  
     {
 224  606
         mTabWidth = aTabWidth;
 225  606
     }
 226  
 
 227  
     @Override
 228  
     public final void log(int aLine, String aKey, Object... aArgs)
 229  
     {
 230  734
         mMessages.add(
 231  
             new LocalizedMessage(
 232  
                 aLine,
 233  
                 getMessageBundle(),
 234  
                 aKey,
 235  
                 aArgs,
 236  
                 getSeverityLevel(),
 237  
                 getId(),
 238  
                 this.getClass(),
 239  
                 this.getCustomMessages().get(aKey)));
 240  734
     }
 241  
 
 242  
 
 243  
     @Override
 244  
     public final void log(int aLineNo, int aColNo, String aKey,
 245  
             Object... aArgs)
 246  
     {
 247  2651
         final int col = 1 + Utils.lengthExpandedTabs(
 248  
             getLines()[aLineNo - 1], aColNo, getTabWidth());
 249  2651
         mMessages.add(
 250  
             new LocalizedMessage(
 251  
                 aLineNo,
 252  
                 col,
 253  
                 getMessageBundle(),
 254  
                 aKey,
 255  
                 aArgs,
 256  
                 getSeverityLevel(),
 257  
                 getId(),
 258  
                 this.getClass(),
 259  
                 this.getCustomMessages().get(aKey)));
 260  2651
     }
 261  
 }