Bug 507840 - [1.8][null] @NonNullByDefault is incorrectly applied to
free type variable as method parameter type

Change-Id: Ie380e4ab474b78520ff0e6f055e1cae4cc23de6d
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java
index 4dfa0cc..8ffad27 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java
@@ -13336,6 +13336,40 @@
 		"----------\n"
 	);
 }
+public void testBug507840() {
+	runConformTestWithLibs(
+		new String[] {
+			"nnbd_on_typevar/AtomicReference.java",
+			"package nnbd_on_typevar;\n" +
+			"\n" +
+			"import org.eclipse.jdt.annotation.NonNullByDefault;\n" +
+			"\n" +
+			"@NonNullByDefault\n" +
+			"class AtomicReference<T> {\n" +
+			"	public void set(T t) {\n" +
+			"	}\n" +
+			"}\n" +
+			"",
+		}, 
+		getCompilerOptions(),
+		""
+	);
+	runConformTestWithLibs(
+		new String[] {
+			"nnbd_on_typevar/Usage.java",
+			"package nnbd_on_typevar;\n" +
+			"\n" +
+			"public class Usage {\n" +
+			"	void m(AtomicReference<String> ref) {\n" +
+			"		ref.set(null);\n" +
+			"	}\n" +
+			"}\n" +
+			"",
+		}, 
+		getCompilerOptions(),
+		""
+	);
+}
 public void testBug508497() {
 	runConformTestWithLibs(
 		new String[] {
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/MethodBinding.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/MethodBinding.java
index 891611d..0e2f812 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/MethodBinding.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/MethodBinding.java
@@ -521,12 +521,16 @@
 
 //pre: null annotation analysis is enabled
 protected void fillInDefaultNonNullness18(AbstractMethodDeclaration sourceMethod, LookupEnvironment env) {
+	MethodBinding original = original();
+	if(original == null) {
+		return;
+	}
 	if (hasNonNullDefaultFor(DefaultLocationParameter, true)) {
 		boolean added = false;
 		int length = this.parameters.length;
 		for (int i = 0; i < length; i++) {
 			TypeBinding parameter = this.parameters[i];
-			if (!parameter.acceptsNonNullDefault())
+			if (!original.parameters[i].acceptsNonNullDefault())
 				continue;
 			long existing = parameter.tagBits & TagBits.AnnotationNullMASK;
 			if (existing == 0L) {
@@ -544,7 +548,7 @@
 		if (added)
 			this.tagBits |= TagBits.HasParameterAnnotations;
 	}
-	if (this.returnType != null && hasNonNullDefaultFor(DefaultLocationReturnType, true) && this.returnType.acceptsNonNullDefault()) {
+	if (original.returnType != null && hasNonNullDefaultFor(DefaultLocationReturnType, true) && original.returnType.acceptsNonNullDefault()) {
 		if ((this.returnType.tagBits & TagBits.AnnotationNullMASK) == 0) {
 			this.returnType = env.createAnnotatedType(this.returnType, new AnnotationBinding[]{env.getNonNullAnnotation()});
 		} else if (sourceMethod instanceof MethodDeclaration && (this.returnType.tagBits & TagBits.AnnotationNonNull) != 0