Search in sources :

Example 26 with IPackageFragmentRoot

use of org.eclipse.jdt.core.IPackageFragmentRoot in project che by eclipse.

the class ImportOrganizeTest method testImportOfMemberFromLocal.

@Test
public void testImportOfMemberFromLocal() 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("        class Local {\n");
    buf.append("            class LocalMember {\n");
    buf.append("            }\n");
    buf.append("            LocalMember x;\n");
    buf.append("            Vector v;\n");
    buf.append("        }\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.Vector;\n");
    buf.append("\n");
    buf.append("public class C {\n");
    buf.append("    public void foo() {\n");
    buf.append("        class Local {\n");
    buf.append("            class LocalMember {\n");
    buf.append("            }\n");
    buf.append("            LocalMember x;\n");
    buf.append("            Vector v;\n");
    buf.append("        }\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 27 with IPackageFragmentRoot

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

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

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

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

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