Fixed bug 495424: Organize imports no longer fixes problems caused by
'moved' types

Change-Id: I14eb413d861462348c498a947acbc859388765df
diff --git a/org.eclipse.jdt.ui.tests/META-INF/MANIFEST.MF b/org.eclipse.jdt.ui.tests/META-INF/MANIFEST.MF
index 9d7e4cf..6e50553 100644
--- a/org.eclipse.jdt.ui.tests/META-INF/MANIFEST.MF
+++ b/org.eclipse.jdt.ui.tests/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@
 Bundle-ManifestVersion: 2
 Bundle-Name: %Plugin.name
 Bundle-SymbolicName: org.eclipse.jdt.ui.tests; singleton:=true
-Bundle-Version: 3.12.0.qualifier
+Bundle-Version: 3.12.1.qualifier
 Bundle-Activator: org.eclipse.jdt.testplugin.JavaTestPlugin
 Bundle-ActivationPolicy: lazy
 Bundle-Vendor: %Plugin.providerName
diff --git a/org.eclipse.jdt.ui.tests/pom.xml b/org.eclipse.jdt.ui.tests/pom.xml
index b98fad1..2dd925b 100644
--- a/org.eclipse.jdt.ui.tests/pom.xml
+++ b/org.eclipse.jdt.ui.tests/pom.xml
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!--
-  Copyright (c) 2012, 2014 Eclipse Foundation and others.
+  Copyright (c) 2012, 2016 Eclipse Foundation and others.
   All rights reserved. This program and the accompanying materials
   are made available under the terms of the Eclipse Distribution License v1.0
   which accompanies this distribution, and is available at
@@ -20,7 +20,7 @@
   </parent>
   <groupId>org.eclipse.jdt</groupId>
   <artifactId>org.eclipse.jdt.ui.tests</artifactId>
-  <version>3.12.0-SNAPSHOT</version>
+  <version>3.12.1-SNAPSHOT</version>
   <packaging>eclipse-test-plugin</packaging>
   <properties>
     <defaultSigning-excludeInnerJars>true</defaultSigning-excludeInnerJars>
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/core/ImportOrganizeTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/core/ImportOrganizeTest.java
index ca615a3..3765721 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/core/ImportOrganizeTest.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/core/ImportOrganizeTest.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
@@ -3388,12 +3388,12 @@
 
 		createOperation(cu, new String[] {}, 99, true, true, true, null).run(null);
 
-		// FromEitherPackage could be imported from com.notimported, but the existing
-		// (though unresolvable) import from com.notfound is preserved instead.
+		// FromEitherPackage is imported from com.notimported, instead of preserving the existing
+		// unresolvable import from com.notfound.
 		StringBuilder expected= new StringBuilder();
 		expected.append("package pack;\n");
 		expected.append("\n");
-		expected.append("import com.notfound.FromEitherPackage;\n");
+		expected.append("import com.notimported.FromEitherPackage;\n");
 		expected.append("import com.notimported.FromNotImportedOnly;\n");
 		expected.append("\n");
 		expected.append("public class Cu {\n");
diff --git a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/OrganizeImportsOperation.java b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/OrganizeImportsOperation.java
index 4811299..fec6543 100644
--- a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/OrganizeImportsOperation.java
+++ b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/OrganizeImportsOperation.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
@@ -19,6 +19,7 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Set;
 
 import org.eclipse.core.runtime.CoreException;
@@ -326,18 +327,8 @@
 				}
 			}
 
-			Set<String> matchingUnresolvableImports= fUnresolvableImportMatcher.matchTypeImports(typeName);
-			if (!matchingUnresolvableImports.isEmpty()) {
-				// If there are matching unresolvable import(s), rely on them to provide the type.
-				fImportsAdded.add(typeName);
-				for (String string : matchingUnresolvableImports) {
-					fImpStructure.addImport(string, UNRESOLVABLE_IMPORT_CONTEXT);
-				}
-			} else {
-				// Only resort to search results if there are no matching unresolvable imports.
-				fImportsAdded.add(typeName);
-				fUnresolvedTypes.put(typeName, new UnresolvedTypeData(ref));
-			}
+			fImportsAdded.add(typeName);
+			fUnresolvedTypes.put(typeName, new UnresolvedTypeData(ref));
 		}
 
 		public boolean process(IProgressMonitor monitor) throws JavaModelException {
@@ -369,6 +360,18 @@
 					}
 				}
 
+				for (Entry<String, UnresolvedTypeData> entry : fUnresolvedTypes.entrySet()) {
+					if (entry.getValue().foundInfos.size() == 0) { // No result found in search
+						Set<String> matchingUnresolvableImports= fUnresolvableImportMatcher.matchTypeImports(entry.getKey());
+						if (!matchingUnresolvableImports.isEmpty()) {
+							// If there are matching unresolvable import(s), rely on them to provide the type.
+							for (String string : matchingUnresolvableImports) {
+								fImpStructure.addImport(string, UNRESOLVABLE_IMPORT_CONTEXT);
+							}
+						}
+					}
+				}
+
 				ArrayList<TypeNameMatch[]> openChoices= new ArrayList<>(nUnresolved);
 				ArrayList<SourceRange> sourceRanges= new ArrayList<>(nUnresolved);
 				for (Iterator<UnresolvedTypeData> iter= fUnresolvedTypes.values().iterator(); iter.hasNext();) {
@@ -590,7 +593,18 @@
 				}
 				for (int i= 0; i < chosen.length; i++) {
 					TypeNameMatch typeInfo= chosen[i];
-					importsRewrite.addImport(typeInfo.getFullyQualifiedName());
+					if (typeInfo != null) {
+						importsRewrite.addImport(typeInfo.getFullyQualifiedName());
+					} else { // Skipped by user
+						String typeName= choices[i][0].getSimpleTypeName();
+						Set<String> matchingUnresolvableImports= unresolvableImportMatcher.matchTypeImports(typeName);
+						if (!matchingUnresolvableImports.isEmpty()) {
+							// If there are matching unresolvable import(s), rely on them to provide the type.
+							for (String string : matchingUnresolvableImports) {
+								importsRewrite.addImport(string, UNRESOLVABLE_IMPORT_CONTEXT);
+							}
+						}
+					}
 				}
 			}