1   package com.stateofflow.eclipse.metrics.calculators;
2   
3   import org.eclipse.jdt.core.dom.FieldDeclaration;
4   import org.eclipse.jdt.core.dom.Modifier;
5   
6   import com.stateofflow.eclipse.metrics.metric.MetricId;
7   
8   public final class NumberOfFieldsCalculator extends AbstractASTVisitorCalculator {
9       public static final MetricId METRIC_ID = MetricId.createTypeId(NumberOfFieldsCalculator.class);
10  
11      private int numberOfFields;
12  
13      protected Object getRestorableState() {
14          return new Integer(numberOfFields);
15      }
16  
17      protected void handleEndOfType() {
18          noteTypeValue(NumberOfFieldsCalculator.METRIC_ID, numberOfFields);
19          super.handleEndOfType();
20      }
21  
22      protected void handleStartOfType() {
23          numberOfFields = 0;
24          super.handleStartOfType();
25      }
26  
27      protected void restoreSpecificState(final Object restorableState) {
28          numberOfFields = ((Integer) restorableState).intValue();
29      }
30  
31      public boolean visit(final FieldDeclaration arg0) {
32          if ((arg0.getModifiers() & Modifier.STATIC) == 0) {
33              numberOfFields++;
34          }
35  
36          return true;
37      }
38  }
39