Search in sources :

Example 41 with ICompilationUnit

use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.

the class AssistQuickFixTest18 method testConvertToLambda10.

@Test
public void testConvertToLambda10() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("interface I {\n");
    buf.append("    int foo(String s);\n");
    buf.append("}\n");
    buf.append("\n");
    buf.append("interface J {\n");
    buf.append("    Integer foo(String s);\n");
    buf.append("}\n");
    buf.append("\n");
    buf.append("public class X {\n");
    buf.append("    static void goo(I i) { }\n");
    buf.append("\n");
    buf.append("    static void goo(J j) { }\n");
    buf.append("\n");
    buf.append("    public static void main(String[] args) {\n");
    buf.append("        goo(new I() {\n");
    buf.append("            @Override\n");
    buf.append("            public int foo(String s) {\n");
    buf.append("                return 0;\n");
    buf.append("            }\n");
    buf.append("        });\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("X.java", buf.toString(), false, null);
    int offset = buf.toString().indexOf("I()");
    AssistContext context = getCorrectionContext(cu, offset, 0);
    assertNoErrors(context);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 2);
    assertCorrectLabels(proposals);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("interface I {\n");
    buf.append("    int foo(String s);\n");
    buf.append("}\n");
    buf.append("\n");
    buf.append("interface J {\n");
    buf.append("    Integer foo(String s);\n");
    buf.append("}\n");
    buf.append("\n");
    buf.append("public class X {\n");
    buf.append("    static void goo(I i) { }\n");
    buf.append("\n");
    buf.append("    static void goo(J j) { }\n");
    buf.append("\n");
    buf.append("    public static void main(String[] args) {\n");
    buf.append("        goo((I) s -> 0);\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected1 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) List(java.util.List) Test(org.junit.Test)

Example 42 with ICompilationUnit

use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.

the class ConvertForLoopQuickFixTest method testBug214340_1.

@Test
public void testBug214340_1() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E1 {\n");
    buf.append("    int[] array = new int[3];\n");
    buf.append("\n");
    buf.append("    boolean same(E1 that) {\n");
    buf.append("        for (int i = 0; i < array.length; i++) {\n");
    buf.append("            if (this.array[i] != that.array[i])\n");
    buf.append("                return false;\n");
    buf.append("        }\n");
    buf.append("        return true;\n");
    buf.append("    }\n");
    buf.append("\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E1.java", buf.toString(), false, null);
    assertFalse(satisfiesPrecondition(cu));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Test(org.junit.Test)

Example 43 with ICompilationUnit

use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.

the class ConvertForLoopQuickFixTest method testExpressionPrecondition07.

@Test
public void testExpressionPrecondition07() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(Object[] x) {\n");
    buf.append("        for (int i = 0, length= x.length; i < length; i++) {}\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    assertTrue(satisfiesPrecondition(cu));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Test(org.junit.Test)

Example 44 with ICompilationUnit

use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.

the class ConvertForLoopQuickFixTest method testBug130139_2.

@Test
public void testBug130139_2() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(String[] strings) {\n");
    buf.append("        for (int i= x(); i < strings.length; i++) {\n");
    buf.append("            System.out.println(strings[i]);\n");
    buf.append("        }  \n");
    buf.append("    }\n");
    buf.append("    private int x(){\n");
    buf.append("        return 0;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    List proposals = fetchConvertingProposal(buf, cu);
    assertNull(fConvertLoopProposal);
    assertCorrectLabels(proposals);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) List(java.util.List) Test(org.junit.Test)

Example 45 with ICompilationUnit

use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.

the class ConvertForLoopQuickFixTest method testBodyPrecondition344674_3.

@Test
public void testBodyPrecondition344674_3() throws Exception {
    //https://bugs.eclipse.org/bugs/show_bug.cgi?id=344674
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test;\n");
    buf.append("public class E {\n");
    buf.append("    Object[] x;\n");
    buf.append("    public void foo(Object obj) {\n");
    buf.append("        for (int i = 0; i < ((E) obj).x.length; i++) {\n");
    buf.append("            System.out.println(((E) obj).x[i]);\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    assertTrue(satisfiesPrecondition(cu));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Test(org.junit.Test)

Aggregations

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1975 Test (org.junit.Test)1482 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1455 ArrayList (java.util.ArrayList)837 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)548 List (java.util.List)476 AssistContext (org.eclipse.jdt.internal.ui.text.correction.AssistContext)385 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)284 Hashtable (java.util.Hashtable)143 IType (org.eclipse.jdt.core.IType)135 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)103 ASTNode (org.eclipse.jdt.core.dom.ASTNode)97 JavaModelException (org.eclipse.jdt.core.JavaModelException)82 IJavaElement (org.eclipse.jdt.core.IJavaElement)81 OrganizeImportsOperation (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation)70 IChooseImportQuery (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery)70 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)65 IJavaProject (org.eclipse.jdt.core.IJavaProject)64 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)62 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)56