Search in sources :

Example 26 with OrganizeImportsOperation

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

the class ImportOrganizeTest method testImportCountAddNew.

@Test
public void testImportCountAddNew() 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("public class C {\n");
    buf.append("    public void foo() {\n");
    buf.append("        HashMap m;\n");
    buf.append("        ArrayList l;\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, 99, false, true, true, query);
    op.run(null);
    buf = new StringBuffer();
    buf.append("package pack1;\n");
    buf.append("\n");
    buf.append("import java.util.ArrayList;\n");
    buf.append("import java.util.HashMap;\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("    }\n");
    buf.append("}\n");
    assertEqualString(cu.getSource(), buf.toString());
    assertEquals(2, 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)

Example 27 with OrganizeImportsOperation

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

the class ImportOrganizeTest method testImportCountKeepStatic.

@Test
public void testImportCountKeepStatic() 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.BitSet;\n");
    buf.append("// some comment;\n");
    buf.append("import java.util.Calendar; /*another comment*/\n");
    buf.append("import static java.io.File.pathSeparator;\n");
    buf.append("import java.util.*;\n");
    buf.append("\n");
    buf.append("public class C {\n");
    buf.append("    public void foo() {\n");
    buf.append("        String s= pathSeparator;\n");
    buf.append("        HashMap m;\n");
    buf.append("        ArrayList l;\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, 99, false, true, true, query);
    op.run(null);
    buf = new StringBuffer();
    buf.append("package pack1;\n");
    buf.append("\n");
    buf.append("import java.util.ArrayList;\n");
    buf.append("import java.util.HashMap;\n");
    buf.append("\n");
    buf.append("import static java.io.File.pathSeparator;\n");
    buf.append("// some comment;\n");
    buf.append("/*another comment*/\n");
    buf.append("\n");
    buf.append("public class C {\n");
    buf.append("    public void foo() {\n");
    buf.append("        String s= pathSeparator;\n");
    buf.append("        HashMap m;\n");
    buf.append("        ArrayList l;\n");
    buf.append("    }\n");
    buf.append("}\n");
    assertEqualString(cu.getSource(), buf.toString());
    assertEquals(2, op.getNumberOfImportsAdded());
    assertEquals(3, 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)

Example 28 with OrganizeImportsOperation

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

the class ImportOrganizeTest method testStaticImports_bug113770.

@Test
public void testStaticImports_bug113770() throws Exception {
    IPackageFragmentRoot sourceFolder = JavaProjectHelper.addSourceContainer(fJProject1, "src");
    IPackageFragment pack0 = sourceFolder.createPackageFragment("pack0", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test;\n");
    buf.append("\n");
    buf.append("public abstract class Test<M>\n");
    buf.append("{\n");
    buf.append("        private static Map<Object, Object[]> facetMap;\n");
    buf.append("\n");
    buf.append("        public void getFacets() {\n");
    buf.append("                facetMap.get(null);\n");
    buf.append("        }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack0.createCompilationUnit("Test.java", buf.toString(), false, null);
    String[] order = new String[] {};
    IChooseImportQuery query = createQuery("Test", new String[] {}, new int[] {});
    OrganizeImportsOperation op = createOperation(cu, order, 99, false, true, true, query);
    op.run(null);
    buf = new StringBuffer();
    buf.append("package test;\n");
    buf.append("\n");
    buf.append("import java.util.Map;\n");
    buf.append("\n");
    buf.append("public abstract class Test<M>\n");
    buf.append("{\n");
    buf.append("        private static Map<Object, Object[]> facetMap;\n");
    buf.append("\n");
    buf.append("        public void getFacets() {\n");
    buf.append("                facetMap.get(null);\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 29 with OrganizeImportsOperation

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

the class ImportOrganizeTest method test_bug135122.

@Test
public void test_bug135122() 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("public class Foo extends Bar {\n");
    buf.append("  public static final int MYCONSTANT= 9;\n");
    buf.append("\n");
    buf.append("  public void anotherMethod() {\n");
    buf.append("    super.testMethod(MYCONSTANT);\n");
    buf.append("  }\n");
    buf.append("}\n");
    buf.append("\n");
    buf.append("class Bar {\n");
    buf.append("    public void testMethod(int something) {\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("Foo.java", buf.toString(), false, null);
    String[] order = new String[] { "", "#" };
    IChooseImportQuery query = createQuery("Foo", new String[] {}, new int[] {});
    OrganizeImportsOperation op = createOperation(cu, order, 99, false, true, true, query);
    op.run(null);
    buf = new StringBuffer();
    buf.append("package pack1;\n");
    buf.append("\n");
    buf.append("public class Foo extends Bar {\n");
    buf.append("  public static final int MYCONSTANT= 9;\n");
    buf.append("\n");
    buf.append("  public void anotherMethod() {\n");
    buf.append("    super.testMethod(MYCONSTANT);\n");
    buf.append("  }\n");
    buf.append("}\n");
    buf.append("\n");
    buf.append("class Bar {\n");
    buf.append("    public void testMethod(int something) {\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 30 with OrganizeImportsOperation

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

the class ImportOrganizeTest method test_bug25113.

@Test
public void test_bug25113() throws Exception {
    String[] types = new String[] { "com.mycompany.Class1", "com.foreigncompany.Class2", "com.foreigncompany.Class3", "com.mycompany.Class4", "com.misc.Class5" };
    String[] order = new String[] { "com", "com.foreigncompany", "com.mycompany" };
    int threshold = 99;
    IPackageFragmentRoot sourceFolder = JavaProjectHelper.addSourceContainer(fJProject1, "src");
    for (int i = 0; i < types.length; i++) {
        String pack = Signature.getQualifier(types[i]);
        if (!pack.startsWith("java.")) {
            String name = Signature.getSimpleName(types[i]);
            IPackageFragment pack2 = sourceFolder.createPackageFragment(pack, false, null);
            StringBuffer buf = new StringBuffer();
            buf.append("package ");
            buf.append(pack);
            buf.append(";\n");
            buf.append("public class ");
            buf.append(name);
            buf.append(" {\n");
            buf.append("}\n");
            pack2.createCompilationUnit(name + ".java", buf.toString(), false, null);
        }
    }
    StringBuffer body = new StringBuffer();
    body.append("public class C {\n");
    for (int i = 0; i < types.length; i++) {
        String name = Signature.getSimpleName(types[i]);
        body.append(name);
        body.append(" a");
        body.append(i);
        body.append(";\n");
    }
    body.append("}\n");
    IPackageFragment pack1 = sourceFolder.createPackageFragment("pack1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package pack1;\n");
    buf.append("\n");
    buf.append(body.toString());
    ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
    IChooseImportQuery query = createQuery("C", new String[] {}, new int[] {});
    OrganizeImportsOperation op = createOperation(cu, order, threshold, false, true, true, query);
    op.run(null);
    buf = new StringBuffer();
    buf.append("package pack1;\n");
    buf.append("\n");
    buf.append("import com.misc.Class5;\n");
    buf.append("\n");
    buf.append("import com.foreigncompany.Class2;\n");
    buf.append("import com.foreigncompany.Class3;\n");
    buf.append("\n");
    buf.append("import com.mycompany.Class1;\n");
    buf.append("import com.mycompany.Class4;\n");
    buf.append("\n");
    buf.append(body.toString());
    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)

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