Fixed bug 476859 enclosing method not found error when EJC compiled,
works fine with oracle jdk compiler

Change-Id: I16c0bffd9156efa0c10f105df46127a3cc8da6a5
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/LambdaExpressionsTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/LambdaExpressionsTest.java
index c33071f..98d0c5f 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/LambdaExpressionsTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/LambdaExpressionsTest.java
@@ -6132,6 +6132,56 @@
 	},
 	"0");
 }
+// https://bugs.eclipse.org/bugs/show_bug.cgi?id=476859 enclosing method not found error when EJC compiled, works fine with oracle jdk compiler
+public void test476859() {
+	this.runConformTest(
+		new String[] {
+			"Test.java",
+			"import java.lang.reflect.Method;\n" + 
+			"import java.util.function.Function;\n" + 
+			"public class Test {\n" + 
+			"  public static void main(String[] args) {\n" + 
+			"    final Function<Void,Method> f = __ -> {\n" + 
+			"    	class Dummy{}\n" + 
+			"      return new Dummy(){}.getClass().getEnclosingMethod();\n" + 
+			"    };\n" + 
+			"    System.out.println(f.apply(null));\n" + 
+			"  }\n" + 
+			"}"
+	},
+	"private static java.lang.reflect.Method Test.lambda$0(java.lang.Void)");
+}
+// https://bugs.eclipse.org/bugs/show_bug.cgi?id=476859 enclosing method not found error when EJC compiled, works fine with oracle jdk compiler
+public void test476859a() {
+	this.runConformTest(
+		new String[] {
+			"Test.java",
+			"import java.lang.reflect.Method;\n" + 
+			"import java.util.function.Function;\n" + 
+			"public class Test {\n" + 
+			"  public static void main(String[] args) {\n" + 
+			"	  \n" + 
+			"    final Function<Void,Method> f = __ -> {\n" + 
+			"    	class Dummy{}\n" + 
+			"      return new Dummy(){}.getClass().getEnclosingMethod();\n" + 
+			"    };\n" + 
+			"    System.out.println(f.apply(null));\n" + 
+			"    new AnotherClass().foo();\n" + 
+			"  }\n" + 
+			"}\n" + 
+			"class AnotherClass {\n" + 
+			"	void foo() {\n" + 
+			"		final Function<Void,Method> f = __ -> {\n" + 
+			"	    	class Dummy{}\n" + 
+			"	      return new Dummy(){}.getClass().getEnclosingMethod();\n" + 
+			"	    };\n" + 
+			"	    System.out.println(f.apply(null));\n" + 
+			"	}\n" + 
+			"}\n"
+	},
+	"private static java.lang.reflect.Method Test.lambda$0(java.lang.Void)\n" +
+	"private java.lang.reflect.Method AnotherClass.lambda$0(java.lang.Void)");
+}
 public static Class testClass() {
 	return LambdaExpressionsTest.class;
 }
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CompilationUnitDeclaration.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CompilationUnitDeclaration.java
index 8f9f460..895ea19 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CompilationUnitDeclaration.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CompilationUnitDeclaration.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2014 IBM Corporation and others.
+ * Copyright (c) 2000, 2016 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -545,7 +545,7 @@
 
 /*
  * Keep track of all lambda/method reference expressions, so as to be able to look it up later without 
- * having to traverse AST. Return the 1 based "ordinal" in the CUD.
+ * having to traverse AST. Return the "ordinal" returned by the enclosing type.
  */
 public int record(FunctionalExpression expression) {
 	if (this.functionalExpressionsCount == 0) {
@@ -553,8 +553,8 @@
 	} else if (this.functionalExpressionsCount == this.functionalExpressions.length) {
 		System.arraycopy(this.functionalExpressions, 0, (this.functionalExpressions = new FunctionalExpression[this.functionalExpressionsCount * 2]), 0, this.functionalExpressionsCount);
 	}
-	this.functionalExpressions[this.functionalExpressionsCount] = expression;
-	return ++this.functionalExpressionsCount;
+	this.functionalExpressions[this.functionalExpressionsCount++] = expression;
+	return expression.enclosingScope.classScope().referenceContext.record(expression);
 }
 
 public void resolve() {
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TypeDeclaration.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TypeDeclaration.java
index a254f4f..30b3d11 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TypeDeclaration.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TypeDeclaration.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2015 IBM Corporation and others.
+ * Copyright (c) 2000, 2016 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -38,6 +38,7 @@
 
 	public int modifiers = ClassFileConstants.AccDefault;
 	public int modifiersSourceStart;
+	public int functionalExpressionsCount = 0;
 	public Annotation[] annotations;
 	public char[] name;
 	public TypeReference superclass;
@@ -989,7 +990,13 @@
 	return print(tab, output);
 }
 
-
+/*
+ * Keep track of number of lambda/method reference expressions in this type declaration.
+ * Return the 0 based "ordinal" in the TypeDeclaration.
+ */
+public int record(FunctionalExpression expression) {
+	return this.functionalExpressionsCount++;
+}
 
 public void resolve() {
 	SourceTypeBinding sourceType = this.binding;