1   package com.stateofflow.eclipse.metrics.calculators.localsinscope;
2   
3   import org.eclipse.jdt.core.dom.Block;
4   import org.eclipse.jdt.core.dom.CatchClause;
5   import org.eclipse.jdt.core.dom.FieldDeclaration;
6   import org.eclipse.jdt.core.dom.MethodDeclaration;
7   import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
8   
9   import com.stateofflow.eclipse.metrics.calculators.AbstractASTVisitorCalculator;
10  import com.stateofflow.eclipse.metrics.metric.MetricId;
11  
12  public final class NumberOfLocalsInScopeCalculator extends AbstractASTVisitorCalculator {
13      public static final MetricId METRIC_ID = MetricId.createMethodId(NumberOfLocalsInScopeCalculator.class);
14  
15      private NumberOfLocalsInScope localsInScope = new NumberOfLocalsInScope();
16  
17      protected Object getRestorableState() {
18          return new NumberOfLocalsInScope(localsInScope);
19      }
20  
21      protected void restoreSpecificState(final Object restorableState) {
22          localsInScope = (NumberOfLocalsInScope) restorableState;
23      }
24  
25      public boolean visit(final MethodDeclaration methodDeclarationNode) {
26          localsInScope.startMethod();
27          return super.visit(methodDeclarationNode);
28      }
29  
30      public void endVisit(final MethodDeclaration methodDeclarationNode) {
31          noteMethodValue(METRIC_ID, localsInScope.getMax());
32          super.endVisit(methodDeclarationNode);
33      }
34  
35      public boolean visit(final Block node) {
36          localsInScope.startNewScope();
37          return super.visit(node);
38      }
39  
40      public void endVisit(final Block node) {
41          localsInScope.endCurrentScope();
42          super.endVisit(node);
43      }
44  
45      public boolean visit(final CatchClause node) {
46          localsInScope.startNewScope();
47          localsInScope.addVariableDeclaration();
48          return super.visit(node);
49      }
50  
51      public void endVisit(final CatchClause node) {
52          localsInScope.endCurrentScope();
53          super.endVisit(node);
54      }
55  
56      public boolean visit(final FieldDeclaration node) {
57          return false;
58      }
59  
60      public boolean visit(final VariableDeclarationFragment node) {
61          localsInScope.addVariableDeclaration();
62          return super.visit(node);
63      }
64  }
65