1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 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 java.util.Locale;
23  
24  import com.puppycrawl.tools.checkstyle.StatelessCheck;
25  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
26  import com.puppycrawl.tools.checkstyle.api.DetailAST;
27  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
28  
29  /**
30   * <p>Abstract class for checking the padding of parentheses. That is whether a
31   * space is required after a left parenthesis and before a right parenthesis,
32   * or such spaces are forbidden.
33   * </p>
34   */
35  @StatelessCheck
36  public abstract class AbstractParenPadCheck
37      extends AbstractCheck {
38  
39      /**
40       * A key is pointing to the warning message text in "messages.properties"
41       * file.
42       */
43      public static final String MSG_WS_FOLLOWED = "ws.followed";
44  
45      /**
46       * A key is pointing to the warning message text in "messages.properties"
47       * file.
48       */
49      public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed";
50  
51      /**
52       * A key is pointing to the warning message text in "messages.properties"
53       * file.
54       */
55      public static final String MSG_WS_PRECEDED = "ws.preceded";
56  
57      /**
58       * A key is pointing to the warning message text in "messages.properties"
59       * file.
60       */
61      public static final String MSG_WS_NOT_PRECEDED = "ws.notPreceded";
62  
63      /** Open parenthesis literal. */
64      private static final char OPEN_PARENTHESIS = '(';
65  
66      /** Close parenthesis literal. */
67      private static final char CLOSE_PARENTHESIS = ')';
68  
69      /** The policy to enforce. */
70      private PadOption option = PadOption.NOSPACE;
71  
72      /**
73       * Set the option to enforce.
74       * @param optionStr string to decode option from
75       * @throws IllegalArgumentException if unable to decode
76       */
77      public void setOption(String optionStr) {
78          option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
79      }
80  
81      /**
82       * Process a token representing a left parentheses.
83       * @param ast the token representing a left parentheses
84       */
85      protected void processLeft(DetailAST ast) {
86          final String line = getLines()[ast.getLineNo() - 1];
87          final int after = ast.getColumnNo() + 1;
88          if (after < line.length()) {
89              if (option == PadOption.NOSPACE
90                  && Character.isWhitespace(line.charAt(after))) {
91                  log(ast, MSG_WS_FOLLOWED, OPEN_PARENTHESIS);
92              }
93              else if (option == PadOption.SPACE
94                       && !Character.isWhitespace(line.charAt(after))
95                       && line.charAt(after) != CLOSE_PARENTHESIS) {
96                  log(ast, MSG_WS_NOT_FOLLOWED, OPEN_PARENTHESIS);
97              }
98          }
99      }
100 
101     /**
102      * Process a token representing a right parentheses.
103      * @param ast the token representing a right parentheses
104      */
105     protected void processRight(DetailAST ast) {
106         final int before = ast.getColumnNo() - 1;
107         if (before >= 0) {
108             final String line = getLines()[ast.getLineNo() - 1];
109             if (option == PadOption.NOSPACE
110                 && Character.isWhitespace(line.charAt(before))
111                 && !CommonUtil.hasWhitespaceBefore(before, line)) {
112                 log(ast, MSG_WS_PRECEDED, CLOSE_PARENTHESIS);
113             }
114             else if (option == PadOption.SPACE
115                 && !Character.isWhitespace(line.charAt(before))
116                 && line.charAt(before) != OPEN_PARENTHESIS) {
117                 log(ast, MSG_WS_NOT_PRECEDED, CLOSE_PARENTHESIS);
118             }
119         }
120     }
121 
122 }