Coverage Report - com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
LeftCurlyCheck
93%
61/65
72%
52/72
7.714
 
 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.blocks;
 20  
 
 21  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 22  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 23  
 import com.puppycrawl.tools.checkstyle.api.Utils;
 24  
 import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck;
 25  
 
 26  
 /**
 27  
  * <p>
 28  
  * Checks the placement of left curly braces on types, methods and
 29  
  * other blocks:
 30  
  *  {@link  TokenTypes#LITERAL_CATCH LITERAL_CATCH},  {@link
 31  
  * TokenTypes#LITERAL_DO LITERAL_DO},  {@link TokenTypes#LITERAL_ELSE
 32  
  * LITERAL_ELSE},  {@link TokenTypes#LITERAL_FINALLY LITERAL_FINALLY},  {@link
 33  
  * TokenTypes#LITERAL_FOR LITERAL_FOR},  {@link TokenTypes#LITERAL_IF
 34  
  * LITERAL_IF},  {@link TokenTypes#LITERAL_SWITCH LITERAL_SWITCH},  {@link
 35  
  * TokenTypes#LITERAL_SYNCHRONIZED LITERAL_SYNCHRONIZED},  {@link
 36  
  * TokenTypes#LITERAL_TRY LITERAL_TRY},  {@link TokenTypes#LITERAL_WHILE
 37  
  * LITERAL_WHILE}.
 38  
  * </p>
 39  
  *
 40  
  * <p>
 41  
  * The policy to verify is specified using the {@link LeftCurlyOption} class and
 42  
  * defaults to {@link LeftCurlyOption#EOL}. Policies {@link LeftCurlyOption#EOL}
 43  
  * and {@link LeftCurlyOption#NLOW} take into account property maxLineLength.
 44  
  * The default value for maxLineLength is 80.
 45  
  * </p>
 46  
  * <p>
 47  
  * An example of how to configure the check is:
 48  
  * </p>
 49  
  * <pre>
 50  
  * &lt;module name="LeftCurly"/&gt;
 51  
  * </pre>
 52  
  * <p>
 53  
  * An example of how to configure the check with policy
 54  
  * {@link LeftCurlyOption#NLOW} and maxLineLength 120 is:
 55  
  * </p>
 56  
  * <pre>
 57  
  * &lt;module name="LeftCurly"&gt;
 58  
  *      &lt;property name="option"
 59  
  * value="nlow"/&gt;     &lt;property name="maxLineLength" value="120"/&gt; &lt;
 60  
  * /module&gt;
 61  
  * </pre>
 62  
  *
 63  
  * @author Oliver Burn
 64  
  * @author lkuehne
 65  
  * @version 1.0
 66  
  */
 67  
 public class LeftCurlyCheck
 68  
     extends AbstractOptionCheck<LeftCurlyOption>
 69  
 {
 70  
     /** default maximum line length */
 71  
     private static final int DEFAULT_MAX_LINE_LENGTH = 80;
 72  
 
 73  
     /** TODO: replace this ugly hack **/
 74  10
     private int mMaxLineLength = DEFAULT_MAX_LINE_LENGTH;
 75  
 
 76  
     /**
 77  
      * Creates a default instance and sets the policy to EOL.
 78  
      */
 79  
     public LeftCurlyCheck()
 80  
     {
 81  10
         super(LeftCurlyOption.EOL, LeftCurlyOption.class);
 82  10
     }
 83  
 
 84  
     /**
 85  
      * Sets the maximum line length used in calculating the placement of the
 86  
      * left curly brace.
 87  
      * @param aMaxLineLength the max allowed line length
 88  
      */
 89  
     public void setMaxLineLength(int aMaxLineLength)
 90  
     {
 91  0
         mMaxLineLength = aMaxLineLength;
 92  0
     }
 93  
 
 94  
     @Override
 95  
     public int[] getDefaultTokens()
 96  
     {
 97  10
         return new int[] {
 98  
             TokenTypes.INTERFACE_DEF,
 99  
             TokenTypes.CLASS_DEF,
 100  
             TokenTypes.ANNOTATION_DEF,
 101  
             TokenTypes.ENUM_DEF,
 102  
             TokenTypes.CTOR_DEF,
 103  
             TokenTypes.METHOD_DEF,
 104  
             TokenTypes.ENUM_CONSTANT_DEF,
 105  
             TokenTypes.LITERAL_WHILE,
 106  
             TokenTypes.LITERAL_TRY,
 107  
             TokenTypes.LITERAL_CATCH,
 108  
             TokenTypes.LITERAL_FINALLY,
 109  
             TokenTypes.LITERAL_SYNCHRONIZED,
 110  
             TokenTypes.LITERAL_SWITCH,
 111  
             TokenTypes.LITERAL_DO,
 112  
             TokenTypes.LITERAL_IF,
 113  
             TokenTypes.LITERAL_ELSE,
 114  
             TokenTypes.LITERAL_FOR,
 115  
             // TODO: need to handle....
 116  
             //TokenTypes.STATIC_INIT,
 117  
         };
 118  
     }
 119  
 
 120  
     @Override
 121  
     public void visitToken(DetailAST aAST)
 122  
     {
 123  
         final DetailAST startToken;
 124  
         final DetailAST brace;
 125  
 
 126  209
         switch (aAST.getType()) {
 127  
         case TokenTypes.CTOR_DEF :
 128  
         case TokenTypes.METHOD_DEF :
 129  92
             startToken = skipAnnotationOnlyLines(aAST);
 130  92
             brace = aAST.findFirstToken(TokenTypes.SLIST);
 131  92
             break;
 132  
 
 133  
         case TokenTypes.INTERFACE_DEF :
 134  
         case TokenTypes.CLASS_DEF :
 135  
         case TokenTypes.ANNOTATION_DEF :
 136  
         case TokenTypes.ENUM_DEF :
 137  
         case TokenTypes.ENUM_CONSTANT_DEF :
 138  58
             startToken = skipAnnotationOnlyLines(aAST);
 139  58
             final DetailAST objBlock = aAST.findFirstToken(TokenTypes.OBJBLOCK);
 140  58
             brace = (objBlock == null)
 141  
                 ? null
 142  
                 : (DetailAST) objBlock.getFirstChild();
 143  58
             break;
 144  
 
 145  
         case TokenTypes.LITERAL_WHILE:
 146  
         case TokenTypes.LITERAL_CATCH:
 147  
         case TokenTypes.LITERAL_SYNCHRONIZED:
 148  
         case TokenTypes.LITERAL_FOR:
 149  
         case TokenTypes.LITERAL_TRY:
 150  
         case TokenTypes.LITERAL_FINALLY:
 151  
         case TokenTypes.LITERAL_DO:
 152  
         case TokenTypes.LITERAL_IF :
 153  46
             startToken = aAST;
 154  46
             brace = aAST.findFirstToken(TokenTypes.SLIST);
 155  46
             break;
 156  
 
 157  
         case TokenTypes.LITERAL_ELSE :
 158  11
             startToken = aAST;
 159  11
             final DetailAST candidate = aAST.getFirstChild();
 160  11
             brace =
 161  
                 (candidate.getType() == TokenTypes.SLIST)
 162  
                 ? candidate
 163  
                 : null; // silently ignore
 164  11
             break;
 165  
 
 166  
         case TokenTypes.LITERAL_SWITCH :
 167  2
             startToken = aAST;
 168  2
             brace = aAST.findFirstToken(TokenTypes.LCURLY);
 169  2
             break;
 170  
 
 171  
         default :
 172  0
             startToken = null;
 173  0
             brace = null;
 174  
         }
 175  
 
 176  209
         if ((brace != null) && (startToken != null)) {
 177  157
             verifyBrace(brace, startToken);
 178  
         }
 179  209
     }
 180  
 
 181  
     /**
 182  
      * Skip lines that only contain <code>TokenTypes.ANNOTATION</code>s.
 183  
      * If the received <code>DetailAST</code>
 184  
      * has annotations within its modifiers then first token on the line
 185  
      * of the first token afer all annotations is return. This might be
 186  
      * an annotation.
 187  
      * Otherwise, the received <code>DetailAST</code> is returned.
 188  
      * @param aAST <code>DetailAST</code>.
 189  
      * @return <code>DetailAST</code>.
 190  
      */
 191  
     private DetailAST skipAnnotationOnlyLines(DetailAST aAST)
 192  
     {
 193  150
         final DetailAST modifiers = aAST.findFirstToken(TokenTypes.MODIFIERS);
 194  150
         if (modifiers == null) {
 195  10
             return aAST;
 196  
         }
 197  140
         DetailAST lastAnnot = findLastAnnotation(modifiers);
 198  140
         if (lastAnnot == null) {
 199  
             // There are no annotations.
 200  122
             return aAST;
 201  
         }
 202  18
         final DetailAST tokenAfterLast = lastAnnot.getNextSibling() != null
 203  
                                        ? lastAnnot.getNextSibling()
 204  
                                        : modifiers.getNextSibling();
 205  18
         if (tokenAfterLast.getLineNo() > lastAnnot.getLineNo()) {
 206  14
             return tokenAfterLast;
 207  
         }
 208  4
         final int lastAnnotLineNumber = lastAnnot.getLineNo();
 209  
         while (lastAnnot.getPreviousSibling() != null
 210  8
                && (lastAnnot.getPreviousSibling().getLineNo()
 211  
                     == lastAnnotLineNumber))
 212  
         {
 213  4
             lastAnnot = lastAnnot.getPreviousSibling();
 214  
         }
 215  4
         return lastAnnot;
 216  
     }
 217  
 
 218  
     /**
 219  
      * Find the last token of type <code>TokenTypes.ANNOTATION</code>
 220  
      * under the given set of modifiers.
 221  
      * @param aModifiers <code>DetailAST</code>.
 222  
      * @return <code>DetailAST</code> or null if there are no annotations.
 223  
      */
 224  
     private DetailAST findLastAnnotation(DetailAST aModifiers)
 225  
     {
 226  140
         DetailAST aAnnot = aModifiers.findFirstToken(TokenTypes.ANNOTATION);
 227  
         while (aAnnot != null && aAnnot.getNextSibling() != null
 228  148
                && aAnnot.getNextSibling().getType() == TokenTypes.ANNOTATION)
 229  
         {
 230  8
             aAnnot = aAnnot.getNextSibling();
 231  
         }
 232  140
         return aAnnot;
 233  
     }
 234  
 
 235  
     /**
 236  
      * Verifies that a specified left curly brace is placed correctly
 237  
      * according to policy.
 238  
      * @param aBrace token for left curly brace
 239  
      * @param aStartToken token for start of expression
 240  
      */
 241  
     private void verifyBrace(final DetailAST aBrace,
 242  
                              final DetailAST aStartToken)
 243  
     {
 244  157
         final String braceLine = getLines()[aBrace.getLineNo() - 1];
 245  
 
 246  
         // calculate the previous line length without trailing whitespace. Need
 247  
         // to handle the case where there is no previous line, cause the line
 248  
         // being check is the first line in the file.
 249  157
         final int prevLineLen = (aBrace.getLineNo() == 1)
 250  
             ? mMaxLineLength
 251  
             : Utils.lengthMinusTrailingWhitespace(
 252  
                 getLines()[aBrace.getLineNo() - 2]);
 253  
 
 254  
         // Check for being told to ignore, or have '{}' which is a special case
 255  157
         if ((braceLine.length() > (aBrace.getColumnNo() + 1))
 256  
             && (braceLine.charAt(aBrace.getColumnNo() + 1) == '}'))
 257  
         {
 258  
             ; // ignore
 259  
         }
 260  147
         else if (getAbstractOption() == LeftCurlyOption.NL) {
 261  60
             if (!Utils.whitespaceBefore(aBrace.getColumnNo(), braceLine)) {
 262  21
                 log(aBrace.getLineNo(), aBrace.getColumnNo(),
 263  
                     "line.new", "{");
 264  
             }
 265  
         }
 266  87
         else if (getAbstractOption() == LeftCurlyOption.EOL) {
 267  76
             if (Utils.whitespaceBefore(aBrace.getColumnNo(), braceLine)
 268  
                 && ((prevLineLen + 2) <= mMaxLineLength))
 269  
             {
 270  40
                 log(aBrace.getLineNo(), aBrace.getColumnNo(),
 271  
                     "line.previous", "{");
 272  
             }
 273  
         }
 274  11
         else if (getAbstractOption() == LeftCurlyOption.NLOW) {
 275  11
             if (aStartToken.getLineNo() == aBrace.getLineNo()) {
 276  
                 ; // all ok as on the same line
 277  
             }
 278  11
             else if ((aStartToken.getLineNo() + 1) == aBrace.getLineNo()) {
 279  7
                 if (!Utils.whitespaceBefore(aBrace.getColumnNo(), braceLine)) {
 280  2
                     log(aBrace.getLineNo(), aBrace.getColumnNo(),
 281  
                         "line.new", "{");
 282  
                 }
 283  5
                 else if ((prevLineLen + 2) <= mMaxLineLength) {
 284  5
                     log(aBrace.getLineNo(), aBrace.getColumnNo(),
 285  
                         "line.previous", "{");
 286  
                 }
 287  
             }
 288  4
             else if (!Utils.whitespaceBefore(aBrace.getColumnNo(), braceLine)) {
 289  4
                 log(aBrace.getLineNo(), aBrace.getColumnNo(),
 290  
                     "line.new", "{");
 291  
             }
 292  
         }
 293  157
     }
 294  
 }