Search in sources :

Example 31 with ICompilationUnit

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

the class AssistQuickFixTest17 method testPickoutTypeFromMulticatch2.

@Test
public void testPickoutTypeFromMulticatch2() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.lang.reflect.InvocationTargetException;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        try {\n");
    buf.append("            String.class.getConstructor().newInstance();\n");
    buf.append("        } catch (InstantiationException | IllegalAccessException\n");
    buf.append("                | IllegalArgumentException | InvocationTargetException\n");
    buf.append("                | java.lang.NoSuchMethodException | SecurityException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    String string = "MethodException";
    int offset = buf.toString().indexOf(string);
    AssistContext context = getCorrectionContext(cu, offset, 0);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 4);
    assertCorrectLabels(proposals);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.lang.reflect.InvocationTargetException;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        try {\n");
    buf.append("            String.class.getConstructor().newInstance();\n");
    buf.append("        } catch (InstantiationException | IllegalAccessException\n");
    buf.append("                | IllegalArgumentException | InvocationTargetException\n");
    buf.append("                | SecurityException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.lang.reflect.InvocationTargetException;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() throws java.lang.NoSuchMethodException {\n");
    buf.append("        try {\n");
    buf.append("            String.class.getConstructor().newInstance();\n");
    buf.append("        } catch (InstantiationException | IllegalAccessException\n");
    buf.append("                | IllegalArgumentException | InvocationTargetException\n");
    buf.append("                | SecurityException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected2 = buf.toString();
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.lang.reflect.InvocationTargetException;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        try {\n");
    buf.append("            String.class.getConstructor().newInstance();\n");
    buf.append("        } catch (InstantiationException | IllegalAccessException\n");
    buf.append("                | IllegalArgumentException | InvocationTargetException\n");
    buf.append("                | SecurityException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        } catch (java.lang.NoSuchMethodException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected3 = buf.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected1, expected2, expected3 });
}
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 32 with ICompilationUnit

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

the class AssistQuickFixTest18 method testConvertToAnonymousClassCreation1.

@Test
public void testConvertToAnonymousClassCreation1() 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("    void method();\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar(I i) {\n");
    buf.append("    }\n");
    buf.append("    void foo() {\n");
    buf.append("        bar(() -> {\n");
    buf.append("            System.out.println();\n");
    buf.append("            System.out.println();\n");
    buf.append("        });\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    int offset = buf.toString().indexOf("->");
    AssistContext context = getCorrectionContext(cu, offset, 0);
    assertNoErrors(context);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 4);
    assertCorrectLabels(proposals);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("interface I {\n");
    buf.append("    void method();\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar(I i) {\n");
    buf.append("    }\n");
    buf.append("    void foo() {\n");
    buf.append("        bar(new I() {\n");
    buf.append("            @Override\n");
    buf.append("            public void method() {\n");
    buf.append("                System.out.println();\n");
    buf.append("                System.out.println();\n");
    buf.append("            }\n");
    buf.append("        });\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 33 with ICompilationUnit

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

the class AssistQuickFixTest18 method testAssignParamToField2.

@Test
public void testAssignParamToField2() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public interface I {\n");
    buf.append("    static void bar(int x) {\n");
    buf.append("        System.out.println(x);\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("I.java", buf.toString(), false, null);
    int offset = buf.toString().indexOf("x");
    AssistContext context = getCorrectionContext(cu, offset, 0);
    assertNoErrors(context);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 0);
}
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 34 with ICompilationUnit

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

the class AssistQuickFixTest18 method testAssignParamToField1.

@Test
public void testAssignParamToField1() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public interface I {\n");
    buf.append("    default void foo(int x) {\n");
    buf.append("        System.out.println(x);\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("I.java", buf.toString(), false, null);
    int offset = buf.toString().indexOf("x");
    AssistContext context = getCorrectionContext(cu, offset, 0);
    assertNoErrors(context);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 0);
}
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 35 with ICompilationUnit

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

the class AssistQuickFixTest18 method testConvertToLambda15.

@Test
public void testConvertToLambda15() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("@FunctionalInterface\n");
    buf.append("interface FI {\n");
    buf.append("    int foo(int x, int y, int z);\n");
    buf.append("}\n");
    buf.append("\n");
    buf.append("class C {\n");
    buf.append("    int i;\n");
    buf.append("    private void test(int x, int y, int z) {\n");
    buf.append("        FI fi = new FI() {\n");
    buf.append("            @Override\n");
    buf.append("            public int foo(int a, int b, int z) {\n");
    buf.append("                int x= 0, y=0; \n");
    buf.append("                return x + y + z;\n");
    buf.append("            }\n");
    buf.append("        };\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
    int offset = buf.toString().indexOf("FI()");
    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("@FunctionalInterface\n");
    buf.append("interface FI {\n");
    buf.append("    int foo(int x, int y, int z);\n");
    buf.append("}\n");
    buf.append("\n");
    buf.append("class C {\n");
    buf.append("    int i;\n");
    buf.append("    private void test(int x, int y, int z) {\n");
    buf.append("        FI fi = (a, b, z1) -> {\n");
    buf.append("            int x1= 0, y1=0; \n");
    buf.append("            return x1 + y1 + z1;\n");
    buf.append("        };\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)

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