1   package com.stateofflow.eclipse.metrics.calculators.cohesion;
2   
3   import org.eclipse.jdt.core.Flags;
4   import org.eclipse.jdt.core.IField;
5   import org.eclipse.jdt.core.IJavaElement;
6   import org.eclipse.jdt.core.JavaModelException;
7   
8   final class DeclaredFieldResultCollector extends FieldResultCollector {
9       public DeclaredFieldResultCollector(final String source) {
10          super(source);
11      }
12  
13      public boolean hasFieldInEnclosingType(final IJavaElement enclosingElement, final int start, final int end) {
14          return hasField(enclosingElement.getAncestor(IJavaElement.TYPE), start, end);
15      }
16  
17      protected void acceptSearchMatch(final IJavaElement enclosingElement, final int start, final int end) throws JavaModelException {
18          // TODO Ignore problems with anonymous/local classes for now
19          if (isNamedInstanceField(enclosingElement, getSource().getFieldName(start, end))) {
20              addField(enclosingElement.getAncestor(IJavaElement.TYPE), start, end);
21          }
22      }
23  
24      private boolean isNamedInstanceField(final IJavaElement enclosingElement, final String fieldName) throws JavaModelException {
25          return enclosingElement.getElementType() == IJavaElement.FIELD && fieldName.equals(enclosingElement.getElementName()) && !Flags.isStatic(((IField) enclosingElement).getFlags());
26      }
27  }
28