Bug 506747: NullPointerException in StateHelper.getState
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java
index 9046895..401d09e 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java
@@ -168,6 +168,9 @@
 	this.compatibleCache = prototype.compatibleCache;
 	this.typeBits = prototype.typeBits;
 	this.singleAbstractMethod = prototype.singleAbstractMethod;
+//{ObjectTeams: most subclasses need model to be set:
+	this.model = new TypeModel(prototype.model, this);
+// SH}
 }
 
 public ReferenceBinding() {
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/model/TypeModel.java b/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/model/TypeModel.java
index 5580844..0dda30c 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/model/TypeModel.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/model/TypeModel.java
@@ -75,10 +75,20 @@
     	{
     		throw new InternalCompilerError("Unexpectedly missing line number information");  //$NON-NLS-1$
     	}
+    	this._state = new StateMemento();
     }
     /* Constructor for regular instances. */
     public TypeModel (ReferenceBinding bind) {
         this._binding = bind;
+    	this._state = new StateMemento();
+    }
+    /** clone constructor: */
+    public TypeModel (TypeModel prototype, ReferenceBinding protoBinding) {
+    	this._binding = protoBinding;
+    	this._state = prototype._state;
+    	this._compilerVersion = prototype._compilerVersion;
+    	this.weavingScheme = prototype.weavingScheme;
+    	this._ast = prototype._ast;
     }
 
     /** Store the byte code attribute which maps constant pool entries (Class_info) to type anchors. */
@@ -95,7 +105,7 @@
     private HashSet<ReferenceBinding> _referencedTeams = null;
 
     /** translation state of this model element */
-    final public StateMemento _state= new StateMemento();
+    final public StateMemento _state;
 
     /** Record here all types which are defined local to a method (TypeDeclaration). */
     private LinkedList<TypeDeclaration> _localPlainTypes = new LinkedList<TypeDeclaration>();
diff --git a/testplugins/org.eclipse.objectteams.otdt.tests/otjld/org/eclipse/objectteams/otdt/tests/otjld/other/Java8.java b/testplugins/org.eclipse.objectteams.otdt.tests/otjld/org/eclipse/objectteams/otdt/tests/otjld/other/Java8.java
index cec4af7..b3fade6 100644
--- a/testplugins/org.eclipse.objectteams.otdt.tests/otjld/org/eclipse/objectteams/otdt/tests/otjld/other/Java8.java
+++ b/testplugins/org.eclipse.objectteams.otdt.tests/otjld/org/eclipse/objectteams/otdt/tests/otjld/other/Java8.java
@@ -19,6 +19,7 @@
 
 import junit.framework.Test;
 
+import org.eclipse.jdt.core.JavaCore;
 import org.eclipse.jdt.core.tests.util.CompilerTestSetup;
 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
 import org.eclipse.objectteams.otdt.core.ext.WeavingScheme;
@@ -387,4 +388,31 @@
     		},
     		"0b1b2b3b4");
     }
+    
+    public void testBug506747() {
+    	Map<String,String> options = getCompilerOptions();
+    	options.put(JavaCore.COMPILER_ANNOTATION_NULL_ANALYSIS, JavaCore.ENABLED); // enable AnnotatableTypeSystem
+    	runNegativeTest(
+    		new String[] {
+    			"p/X.java",
+    			"package p;\n" +
+    			"import java.lang.annotation.*;\n" +
+    			"@Target(ElementType.TYPE_USE)\n" +
+    			"@interface Ann {}\n" +
+    			"public class X {\n" +
+    			"	void m(final @Ann X X) {\n" +
+    			"		X.Inner i;\n" +
+    			"	}\n" +
+    			"}\n"
+    		},
+    		"----------\n" + 
+			"1. ERROR in p\\X.java (at line 7)\n" + 
+			"	X.Inner i;\n" + 
+			"	^^^^^^^\n" + 
+			"X.Inner cannot be resolved to a type\n" + 
+			"----------\n",
+			null, // libs
+			true, // flush
+    		options);
+    }
 }