View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2015 the original author or authors.
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.DetailAST;
23  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24  import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck;
25  
26  /**
27   * <p>
28   * Checks line wrapping with separators.
29   * The policy to verify is specified using the {@link WrapOption} class
30   * and defaults to {@link WrapOption#EOL}.
31   * </p>
32   * <p> By default the check will check the following separators:
33   *  {@link TokenTypes#DOT DOT},
34   *  {@link TokenTypes#COMMA COMMA},
35   * Other acceptable tokens are
36   *  {@link TokenTypes#SEMI SEMI},
37   *  {@link TokenTypes#ELLIPSIS ELLIPSIS},
38   *  {@link TokenTypes#AT AT},
39   *  {@link TokenTypes#LPAREN LPAREN},
40   *  {@link TokenTypes#RPAREN RPAREN},
41   *  {@link TokenTypes#ARRAY_DECLARATOR ARRAY_DECLARATOR},
42   *  {@link TokenTypes#RBRACK RBRACK},
43   * </p>
44   * <p>
45   * Code example for comma and dot at the new line:
46   * </p>
47   * <pre>
48   * s
49   *    .isEmpty();
50   * foo(i
51   *    ,s);
52   * </pre>
53   *  <p>
54   * An example of how to configure the check is:
55   * </p>
56   * <pre>
57   * &lt;module name="SeparatorWrap"/&gt;
58   * </pre>
59   * <p>
60   * Code example for comma and dot at the previous line:
61   * </p>
62   * <pre>
63   * s.
64   *    isEmpty();
65   * foo(i,
66   *    s);
67   * </pre>
68   * <p> An example of how to configure the check for comma at the
69   * new line is:
70   * </p>
71   * <pre>
72   * &lt;module name="SeparatorWrap"&gt;
73   *     &lt;property name="tokens" value="COMMA"/&gt;
74   *     &lt;property name="option" value="nl"/&gt;
75   * &lt;/module&gt;
76   * </pre>
77   *
78   * @author maxvetrenko
79   */
80  public class SeparatorWrapCheck
81      extends AbstractOptionCheck<WrapOption>
82  {
83  
84      /**
85       * A key is pointing to the warning message text in "messages.properties"
86       * file.
87       */
88      public static final String LINE_PREVIOUS = "line.previous";
89  
90      /**
91       * A key is pointing to the warning message text in "messages.properties"
92       * file.
93       */
94      public static final String LINE_NEW = "line.new";
95  
96      /**
97       * Sets the comma wrap option to end of the line.
98       */
99      public SeparatorWrapCheck()
100     {
101         super(WrapOption.EOL, WrapOption.class);
102     }
103 
104     @Override
105     public int[] getDefaultTokens()
106     {
107         return new int[] {
108             TokenTypes.DOT,
109             TokenTypes.COMMA,
110         };
111     }
112 
113     @Override
114     public int[] getAcceptableTokens()
115     {
116         return new int[] {
117             TokenTypes.DOT,
118             TokenTypes.COMMA,
119             TokenTypes.SEMI,
120             TokenTypes.ELLIPSIS,
121             TokenTypes.AT,
122             TokenTypes.LPAREN,
123             TokenTypes.RPAREN,
124             TokenTypes.ARRAY_DECLARATOR,
125             TokenTypes.RBRACK,
126         };
127     }
128 
129     @Override
130     public void visitToken(DetailAST ast)
131     {
132         // TODO: It is a copy/paste from OperatorWrapCheck.
133         //It should be fixed in another issue
134         final String text = ast.getText();
135         final int colNo = ast.getColumnNo();
136         final int lineNo = ast.getLineNo();
137         final String currentLine = getLines()[lineNo - 1];
138         final String substringAfterToken =
139                 currentLine.substring(colNo + text.length()).trim();
140         final String substringBeforeToken =
141                 currentLine.substring(0, colNo).trim();
142         final WrapOption wSp = getAbstractOption();
143 
144         if (wSp == WrapOption.EOL
145                 && substringBeforeToken.length() == 0)
146         {
147             log(lineNo, colNo, LINE_PREVIOUS, text);
148         }
149         else if (wSp == WrapOption.NL
150                  && substringAfterToken.length() == 0)
151         {
152             log(lineNo, colNo, LINE_NEW, text);
153         }
154     }
155 }