Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAyushman Jain2012-05-02 10:58:25 +0000
committerAyushman Jain2012-05-02 10:58:25 +0000
commit8462dab82f576431859045179a5c6b834586b6f0 (patch)
tree8b1b4992cfedf4ea4cd1218b39738aeb48f1a39e
parente795ef784de39eb3529f1750c2504b9afe37e6d0 (diff)
downloadeclipse.jdt.core-8462dab82f576431859045179a5c6b834586b6f0.tar.gz
eclipse.jdt.core-8462dab82f576431859045179a5c6b834586b6f0.tar.xz
eclipse.jdt.core-8462dab82f576431859045179a5c6b834586b6f0.zip
Fixed bug 376930: FUP of bug 24804: Organize imports does not work when
folding imports into on-demand import
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ImportRewriteTest.java684
-rw-r--r--org.eclipse.jdt.core/buildnotes_jdt-core.html4
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ImportRewriteAnalyzer.java62
3 files changed, 737 insertions, 13 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ImportRewriteTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ImportRewriteTest.java
index d3037e209d..41cbf4c73d 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ImportRewriteTest.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ImportRewriteTest.java
@@ -1486,6 +1486,690 @@ public class ImportRewriteTest extends AbstractJavaModelTests {
buf.append("}\n");
assertEqualString(cu.getSource(), buf.toString());
}
+
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=376930
+ public void testBug376930() throws Exception {
+ IPackageFragment pack1 = this.sourceFolder.createPackageFragment("pack1", false, null);
+ StringBuffer buf = new StringBuffer();
+ // 2 imports are in 1 group but third is separated by a comment
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "import java.util.*; // test\n" +
+ "import java.util.Map.Entry;\n" +
+ "//comment 2\n" +
+ "import java.util.Map.SomethingElse;\n" +
+ "// commen 3\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
+
+ String[] order = new String[] { "java", "java.util", "com", "pack" };
+
+ ImportRewrite imports= newImportsRewrite(cu, order, 1, 1, true);
+ imports.setUseContextToFilterImplicitImports(true);
+ imports.addImport("java.io.PrintWriter");
+
+ apply(imports);
+
+ buf = new StringBuffer();
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "import java.io.*;\n" +
+ "\n" +
+ "import java.util.*; // test\n" +
+ "import java.util.Map.Entry;\n" +
+ "//comment 2\n" +
+ "import java.util.Map.SomethingElse;\n" +
+ "// commen 3\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ assertEqualString(cu.getSource(), buf.toString());
+ }
+
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=376930
+ public void testBug376930_2() throws Exception {
+ IPackageFragment pack1 = this.sourceFolder.createPackageFragment("pack1", false, null);
+ StringBuffer buf = new StringBuffer();
+ // all imports are in same group
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "// comment 1\n" +
+ "import java.util.*; // test\n" +
+ "import java.util.Map.Entry; // test2\n" +
+ "import java.util.Map.SomethingElse;\n" +
+ "// commen 3\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
+
+ String[] order = new String[] { "java", "java.util", "com", "pack" };
+
+ ImportRewrite imports= newImportsRewrite(cu, order, 1, 1, true);
+ imports.setUseContextToFilterImplicitImports(true);
+ imports.addImport("java.io.PrintWriter");
+
+ apply(imports);
+
+ buf = new StringBuffer();
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "// comment 1\n" +
+ "import java.io.*;\n" +
+ "\n" +
+ "import java.util.*; // test\n" +
+ "import java.util.Map.Entry; // test2\n" +
+ "import java.util.Map.SomethingElse;\n" +
+ "// commen 3\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ assertEqualString(cu.getSource(), buf.toString());
+ }
+
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=376930
+ public void testBug376930_3() throws Exception {
+ IPackageFragment pack1 = this.sourceFolder.createPackageFragment("pack1", false, null);
+ StringBuffer buf = new StringBuffer();
+ // all imports are in same group
+ // leading and trailing comments
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "// comment 1\n" +
+ "/* lead 1*/ import java.util.*; // test1\n" +
+ "/* lead 2*/import java.util.Map.Entry; // test2\n" +
+ "/* lead 3*/ import java.util.Map.SomethingElse; // test3\n" +
+ "// commen 3\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
+
+ String[] order = new String[] { "java", "java.util", "com", "pack" };
+
+ ImportRewrite imports= newImportsRewrite(cu, order, 1, 1, true);
+ imports.setUseContextToFilterImplicitImports(true);
+ imports.addImport("java.io.PrintWriter");
+
+ apply(imports);
+
+ buf = new StringBuffer();
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "// comment 1\n" +
+ "/* lead 1*/ import java.io.*;\n" +
+ "\n" +
+ "import java.util.*; // test1\n" +
+ "/* lead 2*/import java.util.Map.Entry; // test2\n" +
+ "/* lead 3*/ import java.util.Map.SomethingElse; // test3\n" +
+ "// commen 3\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ assertEqualString(cu.getSource(), buf.toString());
+ }
+
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=376930
+ // remove imports, preserve all comments
+ public void testBug376930_3a() throws Exception {
+ IPackageFragment pack1 = this.sourceFolder.createPackageFragment("pack1", false, null);
+ StringBuffer buf = new StringBuffer();
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "// comment 1\n" +
+ "/* lead 1*/ import java.util.*; // test1\n" +
+ "/* lead 2*/import java.util.Map.Entry; // test2\n" +
+ "/* lead 3*/ import java.util.Map.SomethingElse; // test3\n" +
+ "// commen 3\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
+
+ String[] order = new String[] { "java", "java.util", "com", "pack" };
+
+ ImportRewrite imports= newImportsRewrite(cu, order, 1, 1, false);
+ imports.setUseContextToFilterImplicitImports(true);
+ imports.addImport("java.io.PrintWriter");
+
+ apply(imports);
+
+ buf = new StringBuffer();
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "// comment 1\n" +
+ "/* lead 1*/ " +
+ "import java.io.*;\n" +
+ "// test1\n" +
+ "/* lead 2*/\n" +
+ "// test2\n" +
+ "/* lead 3*/ \n" +
+ "// test3\n" +
+ "// commen 3\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ assertEqualString(cu.getSource(), buf.toString());
+ }
+
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=376930
+ public void testBug376930_4() throws Exception {
+ IPackageFragment pack1 = this.sourceFolder.createPackageFragment("pack1", false, null);
+ StringBuffer buf = new StringBuffer();
+ // all imports are in same group
+ // leading and trailing comments
+ // two on demand imports in the group
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "// comment 1\n" +
+ "/* lead 1*/ import java.util.*; // test1\n" +
+ "/* lead 2*/import java.util.Map.*; // test2\n" +
+ "/* lead 3*/ import java.util.Map.SomethingElse; // test3\n" +
+ "// commen 3\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
+
+ String[] order = new String[] { "java", "java.util", "com", "pack" };
+
+ ImportRewrite imports= newImportsRewrite(cu, order, 1, 1, true);
+ imports.setUseContextToFilterImplicitImports(true);
+ imports.addImport("java.io.PrintWriter");
+
+ apply(imports);
+
+ buf = new StringBuffer();
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "// comment 1\n" +
+ "/* lead 1*/ import java.io.*;\n" +
+ "\n" +
+ "import java.util.*; // test1\n" +
+ "/* lead 2*/import java.util.Map.*; // test2\n" +
+ "/* lead 3*/ import java.util.Map.SomethingElse; // test3\n" +
+ "// commen 3\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ assertEqualString(cu.getSource(), buf.toString());
+ }
+
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=376930
+ // remove imports, preserve all comments
+ public void testBug376930_4a() throws Exception {
+ IPackageFragment pack1 = this.sourceFolder.createPackageFragment("pack1", false, null);
+ StringBuffer buf = new StringBuffer();
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "// comment 1\n" +
+ "/* lead 1*/ import java.util.HashMap; // test1\n" +
+ "/* lead 2*/import java.util.Map.*; // test2\n" +
+ "/* lead 3*/ import java.util.Map.SomethingElse; // test3\n" +
+ "// commen 3\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
+
+ String[] order = new String[] { "java", "java.util", "com", "pack" };
+
+ ImportRewrite imports= newImportsRewrite(cu, order, 1, 1, false);
+ imports.setUseContextToFilterImplicitImports(true);
+ imports.addImport("java.io.PrintWriter");
+
+ apply(imports);
+
+ buf = new StringBuffer();
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "// comment 1\n" +
+ "/* lead 1*/ " +
+ "import java.io.*;\n" +
+ "// test1\n" +
+ "/* lead 2*/\n" +
+ "// test2\n" +
+ "/* lead 3*/ \n" +
+ "// test3\n" +
+ "// commen 3\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ assertEqualString(cu.getSource(), buf.toString());
+ }
+
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=376930
+ public void testBug376930_5() throws Exception {
+ IPackageFragment pack1 = this.sourceFolder.createPackageFragment("pack1", false, null);
+ StringBuffer buf = new StringBuffer();
+ // all imports of same group are scattered around
+ // leading and trailing comments
+ // adding an on-demand import belonging to a group
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "// comment 1\n" +
+ "/* lead 1*/ import java.util.*; // test1\n" +
+ "/* lead 2*/import java.io.PrintWriter.*; // test2\n" +
+ "/* lead 3*/ import java.util.Map.SomethingElse; // test3\n" +
+ "// commen 3\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
+
+ String[] order = new String[] { "java", "java.util", "com", "pack" };
+
+ ImportRewrite imports= newImportsRewrite(cu, order, 1, 1, true);
+ imports.setUseContextToFilterImplicitImports(true);
+ imports.addImport("java.util.Map.*");
+
+ apply(imports);
+
+ buf = new StringBuffer();
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "// comment 1\n" +
+ "/* lead 1*/ import java.util.*; // test1\n" +
+ "import java.util.Map.*;\n" +
+ "\n" +
+ "/* lead 2*/import java.io.PrintWriter.*; // test2\n" +
+ "/* lead 3*/ import java.util.Map.SomethingElse; // test3\n" +
+ "// commen 3\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ assertEqualString(cu.getSource(), buf.toString());
+ }
+
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=376930
+ public void testBug376930_5a() throws Exception {
+ IPackageFragment pack1 = this.sourceFolder.createPackageFragment("pack1", false, null);
+ StringBuffer buf = new StringBuffer();
+ // all imports are in same group
+ // leading and trailing comments
+ // adding an on-demand import belonging to a group
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "// comment 1\n" +
+ "/* lead 2*/import java.io.PrintWriter.*; // test2\n" +
+ "/* lead 1*/ import java.util.*; // test1\n" +
+ "/* lead 3*/ import java.util.Map.SomethingElse; // test3\n" +
+ "// commen 3\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
+
+ String[] order = new String[] { "java", "java.util", "com", "pack" };
+
+ ImportRewrite imports= newImportsRewrite(cu, order, 1, 1, true);
+ imports.setUseContextToFilterImplicitImports(true);
+ imports.addImport("java.util.Map.*");
+
+ apply(imports);
+
+ buf = new StringBuffer();
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "// comment 1\n" +
+ "/* lead 2*/import java.io.PrintWriter.*; // test2\n" +
+ "/* lead 1*/ import java.util.*; // test1\n" +
+ "import java.util.Map.*;\n" +
+ "/* lead 3*/ import java.util.Map.SomethingElse; // test3\n" +
+ "// commen 3\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ assertEqualString(cu.getSource(), buf.toString());
+ }
+
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=376930
+ // added import should get folded into existing *, without touching comments
+ public void testBug376930_5b() throws Exception {
+ IPackageFragment pack1 = this.sourceFolder.createPackageFragment("pack1", false, null);
+ StringBuffer buf = new StringBuffer();
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "// comment 1\n" +
+ "/* lead 2*/import java.io.PrintWriter.*; // test2\n" +
+ "/* lead 1*/ import java.util.*; // test1\n" +
+ "/* lead 3*/ import java.util.Map.SomethingElse; // test3\n" +
+ "// commen 3\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
+
+ String[] order = new String[] { "java", "java.util", "com", "pack" };
+
+ ImportRewrite imports= newImportsRewrite(cu, order, 1, 1, false);
+ imports.setUseContextToFilterImplicitImports(true);
+ imports.addImport("java.util.Map");
+
+ apply(imports);
+
+ buf = new StringBuffer();
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "// comment 1\n" +
+ "/* lead 2*/" +
+ "import java.util.*;\n" +
+ "// test2\n" +
+ "/* lead 1*/ \n" +
+ "// test1\n" +
+ "/* lead 3*/ \n" +
+ "// test3\n" +
+ "// commen 3\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ assertEqualString(cu.getSource(), buf.toString());
+ }
+
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=376930
+ // remove imports, preserve all comments
+ public void testBug376930_5c() throws Exception {
+ IPackageFragment pack1 = this.sourceFolder.createPackageFragment("pack1", false, null);
+ StringBuffer buf = new StringBuffer();
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "// comment 1\n" +
+ "/* lead 1*/ import java.util.*; // test1\n" +
+ "/* lead 2*/import java.io.PrintWriter.*; // test2\n" +
+ "/* lead 3*/ import java.util.Map.SomethingElse; // test3\n" +
+ "// commen 3\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
+
+ String[] order = new String[] { "java", "java.util", "com", "pack" };
+
+ ImportRewrite imports= newImportsRewrite(cu, order, 1, 1, false);
+ imports.setUseContextToFilterImplicitImports(true);
+ imports.addImport("java.util.Map.*");
+
+ apply(imports);
+
+ buf = new StringBuffer();
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "// comment 1\n" +
+ "/* lead 1*/ " +
+ "import java.util.Map.*;\n" +
+ "// test1\n" +
+ "/* lead 2*/\n" +
+ "// test2\n" +
+ "/* lead 3*/ \n" +
+ "// test3\n" +
+ "// commen 3\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ assertEqualString(cu.getSource(), buf.toString());
+ }
+
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=376930
+ // added import should get folded along with existing import into *, without deleting comments
+ public void testBug376930_5d() throws Exception {
+ IPackageFragment pack1 = this.sourceFolder.createPackageFragment("pack1", false, null);
+ StringBuffer buf = new StringBuffer();
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "// comment 1\n" +
+ "/* lead 2*/import java.io.PrintWriter.*; // test2\n" +
+ "/* lead 1*/ import java.util.Map; // test1\n" +
+ "// commen 3\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
+
+ String[] order = new String[] { "java", "java.util", "com", "pack" };
+
+ ImportRewrite imports= newImportsRewrite(cu, order, 2, 2, true);
+ imports.setUseContextToFilterImplicitImports(true);
+ imports.addImport("java.util.HashMap");
+
+ apply(imports);
+
+ buf = new StringBuffer();
+ buf.append(
+ "package pack1;\n" +
+ "\n" +
+ "// comment 1\n" +
+ "/* lead 2*/import java.io.PrintWriter.*; // test2\n" +
+ "\n" +
+ "/* lead 1*/ \n" +
+ " // test1\n" +
+ "// commen 3\n" +
+ "import java.util.*;\n" +
+ "\n" +
+ "public class C {\n" +
+ " public static void main(String[] args) {\n" +
+ " HashMap h;\n" +
+ "\n" +
+ " Map.Entry e= null;\n" +
+ " Entry e2= null;\n" +
+ "\n" +
+ " PrintWriter pw;\n" +
+ " System.out.println(\"hello\");\n" +
+ " }\n" +
+ "}");
+ assertEqualString(cu.getSource(), buf.toString());
+ }
private void assertAddedAndRemoved(ImportRewrite imports, String[] expectedAdded, String[] expectedRemoved, String[] expectedAddedStatic, String[] expectedRemovedStatic) {
assertEqualStringsIgnoreOrder(imports.getAddedImports(), expectedAdded);
diff --git a/org.eclipse.jdt.core/buildnotes_jdt-core.html b/org.eclipse.jdt.core/buildnotes_jdt-core.html
index 248d7f2e9f..78166cab18 100644
--- a/org.eclipse.jdt.core/buildnotes_jdt-core.html
+++ b/org.eclipse.jdt.core/buildnotes_jdt-core.html
@@ -50,7 +50,9 @@ Eclipse SDK 3.6.4 - %date% - December 14, 2011
<h2>What's new in this drop</h2>
<h3>Problem Reports Fixed</h3>
-<a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=376734">376734</a>
+<a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=376930">376930</a>
+FUP of bug 24804: Organize imports does not work when folding imports into on-demand import
+<br><a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=376734">376734</a>
[Backport] Organize imports wipes comments between statements [code manipulation]
<a name="v_A81_R36x"></a>
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ImportRewriteAnalyzer.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ImportRewriteAnalyzer.java
index 5a43dafb1d..563ab90982 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ImportRewriteAnalyzer.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ImportRewriteAnalyzer.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2011 IBM Corporation and others.
+ * Copyright (c) 2000, 2012 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -271,28 +271,39 @@ public final class ImportRewriteAnalyzer {
int nextLength= next.getLength();
int nextOffsetLine= root.getLineNumber(nextOffset);
- int extendedStart = root.getExtendedStartPosition(next);
- int extendedLength = root.getExtendedLength(next);
+ int extendedStart = root.getExtendedStartPosition(curr);
+ if (extendedStart < this.replaceRange.getOffset()) {
+ // don't touch the first comments before the start of import declarations
+ extendedStart = currOffset;
+ }
+ int extendedLength = root.getExtendedLength(curr);
// if next import is on a different line, modify the end position to the next line begin offset
+ int nextLineOffset = nextOffset; // offset at the start of next line. Next import may not start here
if (currEndLine < nextOffsetLine) {
currEndLine++;
- nextOffset= root.getPosition(currEndLine, 0);
+ nextLineOffset = root.getPosition(currEndLine, 0);
+ // There may be some leading comments (or line delimiters) before the next import. The start of those comments
+ // is not the real start offset of the next import. So don't change nextOffset
}
// retrieve preceding and trailing comments if any
IRegion rangeBefore = null;
IRegion rangeAfter = null;
- if (nextOffset != extendedStart) {
- rangeBefore = new Region(extendedStart, extendedStart - nextOffset + 1);
+
+ if (currOffset > extendedStart) {
+ rangeBefore = new Region(extendedStart, currOffset - extendedStart);
}
- if (nextLength != extendedLength) {
- rangeAfter = new Region(nextOffset + nextLength, extendedLength - nextLength + 1);
+ int currLen = curr.getLength();
+ if (currLen < extendedLength - (currOffset - extendedStart)) {
+ int currEndOffset = currOffset + currLen;
+ int rangeBeforeLen = rangeBefore != null? rangeBefore.getLength() : 0;
+ rangeAfter = new Region(currEndOffset, extendedLength - rangeBeforeLen - currLen);
}
currPackage.add(
new ImportDeclEntry(
packName.length(),
name,
isStatic,
- new Region(currOffset, nextOffset - currOffset),
+ new Region(currOffset, nextLineOffset - currOffset), // should not include leading comments of next import, line delimiters, etc.
rangeBefore,
rangeAfter));
currOffset= nextOffset;
@@ -304,7 +315,7 @@ public final class ImportRewriteAnalyzer {
currPackage= new PackageEntry(); // create a comment package entry for this
this.packageEntries.add(currPackage);
- currPackage.add(new ImportDeclEntry(packName.length(), null, false, new Region(currOffset, nextOffset - currOffset)));
+ currPackage.add(new ImportDeclEntry(packName.length(), null, false, new Region(nextLineOffset, nextOffset - nextLineOffset)));
currOffset= nextOffset;
}
@@ -318,8 +329,23 @@ public final class ImportRewriteAnalyzer {
currPackage= new PackageEntry(packName, null, isStatic);
this.packageEntries.add(currPackage);
}
- int length= this.replaceRange.getOffset() + this.replaceRange.getLength() - curr.getStartPosition();
- currPackage.add(new ImportDeclEntry(packName.length(), name, isStatic, new Region(curr.getStartPosition(), length)));
+ int currStartOffset = curr.getStartPosition();
+ int currLen = curr.getLength();
+ int extendedStartOffset = root.getExtendedStartPosition(curr);
+ IRegion leadingComments = null;
+ IRegion allTrailingComments = null;
+
+ if (currStartOffset > extendedStartOffset) {
+ leadingComments = new Region(extendedStartOffset, currOffset - extendedStartOffset);
+ }
+ int length= this.replaceRange.getOffset() + this.replaceRange.getLength() - currStartOffset;
+ int extendedLength = root.getExtendedLength(curr);
+ if (currLen < extendedLength - (currOffset - extendedStartOffset)) {
+ int currEndOffset = currOffset + currLen;
+ int leadingCommentsLen = leadingComments != null? leadingComments.getLength() : 0;
+ allTrailingComments = new Region(currEndOffset, extendedLength - leadingCommentsLen - currLen);
+ }
+ currPackage.add(new ImportDeclEntry(packName.length(), name, isStatic, new Region(curr.getStartPosition(), length), leadingComments, allTrailingComments));
}
private IRegion[] retrieveExistingCommentsInImports(CompilationUnit root) {
@@ -766,6 +792,14 @@ public final class ImportRewriteAnalyzer {
}
} else if (!doStarImport || currDecl.isOnDemand() || onDemandConflicts == null || onDemandConflicts.contains(currDecl.getSimpleName())) {
int offset= region.getOffset();
+ IRegion rangeBefore = currDecl.getPrecedingCommentRange();
+ if (rangeBefore != null && currPos > rangeBefore.getOffset()) {
+ // moved ahead of the leading comments, bring the currPos back
+ currPos = rangeBefore.getOffset();
+ }
+ if (rangeBefore != null) {
+ stringsToInsert.add(buffer.getText(rangeBefore.getOffset(), rangeBefore.getLength()));
+ }
removeAndInsertNew(buffer, currPos, offset, stringsToInsert, resEdit);
stringsToInsert.clear();
currPos= offset + region.getLength();
@@ -773,6 +807,10 @@ public final class ImportRewriteAnalyzer {
String simpleName = currDecl.getTypeQualifiedName();
if (simpleName.indexOf('.') != -1) {
IRegion rangeBefore = currDecl.getPrecedingCommentRange();
+ if (rangeBefore != null && currPos > rangeBefore.getOffset()) {
+ // moved ahead of the leading comments, bring the currPos back
+ currPos = rangeBefore.getOffset();
+ }
if (rangeBefore != null) {
stringsToInsert.add(buffer.getText(rangeBefore.getOffset(), rangeBefore.getLength()));
}

Back to the top