1   package com.stateofflow.eclipse.metrics.calculators.statements;
2   
3   import org.eclipse.jdt.core.dom.Assignment;
4   import org.eclipse.jdt.core.dom.BreakStatement;
5   import org.eclipse.jdt.core.dom.CatchClause;
6   import org.eclipse.jdt.core.dom.ConstructorInvocation;
7   import org.eclipse.jdt.core.dom.ContinueStatement;
8   import org.eclipse.jdt.core.dom.DoStatement;
9   import org.eclipse.jdt.core.dom.EnhancedForStatement;
10  import org.eclipse.jdt.core.dom.ForStatement;
11  import org.eclipse.jdt.core.dom.IfStatement;
12  import org.eclipse.jdt.core.dom.MethodDeclaration;
13  import org.eclipse.jdt.core.dom.MethodInvocation;
14  import org.eclipse.jdt.core.dom.PostfixExpression;
15  import org.eclipse.jdt.core.dom.PrefixExpression;
16  import org.eclipse.jdt.core.dom.ReturnStatement;
17  import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
18  import org.eclipse.jdt.core.dom.SwitchCase;
19  import org.eclipse.jdt.core.dom.ThrowStatement;
20  import org.eclipse.jdt.core.dom.TryStatement;
21  import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
22  import org.eclipse.jdt.core.dom.WhileStatement;
23  
24  import com.stateofflow.eclipse.metrics.calculators.AbstractASTVisitorCalculator;
25  import com.stateofflow.eclipse.metrics.metric.MetricId;
26  
27  public final class NumberOfStatementsCalculator extends AbstractASTVisitorCalculator {
28      public static final MetricId METRIC_ID = MetricId.createMethodId(NumberOfStatementsCalculator.class);
29  
30      private Statements statements = new Statements();
31  
32      public void endVisit(final MethodDeclaration arg0) {
33          if (isConcrete(arg0)) {
34              noteMethodValue(NumberOfStatementsCalculator.METRIC_ID, statements.getStatementCount());
35              super.endVisit(arg0);
36          }
37      }
38  
39      protected Object getRestorableState() {
40          return new Statements(statements);
41      }
42  
43      protected void handleNestedClass(final Object inner) {
44          statements.handleInnerClass((Statements) inner);
45      }
46  
47      protected void handleStartOfType() {
48          statements.startType();
49          super.handleStartOfType();
50      }
51  
52      protected void restoreSpecificState(final Object restorableState) {
53          statements = (Statements) restorableState;
54      }
55  
56      public boolean visit(final Assignment node) {
57          statements.increment();
58          return super.visit(node);
59      }
60  
61      public boolean visit(final BreakStatement arg0) {
62          statements.increment();
63          return super.visit(arg0);
64      }
65  
66      public boolean visit(final CatchClause arg0) {
67          statements.increment();
68          return super.visit(arg0);
69      }
70  
71      public boolean visit(final ConstructorInvocation arg0) {
72          statements.increment();
73          return super.visit(arg0);
74      }
75  
76      public boolean visit(final ContinueStatement arg0) {
77          statements.increment();
78          return super.visit(arg0);
79      }
80  
81      public boolean visit(final DoStatement arg0) {
82          statements.increment();
83          return super.visit(arg0);
84      }
85  
86      public boolean visit(final EnhancedForStatement arg0) {
87          statements.increment();
88          return super.visit(arg0);
89      }
90  
91      public boolean visit(final ForStatement arg0) {
92          statements.increment();
93          return super.visit(arg0);
94      }
95  
96      public boolean visit(final IfStatement arg0) {
97          statements.increment();
98          return super.visit(arg0);
99      }
100 
101     public boolean visit(final MethodDeclaration arg0) {
102         if (isConcrete(arg0)) {
103             statements.startMethod();
104             return super.visit(arg0);
105         }
106         return false;
107     }
108 
109     public boolean visit(final MethodInvocation arg0) {
110         statements.increment();
111         return super.visit(arg0);
112     }
113 
114     public boolean visit(final PostfixExpression arg0) {
115         statements.increment();
116         return super.visit(arg0);
117     }
118 
119     public boolean visit(final PrefixExpression arg0) {
120         final PrefixExpression.Operator operator = arg0.getOperator();
121         if (operator.equals(PrefixExpression.Operator.DECREMENT) || operator.equals(PrefixExpression.Operator.INCREMENT)) {
122             statements.increment();
123         }
124         return super.visit(arg0);
125     }
126 
127     public boolean visit(final ReturnStatement arg0) {
128         statements.increment();
129         return super.visit(arg0);
130     }
131 
132     public boolean visit(final SuperConstructorInvocation arg0) {
133         statements.increment();
134         return super.visit(arg0);
135     }
136 
137     public boolean visit(final SwitchCase arg0) {
138         statements.increment();
139         return super.visit(arg0);
140     }
141 
142     public boolean visit(final ThrowStatement arg0) {
143         statements.increment();
144         return super.visit(arg0);
145     }
146 
147     public boolean visit(final TryStatement arg0) {
148         statements.increment();
149         if (arg0.getFinally() != null) {
150             statements.increment();
151         }
152         return super.visit(arg0);
153     }
154 
155     public boolean visit(final VariableDeclarationStatement arg0) {
156         statements.increase(arg0.fragments().size());
157         return super.visit(arg0);
158     }
159 
160     public boolean visit(final WhileStatement arg0) {
161         statements.increment();
162         return super.visit(arg0);
163     }
164 }
165