use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal in project eclipse.jdt.ui by eclipse-jdt.
the class AssistQuickFixTest method testAssignParamToFieldInGeneric.
@Test
public void testAssignParamToFieldInGeneric() throws Exception {
Preferences corePrefs = JavaPlugin.getJavaCorePluginPreferences();
corePrefs.setValue(JavaCore.CODEASSIST_FIELD_PREFIXES, "f");
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("import java.util.Vector;\n");
buf.append("public class E<T> {\n");
buf.append(" public E(int count, Vector<String>[] vec) {\n");
buf.append(" super();\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
String selection = "vec";
int offset = buf.toString().indexOf(selection);
AssistContext context = getCorrectionContext(cu, offset, selection.length());
List<IJavaCompletionProposal> proposals = collectAssists(context, false);
assertNumberOfProposals(proposals, 2);
assertCorrectLabels(proposals);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("import java.util.Vector;\n");
buf.append("public class E<T> {\n");
buf.append(" private Vector<String>[] fVec;\n");
buf.append("\n");
buf.append(" public E(int count, Vector<String>[] vec) {\n");
buf.append(" super();\n");
buf.append(" fVec = vec;\n");
buf.append(" }\n");
buf.append("}\n");
String ex1 = buf.toString();
assertExpectedExistInProposals(proposals, new String[] { ex1 });
}
use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal in project eclipse.jdt.ui by eclipse-jdt.
the class AssistQuickFixTest method testExtractToMethod3.
@Test
public void testExtractToMethod3() throws Exception {
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=41302
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" void foo() {\n");
buf.append(" int a = 1;\n");
buf.append(" int b = 1;\n");
buf.append(" int d = a + b;\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
int offset1 = buf.toString().indexOf("int a = 1;");
int offset2 = buf.toString().indexOf("a + b;") + 6;
AssistContext context = getCorrectionContext(cu, offset1, offset2 - offset1);
List<IJavaCompletionProposal> proposals = collectAssists(context, false);
assertNumberOfProposals(proposals, 3);
assertCorrectLabels(proposals);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" void foo() {\n");
buf.append(" extracted();\n");
buf.append(" }\n");
buf.append("\n");
buf.append(" private void extracted() {\n");
buf.append(" int a = 1;\n");
buf.append(" int b = 1;\n");
buf.append(" int d = a + b;\n");
buf.append(" }\n");
buf.append("}\n");
String ex1 = buf.toString();
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" void foo() {\n");
buf.append(" final int a = 1;\n");
buf.append(" final int b = 1;\n");
buf.append(" final int d = a + b;\n");
buf.append(" }\n");
buf.append("}\n");
String ex2 = buf.toString();
// Wrap in buf.append() (to clipboard)
String ex3 = null;
assertExpectedExistInProposals(proposals, new String[] { ex1, ex2, ex3 });
}
use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal in project eclipse.jdt.ui by eclipse-jdt.
the class AssistQuickFixTest method testInvertEquals6.
@Test
public void testInvertEquals6() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("class A {\n");
buf.append(" static String get() {\n");
buf.append(" return \"a\";\n");
buf.append(" }\n");
buf.append("}\n");
buf.append("public class E {\n");
buf.append(" public void foo() {\n");
buf.append(" \"a\".equals(A.get());\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
String str = "equals";
AssistContext context = getCorrectionContext(cu, buf.toString().indexOf(str), 0);
List<IJavaCompletionProposal> proposals = collectAssists(context, FILTER_EQ);
assertNumberOfProposals(proposals, 1);
assertCorrectLabels(proposals);
cu = pack1.createCompilationUnit("E.java", buf.toString(), true, null);
CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
String preview = getPreviewContent(proposal);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("class A {\n");
buf.append(" static String get() {\n");
buf.append(" return \"a\";\n");
buf.append(" }\n");
buf.append("}\n");
buf.append("public class E {\n");
buf.append(" public void foo() {\n");
buf.append(" A.get().equals(\"a\");\n");
buf.append(" }\n");
buf.append("}\n");
assertEqualString(preview, buf.toString());
cu = pack1.createCompilationUnit("E.java", buf.toString(), true, null);
context = getCorrectionContext(cu, buf.toString().indexOf(str), 0);
proposals = collectAssists(context, FILTER_EQ);
assertNumberOfProposals(proposals, 1);
assertCorrectLabels(proposals);
proposal = (CUCorrectionProposal) proposals.get(0);
preview = getPreviewContent(proposal);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("class A {\n");
buf.append(" static String get() {\n");
buf.append(" return \"a\";\n");
buf.append(" }\n");
buf.append("}\n");
buf.append("public class E {\n");
buf.append(" public void foo() {\n");
buf.append(" \"a\".equals(A.get());\n");
buf.append(" }\n");
buf.append("}\n");
assertEqualString(preview, buf.toString());
}
use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal in project eclipse.jdt.ui by eclipse-jdt.
the class AssistQuickFixTest method testAssignToLocal16.
// bug 217984
@Test
public void testAssignToLocal16() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("import java.util.ArrayList;\n");
buf.append("import java.util.List;\n");
buf.append("import java.util.RandomAccess;\n");
buf.append("import java.util.Vector;\n");
buf.append("\n");
buf.append("class Gen<E extends List<String> & RandomAccess> extends ArrayList<E> {\n");
buf.append(" void foo() {\n");
buf.append(" Gen<? super Vector<String>> gs = new Gen<>();\n");
buf.append(" gs.get(0);\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("Gen.java", buf.toString(), false, null);
String str = "gs.get(0)";
AssistContext context = getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0);
List<IJavaCompletionProposal> proposals = collectAssists(context, false);
assertNumberOfProposals(proposals, 7);
assertCorrectLabels(proposals);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("import java.util.ArrayList;\n");
buf.append("import java.util.List;\n");
buf.append("import java.util.RandomAccess;\n");
buf.append("import java.util.Vector;\n");
buf.append("\n");
buf.append("class Gen<E extends List<String> & RandomAccess> extends ArrayList<E> {\n");
buf.append(" void foo() {\n");
buf.append(" Gen<? super Vector<String>> gs = new Gen<>();\n");
buf.append(" List<String> list = gs.get(0);\n");
buf.append(" }\n");
buf.append("}\n");
String expected1 = buf.toString();
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("import java.util.ArrayList;\n");
buf.append("import java.util.List;\n");
buf.append("import java.util.RandomAccess;\n");
buf.append("import java.util.Vector;\n");
buf.append("\n");
buf.append("class Gen<E extends List<String> & RandomAccess> extends ArrayList<E> {\n");
buf.append(" private List<String> list;\n");
buf.append("\n");
buf.append(" void foo() {\n");
buf.append(" Gen<? super Vector<String>> gs = new Gen<>();\n");
buf.append(" list = gs.get(0);\n");
buf.append(" }\n");
buf.append("}\n");
String expected2 = buf.toString();
assertExpectedExistInProposals(proposals, new String[] { expected1, expected2 });
}
use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal in project eclipse.jdt.ui by eclipse-jdt.
the class AssistQuickFixTest method testConvertToStringBufferNoFixWithoutString.
@Test
public void testConvertToStringBufferNoFixWithoutString() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class A {\n");
buf.append(" public void foo() {\n");
buf.append(" int strX = 5+1;\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("A.java", buf.toString(), false, null);
AssistContext context = getCorrectionContext(cu, buf.toString().indexOf("strX ="), 0);
List<IJavaCompletionProposal> proposals = collectAssists(context, false);
assertCommandIdDoesNotExist(proposals, QuickAssistProcessor.CONVERT_TO_STRING_BUFFER_ID);
}
Aggregations