Search in sources :

Example 56 with OrganizeImportsOperation

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

the class ImportOrganizeTest method testReplaceImports.

@Test
public void testReplaceImports() 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("\n");
    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("package test1;\n");
    buf.append("\n");
    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 57 with OrganizeImportsOperation

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

the class ImportOrganizeTest method test_bug78397.

@Test
public void test_bug78397() 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("import java.util.Collection;\n");
    buf.append("public class A {\n");
    buf.append("    Collection<java.sql.Date> foo;\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("import java.util.Collection;\n");
    buf.append("public class A {\n");
    // no import for Date
    buf.append("    Collection<java.sql.Date> foo;\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 58 with OrganizeImportsOperation

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

the class ImportOrganizeTest method testVisibility_bug56704.

@Test
public void testVisibility_bug56704() 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 class A {\n");
    buf.append("	public class AX {\n");
    buf.append("	}\n");
    buf.append("}\n");
    pack2.createCompilationUnit("A.java", buf.toString(), false, null);
    buf = new StringBuffer();
    buf.append("package pack0;\n");
    buf.append("\n");
    buf.append("import pack0.A.AX;\n");
    buf.append("public class B extends A {\n");
    buf.append("	public class BX extends AX {\n");
    buf.append("	}\n");
    buf.append("}\n");
    ICompilationUnit cu = pack2.createCompilationUnit("B.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 B extends A {\n");
    buf.append("	public class BX extends AX {\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 59 with OrganizeImportsOperation

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

the class ImportOrganizeTest method testVisibility_bug131305.

@Test
public void testVisibility_bug131305() throws Exception {
    IPackageFragmentRoot sourceFolder = JavaProjectHelper.addSourceContainer(fJProject1, "src");
    IPackageFragment packUtil = sourceFolder.createPackageFragment("util", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package util;\n");
    buf.append("\n");
    buf.append("public interface Map \n");
    buf.append("        public static interface Entry {\n");
    buf.append("        }\n");
    buf.append("}\n");
    packUtil.createCompilationUnit("Map.java", buf.toString(), false, null);
    buf = new StringBuffer();
    buf.append("package util;\n");
    buf.append("\n");
    buf.append("public interface HashMap implements Map {\n");
    buf.append("        private static interface Entry {\n");
    buf.append("        }\n");
    buf.append("}\n");
    packUtil.createCompilationUnit("HashMap.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 util.HashMap;\n");
    buf.append("import util.Map;\n");
    buf.append("import util.Map.Entry;\n");
    buf.append("\n");
    buf.append("public class A extends HashMap {\n");
    buf.append("        public A(Map m, Entry e) {\n");
    buf.append("        }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("A.java", buf.toString(), false, null);
    String[] order = new String[] {};
    IChooseImportQuery query = createQuery("testVisibility_bug131305", new String[] {}, new int[] {});
    OrganizeImportsOperation op = createOperation(cu, order, 99, false, true, true, query);
    op.run(null);
    // no changes, import for Entry is required
    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 60 with OrganizeImportsOperation

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

the class ImportOrganizeTest method testStaticImports1.

@Test
public void testStaticImports1() 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 static java.lang.System.out;\n");
    buf.append("\n");
    buf.append("public class C {\n");
    buf.append("    public int foo() {\n");
    buf.append("        out.print(File.separator);\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
    String[] order = new String[] { "java", "pack", "#java" };
    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.io.File;\n");
    buf.append("\n");
    buf.append("import static java.lang.System.out;\n");
    buf.append("\n");
    buf.append("public class C {\n");
    buf.append("    public int foo() {\n");
    buf.append("        out.print(File.separator);\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

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