Bug 579044 - Use declaring class as qualifier cleanup fails
- fix CodeStyleFixCore.createToStaticAccessOperations() method
to not change the static reference if the declaring class is
package-private
- add new test to CleanUpTest
Change-Id: I88405d967f3deb1969bf5655990c15ac382ca84e
Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.ui/+/191394
Tested-by: JDT Bot <jdt-bot@eclipse.org>
Tested-by: Jeff Johnston <jjohnstn@redhat.com>
Reviewed-by: Jeff Johnston <jjohnstn@redhat.com>
diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/CodeStyleFixCore.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/CodeStyleFixCore.java
index de3d50a..66529a1 100644
--- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/CodeStyleFixCore.java
+++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/CodeStyleFixCore.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2019 IBM Corporation and others.
+ * Copyright (c) 2019, 2022 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -44,6 +44,7 @@
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Name;
+import org.eclipse.jdt.core.dom.PackageDeclaration;
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SimpleType;
@@ -711,7 +712,19 @@
ITypeBinding declaringTypeBinding= getDeclaringTypeBinding(accessBinding);
if (declaringTypeBinding != null) {
declaringTypeBinding= declaringTypeBinding.getTypeDeclaration(); // use generic to avoid any type arguments
-
+ int modifiers= declaringTypeBinding.getModifiers();
+ if (!Modifier.isPublic(modifiers) && !Modifier.isProtected(modifiers) && !Modifier.isPrivate(modifiers)) {
+ PackageDeclaration packageDecl= astRoot.getPackage();
+ if (packageDecl == null) {
+ if (declaringTypeBinding.getPackage() != null) {
+ return null;
+ }
+ } else {
+ if (!declaringTypeBinding.getPackage().isEqualTo(packageDecl.resolveBinding())) {
+ return null;
+ }
+ }
+ }
declaring= new ToStaticAccessOperation(declaringTypeBinding, qualifier, createdBlocks);
}
ToStaticAccessOperation instance= null;
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java
index 2b4b770..9e88621 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java
@@ -3541,6 +3541,36 @@
}
@Test
+ public void testCodeStyleBug579044() throws Exception {
+ IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
+ String sample= "" //
+ + "package test1;\n" //
+ + "class E1 {public static String FOO = \"FOO\";}\n";
+ pack1.createCompilationUnit("E1.java", sample, false, null);
+
+ sample= "" //
+ + "package test1;\n" //
+ + "public class E2 extends E1 {}\n";
+ pack1.createCompilationUnit("E2.java", sample, false, null);
+
+ IPackageFragment pack2= fSourceFolder.createPackageFragment("test2", false, null);
+ sample= "" //
+ + "package test2;\n" //
+ + "import test1.E2;\n" //
+ + "public class E3 {\n" //
+ + " public String foo() {\n" //
+ + " return E2.FOO;\n" //
+ + " }\n" //
+ + "}\n";
+ ICompilationUnit cu1= pack2.createCompilationUnit("E3.java", sample, false, null);
+
+ enable(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS);
+ enable(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_SUBTYPE_ACCESS);
+
+ assertRefactoringHasNoChange(new ICompilationUnit[] {cu1});
+ }
+
+ @Test
public void testCodeStyleBug189398() throws Exception {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
String sample= "" //