Bug 138952 - Inline Method wrongly qualifies enum consts

- do not qualify enums that are used in switch-case
- do not add import for class that is in current Compilation Unit
- add new tests to InlineMethodTests

Change-Id: I485cc43607eef015e865721d5027b0697866067c
Signed-off-by: Markus Oley <markus.oley@googlemail.com>
Signed-off-by: Jeff Johnston <jjohnstn@redhat.com>
Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.ui/+/70055
Tested-by: JDT Bot <jdt-bot@eclipse.org>
diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/code/SourceProvider.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/code/SourceProvider.java
index 6d6e152..63b388a 100644
--- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/code/SourceProvider.java
+++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/code/SourceProvider.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2021 IBM Corporation and others.
+ * Copyright (c) 2000, 2022 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -23,6 +23,7 @@
 package org.eclipse.jdt.internal.corext.refactoring.code;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -50,6 +51,7 @@
 
 import org.eclipse.jdt.core.ICompilationUnit;
 import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.IType;
 import org.eclipse.jdt.core.ITypeRoot;
 import org.eclipse.jdt.core.JavaModelException;
 import org.eclipse.jdt.core.dom.AST;
@@ -82,6 +84,7 @@
 import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
 import org.eclipse.jdt.core.dom.Statement;
 import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
+import org.eclipse.jdt.core.dom.SwitchCase;
 import org.eclipse.jdt.core.dom.ThisExpression;
 import org.eclipse.jdt.core.dom.WhileStatement;
 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
@@ -509,11 +512,13 @@
 				if (vb.isField()) {
 					Expression receiver= createReceiver(rewriter, context, vb, importRewriteContext);
 					if (receiver != null) {
-						FieldAccess access= node.getAST().newFieldAccess();
-						ASTNode target= rewriter.createMoveTarget(node);
-						access.setName((SimpleName)target);
-						access.setExpression(receiver);
-						rewriter.replace(node, access, null);
+						if (!vb.isEnumConstant() || node.getLocationInParent() != SwitchCase.EXPRESSIONS2_PROPERTY) {
+							FieldAccess access= node.getAST().newFieldAccess();
+							ASTNode target= rewriter.createMoveTarget(node);
+							access.setName((SimpleName)target);
+							access.setExpression(receiver);
+							rewriter.replace(node, access, null);
+						}
 					}
 				}
 			}
@@ -533,9 +538,22 @@
 				if (binding.isParameterizedType()) {
 					binding= binding.getTypeDeclaration();
 				}
-				String s= importer.addImport(binding);
-				if (!ASTNodes.asString(element).equals(s)) {
-					rewriter.replace(element, rewriter.createStringPlaceholder(s, ASTNode.SIMPLE_NAME), null);
+				String[] bindingNameComponents= Bindings.getNameComponents(binding);
+				try {
+					IType[] types= context.compilationUnit.getAllTypes();
+					for (IType type : types) {
+						String typeName= type.getFullyQualifiedName();
+						String[] typeNameComponents= typeName.split("\\.|\\$"); //$NON-NLS-1$
+						if (Arrays.equals(bindingNameComponents, typeNameComponents)) {
+							return;
+						}
+					}
+					String s= importer.addImport(binding);
+					if (!ASTNodes.asString(element).equals(s)) {
+						rewriter.replace(element, rewriter.createStringPlaceholder(s, ASTNode.SIMPLE_NAME), null);
+					}
+				} catch (JavaModelException e) {
+					// do nothing
 				}
 			}
 		}
@@ -574,6 +592,11 @@
 		String receiver= context.receiver;
 		ITypeBinding invocationType= ASTNodes.getEnclosingType(context.invocation);
 		ITypeBinding sourceType= fDeclaration.resolveBinding().getDeclaringClass();
+
+		if (invocationType != null && invocationType.getName().equals(receiver)) {
+			return null;
+		}
+
 		if (!context.receiverIsStatic && Modifier.isStatic(modifiers)) {
 			if ("this".equals(receiver) && invocationType != null && Bindings.equals(invocationType, sourceType)) { //$NON-NLS-1$
 				receiver= null;
diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/InlineMethodWorkspace/TestCases/enum_in/Test_138952.java b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineMethodWorkspace/TestCases/enum_in/Test_138952.java
new file mode 100644
index 0000000..0b531ba
--- /dev/null
+++ b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineMethodWorkspace/TestCases/enum_in/Test_138952.java
@@ -0,0 +1,22 @@
+package enum_in;
+
+class Infomat {
+
+    public enum Fruit { 
+	    APPLE, BANANA, DATE; 
+    }
+	
+    public static String getTaste(Fruit fruit) {
+	    switch(fruit) {
+	        case APPLE	: return "yammy";
+	        case BANANA : return "ok";
+	        case DATE 	: return "very yammy";
+	        default:	return "never eat that one..";
+        }
+    }
+	
+    public String getInstanceTaste(Fruit f) {
+	    return /*]*/Infomat.getTaste(f)/*[*/;
+    }
+
+}
\ No newline at end of file
diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/InlineMethodWorkspace/TestCases/enum_out/Test_138952.java b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineMethodWorkspace/TestCases/enum_out/Test_138952.java
new file mode 100644
index 0000000..5a24663
--- /dev/null
+++ b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineMethodWorkspace/TestCases/enum_out/Test_138952.java
@@ -0,0 +1,27 @@
+package enum_in;
+
+class Infomat {
+
+    public enum Fruit { 
+	    APPLE, BANANA, DATE; 
+    }
+	
+    public static String getTaste(Fruit fruit) {
+	    switch(fruit) {
+	        case APPLE	: return "yammy";
+	        case BANANA : return "ok";
+	        case DATE 	: return "very yammy";
+	        default:	return "never eat that one..";
+        }
+    }
+	
+    public String getInstanceTaste(Fruit f) {
+	    switch(f) {
+		    case APPLE	: return "yammy";
+		    case BANANA : return "ok";
+		    case DATE 	: return "very yammy";
+		    default:	return "never eat that one..";
+		}
+    }
+
+}
\ No newline at end of file
diff --git a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineMethodTests.java b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineMethodTests.java
index 7dd3631..b182775 100644
--- a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineMethodTests.java
+++ b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineMethodTests.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2020 IBM Corporation and others.
+ * Copyright (c) 2000, 2022 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -1071,6 +1071,11 @@
 		performEnumTest();
 	}
 
+	@Test
+	public void test_138952() throws Exception {
+		performEnumTest();
+	}
+
 	/* *********************** Generic Tests ******************************* */
 
 	private void performGenericTest() throws Exception {