1   package com.stateofflow.eclipse.metrics.calculators;
2   
3   import org.eclipse.jdt.core.dom.IMethodBinding;
4   import org.eclipse.jdt.core.dom.MethodDeclaration;
5   
6   import com.stateofflow.eclipse.metrics.bindings.Bindings;
7   import com.stateofflow.eclipse.metrics.configuration.MetricsConfiguration;
8   import com.stateofflow.eclipse.metrics.metric.MetricId;
9   import com.stateofflow.eclipse.metrics.metric.MetricPropertyKey;
10  
11  public final class NumberOfParametersCalculator extends AbstractASTVisitorCalculator {
12      public static final MetricId METRIC_ID = MetricId.createMethodId(NumberOfParametersCalculator.class);
13      public static final MetricPropertyKey INCLUDE_METHODS_DECLARED_IN_SUPERTYPE_KEY = new MetricPropertyKey(METRIC_ID, "includeSupertypeMethods");
14  
15      private final MetricsConfiguration configuration;
16  
17      public NumberOfParametersCalculator(final MetricsConfiguration configuration) {
18          this.configuration = configuration;
19      }
20  
21      public void endVisit(final MethodDeclaration method) {
22          if (shouldCalculate(method)) {
23              noteMethodValue(NumberOfParametersCalculator.METRIC_ID, method.parameters().size());
24          }
25          super.endVisit(method);
26      }
27  
28      protected boolean isInterfaceCalculator() {
29          return true;
30      }
31  
32      private boolean isMethodDeclaredInSupertype(final MethodDeclaration method) {
33          final IMethodBinding binding = method.resolveBinding();
34          return binding == null ? false : Bindings.findOverriddenMethod(binding) != null;
35      }
36  
37      private boolean shouldCalculate(final MethodDeclaration method) {
38          return configuration.getBoolean(INCLUDE_METHODS_DECLARED_IN_SUPERTYPE_KEY) || !isMethodDeclaredInSupertype(method);
39      }
40  }
41