Search in sources :

Example 41 with IChooseImportQuery

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

the class ImportOrganizeTest method testVisibility_bug26746.

@Test
public void testVisibility_bug26746() throws Exception {
    IPackageFragmentRoot sourceFolder = JavaProjectHelper.addSourceContainer(fJProject1, "src");
    IPackageFragment pack2 = sourceFolder.createPackageFragment("pack0", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package pack0;\n");
    buf.append("public interface MyInterface {\n");
    buf.append("	public interface MyInnerInterface {\n");
    buf.append("	}\n");
    buf.append("}\n");
    pack2.createCompilationUnit("MyInterface.java", buf.toString(), false, null);
    buf = new StringBuffer();
    buf.append("package pack0;\n");
    buf.append("\n");
    buf.append("import pack0.MyInterface.MyInnerInterface;\n");
    buf.append("public class MyClass implements MyInterface {\n");
    buf.append("	public MyInnerInterface myMethod() {\n");
    buf.append("		return null;\n");
    buf.append("	}\n");
    buf.append("}\n");
    ICompilationUnit cu = pack2.createCompilationUnit("MyClass.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("public class MyClass implements MyInterface {\n");
    buf.append("	public MyInnerInterface myMethod() {\n");
    buf.append("		return 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 42 with IChooseImportQuery

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

the class ImportOrganizeTest method test_bug25773.

@Test
public void test_bug25773() throws Exception {
    String[] types = new String[] { "java.util.Vector", "java.util.Map", "java.util.Set", "org.eclipse.gef.X1", "org.eclipse.gef.X2", "org.eclipse.gef.X3", "org.eclipse.core.runtime.IAdaptable", "org.eclipse.draw2d.IFigure", "org.eclipse.draw2d.LayoutManager", "org.eclipse.draw2d.geometry.Point", "org.eclipse.draw2d.geometry.Rectangle", "org.eclipse.swt.accessibility.ACC", "org.eclipse.swt.accessibility.AccessibleControlEvent" };
    String[] order = new String[] { "java", "org.eclipse", "org.eclipse.gef", "org.eclipse.draw2d", "org.eclipse.gef.examples" };
    int threshold = 3;
    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 java.util.*;\n");
    buf.append("\n");
    buf.append("import org.eclipse.core.runtime.IAdaptable;\n");
    buf.append("import org.eclipse.swt.accessibility.ACC;\n");
    buf.append("import org.eclipse.swt.accessibility.AccessibleControlEvent;\n");
    buf.append("\n");
    buf.append("import org.eclipse.gef.*;\n");
    buf.append("\n");
    buf.append("import org.eclipse.draw2d.IFigure;\n");
    buf.append("import org.eclipse.draw2d.LayoutManager;\n");
    buf.append("import org.eclipse.draw2d.geometry.Point;\n");
    buf.append("import org.eclipse.draw2d.geometry.Rectangle;\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)

Example 43 with IChooseImportQuery

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

the class ImportOrganizeTest method testStaticImports_bug78585.

@Test
public void testStaticImports_bug78585() 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("public class Test1 {\n");
    buf.append("	public static final <T> void assertNotEquals(final String msg, final T expected, final T toCheck) {\n");
    buf.append("	}\n");
    buf.append("}\n");
    pack0.createCompilationUnit("Test1.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 pack0.Test1;\n");
    buf.append("import java.util.List;\n");
    buf.append("\n");
    buf.append("public class Test2 extends Test1 {\n");
    buf.append("	public void testMe() {\n");
    buf.append("	    assertNotEquals(\"A\", \"B\", \"C\");\n");
    buf.append("	}\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("Test2.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 pack1;\n");
    buf.append("\n");
    // no static import for 'assertNotEquals'
    buf.append("import pack0.Test1;\n");
    buf.append("\n");
    buf.append("public class Test2 extends Test1 {\n");
    buf.append("	public void testMe() {\n");
    buf.append("	    assertNotEquals(\"A\", \"B\", \"C\");\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 44 with IChooseImportQuery

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

the class ImportOrganizeTest method testReplaceImportsNoPackage.

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

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

the class ImportOrganizeTest method testTypeArgumentImports.

@Test
public void testTypeArgumentImports() 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 B {\n");
    buf.append("	   public B() {\n");
    // constructor invocation
    buf.append("        <File> this(null);\n");
    buf.append("    }\n");
    buf.append("    public <T> B(T t) {\n");
    buf.append("    }\n");
    buf.append("    public <T> void foo(T t) {\n");
    // method invocation
    buf.append("        this.<Socket> foo(null);\n");
    // class instance creation
    buf.append("        new<URL> B(null);\n");
    buf.append("    }\n");
    buf.append("    class C extends B {\n");
    buf.append("        public C() {\n");
    // super constructor invocation
    buf.append("            <Vector> super(null);\n");
    // super method invocation
    buf.append("            super.<HashMap> foo(null);\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("B.java", buf.toString(), false, null);
    String[] order = new String[] { "", "#" };
    IChooseImportQuery query = createQuery("B", 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.io.File;\n");
    buf.append("import java.net.Socket;\n");
    buf.append("import java.net.URL;\n");
    buf.append("import java.util.HashMap;\n");
    buf.append("import java.util.Vector;\n");
    buf.append("\n");
    buf.append("public class B {\n");
    buf.append("	   public B() {\n");
    buf.append("        <File> this(null);\n");
    buf.append("    }\n");
    buf.append("    public <T> B(T t) {\n");
    buf.append("    }\n");
    buf.append("    public <T> void foo(T t) {\n");
    buf.append("        this.<Socket> foo(null);\n");
    buf.append("        new<URL> B(null);\n");
    buf.append("    }\n");
    buf.append("    class C extends B {\n");
    buf.append("        public C() {\n");
    buf.append("            <Vector> super(null);\n");
    buf.append("            super.<HashMap> foo(null);\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)

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