1   package com.stateofflow.eclipse.metrics.calculators.levels;
2   
3   import org.eclipse.jdt.core.dom.DoStatement;
4   import org.eclipse.jdt.core.dom.EnhancedForStatement;
5   import org.eclipse.jdt.core.dom.ForStatement;
6   import org.eclipse.jdt.core.dom.IfStatement;
7   import org.eclipse.jdt.core.dom.MethodDeclaration;
8   import org.eclipse.jdt.core.dom.Modifier;
9   import org.eclipse.jdt.core.dom.SwitchStatement;
10  import org.eclipse.jdt.core.dom.TryStatement;
11  import org.eclipse.jdt.core.dom.WhileStatement;
12  
13  import com.stateofflow.eclipse.metrics.calculators.AbstractASTVisitorCalculator;
14  import com.stateofflow.eclipse.metrics.metric.MetricId;
15  
16  public final class NumberOfLevelsCalculator extends AbstractASTVisitorCalculator {
17      public static final MetricId METRIC_ID = MetricId.createMethodId(NumberOfLevelsCalculator.class);
18  
19      private Level levels = new Level();
20  
21      public void endVisit(final DoStatement arg0) {
22          levels.decrement();
23          super.endVisit(arg0);
24      }
25  
26      public void endVisit(final EnhancedForStatement arg0) {
27          levels.decrement();
28          super.endVisit(arg0);
29      }
30  
31      public void endVisit(final ForStatement arg0) {
32          levels.decrement();
33          super.endVisit(arg0);
34      }
35  
36      public void endVisit(final IfStatement arg0) {
37          levels.decrement();
38          super.endVisit(arg0);
39      }
40  
41      public void endVisit(final MethodDeclaration arg0) {
42          if (isConcrete(arg0)) {
43              noteMethodValue(NumberOfLevelsCalculator.METRIC_ID, levels.getMethodMaximum());
44          }
45          super.endVisit(arg0);
46      }
47  
48      public void endVisit(final SwitchStatement arg0) {
49          levels.decrement();
50          super.endVisit(arg0);
51      }
52  
53      public void endVisit(final TryStatement arg0) {
54          levels.decrement();
55          super.endVisit(arg0);
56      }
57  
58      public void endVisit(final WhileStatement arg0) {
59          levels.decrement();
60          super.endVisit(arg0);
61      }
62  
63      protected Object getRestorableState() {
64          return new Level(levels);
65      }
66  
67      protected void handleNestedClass(final Object inner) {
68          levels.handleNestedClass((Level) inner);
69      }
70  
71      protected void handleStartOfType() {
72          levels.startType();
73          super.handleStartOfType();
74      }
75  
76      protected void restoreSpecificState(final Object restorableState) {
77          levels = (Level) restorableState;
78      }
79  
80      public boolean visit(final DoStatement arg0) {
81          levels.increment();
82          return super.visit(arg0);
83      }
84  
85      public boolean visit(final EnhancedForStatement arg0) {
86          levels.increment();
87          return super.visit(arg0);
88      }
89  
90      public boolean visit(final ForStatement arg0) {
91          levels.increment();
92          return super.visit(arg0);
93      }
94  
95      public boolean visit(final IfStatement arg0) {
96          levels.increment();
97          return super.visit(arg0);
98      }
99  
100     public boolean visit(final MethodDeclaration arg0) {
101         if ((arg0.getModifiers() & Modifier.ABSTRACT) == 0) {
102             levels.startMethod();
103             return super.visit(arg0);
104         }
105         return false;
106     }
107 
108     public boolean visit(final SwitchStatement arg0) {
109         levels.increment();
110         return super.visit(arg0);
111     }
112 
113     public boolean visit(final TryStatement arg0) {
114         levels.increment();
115         return super.visit(arg0);
116     }
117 
118     public boolean visit(final WhileStatement arg0) {
119         levels.increment();
120         return super.visit(arg0);
121     }
122 }