Search in sources :

Example 46 with IChooseImportQuery

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

the class ImportOrganizeTest method testClearImportsNoPackage.

@Test
public void testClearImportsNoPackage() throws Exception {
    IPackageFragmentRoot sourceFolder = JavaProjectHelper.addSourceContainer(fJProject1, "src");
    IPackageFragment pack1 = sourceFolder.getPackageFragment("");
    StringBuffer buf = new StringBuffer();
    buf.append("import java.util.Vector;\n");
    buf.append("public class C {\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("public class C {\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 47 with IChooseImportQuery

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

the class ImportOrganizeTest method testImportToStarWithExplicit5.

@Test
public void testImportToStarWithExplicit5() throws Exception {
    // unrelated project, to fill the all types cache
    IJavaProject project2 = JavaProjectHelper.createJavaProject("TestProject2", "bin");
    try {
        assertTrue("rt not found", JavaProjectHelper.addRTJar(project2) != null);
        IPackageFragmentRoot sourceFolder2 = JavaProjectHelper.addSourceContainer(project2, "src");
        IPackageFragment pack22 = sourceFolder2.createPackageFragment("packx", false, null);
        StringBuffer buf = new StringBuffer();
        buf.append("package pack;\n");
        buf.append("public class Vector {\n");
        buf.append("}\n");
        pack22.createCompilationUnit("List.java", buf.toString(), false, null);
        buf = new StringBuffer();
        buf.append("package pack;\n");
        buf.append("public class Set {\n");
        buf.append("}\n");
        pack22.createCompilationUnit("Set.java", buf.toString(), false, null);
        IPackageFragmentRoot sourceFolder = JavaProjectHelper.addSourceContainer(fJProject1, "src");
        IPackageFragment pack2 = sourceFolder.createPackageFragment("pack", false, null);
        buf = new StringBuffer();
        buf.append("package pack;\n");
        buf.append("public class List {\n");
        buf.append("}\n");
        pack2.createCompilationUnit("List.java", buf.toString(), false, null);
        IPackageFragment pack1 = sourceFolder.createPackageFragment("pack1", false, null);
        buf = new StringBuffer();
        buf.append("package pack1;\n");
        buf.append("\n");
        buf.append("import java.util.Set;\n");
        buf.append("import java.util.Vector;\n");
        buf.append("import java.util.Map;\n");
        buf.append("\n");
        buf.append("import pack.List;\n");
        buf.append("\n");
        buf.append("public class C {\n");
        buf.append("    Vector v;\n");
        buf.append("    Set v2;\n");
        buf.append("    Map v3;\n");
        buf.append("    List v4;\n");
        buf.append("    String v6;\n");
        buf.append("}\n");
        ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
        String[] order = new String[] { "java", "pack" };
        IChooseImportQuery query = createQuery("C", new String[] {}, new int[] {});
        OrganizeImportsOperation op = createOperation(cu, order, 1, false, true, true, query);
        op.run(null);
        buf = new StringBuffer();
        buf.append("package pack1;\n");
        buf.append("\n");
        buf.append("import java.util.*;\n");
        buf.append("\n");
        buf.append("import pack.List;\n");
        buf.append("\n");
        buf.append("public class C {\n");
        buf.append("    Vector v;\n");
        buf.append("    Set v2;\n");
        buf.append("    Map v3;\n");
        buf.append("    List v4;\n");
        buf.append("    String v6;\n");
        buf.append("}\n");
        assertEqualString(cu.getSource(), buf.toString());
    } finally {
        JavaProjectHelper.delete(project2);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) OrganizeImportsOperation(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation) IJavaProject(org.eclipse.jdt.core.IJavaProject) IChooseImportQuery(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Test(org.junit.Test)

Example 48 with IChooseImportQuery

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

the class ImportOrganizeTest method testStaticImports_bug159424.

public void testStaticImports_bug159424() throws Exception {
    IPackageFragmentRoot sourceFolder = JavaProjectHelper.addSourceContainer(fJProject1, "src");
    IPackageFragment pack0 = sourceFolder.createPackageFragment("pack0", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package pack0;\n");
    buf.append("\n");
    buf.append("import java.util.List;\n");
    buf.append("\n");
    buf.append("public abstract class B {\n");
    buf.append("    private static List logger;\n");
    buf.append("}\n");
    pack0.createCompilationUnit("B.java", buf.toString(), false, null);
    IPackageFragment pack1 = sourceFolder.createPackageFragment("pack1", false, null);
    buf = new StringBuffer();
    buf.append("package pack1;\n");
    buf.append("\n");
    buf.append("import java.util.List;\n");
    buf.append("import pack0.B;\n");
    buf.append("\n");
    buf.append("public abstract class A {\n");
    buf.append("    private static List logger;\n");
    buf.append("\n");
    buf.append("    protected class BSubClass extends B {\n");
    buf.append("        public void someMethod() {\n");
    buf.append("            logger.toString();\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_bug159424", new String[] {}, new int[] {});
    OrganizeImportsOperation op = createOperation(cu, order, 99, false, true, true, query);
    op.run(null);
    // no changes, don't add 'logger' 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)

Example 49 with IChooseImportQuery

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

the class ImportOrganizeTest method test_bug78533.

@Test
public void test_bug78533() throws Exception {
    IPackageFragmentRoot sourceFolder = JavaProjectHelper.addSourceContainer(fJProject1, "src");
    IPackageFragment pack1 = sourceFolder.createPackageFragment("pack0", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package pack0;\n");
    buf.append("public class A {\n");
    buf.append("    public <T extends Collection> void method1() { }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("A.java", buf.toString(), false, null);
    String[] order = new String[] {};
    IChooseImportQuery query = createQuery("MyClass", new String[] {}, new int[] {});
    OrganizeImportsOperation op = createOperation(cu, order, 99, false, true, true, query);
    op.run(null);
    buf = new StringBuffer();
    buf.append("package pack0;\n");
    buf.append("\n");
    buf.append("import java.util.Collection;\n");
    buf.append("\n");
    buf.append("public class A {\n");
    buf.append("    public <T extends Collection> void method1() { }\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 50 with IChooseImportQuery

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

the class ImportOrganizeTest method testImportCountKeepStar.

@Test
public void testImportCountKeepStar() throws Exception {
    IPackageFragmentRoot sourceFolder = JavaProjectHelper.addSourceContainer(fJProject1, "src");
    IPackageFragment pack1 = sourceFolder.createPackageFragment("pack1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package pack1;\n");
    buf.append("\n");
    buf.append("import java.util.*;\n");
    buf.append("\n");
    buf.append("public class C {\n");
    buf.append("    public void foo() {\n");
    buf.append("        HashMap m;\n");
    buf.append("        ArrayList l;\n");
    buf.append("        Collection c;\n");
    buf.append("        Socket s;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
    String[] order = new String[] { "java", "pack" };
    IChooseImportQuery query = createQuery("C", new String[] {}, new int[] {});
    OrganizeImportsOperation op = createOperation(cu, order, 2, false, true, true, query);
    op.run(null);
    buf = new StringBuffer();
    buf.append("package pack1;\n");
    buf.append("\n");
    buf.append("import java.net.Socket;\n");
    buf.append("import java.util.*;\n");
    buf.append("\n");
    buf.append("public class C {\n");
    buf.append("    public void foo() {\n");
    buf.append("        HashMap m;\n");
    buf.append("        ArrayList l;\n");
    buf.append("        Collection c;\n");
    buf.append("        Socket s;\n");
    buf.append("    }\n");
    buf.append("}\n");
    assertEqualString(cu.getSource(), buf.toString());
    assertEquals(1, op.getNumberOfImportsAdded());
    assertEquals(0, op.getNumberOfImportsRemoved());
}
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

IChooseImportQuery (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery)71 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)70 OrganizeImportsOperation (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation)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 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 TypeNameMatch (org.eclipse.jdt.core.search.TypeNameMatch)1