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;
}
};
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
Aggregations