View Javadoc
1   /*
2    * Copyright (c) 2011-2024 Qulice.com
3    *
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met: 1) Redistributions of source code must retain the above
9    * copyright notice, this list of conditions and the following
10   * disclaimer. 2) Redistributions in binary form must reproduce the above
11   * copyright notice, this list of conditions and the following
12   * disclaimer in the documentation and/or other materials provided
13   * with the distribution. 3) Neither the name of the Qulice.com nor
14   * the names of its contributors may be used to endorse or promote
15   * products derived from this software without specific prior written
16   * permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29   * OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package com.qulice.checkstyle.parameters;
32  
33  import com.puppycrawl.tools.checkstyle.api.DetailAST;
34  import java.util.ArrayList;
35  import java.util.Collections;
36  import java.util.List;
37  
38  /**
39   * Abstract parameters. Is used for Generic type parameters or
40   *  method(constructor) arguments.
41   *
42   * @since 0.18.18
43   */
44  public class Parameters {
45  
46      /**
47       * Class, interface, constructor or method definition node.
48       */
49      private final DetailAST node;
50  
51      /**
52       * Parent TokenType (TYPE_PARAMETERS or PARAMETERS).
53       * @see com.puppycrawl.tools.checkstyle.api.TokenTypes
54       */
55      private final int parent;
56  
57      /**
58       * Childs TokenType (TYPE_PARAMETER or PARAMETER_DEF).
59       * @see com.puppycrawl.tools.checkstyle.api.TokenTypes
60       */
61      private final int childs;
62  
63      /**
64       * Primary ctor.
65       * @param node Class, interface, constructor or method definition node.
66       * @param parent Parent TokenType (TYPE_PARAMETERS or PARAMETERS).
67       * @param childs Childs TokenType (TYPE_PARAMETER or PARAMETER_DEF).
68       */
69      public Parameters(
70          final DetailAST node, final int parent, final int childs
71      ) {
72          this.node = node;
73          this.parent = parent;
74          this.childs = childs;
75      }
76  
77      /**
78       * Return number of arguments.
79       * @return Number of parameters.
80       */
81      public final int count() {
82          final int result;
83          final DetailAST params = this.node.findFirstToken(this.parent);
84          if (params == null) {
85              result = 0;
86          } else {
87              result = params.getChildCount(this.childs);
88          }
89          return result;
90      }
91  
92      /**
93       * Return parameters for this node.
94       * @return Parameters for this node.
95       */
96      public final List<DetailAST> parameters() {
97          final List<DetailAST> result;
98          final int count = this.count();
99          if (count == 0) {
100             result = Collections.emptyList();
101         } else {
102             final DetailAST params = this.node.findFirstToken(this.parent);
103             result = new ArrayList<>(count);
104             DetailAST child = params.getFirstChild();
105             while (child != null) {
106                 if (child.getType() == this.childs) {
107                     result.add(child);
108                 }
109                 child = child.getNextSibling();
110             }
111         }
112         return result;
113     }
114 }