View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.pmd.rules;
6   
7   import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration;
8   import net.sourceforge.pmd.lang.java.ast.ASTFormalParameters;
9   import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
10  
11  /**
12   * Rule to check that a method does not take too many parameters. Unlike the
13   * PMD built-in {@code ExcessiveParameterList} rule, which exempts private
14   * constructors only, this implementation skips every constructor: a value
15   * object with many immutable attributes gets a parameter count that tracks
16   * its field count rather than its complexity, and the class has nowhere
17   * else to accept those attributes. The rule stays active for methods,
18   * where a long parameter list is a design smell worth reporting.
19   * Skipping constructors here instead of through a
20   * {@code violationSuppressXPath} keeps a redundant
21   * {@code @SuppressWarnings("PMD.ExcessiveParameterList")} visible to
22   * {@code UnnecessaryWarningSuppression}, which PMD credits to the
23   * annotation whenever the annotation suppressor runs first.
24   *
25   * @since 1.0
26   */
27  public final class ExcessiveParameterListRule
28      extends AbstractJavaRulechainRule {
29  
30      /**
31       * The number of parameters at or above which a method is reported.
32       */
33      private static final int THRESHOLD = 10;
34  
35      /**
36       * Default constructor.
37       */
38      public ExcessiveParameterListRule() {
39          super(ASTFormalParameters.class);
40      }
41  
42      @Override
43      public Object visit(final ASTFormalParameters params, final Object data) {
44          final int count = params.size();
45          if (count >= ExcessiveParameterListRule.THRESHOLD
46              && !(params.getParent() instanceof ASTConstructorDeclaration)) {
47              this.asCtx(data).addViolation(
48                  params, count, ExcessiveParameterListRule.THRESHOLD
49              );
50          }
51          return data;
52      }
53  }