1   package com.stateofflow.eclipse.metrics.type;
2   
3   import org.eclipse.jdt.core.dom.ASTNode;
4   import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
5   import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
6   import org.eclipse.jdt.core.dom.EnumDeclaration;
7   import org.eclipse.jdt.core.dom.ITypeBinding;
8   import org.eclipse.jdt.core.dom.TypeDeclaration;
9   
10  public abstract class NonNullType extends Type {
11      private final ASTNode node;
12  
13      public NonNullType(final ASTNode node) {
14          this.node = node;
15      }
16  
17      public Type create(final ASTNode node) {
18          return node == null ? new NullType() : createForNonNull(node);
19      }
20  
21      private Type createForNonNull(final ASTNode node) {
22          switch (node.getNodeType()) {
23              case ASTNode.ANONYMOUS_CLASS_DECLARATION :
24                  return new AnonymousClassDeclarationAdapter((AnonymousClassDeclaration) node);
25              case ASTNode.ENUM_DECLARATION :
26                  return new EnumDeclarationAdapter((EnumDeclaration) node);
27              case ASTNode.ENUM_CONSTANT_DECLARATION :
28                  return new EnumConstantDeclarationAdapter((EnumConstantDeclaration) node);
29              case ASTNode.TYPE_DECLARATION :
30                  return new TypeDeclarationAdapter((TypeDeclaration) node);
31              default :
32                  return createParent(node);
33          }
34      }
35  
36      private Type createParent(final ASTNode node) {
37          return create(node.getParent());
38      }
39  
40      public final String getName() {
41          return getNameWithParentNamePrepended(createParent(node));
42      }
43  
44      protected abstract String getNamePart();
45  
46      protected String getNameWithParentNamePrepended(final Type parent) {
47          return parent.getNameWithChildNameAppended(this);
48      }
49  
50      public final ASTNode getNode() {
51          return node;
52      }
53  
54      protected String getPrefix() {
55          return ".";
56      }
57  
58      public abstract int getStartPosition();
59  
60      public abstract ITypeBinding resolveBinding();
61  }
62