Search in sources :

Example 36 with IPackageFragmentRoot

use of org.eclipse.jdt.core.IPackageFragmentRoot 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 37 with IPackageFragmentRoot

use of org.eclipse.jdt.core.IPackageFragmentRoot 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 38 with IPackageFragmentRoot

use of org.eclipse.jdt.core.IPackageFragmentRoot 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 39 with IPackageFragmentRoot

use of org.eclipse.jdt.core.IPackageFragmentRoot 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 40 with IPackageFragmentRoot

use of org.eclipse.jdt.core.IPackageFragmentRoot 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

IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)179 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)96 Test (org.junit.Test)95 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)78 OrganizeImportsOperation (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation)67 IChooseImportQuery (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery)67 IJavaProject (org.eclipse.jdt.core.IJavaProject)37 File (java.io.File)26 IJavaElement (org.eclipse.jdt.core.IJavaElement)22 IResource (org.eclipse.core.resources.IResource)17 JarEntry (org.eclipse.che.ide.ext.java.shared.JarEntry)16 IPath (org.eclipse.core.runtime.IPath)15 ArrayList (java.util.ArrayList)13 IType (org.eclipse.jdt.core.IType)12 CoreException (org.eclipse.core.runtime.CoreException)11 Path (org.eclipse.core.runtime.Path)11 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)11 JavaModelException (org.eclipse.jdt.core.JavaModelException)10 IFile (org.eclipse.core.resources.IFile)9 IProject (org.eclipse.core.resources.IProject)9