View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.checkstyle.parameters;
6   
7   import com.puppycrawl.tools.checkstyle.api.DetailAST;
8   import java.util.ArrayList;
9   import java.util.Collections;
10  import java.util.List;
11  
12  /**
13   * Abstract parameters. Is used for Generic type parameters or
14   * method(constructor) arguments.
15   *
16   * @since 0.18.18
17   */
18  public class Parameters {
19  
20      /**
21       * Class, interface, constructor or method definition node.
22       */
23      private final DetailAST node;
24  
25      /**
26       * Parent TokenType (TYPE_PARAMETERS or PARAMETERS).
27       *
28       * @see com.puppycrawl.tools.checkstyle.api.TokenTypes
29       */
30      private final int parent;
31  
32      /**
33       * Children TokenType (TYPE_PARAMETER or PARAMETER_DEF).
34       *
35       * @see com.puppycrawl.tools.checkstyle.api.TokenTypes
36       */
37      private final int children;
38  
39      /**
40       * Primary ctor.
41       *
42       * @param node Class, interface, constructor or method definition node
43       * @param parent Parent TokenType (TYPE_PARAMETERS or PARAMETERS)
44       * @param children Children TokenType (TYPE_PARAMETER or PARAMETER_DEF)
45       */
46      public Parameters(
47          final DetailAST node, final int parent, final int children
48      ) {
49          this.node = node;
50          this.parent = parent;
51          this.children = children;
52      }
53  
54      /**
55       * Return number of arguments.
56       *
57       * @return Number of parameters
58       */
59      public final int count() {
60          final int result;
61          final DetailAST params = this.node.findFirstToken(this.parent);
62          if (params == null) {
63              result = 0;
64          } else {
65              result = params.getChildCount(this.children);
66          }
67          return result;
68      }
69  
70      /**
71       * Return parameters for this node.
72       *
73       * @return Parameters for this node
74       */
75      public final List<DetailAST> parameters() {
76          final List<DetailAST> result;
77          final int count = this.count();
78          if (count == 0) {
79              result = Collections.emptyList();
80          } else {
81              final DetailAST params = this.node.findFirstToken(this.parent);
82              result = new ArrayList<>(count);
83              DetailAST child = params.getFirstChild();
84              while (child != null) {
85                  if (child.getType() == this.children) {
86                      result.add(child);
87                  }
88                  child = child.getNextSibling();
89              }
90          }
91          return result;
92      }
93  }