master - Fix for bug 351697: ClassCastException while copying a .class file
to wrong source package
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaProjectTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaProjectTests.java
index a4da2e3..b5e07e9 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaProjectTests.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaProjectTests.java
@@ -27,6 +27,9 @@
 import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
 import org.eclipse.jdt.core.*;
+import org.eclipse.jdt.core.dom.AST;
+import org.eclipse.jdt.core.dom.ASTNode;
+import org.eclipse.jdt.core.dom.ASTParser;
 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
 import org.eclipse.jdt.internal.core.JavaModelManager;
 import org.eclipse.jdt.internal.core.PackageFragmentRoot;
@@ -37,7 +40,7 @@
 	super(name);
 }
 static {
-//	TESTS_NAMES = new String[] { "testBug360164" };
+//	TESTS_NAMES = new String[] { "testBug351697" };
 }
 public static Test suite() {
 	TestSuite suite = (TestSuite) buildModelTestSuite(JavaProjectTests.class);
@@ -2530,4 +2533,54 @@
 		this.deleteProject("P");
 	}
 }
+/**
+ * @bug 351697: java.lang.ClassCastException
+ * @test Verify that ClassCastException is not thrown when a .class file is copied to a wrong source package.
+ * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=351697"
+ */
+public void testBug351697() throws Exception {
+	try {
+		IJavaProject proj = this.createJavaProject("P", new String[] {"src"}, "bin");
+		proj.getProject().open(null);
+		createFolder("/P/src/p");
+		createFolder("/P/src/q");
+		createFolder("/P/temp_folder");
+
+		IFile toEdit = createFile("/P/src/p/P.java",
+				"package p;" +
+				"public class P {" +
+				"	Q b = new Q();" +
+				"	public void foo() {" +
+				"	}");
+
+		IFile toDelete = createFile("/P/src/q/Q.java",
+				"package q;" +
+				"public class Q {" +
+				"}");
+		proj.getProject().build(IncrementalProjectBuilder.FULL_BUILD, null);
+
+		moveFile("/P/bin/q/Q.class", "/P/temp_folder/Q.class");
+		deleteResource(toDelete);
+		proj.getProject().build(IncrementalProjectBuilder.FULL_BUILD, null);
+
+		ICompilationUnit unit = (ICompilationUnit)JavaCore.create(toEdit);
+		unit.open(null);
+		moveFile( "/P/temp_folder/Q.class", "/P/src/p/Q.class/");
+		proj.getProject().build(IncrementalProjectBuilder.FULL_BUILD, null);
+
+		try {
+			ASTParser parser= ASTParser.newParser(AST.JLS4);
+			parser.setSource(unit);
+			parser.setResolveBindings(true);
+			ASTNode node = parser.createAST(null);
+			assertNotNull("ASTNode should not be null", node);
+		}
+		catch(ClassCastException cce) {
+			fail("ClassCastException:" + cce.getMessage());
+		}
+
+	} finally {
+		this.deleteProject("P");
+	}
+}
 }
diff --git a/org.eclipse.jdt.core/buildnotes_jdt-core.html b/org.eclipse.jdt.core/buildnotes_jdt-core.html
index 5a05863..0a53703 100644
--- a/org.eclipse.jdt.core/buildnotes_jdt-core.html
+++ b/org.eclipse.jdt.core/buildnotes_jdt-core.html
@@ -52,7 +52,9 @@
 <h2>What's new in this drop</h2>
 
 <h3>Problem Reports Fixed</h3>
-<a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=375394">375394</a>
+<a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=351697">351697</a>
+ClassCastException while copying a .class file to wrong source package
+<br><a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=375394">375394</a>
 Incorrect type checking for parameterized types
 <br><a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=375326">375326</a>
 try-with-resources assignment in statement produces stack map exception
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/NameLookup.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/NameLookup.java
index d08dff5..ebfb67b 100644
--- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/NameLookup.java
+++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/NameLookup.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2011 IBM Corporation and others.
+ * Copyright (c) 2000, 2012 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
@@ -1087,6 +1087,11 @@
 						int lastDot = cuName.lastIndexOf('.');
 						if (lastDot != topLevelTypeName.length() || !topLevelTypeName.regionMatches(0, cuName, 0, lastDot))
 							continue;
+
+						// https://bugs.eclipse.org/bugs/show_bug.cgi?id=351697
+						// If we are looking at source location, just ignore binary types
+						if (!(cu instanceof ICompilationUnit))
+							continue;
 						IType type = ((ICompilationUnit) cu).getType(topLevelTypeName);
 						type = getMemberType(type, name, firstDot);
 						if (acceptType(type, acceptFlags, true/*a source type*/)) { // accept type checks for existence