Test (enabled) & fix for Bug 372391 - [compiler] creating bound role in
field declaration throws NPE on role cache
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/lifting/LiftingEnvironment.java b/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/lifting/LiftingEnvironment.java
index 2644ed6..79b17aa 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/lifting/LiftingEnvironment.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/lifting/LiftingEnvironment.java
@@ -338,7 +338,7 @@
 		    };
 	    }
 	    
-	    AstEdit.addField(teamDecl, field, true, false/*typeProblem*/);
+	    AstEdit.addField(teamDecl, field, true, false/*typeProblem*/, false);
 		teamDecl.getTeamModel().addCache(field);
 	}
 
@@ -468,7 +468,7 @@
 					gen.singleTypeReference("boolean".toCharArray()), //$NON-NLS-1$
 					IOTConstants.CACHE_INIT_TRIGGERER,
 					gen.messageSend(thisReference, IOTConstants.OT_INIT_CACHES, new Expression[0]));
-			AstEdit.addField(teamType, trigger, true, false/*typeProblem*/);
+			AstEdit.addField(teamType, trigger, true, false/*typeProblem*/, true/*addToFront*/); // ensure this field's init is generated first into the constructor
 			trigger.binding.modifiers |= ExtraCompilerModifiers.AccLocallyUsed; //  prevent 'unused' warning;
 
 			// resolve them both:
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/statemachine/copyinheritance/CopyInheritance.java b/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/statemachine/copyinheritance/CopyInheritance.java
index 63af192..a8fa78d 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/statemachine/copyinheritance/CopyInheritance.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/statemachine/copyinheritance/CopyInheritance.java
@@ -1315,7 +1315,7 @@
         AstGenerator gen = new AstGenerator(roleDeclaration.sourceStart, roleDeclaration.sourceEnd);
         FieldDeclaration fieldDeclaration =
                 AstConverter.createField(field, roleDeclaration, gen);
-        AstEdit.addField(roleDeclaration,fieldDeclaration, true, false/*typeProblem*/);
+        AstEdit.addField(roleDeclaration,fieldDeclaration, true, false/*typeProblem*/, false);
         if (fieldDeclaration.binding != null) {
         	fieldDeclaration.binding.modifiers |= ExtraCompilerModifiers.AccLocallyUsed; 
         	fieldDeclaration.binding.modifiers |= (field.modifiers&ExtraCompilerModifiers.AccBlankFinal); // BlankFinal was omitted on the fieldDecl
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/statemachine/transformer/RoleSplitter.java b/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/statemachine/transformer/RoleSplitter.java
index 1054343..769b331 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/statemachine/transformer/RoleSplitter.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/statemachine/transformer/RoleSplitter.java
@@ -151,7 +151,7 @@
                     && (field.modifiers & AccPrivate) == 0)
                 {
                     // move the field:
-					AstEdit.addField(roleIfcDecl, field, false, false/*typeProblem*/);
+					AstEdit.addField(roleIfcDecl, field, false, false/*typeProblem*/, false);
 					AstEdit.removeField(roleClassDecl, field);
 				}
 			}
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/statemachine/transformer/StandardElementGenerator.java b/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/statemachine/transformer/StandardElementGenerator.java
index 76c95fb..ebc0f8a 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/statemachine/transformer/StandardElementGenerator.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/statemachine/transformer/StandardElementGenerator.java
@@ -615,7 +615,7 @@
 											   _OT_BASE,
 											   null);
 		boolean hasTypeProblem = baseclass instanceof MissingTypeBinding;
-		AstEdit.addField(roleType, baseField, createBinding, hasTypeProblem);
+		AstEdit.addField(roleType, baseField, createBinding, hasTypeProblem, false);
 		if (hasTypeProblem) {
 			// faked resolving for missing type
 			baseField.binding.type= baseclass;
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/statemachine/transformer/TeamMethodGenerator.java b/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/statemachine/transformer/TeamMethodGenerator.java
index 956cfc6..b9c49a9 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/statemachine/transformer/TeamMethodGenerator.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/statemachine/transformer/TeamMethodGenerator.java
@@ -339,7 +339,7 @@
     }
 	void addPrivateField(TypeDeclaration teamDecl, AstGenerator gen, TypeReference type, char[] name, Expression init) {
     	FieldDeclaration field = gen.field(AccPrivate, type, name, init);
-    	AstEdit.addField(teamDecl, field, true, false);
+    	AstEdit.addField(teamDecl, field, true, false, false);
     	field.binding.modifiers |= ExtraCompilerModifiers.AccLocallyUsed;     	
     }
 	QualifiedTypeReference weakHashMapTypeReference(AstGenerator gen) {
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/util/AstEdit.java b/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/util/AstEdit.java
index dd364cb..a465945 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/util/AstEdit.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/objectteams/otdt/internal/core/compiler/util/AstEdit.java
@@ -59,8 +59,9 @@
 	 * @param field
 	 * @param createBinding should FieldBinding be created?
 	 * @param hasTypeProblem
+	 * @param addToFront  should the field be inserted at the front of the array?
 	 */
-	public static void addField(TypeDeclaration decl, FieldDeclaration field, boolean createBinding, boolean hasTypeProblem) {
+	public static void addField(TypeDeclaration decl, FieldDeclaration field, boolean createBinding, boolean hasTypeProblem, boolean addToFront) {
         if (field.declarationSourceStart <= 0 ||
                 field.declarationSourceEnd <= 0 ||
                 field.declarationEnd <= 0 ||
@@ -82,11 +83,14 @@
 				fields,
 				0,
 				(fields = new FieldDeclaration[length + 1]),
-				0,
+				addToFront ? 1 : 0,
 				length);
 		}
 
-		fields[length] = field;
+		if (addToFront)
+			fields[0] = field;
+		else
+			fields[length] = field;
 
 		decl.fields = fields;
 		boolean modifiersAdjusted = FieldModel.checkCreateModifiersAttribute(decl, field);
diff --git a/testplugins/org.eclipse.objectteams.otdt.tests/otjld/org/eclipse/objectteams/otdt/tests/otjld/roleplaying/BaseClassVisibility.java b/testplugins/org.eclipse.objectteams.otdt.tests/otjld/org/eclipse/objectteams/otdt/tests/otjld/roleplaying/BaseClassVisibility.java
index d846bfe..503a577 100644
--- a/testplugins/org.eclipse.objectteams.otdt.tests/otjld/org/eclipse/objectteams/otdt/tests/otjld/roleplaying/BaseClassVisibility.java
+++ b/testplugins/org.eclipse.objectteams.otdt.tests/otjld/org/eclipse/objectteams/otdt/tests/otjld/roleplaying/BaseClassVisibility.java
@@ -2198,7 +2198,7 @@
 
     // a team uses a base-imported class as a return type for one of its methods
     // also: Bug 372391 - [compiler] creating bound role in field declaration throws NPE on role cache
-    public void _test247_baseImportScope6() {
+    public void test247_baseImportScope6() {
         runConformTest(
             new String[] {
         "T247bis6Main.java",