Bug 541630 - Quick assist for static import removes non static import
statements when it should not

MethodInvocations on ClassInstanceCreations weren't considered.

Change-Id: I79dc099aa67702a94fedbbf21b0cf17b7100bf0e
Signed-off-by: Fabian Pfaff <fabian.pfaff@vogella.com>
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest.java
index 86a880a..bca39f2 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest.java
@@ -10591,6 +10591,72 @@
 		assertProposalDoesNotExist(proposals, "Convert to static import (replace all occurrences)");
 	}
 
+	public void testDoesntRemoveImportWithReferenceFromClassInstanceCreation() throws Exception {
+		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
+		StringBuffer buf= new StringBuffer();
+		buf.append("package test1;\n");
+		buf.append("public class T {\n");
+		buf.append("    public static void foo() { };\n");
+		buf.append("    public void bar() { };\n");
+		buf.append("}\n");
+		pack1.createCompilationUnit("T.java", buf.toString(), false, null);
+
+		IPackageFragment pack2= fSourceFolder.createPackageFragment("test2", false, null);
+		buf= new StringBuffer();
+		buf.append("package test2;\n");
+		buf.append("\n");
+		buf.append("import test1.T;\n");
+		buf.append("public class S {\n");
+		buf.append("    public S() {\n");
+		buf.append("        T.foo();\n");
+		buf.append("        T.foo();\n");
+		buf.append("        new T().foo();\n");
+		buf.append("        new T().bar();\n");
+		buf.append("    }\n");
+		buf.append("}\n");
+		ICompilationUnit cu= pack2.createCompilationUnit("S.java", buf.toString(), false, null);
+
+		String selection= "foo";
+		int offset= buf.toString().indexOf(selection);
+		AssistContext context= getCorrectionContext(cu, offset, selection.length());
+		ArrayList<IJavaCompletionProposal> proposals= collectAssists(context, false);
+
+		assertNumberOfProposals(proposals, 2);
+		assertCorrectLabels(proposals);
+
+		StringBuffer expectation= new StringBuffer();
+		expectation.append("package test2;\n");
+		expectation.append("\n");
+		expectation.append("import static test1.T.foo;\n");
+		expectation.append("\n");
+		expectation.append("import test1.T;\n");
+		expectation.append("public class S {\n");
+		expectation.append("    public S() {\n");
+		expectation.append("        foo();\n");
+		expectation.append("        T.foo();\n");
+		expectation.append("        new T().foo();\n");
+		expectation.append("        new T().bar();\n");
+		expectation.append("    }\n");
+		expectation.append("}\n");
+		assertProposalPreviewEquals(expectation.toString(), "Convert to static import", proposals);
+
+		expectation= new StringBuffer();
+		expectation.append("package test2;\n");
+		expectation.append("\n");
+		expectation.append("import static test1.T.foo;\n");
+		expectation.append("\n");
+		expectation.append("import test1.T;\n");
+		expectation.append("public class S {\n");
+		expectation.append("    public S() {\n");
+		expectation.append("        foo();\n");
+		expectation.append("        foo();\n");
+		expectation.append("        new T().foo();\n");
+		expectation.append("        new T().bar();\n");
+		expectation.append("    }\n");
+		expectation.append("}\n");
+		assertProposalPreviewEquals(expectation.toString(), "Convert to static import (replace all occurrences)", proposals);
+	}
+
 	public void testCreateJUnitTestCase() throws Exception {
 		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
 		StringBuffer buf= new StringBuffer();
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java
index 858257d..b694135 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java
@@ -4143,6 +4143,7 @@
 					if (methodInvocationExpression == null) {
 						return super.visit(methodInvocation);
 					}
+
 					if (methodInvocationExpression instanceof Name) {
 						String fullyQualifiedName= ((Name) methodInvocationExpression).getFullyQualifiedName();
 						if (miFinal != null &&
@@ -4155,7 +4156,17 @@
 							allReferencesToDeclaringClass[0]++;
 							referencesFromOtherOccurences[0]++;
 						}
+					} else if (methodInvocationExpression instanceof ClassInstanceCreation) {
+						ClassInstanceCreation classInstanceCreation= (ClassInstanceCreation) methodInvocationExpression;
+						if (classInstanceCreation.getType() instanceof SimpleType) {
+							String typeName= ((SimpleType) classInstanceCreation.getType()).getName().getFullyQualifiedName();
+							if (typeName.equals(declaringClass.getName())) {
+								allReferencesToDeclaringClass[0]++;
+								referencesFromOtherOccurences[0]++;
+							}
+						}
 					}
+
 					return super.visit(methodInvocation);
 				}
 			});