diff options
| author | Jeff Johnston | 2021-06-18 19:35:46 +0000 |
|---|---|---|
| committer | Jeff Johnston | 2021-06-23 21:31:43 +0000 |
| commit | fd3b7a4a3e046735d4b0453ecc6b2fc8cf91e82f (patch) | |
| tree | 6d4c4f2df8889a0054d94693f0bf441a4d87f56d | |
| parent | 11a52d88777f364e70e7fce5d575ef61ddf38bae (diff) | |
| download | eclipse.jdt.ui-fd3b7a4a3e046735d4b0453ecc6b2fc8cf91e82f.tar.gz eclipse.jdt.ui-fd3b7a4a3e046735d4b0453ecc6b2fc8cf91e82f.tar.xz eclipse.jdt.ui-fd3b7a4a3e046735d4b0453ecc6b2fc8cf91e82f.zip | |
Bug 221003 - [inline] inline constant removes needed static importI20210624-1210
- modify InlineConstantRefactoring.InitializerTraversal
areInSameType() method to look for a SimpleName or
MethodInvocation being passed in and use bindings to find its
real declaring class as opposed to walking up the parent nodes
- add new tests to InlineConstantTests
Change-Id: I51ac057b450cb6c24ae14ae1f74b3594bc0bf17c
Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.ui/+/182194
Tested-by: JDT Bot <jdt-bot@eclipse.org>
Tested-by: Jeff Johnston <jjohnstn@redhat.com>
Reviewed-by: Jeff Johnston <jjohnstn@redhat.com>
10 files changed, 107 insertions, 2 deletions
diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/code/InlineConstantRefactoring.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/code/InlineConstantRefactoring.java index 70ac4e6a5e..d9097b3f9c 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/code/InlineConstantRefactoring.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/code/InlineConstantRefactoring.java @@ -150,9 +150,26 @@ public class InlineConstantRefactoring extends Refactoring { return false; ITypeBinding onesContainerBinding= getTypeBindingForTypeDeclaration(onesContainer); + if (one instanceof SimpleName) { + IBinding oneTypeBinding= ((SimpleName)one).resolveBinding(); + if (oneTypeBinding instanceof IVariableBinding) { + ITypeBinding onesDeclaringClassBinding= ((IVariableBinding)oneTypeBinding).getDeclaringClass(); + if (onesDeclaringClassBinding != null) { + onesContainerBinding= onesDeclaringClassBinding; + } + } + } else if (one instanceof MethodInvocation) { + IMethodBinding oneMethodBinding= ((MethodInvocation)one).resolveMethodBinding(); + if (oneMethodBinding != null) { + ITypeBinding onesDeclaringClassBinding= oneMethodBinding.getDeclaringClass(); + if (onesDeclaringClassBinding != null) { + onesContainerBinding= onesDeclaringClassBinding; + } + } + } ITypeBinding othersContainerBinding= getTypeBindingForTypeDeclaration(othersContainer); - Assert.isNotNull(onesContainerBinding); + Assert.isNotNull(othersContainerBinding); String onesKey= onesContainerBinding.getKey(); diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test44/in/A.java b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test44/in/A.java new file mode 100644 index 0000000000..c346644abe --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test44/in/A.java @@ -0,0 +1,8 @@ +package p1; + +public class A { + + public static String version() { + return "3"; + } +}
\ No newline at end of file diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test44/in/B.java b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test44/in/B.java new file mode 100644 index 0000000000..c27c6ebddc --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test44/in/B.java @@ -0,0 +1,13 @@ +// 8,33 -> 8,40 replaceAll == true +package p1; + +import static p1.A.version; + +public class B { + + private static final String version = version(); + + private void test() { + String copy = version; + } +}
\ No newline at end of file diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test44/out/A.java b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test44/out/A.java new file mode 100644 index 0000000000..c346644abe --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test44/out/A.java @@ -0,0 +1,8 @@ +package p1; + +public class A { + + public static String version() { + return "3"; + } +}
\ No newline at end of file diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test44/out/B.java b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test44/out/B.java new file mode 100644 index 0000000000..bd3e66ab32 --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test44/out/B.java @@ -0,0 +1,11 @@ +// 8,33 -> 8,40 replaceAll == true +package p1; + +import static p1.A.version; + +public class B { + + private void test() { + String copy = version(); + } +}
\ No newline at end of file diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test45/in/A.java b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test45/in/A.java new file mode 100644 index 0000000000..e18ece8a39 --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test45/in/A.java @@ -0,0 +1,7 @@ +package p1; + +public class A { + + public static String version2; + +}
\ No newline at end of file diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test45/in/B.java b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test45/in/B.java new file mode 100644 index 0000000000..1168ffdcb0 --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test45/in/B.java @@ -0,0 +1,13 @@ +// 8,33 -> 8,40 replaceAll == true +package p1; + +import static p1.A.version2; + +public class B { + + private static final String version = version2; + + private void test() { + String copy = version; + } +}
\ No newline at end of file diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test45/out/A.java b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test45/out/A.java new file mode 100644 index 0000000000..e18ece8a39 --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test45/out/A.java @@ -0,0 +1,7 @@ +package p1; + +public class A { + + public static String version2; + +}
\ No newline at end of file diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test45/out/B.java b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test45/out/B.java new file mode 100644 index 0000000000..52abeda8ac --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineConstant/canInline/test45/out/B.java @@ -0,0 +1,11 @@ +// 8,33 -> 8,40 replaceAll == true +package p1; + +import static p1.A.version2; + +public class B { + + private void test() { + String copy = version2; + } +}
\ No newline at end of file diff --git a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineConstantTests.java b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineConstantTests.java index b85e0533c4..d861771972 100644 --- a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineConstantTests.java +++ b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineConstantTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2020 IBM Corporation and others. + * Copyright (c) 2000, 2021 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -389,6 +389,16 @@ public class InlineConstantTests extends GenericRefactoringTest { helper1("p.A", 4, 39, 4, 40, true, true); } + @Test + public void test44() throws Exception { // test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=221003 + helper1(new String[] {"p1.A", "p2.B"}, "p2.B", 8, 33, 8, 40, true, true); + } + + @Test + public void test45() throws Exception { // test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=221003 + helper1(new String[] {"p1.A", "p2.B"}, "p2.B", 8, 33, 8, 40, true, true); + } + // -- testing failing preconditions @Test |
