Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/puppycrawl/tools/checkstyle/checks/WhitespaceAfterCheck.java


1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2002  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;
20  
21  import com.puppycrawl.tools.checkstyle.api.Check;
22  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
23  import com.puppycrawl.tools.checkstyle.api.DetailAST;
24  
25  /**
26   * <p>
27   * Checks that a token is followed by whitespace, with the exception that it
28   * does not check for whitespace after the semicolon of an empty for iterator.
29   * Use Check {@link EmptyForIteratorPad} to validate empty for iterators.
30   * </p>
31   * <p> By default the check will check the following tokens:
32   *  {@link TokenTypes#COMMA COMMA},
33   *  {@link TokenTypes#SEMI SEMI},
34   *  {@link TokenTypes#TYPECAST TYPECAST}.
35   * </p>
36   * <p>
37   * An example of how to configure the check is:
38   * </p>
39   * <pre>
40   * &lt;module name="WhitespaceAfter"/&gt;
41   * </pre> 
42   * <p> An example of how to configure the check for whitespace only after
43   * {@link TokenTypes#COMMA COMMA} and {@link TokenTypes#SEMI SEMI} tokens is:
44   * </p>
45   * <pre>
46   * &lt;module name="WhitespaceAfter"&gt;
47   *     &lt;property name="tokens" value="COMMA, SEMI"/&gt;
48   * &lt;/module&gt;
49   * </pre>
50   * @author <a href="mailto:checkstyle@puppycrawl.com">Oliver Burn</a>
51   * @author Rick Giles
52   * @version 1.0
53   */
54  public class WhitespaceAfterCheck
55      extends Check
56  {   
57      /** @see com.puppycrawl.tools.checkstyle.api.Check */
58      public int[] getDefaultTokens()
59      {
60          return new int[] {
61              TokenTypes.COMMA,
62              TokenTypes.SEMI,
63              TokenTypes.TYPECAST,
64          };
65      }
66  
67      /** @see com.puppycrawl.tools.checkstyle.api.Check */
68      public void visitToken(DetailAST aAST)
69      {
70          final String[] lines = getLines();
71          final Object[] message;
72          final DetailAST targetAST;
73          if (aAST.getType() == TokenTypes.TYPECAST) {
74              targetAST = aAST.findFirstToken(TokenTypes.RPAREN);
75              // TODO: i18n
76              message = new Object[]{"cast"};
77          }
78          else {
79              targetAST = aAST;
80              message = new Object[]{aAST.getText()};
81          }
82          final String line = lines[targetAST.getLineNo() - 1];
83          final int after =
84              targetAST.getColumnNo() + targetAST.getText().length();
85  
86          if (after < line.length()) {
87              
88              final char charAfter = line.charAt(after);
89              if ((targetAST.getType() == TokenTypes.SEMI)
90                  && ((charAfter == ';') || (charAfter == ')')))
91              {
92                  return;
93              }
94              if (!Character.isWhitespace(charAfter)) {
95                  //empty FOR_ITERATOR?
96                  if (targetAST.getType() == TokenTypes.SEMI) {
97                      final DetailAST sibling =
98                          (DetailAST) targetAST.getNextSibling();
99                      if ((sibling != null)
100                         && (sibling.getType() == TokenTypes.FOR_ITERATOR)
101                         && (sibling.getChildCount() == 0))
102                     {
103                         return;
104                     }
105                 }   
106                 log(targetAST.getLineNo(),
107                     targetAST.getColumnNo() + targetAST.getText().length(),
108                     "ws.notFollowed",
109                     message);
110             }
111         }
112     }
113 }