View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.indentation;
21  
22  import java.util.Optional;
23  
24  import com.puppycrawl.tools.checkstyle.api.DetailAST;
25  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26  
27  /**
28   * Handler for member definitions.
29   *
30   */
31  public class MemberDefHandler extends AbstractExpressionHandler {
32  
33      /**
34       * Construct an instance of this handler with the given indentation check,
35       * abstract syntax tree, and parent handler.
36       *
37       * @param indentCheck   the indentation check
38       * @param ast           the abstract syntax tree
39       * @param parent        the parent handler
40       */
41      public MemberDefHandler(IndentationCheck indentCheck,
42          DetailAST ast, AbstractExpressionHandler parent) {
43          super(indentCheck, "member def", ast, parent);
44      }
45  
46      @Override
47      public void checkIndentation() {
48          final DetailAST modifiersNode = getMainAst().findFirstToken(TokenTypes.MODIFIERS);
49          if (modifiersNode.hasChildren()) {
50              checkModifiers();
51          }
52          else {
53              checkType();
54          }
55          final DetailAST firstNode = getMainAst();
56          final DetailAST lastNode = getArrayInitNode(firstNode)
57              .orElseGet(() -> getVarDefStatementSemicolon(firstNode));
58  
59          if (lastNode != null) {
60              checkWrappingIndentation(firstNode, lastNode);
61          }
62      }
63  
64      /**
65       * Finds the array init node.
66       *
67       * @param firstNode Node to begin searching
68       * @return array init node
69       */
70      private static Optional<DetailAST> getArrayInitNode(DetailAST firstNode) {
71          return Optional.ofNullable(firstNode.findFirstToken(TokenTypes.ASSIGN))
72              .map(assign -> {
73                  return Optional.ofNullable(assign.findFirstToken(TokenTypes.EXPR))
74                      .map(expr -> expr.findFirstToken(TokenTypes.LITERAL_NEW))
75                      .orElse(assign);
76              })
77              .map(node -> node.findFirstToken(TokenTypes.ARRAY_INIT));
78      }
79  
80      @Override
81      public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child) {
82          return getIndent();
83      }
84  
85      @Override
86      protected void checkModifiers() {
87          final DetailAST modifier = getMainAst().findFirstToken(TokenTypes.MODIFIERS);
88          if (isOnStartOfLine(modifier)
89              && !getIndent().isAcceptable(expandedTabsColumnNo(modifier))) {
90              logError(modifier, "modifier", expandedTabsColumnNo(modifier));
91          }
92      }
93  
94      /**
95       * Check the indentation of the method type.
96       */
97      private void checkType() {
98          final DetailAST type = getMainAst().findFirstToken(TokenTypes.TYPE);
99          final DetailAST ident = AbstractExpressionHandler.getFirstToken(type);
100         final int columnNo = expandedTabsColumnNo(ident);
101         if (isOnStartOfLine(ident) && !getIndent().isAcceptable(columnNo)) {
102             logError(ident, "type", columnNo);
103         }
104     }
105 
106     /**
107      * Returns semicolon for variable definition statement.
108      *
109      * @param variableDef
110      *          ast node of type TokenTypes.VARIABLE_DEF
111      * @return ast node of type TokenTypes.SEMI
112      */
113     private static DetailAST getVarDefStatementSemicolon(DetailAST variableDef) {
114         DetailAST lastNode = variableDef.getLastChild();
115         if (lastNode.getType() != TokenTypes.SEMI) {
116             lastNode = variableDef.getNextSibling();
117         }
118         return lastNode;
119     }
120 
121 }