Java Code Examples for org.eclipse.jdt.core.IPackageFragment#createCompilationUnit()

The following examples show how to use org.eclipse.jdt.core.IPackageFragment#createCompilationUnit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DocumentLifeCycleHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testReconcile() throws Exception {
	IJavaProject javaProject = newEmptyProject();
	IPackageFragmentRoot sourceFolder = javaProject.getPackageFragmentRoot(javaProject.getProject().getFolder("src"));
	IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
	StringBuilder buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("public class E123 {\n");
	buf.append("    public void testing() {\n");
	buf.append("        int someIntegerChanged = 5;\n");
	buf.append("        int i = someInteger + 5\n");
	buf.append("    }\n");
	buf.append("}\n");
	ICompilationUnit cu1 = pack1.createCompilationUnit("E123.java", buf.toString(), false, null);
	openDocument(cu1, cu1.getSource(), 1);
	assertEquals(true, cu1.isWorkingCopy());
	assertEquals(false, cu1.hasUnsavedChanges());
	List<PublishDiagnosticsParams> diagnosticsParams = getClientRequests("publishDiagnostics");
	assertEquals(1, diagnosticsParams.size());
	PublishDiagnosticsParams diagnosticsParam = diagnosticsParams.get(0);
	List<Diagnostic> diagnostics = diagnosticsParam.getDiagnostics();
	assertEquals(2, diagnostics.size());
	diagnosticsParams.clear();
	closeDocument(cu1);
}
 
Example 2
Source File: JavaProjectUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Creates an {@link ICompilationUnit} with the given fully qualified name and
 * code in the <code>javaProject</code>.
 *
 * @param javaProject java project to host the new class
 * @param fullyQualifiedClassName fully qualified name for the class
 * @param source code for the classs
 * @return newly created {@link ICompilationUnit}
 * @throws JavaModelException
 */
public static ICompilationUnit createCompilationUnit(
    IJavaProject javaProject, String fullyQualifiedClassName, String source)
    throws JavaModelException {
  IPackageFragmentRoot root = javaProject.findPackageFragmentRoot(javaProject.getPath());
  if (root == null) {
    addRawClassPathEntry(javaProject,
        JavaCore.newSourceEntry(javaProject.getPath()));
    root = javaProject.findPackageFragmentRoot(javaProject.getPath());
  }

  String qualifier = Signature.getQualifier(fullyQualifiedClassName);
  IProgressMonitor monitor = new NullProgressMonitor();
  IPackageFragment packageFragment = root.createPackageFragment(qualifier,
      true, monitor);
  String name = Signature.getSimpleName(fullyQualifiedClassName);
  ICompilationUnit cu = packageFragment.createCompilationUnit(name + ".java",
      source, false, monitor);
  JobsUtilities.waitForIdle();
  return cu;
}
 
Example 3
Source File: HoverHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testHoverTypeParameters() throws Exception {
	IPackageFragment pack1 = sourceFolder.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 T foo(T s) { }\n");
	buf.append("   public <U> U bar(U s) { }\n");
	buf.append("}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	assertEquals("T", getTitleHover(cu, 3, 10));
	assertEquals("T test1.E.foo(T s)", getTitleHover(cu, 3, 13));
	assertEquals("<U> U test1.E.bar(U s)", getTitleHover(cu, 4, 17));
}
 
Example 4
Source File: UnresolvedTypesQuickFixTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testTypeInAnnotation_bug153881() throws Exception {
	IPackageFragment pack1 = fSourceFolder.createPackageFragment("a", false, null);
	StringBuilder buf = new StringBuilder();
	buf.append("package a;\n");
	buf.append("public class SomeClass {\n");
	buf.append("        @scratch.Unimportant void foo() {}\n");
	buf.append("}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("SomeClass.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package scratch;\n");
	buf.append("\n");
	buf.append("public @interface Unimportant {\n");
	buf.append("\n");
	buf.append("}\n");
	Expected e1 = new Expected("Create annotation 'Unimportant' in package 'scratch'", buf.toString());

	assertCodeActions(cu, e1);
}
 
Example 5
Source File: HoverHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testHoverOverNullElement() throws Exception {
	IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
	StringBuilder buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("import javax.xml.bind.Binder;\n");
	buf.append("public class E {}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
	Hover hover = getHover(cu, 1, 8);
	assertNotNull(hover);
	assertEquals(1, hover.getContents().getLeft().size());
	assertEquals("Unexpected hover ", "javax", hover.getContents().getLeft().get(0).getRight().getValue());
}
 
Example 6
Source File: JavadocQuickFixTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testMissingFieldComment() throws Exception {
	Map<String, String> original = fJProject1.getOptions(false);
	HashMap<String, String> newOptions = new HashMap<>(original);
	this.setIgnoredCommands("Extract.*");
	// newOptions.put(JavaCore.COMPILER_PB_MISSING_JAVADOC_COMMENTS,
	// JavaCore.ERROR);
	// newOptions.put(JavaCore.COMPILER_PB_MISSING_JAVADOC_COMMENTS_VISIBILITY,
	// JavaCore.PUBLIC);
	fJProject1.setOptions(newOptions);

	IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
	StringBuilder buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("/**\n");
	buf.append(" */\n");
	buf.append("public class E {\n");
	buf.append("    public static final int COLOR= 1;\n");
	buf.append("}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("/**\n");
	buf.append(" */\n");
	buf.append("public class E {\n");
	buf.append("    /**\n");
	buf.append("     *\n");
	buf.append("     */\n");
	buf.append("    public static final int COLOR= 1;\n");
	buf.append("}\n");
	Expected e1 = new Expected("Add Javadoc comment", buf.toString());
	assertCodeActions(cu, e1);
}
 
Example 7
Source File: AbstractMethodQuickFixTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testAbstractMethodInEnum() throws Exception {
	IPackageFragment pack = fSourceFolder.createPackageFragment("test", false, null);
	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public enum TestEnum {\n");
	buf.append("    public abstract void TestMethod() {\n");
	buf.append("    }\n");
	buf.append("}\n");
	ICompilationUnit cu = pack.createCompilationUnit("TestEnum.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public enum TestEnum {\n");
	buf.append("    public void TestMethod() {\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e1 = new Expected("Remove 'abstract' modifier", buf.toString());

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public enum TestEnum {\n");
	buf.append("    public abstract void TestMethod();\n");
	buf.append("}\n");
	Expected e2 = new Expected("Remove method body", buf.toString());

	assertCodeActions(cu, e1, e2);
}
 
Example 8
Source File: LocalCorrectionQuickFixTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testRemoveUnreachableCodeStmt2() throws Exception {
	IPackageFragment pack1 = fSourceFolder.createPackageFragment("test", false, null);
	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public String getName() {\n");
	buf.append("        try{\n");
	buf.append("            return \"fred\";\n");
	buf.append("        }\n");
	buf.append("        catch (Exception e){\n");
	buf.append("            return e.getLocalizedMessage();\n");
	buf.append("        }\n");
	buf.append("        System.err.print(\"wow\");\n");
	buf.append("    }\n");
	buf.append("}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);


	String[] expected = new String[1];
	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public String getName() {\n");
	buf.append("        try{\n");
	buf.append("            return \"fred\";\n");
	buf.append("        }\n");
	buf.append("        catch (Exception e){\n");
	buf.append("            return e.getLocalizedMessage();\n");
	buf.append("        }\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e1 = new Expected("Remove", buf.toString());

	assertCodeActions(cu, e1);
}
 
Example 9
Source File: UnresolvedVariablesQuickFixTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testVarMultipleOccurances3() throws Exception {
	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("        for (i = 0; i > 9;) {\n");
	buf.append("        }\n");
	buf.append("        i= 9;\n");
	buf.append("    }\n");
	buf.append("}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("public class E {\n");
	buf.append("    void foo() {\n");
	buf.append("        int i;\n");
	buf.append("        for (i = 0; i > 9;) {\n");
	buf.append("        }\n");
	buf.append("        i= 9;\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e1 = new Expected("Create local variable 'i'", buf.toString());

	assertCodeActionExists(cu, e1);
}
 
Example 10
Source File: InlineMethodTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testInlineMethod_DeclarationSelected() throws Exception {
	IPackageFragment pack1 = testSourceFolder.createPackageFragment("test", false, null);

	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public String /*]*/foo/*[*/() {\n");
	buf.append("        String temp = \"method declaration\";\n");
	buf.append("        return temp;\n");
	buf.append("    }\n");
	buf.append("    public void bar() {\n");
	buf.append("        String value = foo();\n");
	buf.append("    }\n");
	buf.append("    public void bar2() {\n");
	buf.append("        String value = foo();\n");
	buf.append("    }\n");
	buf.append("}\n");

	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class E {\n");
	buf.append("    public void bar() {\n");
	buf.append("        String temp = \"method declaration\";\n");
	buf.append("        String value = temp;\n");
	buf.append("    }\n");
	buf.append("    public void bar2() {\n");
	buf.append("        String temp = \"method declaration\";\n");
	buf.append("        String value = temp;\n");
	buf.append("    }\n");
	buf.append("}\n");

	Expected expected = new Expected(INLINE_METHOD, buf.toString(), CodeActionKind.RefactorInline);
	assertCodeActions(cu, expected);
}
 
Example 11
Source File: PrepareRenameHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test(expected = ResponseErrorException.class)
public void testRenamePackage() throws JavaModelException, BadLocationException {
	when(clientPreferences.isResourceOperationSupported()).thenReturn(true);

	IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
	IPackageFragment pack2 = sourceFolder.createPackageFragment("parent.test2", false, null);

	String[] codes1= {
			"package test1;\n",
			"import parent.test2.B;\n",
			"public class A {\n",
			"   public void foo(){\n",
			"		B b = new B();\n",
			"		b.foo();\n",
			"	}\n",
			"}\n"
	};

	String[] codes2 = {
			"package parent.test2|*;\n",
			"public class B {\n",
			"	public B() {}\n",
			"   public void foo() {}\n",
			"}\n"
	};
	StringBuilder builderA = new StringBuilder();
	mergeCode(builderA, codes1);
	pack1.createCompilationUnit("A.java", builderA.toString(), false, null);

	StringBuilder builderB = new StringBuilder();
	Position pos = mergeCode(builderB, codes2);
	ICompilationUnit cuB = pack2.createCompilationUnit("B.java", builderB.toString(), false, null);

	prepareRename(cuB, pos, "parent.newpackage");
}
 
Example 12
Source File: UnresolvedMethodsQuickFixTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testArrayAccess() throws Exception {
	IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);

	StringBuilder buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("public class E {\n");
	buf.append("    private static Object obj;\n");
	buf.append("    public String foo(Object[] array) {\n");
	buf.append("        return array.tostring();\n");
	buf.append("    }\n");
	buf.append("}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("public class E {\n");
	buf.append("    private static Object obj;\n");
	buf.append("    public String foo(Object[] array) {\n");
	buf.append("        return array.length;\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e1 = new Expected("Change to 'length'", buf.toString());

	buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("public class E {\n");
	buf.append("    private static Object obj;\n");
	buf.append("    public String foo(Object[] array) {\n");
	buf.append("        return array.toString();\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e2 = new Expected("Change to 'toString(..)'", buf.toString());

	assertCodeActions(cu, e1, e2);

}
 
Example 13
Source File: UnresolvedVariablesQuickFixTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testSimilarVarsAndVisibility() throws Exception {
	IPackageFragment pack1 = fSourceFolder.createPackageFragment("test", false, null);
	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("\n");
	buf.append("public class E {\n");
	buf.append("    int var1;\n");
	buf.append("    static int var2;\n");
	buf.append("    public static void main(String[] var3) {\n");
	buf.append("        println(var);\n");
	buf.append("    }\n");
	buf.append("    public static void println(String[] s) {}\n");
	buf.append("}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("\n");
	buf.append("public class E {\n");
	buf.append("    int var1;\n");
	buf.append("    static int var2;\n");
	buf.append("    public static void main(String[] var3) {\n");
	buf.append("        String[] var;\n");
	buf.append("        println(var);\n");
	buf.append("    }\n");
	buf.append("    public static void println(String[] s) {}\n");
	buf.append("}\n");
	Expected e1 = new Expected("Create local variable 'var'", buf.toString());

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("\n");
	buf.append("public class E {\n");
	buf.append("    int var1;\n");
	buf.append("    static int var2;\n");
	buf.append("    private static String[] var;\n");
	buf.append("    public static void main(String[] var3) {\n");
	buf.append("        println(var);\n");
	buf.append("    }\n");
	buf.append("    public static void println(String[] s) {}\n");
	buf.append("}\n");
	Expected e2 = new Expected("Create field 'var'", buf.toString());

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("\n");
	buf.append("public class E {\n");
	buf.append("    int var1;\n");
	buf.append("    static int var2;\n");
	buf.append("    public static void main(String[] var3) {\n");
	buf.append("        println(var3);\n");
	buf.append("    }\n");
	buf.append("    public static void println(String[] s) {}\n");
	buf.append("}\n");
	Expected e3 = new Expected("Change to 'var3'", buf.toString());

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("\n");
	buf.append("public class E {\n");
	buf.append("    int var1;\n");
	buf.append("    static int var2;\n");
	buf.append("    public static void main(String[] var3, String[] var) {\n");
	buf.append("        println(var);\n");
	buf.append("    }\n");
	buf.append("    public static void println(String[] s) {}\n");
	buf.append("}\n");
	Expected e4 = new Expected("Create parameter 'var'", buf.toString());

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("\n");
	buf.append("public class E {\n");
	buf.append("    private static final String[] var = null;\n");
	buf.append("    int var1;\n");
	buf.append("    static int var2;\n");
	buf.append("    public static void main(String[] var3) {\n");
	buf.append("        println(var);\n");
	buf.append("    }\n");
	buf.append("    public static void println(String[] s) {}\n");
	buf.append("}\n");
	Expected e5 = new Expected("Create constant 'var'", buf.toString());

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("\n");
	buf.append("public class E {\n");
	buf.append("    int var1;\n");
	buf.append("    static int var2;\n");
	buf.append("    public static void main(String[] var3) {\n");
	buf.append("        println(var2);\n");
	buf.append("    }\n");
	buf.append("    public static void println(String[] s) {}\n");
	buf.append("}\n");
	Expected e6 = new Expected("Change to 'var2'", buf.toString());

	assertCodeActions(cu, e1, e2, e3, e4, e5, e6);
}
 
Example 14
Source File: StaticImportQuickAssistTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testConvertToStaticFieldImport() throws Exception {
	IPackageFragment pack = fSourceFolder.createPackageFragment("test", false, null);
	StringBuilder buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class A {\n");
	buf.append("	public static final String FOO = \"BAR\";\n");
	buf.append("}\n");
	pack.createCompilationUnit("A.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("public class B {\n");
	buf.append("	public String bar = A.FOO;\n");
	buf.append("	public String bar1 = A.FOO;\n");
	buf.append("}\n");
	ICompilationUnit cu = pack.createCompilationUnit("B.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("\n");
	buf.append("import static test.A.FOO;\n");
	buf.append("\n");
	buf.append("public class B {\n");
	buf.append("	public String bar = A.FOO;\n");
	buf.append("	public String bar1 = FOO;\n");
	buf.append("}\n");
	Expected e1 = new Expected("Convert to static import", buf.toString());

	buf = new StringBuilder();
	buf.append("package test;\n");
	buf.append("\n");
	buf.append("import static test.A.FOO;\n");
	buf.append("\n");
	buf.append("public class B {\n");
	buf.append("	public String bar = FOO;\n");
	buf.append("	public String bar1 = FOO;\n");
	buf.append("}\n");
	Expected e2 = new Expected("Convert to static import (replace all occurrences)", buf.toString());

	Range selection = CodeActionUtil.getRange(cu, "FOO");
	assertCodeActions(cu, selection, e1, e2);
}
 
Example 15
Source File: UnresolvedMethodsQuickFixTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testParameterMismatchChangeVarTypeInGeneric() throws Exception {
	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 A<T> {\n");
	buf.append("    public void goo(Vector<T> v) {\n");
	buf.append("    }\n");
	buf.append("}\n");
	pack1.createCompilationUnit("A.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("public class E<T> {\n");
	buf.append("    public void foo(A<Number> a, long x) {\n");
	buf.append("        a.goo(x);\n");
	buf.append("    }\n");
	buf.append("}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("import java.util.Vector;\n");
	buf.append("public class A<T> {\n");
	buf.append("    public void goo(long x) {\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e1 = new Expected("Change method 'goo(Vector<T>)' to 'goo(long)'", buf.toString());

	buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("\n");
	buf.append("import java.util.Vector;\n");
	buf.append("\n");
	buf.append("public class E<T> {\n");
	buf.append("    public void foo(A<Number> a, Vector<Number> x) {\n");
	buf.append("        a.goo(x);\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e2 = new Expected("Change type of 'x' to 'Vector<Number>'", buf.toString());

	buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("import java.util.Vector;\n");
	buf.append("public class A<T> {\n");
	buf.append("    public void goo(Vector<T> v) {\n");
	buf.append("    }\n");
	buf.append("\n");
	buf.append("    public void goo(long x) {\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e3 = new Expected("Create method 'goo(long)' in type 'A'", buf.toString());

	assertCodeActions(cu, e1, e2, e3);
}
 
Example 16
Source File: TypeMismatchQuickFixTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testTypeMismatchInVarDecl2() throws Exception {
	IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
	StringBuilder buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("import java.util.List;\n");
	buf.append("public class Container {\n");
	buf.append("    public List[] getLists() {\n");
	buf.append("        return null;\n");
	buf.append("    }\n");
	buf.append("}\n");
	pack1.createCompilationUnit("Container.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("import java.util.ArrayList;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo(Container c) {\n");
	buf.append("         ArrayList[] lists= c.getLists();\n");
	buf.append("    }\n");
	buf.append("}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("import java.util.ArrayList;\n");
	buf.append("public class E {\n");
	buf.append("    public void foo(Container c) {\n");
	buf.append("         ArrayList[] lists= (ArrayList[]) c.getLists();\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e1 = new Expected("Add cast to 'ArrayList[]'", 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("public class E {\n");
	buf.append("    public void foo(Container c) {\n");
	buf.append("         List[] lists= c.getLists();\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e2 = new Expected("Change type of 'lists' to 'List[]'", 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("public class Container {\n");
	buf.append("    public ArrayList[] getLists() {\n");
	buf.append("        return null;\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e3 = new Expected("Change return type of 'getLists(..)' to 'ArrayList[]'", buf.toString());

	assertCodeActions(cu, e1, e2, e3);
}
 
Example 17
Source File: UnresolvedMethodsQuickFixTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testMethodInAnonymousGenericType() throws Exception {
	IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
	StringBuilder buf = new StringBuilder();

	buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("public class E<T> {\n");
	buf.append("    public void foo() {\n");
	buf.append("        new Comparable<String>() {\n");
	buf.append("            public int compareTo(String s) {\n");
	buf.append("                xoo();\n");
	buf.append("            }\n");
	buf.append("        };\n");
	buf.append("    }\n");
	buf.append("}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("public class E<T> {\n");
	buf.append("    public void foo() {\n");
	buf.append("        new Comparable<String>() {\n");
	buf.append("            public int compareTo(String s) {\n");
	buf.append("                foo();\n");
	buf.append("            }\n");
	buf.append("        };\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e1 = new Expected("Change to 'foo(..)'", buf.toString());

	buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("public class E<T> {\n");
	buf.append("    public void foo() {\n");
	buf.append("        new Comparable<String>() {\n");
	buf.append("            public int compareTo(String s) {\n");
	buf.append("                xoo();\n");
	buf.append("            }\n");
	buf.append("\n");
	buf.append("            private void xoo() {\n");
	buf.append("            }\n");
	buf.append("        };\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e2 = new Expected("Create method 'xoo()'", buf.toString());

	buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("public class E<T> {\n");
	buf.append("    public void foo() {\n");
	buf.append("        new Comparable<String>() {\n");
	buf.append("            public int compareTo(String s) {\n");
	buf.append("                xoo();\n");
	buf.append("            }\n");
	buf.append("        };\n");
	buf.append("    }\n");
	buf.append("\n");
	buf.append("    protected void xoo() {\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e3 = new Expected("Create method 'xoo()' in type 'E<T>'", buf.toString());

	assertCodeActions(cu, e1, e2, e3);

}
 
Example 18
Source File: UnresolvedVariablesQuickFixTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testVarInArrayAccess() throws Exception {
	// bug 194913
	IPackageFragment pack1 = fSourceFolder.createPackageFragment("p", false, null);
	StringBuilder buf = new StringBuilder();
	buf.append("package p;\n");
	buf.append("\n");
	buf.append("public class E {\n");
	buf.append("    void foo(int i) {\n");
	buf.append("        bar[0][i] = \"bar\";\n");
	buf.append("    }\n");
	buf.append("}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package p;\n");
	buf.append("\n");
	buf.append("public class E {\n");
	buf.append("    void foo(int i) {\n");
	buf.append("        String[][] bar;\n");
	buf.append("        bar[0][i] = \"bar\";\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e1 = new Expected("Create local variable 'bar'", buf.toString());

	buf = new StringBuilder();
	buf.append("package p;\n");
	buf.append("\n");
	buf.append("public class E {\n");
	buf.append("    private String[][] bar;\n");
	buf.append("\n");
	buf.append("    void foo(int i) {\n");
	buf.append("        bar[0][i] = \"bar\";\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e2 = new Expected("Create field 'bar'", buf.toString());

	buf = new StringBuilder();
	buf.append("package p;\n");
	buf.append("\n");
	buf.append("public class E {\n");
	buf.append("    void foo(int i, String[][] bar) {\n");
	buf.append("        bar[0][i] = \"bar\";\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e3 = new Expected("Create parameter 'bar'", buf.toString());

	buf = new StringBuilder();
	buf.append("package p;\n");
	buf.append("\n");
	buf.append("public class E {\n");
	buf.append("    private static final String[][] bar = null;\n");
	buf.append("\n");
	buf.append("    void foo(int i) {\n");
	buf.append("        bar[0][i] = \"bar\";\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e4 = new Expected("Create constant 'bar'", buf.toString());

	assertCodeActions(cu, e1, e2, e3, e4);
}
 
Example 19
Source File: UnresolvedVariablesQuickFixTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testVarAssingmentInThenBody() throws Exception {
	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 {\n");
	buf.append("    void foo(Vector vec) {\n");
	buf.append("        if (vec == null) {\n");
	buf.append("        } else\n");
	buf.append("            iter= vec.iterator();\n");
	buf.append("    }\n");
	buf.append("}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("import java.util.Iterator;\n");
	buf.append("import java.util.Vector;\n");
	buf.append("public class E {\n");
	buf.append("    void foo(Vector vec) {\n");
	buf.append("        Iterator iter;\n");
	buf.append("        if (vec == null) {\n");
	buf.append("        } else\n");
	buf.append("            iter= vec.iterator();\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e1 = new Expected("Create local variable 'iter'", buf.toString());

	buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("import java.util.Iterator;\n");
	buf.append("import java.util.Vector;\n");
	buf.append("public class E {\n");
	buf.append("    private Iterator iter;\n");
	buf.append("\n");
	buf.append("    void foo(Vector vec) {\n");
	buf.append("        if (vec == null) {\n");
	buf.append("        } else\n");
	buf.append("            iter= vec.iterator();\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e2 = new Expected("Create field 'iter'", buf.toString());

	buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("import java.util.Iterator;\n");
	buf.append("import java.util.Vector;\n");
	buf.append("public class E {\n");
	buf.append("    void foo(Vector vec, Iterator iter) {\n");
	buf.append("        if (vec == null) {\n");
	buf.append("        } else\n");
	buf.append("            iter= vec.iterator();\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e3 = new Expected("Create parameter 'iter'", buf.toString());

	buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("import java.util.Vector;\n");
	buf.append("public class E {\n");
	buf.append("    void foo(Vector vec) {\n");
	buf.append("        if (vec == null) {\n");
	buf.append("        } else {\n");
	buf.append("        }\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e4 = new Expected("Remove assignment", buf.toString());

	assertCodeActions(cu, e1, e2, e3, e4);
}
 
Example 20
Source File: SerialVersionQuickFixTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testLocalClass() throws Exception {
	IPackageFragment pack1= fSourceFolder.createPackageFragment("test3", false, null);
	StringBuilder buf = new StringBuilder();
	buf.append("package test3;\n");
	buf.append("import java.io.Serializable;\n");
	buf.append("public class Test5 {\n");
	buf.append("    public void test() {\n");
	buf.append("        class X implements Serializable, Cloneable, Runnable {\n");
	buf.append("            private static final int x= 1;\n");
	buf.append("            private Object y;\n");
	buf.append("            public X() {\n");
	buf.append("            }\n");
	buf.append("            public void run() {}\n");
	buf.append("            public synchronized strictfp void bar() {}\n");
	buf.append("            public String bar(int x, int y) { return null; };\n");
	buf.append("        }\n");
	buf.append("    }\n");
	buf.append("}\n");
	ICompilationUnit cu= pack1.createCompilationUnit("Test5.java", buf.toString(), false, null);

	buf = new StringBuilder();
	buf.append("package test3;\n");
	buf.append("import java.io.Serializable;\n");
	buf.append("public class Test5 {\n");
	buf.append("    public void test() {\n");
	buf.append("        class X implements Serializable, Cloneable, Runnable {\n");
	buf.append("            /**\n");
	buf.append("             *\n");
	buf.append("             */\n");
	buf.append("            private static final long serialVersionUID = 1L;\n");
	buf.append("            private static final int x= 1;\n");
	buf.append("            private Object y;\n");
	buf.append("            public X() {\n");
	buf.append("            }\n");
	buf.append("            public void run() {}\n");
	buf.append("            public synchronized strictfp void bar() {}\n");
	buf.append("            public String bar(int x, int y) { return null; };\n");
	buf.append("        }\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e1 = new Expected("Add default serial version ID", buf.toString());

	buf = new StringBuilder();
	buf.append("package test3;\n");
	buf.append("import java.io.Serializable;\n");
	buf.append("public class Test5 {\n");
	buf.append("    public void test() {\n");
	buf.append("        class X implements Serializable, Cloneable, Runnable {\n");
	buf.append("            /**\n");
	buf.append("             *\n");
	buf.append("             */\n");
	buf.append("            private static final long serialVersionUID = -4564939359985118485L;\n");
	buf.append("            private static final int x= 1;\n");
	buf.append("            private Object y;\n");
	buf.append("            public X() {\n");
	buf.append("            }\n");
	buf.append("            public void run() {}\n");
	buf.append("            public synchronized strictfp void bar() {}\n");
	buf.append("            public String bar(int x, int y) { return null; };\n");
	buf.append("        }\n");
	buf.append("    }\n");
	buf.append("}\n");
	Expected e2 = new Expected("Add generated serial version ID", buf.toString());

	assertCodeActions(cu, e1, e2);
}