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/whitespace/NoWhitespaceAfterCheck.java


1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2003  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  
20  package com.puppycrawl.tools.checkstyle.checks.whitespace;
21  
22  import com.puppycrawl.tools.checkstyle.api.Check;
23  import com.puppycrawl.tools.checkstyle.api.DetailAST;
24  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
25  
26  /**
27   * <p>
28   * Checks that there is no whitespace after a token.
29   * More specifically, it checks that it is not followed by whitespace,
30   * or (if linebreaks are allowed) all characters on the line after are
31   * whitespace. To forbid linebreaks afer a token, set property
32   * allowLineBreaks to false.
33   * </p>
34    * <p> By default the check will check the following operators:
35   *  {@link TokenTypes#ARRAY_INIT ARRAY_INIT},
36   *  {@link TokenTypes#BNOT BNOT},
37   *  {@link TokenTypes#DEC DEC},
38   *  {@link TokenTypes#DOT DOT},
39   *  {@link TokenTypes#INC INC},
40   *  {@link TokenTypes#LNOT LNOT},
41   *  {@link TokenTypes#UNARY_MINUS UNARY_MINUS},
42   *  {@link TokenTypes#UNARY_PLUS UNARY_PLUS}.
43   * </p>
44   * <p>
45   * An example of how to configure the check is:
46   * </p>
47   * <pre>
48   * &lt;module name="NoWhitespaceAfter"/&gt;
49   * </pre>
50   * <p> An example of how to configure the check to forbid linebreaks after
51   * a {@link TokenTypes#DOT DOT} token is:
52   * <pre>
53   * &lt;module name="NoWhitespaceAfter"&gt;
54   *     &lt;property name="tokens" value="DOT"/&gt;
55   *     &lt;property name="allowLineBreaks" value="false"/&gt;
56   * &lt;/module&gt;
57   * </pre>
58   * @author Rick Giles
59   * @author lkuehne
60   * @version 1.0
61   */
62  public class NoWhitespaceAfterCheck
63      extends Check
64  {
65      /** Whether whitespace is allowed if the AST is at a linebreak */
66      private boolean mAllowLineBreaks = true;
67  
68      /** @see com.puppycrawl.tools.checkstyle.api.Check */
69      public int[] getDefaultTokens()
70      {
71          return new int[] {
72              TokenTypes.ARRAY_INIT,
73              TokenTypes.INC,
74              TokenTypes.DEC,
75              TokenTypes.UNARY_MINUS,
76              TokenTypes.UNARY_PLUS,
77              TokenTypes.BNOT,
78              TokenTypes.LNOT,
79              TokenTypes.DOT,
80          };
81      }
82  
83      /** @see com.puppycrawl.tools.checkstyle.api.Check */
84      public void visitToken(DetailAST aAST)
85      {
86          final String[] lines = getLines();
87          final String line = lines[aAST.getLineNo() - 1];
88          final int after = aAST.getColumnNo() + aAST.getText().length();
89  
90          if (after >= line.length()
91              || Character.isWhitespace(line.charAt(after)))
92          {
93              boolean flag = !mAllowLineBreaks;
94              for (int i = after + 1; !flag && i < line.length(); i++) {
95                  if (!Character.isWhitespace(line.charAt(i))) {
96                      flag = true;
97                  }
98              }
99              if (flag) {
100                 log(aAST.getLineNo(), after, "ws.followed", aAST.getText());
101             }
102         }
103     }
104 
105     /**
106      * Control whether whitespace is flagged at linebreaks.
107      * @param aAllowLineBreaks whether whitespace should be
108      * flagged at linebreaks.
109      */
110     public void setAllowLineBreaks(boolean aAllowLineBreaks)
111     {
112         mAllowLineBreaks = aAllowLineBreaks;
113     }
114 }