Search in sources :

Example 16 with OrganizeImportsOperation

use of org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation in project che by eclipse.

the class ImportOrganizeTest method test2.

@Test
public void test2() throws Exception {
    File junitSrcArchive = new File(JUnitSourceSetup.class.getClassLoader().getResource(JavaProjectHelper.JUNIT_SRC_381.toOSString()).getFile());
    assertTrue("junit src not found", junitSrcArchive != null && junitSrcArchive.exists());
    JavaProjectHelper.addSourceContainerWithImport(fJProject1, "src", junitSrcArchive, JavaProjectHelper.JUNIT_SRC_ENCODING);
    ICompilationUnit cu = (ICompilationUnit) fJProject1.findElement(new Path("junit/runner/LoadingTestCollector.java"));
    assertNotNull("LoadingTestCollector.java", cu);
    String[] order = new String[0];
    IChooseImportQuery query = createQuery("LoadingTestCollector", new String[] {}, new int[] {});
    OrganizeImportsOperation op = createOperation(cu, order, 99, false, true, true, query);
    op.run(null);
    assertImports(cu, new String[] { "java.lang.reflect.Modifier", "junit.framework.Test", "junit.framework.TestSuite" });
}
Also used : Path(org.eclipse.core.runtime.Path) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) OrganizeImportsOperation(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation) IChooseImportQuery(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery) JUnitSourceSetup(org.eclipse.che.plugin.java.server.jdt.search.JUnitSourceSetup) File(java.io.File) Test(org.junit.Test)

Example 17 with OrganizeImportsOperation

use of org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation in project che by eclipse.

the class ImportOrganizeTest method testNewImportsNoPackage.

@Test
public void testNewImportsNoPackage() throws Exception {
    IPackageFragmentRoot sourceFolder = JavaProjectHelper.addSourceContainer(fJProject1, "src");
    IPackageFragment pack1 = sourceFolder.getPackageFragment("");
    StringBuffer buf = new StringBuffer();
    buf.append("public class C extends Vector {\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
    String[] order = new String[0];
    IChooseImportQuery query = createQuery("C", new String[] {}, new int[] {});
    OrganizeImportsOperation op = createOperation(cu, order, 99, false, true, true, query);
    op.run(null);
    buf = new StringBuffer();
    buf.append("import java.util.Vector;\n");
    buf.append("\n");
    buf.append("public class C extends Vector {\n");
    buf.append("}\n");
    assertEqualString(cu.getSource(), buf.toString());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) OrganizeImportsOperation(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation) IChooseImportQuery(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Test(org.junit.Test)

Example 18 with OrganizeImportsOperation

use of org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation in project che by eclipse.

the class ImportOrganizeTest method testStaticImports_bug187004b.

@Test
public void testStaticImports_bug187004b() throws Exception {
    IPackageFragmentRoot sourceFolder = JavaProjectHelper.addSourceContainer(fJProject1, "src");
    IPackageFragment pack0 = sourceFolder.createPackageFragment("b", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package b;\n");
    buf.append("\n");
    buf.append("abstract public class Parent<T> {\n");
    buf.append("        protected static final int CONSTANT() { return 42; }\n");
    buf.append("}\n");
    pack0.createCompilationUnit("Parent.java", buf.toString(), false, null);
    IPackageFragment pack1 = sourceFolder.createPackageFragment("a", false, null);
    buf = new StringBuffer();
    buf.append("package a;\n");
    buf.append("\n");
    buf.append("import b.Parent;\n");
    buf.append("\n");
    buf.append("public class Child extends Parent<String> {\n");
    buf.append("        public Child() {\n");
    buf.append("                System.out.println(CONSTANT());\n");
    buf.append("        }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("Child.java", buf.toString(), false, null);
    String[] order = new String[] {};
    IChooseImportQuery query = createQuery("Child", new String[] {}, new int[] {});
    OrganizeImportsOperation op = createOperation(cu, order, 99, false, true, true, query);
    op.run(null);
    buf = new StringBuffer();
    buf.append("package a;\n");
    buf.append("\n");
    // no static import for CONSTANT()
    buf.append("import b.Parent;\n");
    buf.append("\n");
    buf.append("public class Child extends Parent<String> {\n");
    buf.append("        public Child() {\n");
    buf.append("                System.out.println(CONSTANT());\n");
    buf.append("        }\n");
    buf.append("}\n");
    assertEqualString(cu.getSource(), buf.toString());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) OrganizeImportsOperation(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation) IChooseImportQuery(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Test(org.junit.Test)

Example 19 with OrganizeImportsOperation

use of org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation in project che by eclipse.

the class ImportOrganizeTest method testStaticImports_bug175498.

@Test
public void testStaticImports_bug175498() throws Exception {
    IPackageFragmentRoot sourceFolder = JavaProjectHelper.addSourceContainer(fJProject1, "src");
    IPackageFragment pack1 = sourceFolder.createPackageFragment("p", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package p;\n");
    buf.append("public class Test<T> {\n");
    buf.append("        public static enum TestEnum {\n");
    buf.append("                V1,\n");
    buf.append("                V2\n");
    buf.append("        }\n");
    buf.append("\n");
    buf.append("        public void test(final TestEnum value) {\n");
    buf.append("                switch (value) {\n");
    buf.append("                        case V1:\n");
    buf.append("                        case V2:\n");
    buf.append("                }\n");
    buf.append("        }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("A.java", buf.toString(), false, null);
    String[] order = new String[] {};
    IChooseImportQuery query = createQuery("testStaticImports_bug175498", new String[] {}, new int[] {});
    OrganizeImportsOperation op = createOperation(cu, order, 99, false, true, true, query);
    op.run(null);
    // no changes, don't add 'V1' and 'V2' as static import
    assertEqualString(cu.getSource(), buf.toString());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) OrganizeImportsOperation(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation) IChooseImportQuery(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Test(org.junit.Test)

Example 20 with OrganizeImportsOperation

use of org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation in project che by eclipse.

the class ImportOrganizeTest method testInnerClassVisibility.

@Test
public void testInnerClassVisibility() throws Exception {
    IPackageFragmentRoot sourceFolder = JavaProjectHelper.addSourceContainer(fJProject1, "src");
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class C {\n");
    buf.append("  public static class C1 {\n");
    buf.append("    public static class C2 {\n");
    buf.append("    }\n");
    buf.append("  }\n");
    buf.append("}\n");
    pack1.createCompilationUnit("C.java", buf.toString(), false, null);
    IPackageFragment pack2 = sourceFolder.createPackageFragment("test2", false, null);
    buf = new StringBuffer();
    buf.append("package test2;\n");
    buf.append("import test2.A.A1;\n");
    buf.append("import test2.A.A1.A2;\n");
    buf.append("import test2.A.A1.A2.A3;\n");
    buf.append("import test2.A.B1;\n");
    buf.append("import test2.A.B1.B2;\n");
    buf.append("import test1.C;\n");
    buf.append("import test1.C.C1.C2;\n");
    buf.append("public class A {\n");
    buf.append("    public static class A1 {\n");
    buf.append("        public static class A2 {\n");
    buf.append("            public static class A3 {\n");
    buf.append("            }\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("    public static class B1 {\n");
    buf.append("        public static class B2 {\n");
    buf.append("        }\n");
    buf.append("        public static class B3 {\n");
    buf.append("            public static class B4 extends C {\n");
    buf.append("                B4 b4;\n");
    buf.append("                B3 b3;\n");
    buf.append("                B2 b2;\n");
    buf.append("                B1 b1;\n");
    buf.append("                A1 a1;\n");
    buf.append("                A2 a2;\n");
    buf.append("                A3 a3;\n");
    buf.append("                C1 c1;\n");
    buf.append("                C2 c2;\n");
    buf.append("            }\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu2 = pack2.createCompilationUnit("A.java", buf.toString(), false, null);
    String[] order = new String[0];
    IChooseImportQuery query = createQuery("A", new String[] {}, new int[] {});
    OrganizeImportsOperation op = createOperation(cu2, order, 99, false, true, true, query);
    op.run(null);
    assertImports(cu2, new String[] { "test1.C", "test1.C.C1.C2", "test2.A.A1.A2", "test2.A.A1.A2.A3" });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) OrganizeImportsOperation(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation) IChooseImportQuery(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Test(org.junit.Test)

Aggregations

OrganizeImportsOperation (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation)71 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)70 IChooseImportQuery (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery)70 Test (org.junit.Test)69 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)67 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)67 File (java.io.File)5 JUnitSourceSetup (org.eclipse.che.plugin.java.server.jdt.search.JUnitSourceSetup)5 Path (org.eclipse.core.runtime.Path)5 OrganizeImportResult (org.eclipse.che.ide.ext.java.shared.dto.OrganizeImportResult)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 IBuffer (org.eclipse.jdt.core.IBuffer)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 CodeGenerationSettings (org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings)1 DocumentChangeListener (org.eclipse.jdt.internal.corext.format.DocumentChangeListener)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 Document (org.eclipse.jface.text.Document)1 IDocument (org.eclipse.jface.text.IDocument)1 TextEdit (org.eclipse.text.edits.TextEdit)1