diff options
| author | Jeff Johnston | 2021-06-15 22:24:22 +0000 |
|---|---|---|
| committer | Jeff Johnston | 2021-06-16 16:10:54 +0000 |
| commit | 94d1776ab9258b42a8247433ba79402f8d1dc533 (patch) | |
| tree | b397119e3f24a24285bec82f97fc384cf3198f3f | |
| parent | 8f92896815d7f4b5ea6223e403427131f06726bf (diff) | |
| download | eclipse.jdt.ui-94d1776ab9258b42a8247433ba79402f8d1dc533.tar.gz eclipse.jdt.ui-94d1776ab9258b42a8247433ba79402f8d1dc533.tar.xz eclipse.jdt.ui-94d1776ab9258b42a8247433ba79402f8d1dc533.zip | |
Bug 573629 - Error running the new default cleanup profile on JDT codeI20210616-1800
- modify OrganizeImportsOperation.addStaticImports() method to
get the primary to pass to JavaModelUtil.getStaticImportFavorites()
because it can discard the working copy being used by importRewrite
- add new CleanUp test which has a break to label and for loop to modify
Change-Id: Ia63980bf3038c86c4ea0af4bcd4e446c057102e7
Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.ui/+/182016
Tested-by: JDT Bot <jdt-bot@eclipse.org>
Tested-by: Jeff Johnston <jjohnstn@redhat.com>
Reviewed-by: Lars Vogel <Lars.Vogel@vogella.com>
Reviewed-by: Jeff Johnston <jjohnstn@redhat.com>
2 files changed, 64 insertions, 2 deletions
diff --git a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/core/manipulation/OrganizeImportsOperation.java b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/core/manipulation/OrganizeImportsOperation.java index 6f0b0f1e2f..41343cc735 100644 --- a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/core/manipulation/OrganizeImportsOperation.java +++ b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/core/manipulation/OrganizeImportsOperation.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 @@ -682,7 +682,8 @@ public class OrganizeImportsOperation implements IWorkspaceRunnable { try { // check favourite static imports boolean isMethod= name.getParent() instanceof MethodInvocation; - String[] staticFavourites= JavaModelUtil.getStaticImportFavorites(importRewrite.getCompilationUnit(), identifier, isMethod, favourites); + ICompilationUnit cu= importRewrite.getCompilationUnit().getPrimary(); + String[] staticFavourites= JavaModelUtil.getStaticImportFavorites(cu, identifier, isMethod, favourites); if (staticFavourites.length > 0) { String qualifiedTypeName= Signature.getQualifier(staticFavourites[0]); importRewrite.addStaticImport(qualifiedTypeName, identifier, !isMethod); diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest1d5.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest1d5.java index bda9c3bd93..3e97707221 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest1d5.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest1d5.java @@ -3594,4 +3594,65 @@ public class CleanUpTest1d5 extends CleanUpTestCase { assertRefactoringHasNoChange(new ICompilationUnit[] { cu }); } + + @Test + public void testOrganizeImportsBug573629() throws Exception { + IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null); + String sample= "" // + + "package test1;\n" // + + "import java.util.List;\n" // + + "public class TestOrganizeImports {\n" // + + " public void foo(int a, List<String> list) {\n" // + + " label_1:\n" // + + " switch (a) {\n" // + + " case 0:\n" // + + " while (true) {\n" // + + " int len = 0;\n" // + + " for (int i = 0; i < list.size(); ++i) {\n" // + + " String s = list.get(i);\n" // + + " len += s.length();\n" // + + " }\n" // + + " if (len < 100) {\n" + + " break label_1;\n" // + + " }\n" // + + " break;\n" // + + " }\n" // + + " break;\n" // + + " default:\n" // + + " }\n" // + + " }\n" // + + "}\n"; // + ICompilationUnit cu1= pack1.createCompilationUnit("TestOrganizeImports.java", sample, false, null); + + enable(CleanUpConstants.ORGANIZE_IMPORTS); + enable(CleanUpConstants.CONTROL_STATEMENTS_CONVERT_FOR_LOOP_TO_ENHANCED); + + sample= "" // + + "package test1;\n" // + + "import java.util.List;\n" // + + "public class TestOrganizeImports {\n" // + + " public void foo(int a, List<String> list) {\n" // + + " label_1:\n" // + + " switch (a) {\n" // + + " case 0:\n" // + + " while (true) {\n" // + + " int len = 0;\n" // + + " for (String s : list) {\n" // + + " len += s.length();\n" // + + " }\n" // + + " if (len < 100) {\n" + + " break label_1;\n" // + + " }\n" // + + " break;\n" // + + " }\n" // + + " break;\n" // + + " default:\n" // + + " }\n" // + + " }\n" // + + "}\n"; // + String expected1= sample; + + assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 }, null); + } + } |
