Search in sources :

Example 1 with IChooseImportQuery

use of org.eclipse.jdt.core.manipulation.OrganizeImportsOperation.IChooseImportQuery in project eclipse.jdt.ls by eclipse.

the class ImportOrganizeTest method createQuery.

protected IChooseImportQuery createQuery(final String name, final String[] choices, final int[] nEntries) {
    return new IChooseImportQuery() {

        @Override
        public TypeNameMatch[] chooseImports(TypeNameMatch[][] openChoices, ISourceRange[] ranges) {
            assertTrue(name + "-query-nchoices1", choices.length == openChoices.length);
            assertTrue(name + "-query-nchoices2", nEntries.length == openChoices.length);
            for (int i = 0; i < nEntries.length; i++) {
                assertTrue(name + "-query-cnt" + i, openChoices[i].length == nEntries[i]);
            }
            TypeNameMatch[] res = new TypeNameMatch[openChoices.length];
            for (int i = 0; i < openChoices.length; i++) {
                TypeNameMatch[] selection = openChoices[i];
                assertNotNull(name + "-query-setset" + i, selection);
                assertTrue(name + "-query-setlen" + i, selection.length > 0);
                TypeNameMatch found = null;
                for (int k = 0; k < selection.length; k++) {
                    if (selection[k].getFullyQualifiedName().equals(choices[i])) {
                        found = selection[k];
                    }
                }
                assertNotNull(name + "-query-notfound" + i, found);
                res[i] = found;
            }
            return res;
        }
    };
}
Also used : TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) IChooseImportQuery(org.eclipse.jdt.core.manipulation.OrganizeImportsOperation.IChooseImportQuery)

Example 2 with IChooseImportQuery

use of org.eclipse.jdt.core.manipulation.OrganizeImportsOperation.IChooseImportQuery in project eclipse.jdt.ls by eclipse.

the class ImportOrganizeTest method testStaticImports1.

@Test
public void testStaticImports1() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("pack1", false, null);
    StringBuilder buf = new StringBuilder();
    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, false, true, true, query);
    TextEdit edit = op.createTextEdit(new NullProgressMonitor());
    IDocument document = new Document(cu.getSource());
    edit.apply(document);
    try {
        cu.becomeWorkingCopy(new NullProgressMonitor());
        cu.getBuffer().setContents(document.get());
        cu.reconcile(ICompilationUnit.NO_AST, true, null, new NullProgressMonitor());
        buf = new StringBuilder();
        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");
        assertTrue(cu.getSource().equals(buf.toString()));
    } finally {
        cu.discardWorkingCopy();
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) OrganizeImportsOperation(org.eclipse.jdt.core.manipulation.OrganizeImportsOperation) TextEdit(org.eclipse.text.edits.TextEdit) IChooseImportQuery(org.eclipse.jdt.core.manipulation.OrganizeImportsOperation.IChooseImportQuery) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument) AbstractMavenBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractMavenBasedTest) Test(org.junit.Test)

Example 3 with IChooseImportQuery

use of org.eclipse.jdt.core.manipulation.OrganizeImportsOperation.IChooseImportQuery in project eclipse.jdt.ls by eclipse.

the class ImportOrganizeTest method testStaticImports2.

@Test
public void testStaticImports2() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("pack1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package pack1;\n");
    buf.append("\n");
    buf.append("import static java.io.File.*;\n");
    buf.append("\n");
    buf.append("public class C {\n");
    buf.append("    public String foo() {\n");
    buf.append("        return pathSeparator + separator + File.separator;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
    String[] order = new String[] { "#java.io.File", "java", "pack" };
    IChooseImportQuery query = createQuery("C", new String[] {}, new int[] {});
    OrganizeImportsOperation op = createOperation(cu, order, false, true, true, query);
    TextEdit edit = op.createTextEdit(new NullProgressMonitor());
    IDocument document = new Document(cu.getSource());
    edit.apply(document);
    try {
        cu.becomeWorkingCopy(new NullProgressMonitor());
        cu.getBuffer().setContents(document.get());
        cu.reconcile(ICompilationUnit.NO_AST, true, null, new NullProgressMonitor());
        buf = new StringBuilder();
        buf.append("package pack1;\n");
        buf.append("\n");
        buf.append("import static java.io.File.pathSeparator;\n");
        buf.append("import static java.io.File.separator;\n");
        buf.append("\n");
        buf.append("import java.io.File;\n");
        buf.append("\n");
        buf.append("public class C {\n");
        buf.append("    public String foo() {\n");
        buf.append("        return pathSeparator + separator + File.separator;\n");
        buf.append("    }\n");
        buf.append("}\n");
        assertTrue(cu.getSource().equals(buf.toString()));
    } finally {
        cu.discardWorkingCopy();
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) OrganizeImportsOperation(org.eclipse.jdt.core.manipulation.OrganizeImportsOperation) TextEdit(org.eclipse.text.edits.TextEdit) IChooseImportQuery(org.eclipse.jdt.core.manipulation.OrganizeImportsOperation.IChooseImportQuery) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument) AbstractMavenBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractMavenBasedTest) Test(org.junit.Test)

Example 4 with IChooseImportQuery

use of org.eclipse.jdt.core.manipulation.OrganizeImportsOperation.IChooseImportQuery in project eclipse.jdt.ls by eclipse.

the class ImportOrganizeTest method test1.

@Test
public void test1() throws Exception {
    requireJUnitSources();
    ICompilationUnit cu = (ICompilationUnit) javaProject.findElement(new Path("junit/runner/BaseTestRunner.java"));
    assertNotNull("BaseTestRunner.java", cu);
    IPackageFragmentRoot root = (IPackageFragmentRoot) cu.getParent().getParent();
    IPackageFragment pack = root.createPackageFragment("mytest", true, null);
    ICompilationUnit colidingCU = pack.getCompilationUnit("TestListener.java");
    colidingCU.createType("public abstract class TestListener {\n}\n", null, true, null);
    String[] order = new String[0];
    IChooseImportQuery query = createQuery("BaseTestRunner", new String[] { "junit.framework.TestListener" }, new int[] { 2 });
    OrganizeImportsOperation op = createOperation(cu, order, false, true, true, query);
    TextEdit edit = op.createTextEdit(new NullProgressMonitor());
    IDocument document = new Document(cu.getSource());
    edit.apply(document);
    try {
        cu.becomeWorkingCopy(new NullProgressMonitor());
        cu.getBuffer().setContents(document.get());
        cu.reconcile(ICompilationUnit.NO_AST, true, null, new NullProgressMonitor());
        // @formatter:off
        assertImports(cu, new String[] { "java.io.BufferedReader", "java.io.File", "java.io.FileInputStream", "java.io.FileOutputStream", "java.io.IOException", "java.io.InputStream", "java.io.PrintWriter", "java.io.StringReader", "java.io.StringWriter", "java.lang.reflect.InvocationTargetException", "java.lang.reflect.Method", "java.lang.reflect.Modifier", "java.text.NumberFormat", "java.util.Properties", "junit.framework.AssertionFailedError", "junit.framework.Test", "junit.framework.TestListener", "junit.framework.TestSuite" });
    // @formatter:on
    } finally {
        cu.discardWorkingCopy();
    }
}
Also used : Path(org.eclipse.core.runtime.Path) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) OrganizeImportsOperation(org.eclipse.jdt.core.manipulation.OrganizeImportsOperation) TextEdit(org.eclipse.text.edits.TextEdit) IChooseImportQuery(org.eclipse.jdt.core.manipulation.OrganizeImportsOperation.IChooseImportQuery) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) AbstractMavenBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractMavenBasedTest) Test(org.junit.Test)

Example 5 with IChooseImportQuery

use of org.eclipse.jdt.core.manipulation.OrganizeImportsOperation.IChooseImportQuery in project eclipse.jdt.ls by eclipse.

the class ImportOrganizeTest method test1WithOrder.

@Test
public void test1WithOrder() throws Exception {
    requireJUnitSources();
    ICompilationUnit cu = (ICompilationUnit) javaProject.findElement(new Path("junit/runner/BaseTestRunner.java"));
    assertNotNull("BaseTestRunner.java is missing", cu);
    IPackageFragmentRoot root = (IPackageFragmentRoot) cu.getParent().getParent();
    IPackageFragment pack = root.createPackageFragment("mytest", true, null);
    ICompilationUnit colidingCU = pack.getCompilationUnit("TestListener.java");
    colidingCU.createType("public abstract class TestListener {\n}\n", null, true, null);
    String[] order = new String[] { "junit", "java.text", "java.io", "java" };
    IChooseImportQuery query = createQuery("BaseTestRunner", new String[] { "junit.framework.TestListener" }, new int[] { 2 });
    OrganizeImportsOperation op = createOperation(cu, order, false, true, true, query);
    TextEdit edit = op.createTextEdit(new NullProgressMonitor());
    IDocument document = new Document(cu.getSource());
    edit.apply(document);
    try {
        cu.becomeWorkingCopy(new NullProgressMonitor());
        cu.getBuffer().setContents(document.get());
        cu.reconcile(ICompilationUnit.NO_AST, true, null, new NullProgressMonitor());
        // @formatter:off
        assertImports(cu, new String[] { "junit.framework.AssertionFailedError", "junit.framework.Test", "junit.framework.TestListener", "junit.framework.TestSuite", "java.text.NumberFormat", "java.io.BufferedReader", "java.io.File", "java.io.FileInputStream", "java.io.FileOutputStream", "java.io.IOException", "java.io.InputStream", "java.io.PrintWriter", "java.io.StringReader", "java.io.StringWriter", "java.lang.reflect.InvocationTargetException", "java.lang.reflect.Method", "java.lang.reflect.Modifier", "java.util.Properties" });
    // @formatter:on
    } finally {
        cu.discardWorkingCopy();
    }
}
Also used : Path(org.eclipse.core.runtime.Path) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) OrganizeImportsOperation(org.eclipse.jdt.core.manipulation.OrganizeImportsOperation) TextEdit(org.eclipse.text.edits.TextEdit) IChooseImportQuery(org.eclipse.jdt.core.manipulation.OrganizeImportsOperation.IChooseImportQuery) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) AbstractMavenBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractMavenBasedTest) Test(org.junit.Test)

Aggregations

IChooseImportQuery (org.eclipse.jdt.core.manipulation.OrganizeImportsOperation.IChooseImportQuery)5 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4 OrganizeImportsOperation (org.eclipse.jdt.core.manipulation.OrganizeImportsOperation)4 AbstractMavenBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractMavenBasedTest)4 Document (org.eclipse.jface.text.Document)4 IDocument (org.eclipse.jface.text.IDocument)4 TextEdit (org.eclipse.text.edits.TextEdit)4 Test (org.junit.Test)4 Path (org.eclipse.core.runtime.Path)2 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)2 TypeNameMatch (org.eclipse.jdt.core.search.TypeNameMatch)1