Search in sources :

Example 86 with AssistContext

use of org.eclipse.jdt.internal.ui.text.correction.AssistContext in project che by eclipse.

the class AdvancedQuickAssistTest18 method testConvertToIfReturn1.

@Test
public void testConvertToIfReturn1() throws Exception {
    // 'if' in lambda body - positive cases
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("@FunctionalInterface\n");
    buf.append("interface A {\n");
    buf.append("    void run(int n);\n");
    buf.append("}\n");
    buf.append("\n");
    buf.append("@FunctionalInterface\n");
    buf.append("interface B {\n");
    buf.append("    A foo(int x);\n");
    buf.append("}\n");
    buf.append("\n");
    buf.append("public class Test {\n");
    buf.append("    A fi0 = (n1) -> {\n");
    buf.append("        if (n1 == 0) {\n");
    buf.append("            System.out.println(n1);\n");
    buf.append("            return;\n");
    buf.append("        }\n");
    buf.append("    };\n");
    buf.append("    \n");
    buf.append("    int fun1(int a, int b) {\n");
    buf.append("        A fi2 = (n2) -> {\n");
    buf.append("            if (a == b) {\n");
    buf.append("                System.out.println(n2);\n");
    buf.append("                return;\n");
    buf.append("            }\n");
    buf.append("        };\n");
    buf.append("        return a + b;\n");
    buf.append("    }\n");
    buf.append("\n");
    buf.append("    A fun2(int a1, int b1) {\n");
    buf.append("        return (n) -> {\n");
    buf.append("            if (a1 == b1) {\n");
    buf.append("                System.out.println(n);\n");
    buf.append("                return;\n");
    buf.append("            }\n");
    buf.append("        };\n");
    buf.append("    }\n");
    buf.append("\n");
    buf.append("    int fun3(int a2, int b2) {\n");
    buf.append("        B fi3 = (x) -> (n) -> {\n");
    buf.append("            if (a2 == b2) {\n");
    buf.append("                System.out.println(a2);\n");
    buf.append("                return;\n");
    buf.append("            }\n");
    buf.append("        };\n");
    buf.append("        return a2 + b2;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("Test.java", buf.toString(), false, null);
    String str = "if (n1 == 0)";
    AssistContext context = getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 4);
    assertCorrectLabels(proposals);
    StringBuffer buf1 = new StringBuffer();
    buf1.append("package test1;\n");
    buf1.append("@FunctionalInterface\n");
    buf1.append("interface A {\n");
    buf1.append("    void run(int n);\n");
    buf1.append("}\n");
    buf1.append("\n");
    buf1.append("@FunctionalInterface\n");
    buf1.append("interface B {\n");
    buf1.append("    A foo(int x);\n");
    buf1.append("}\n");
    buf1.append("\n");
    buf1.append("public class Test {\n");
    buf1.append("    A fi0 = (n1) -> {\n");
    buf1.append("        if (n1 != 0)\n");
    buf1.append("            return;\n");
    buf1.append("        System.out.println(n1);\n");
    buf1.append("    };\n");
    buf1.append("    \n");
    buf1.append("    int fun1(int a, int b) {\n");
    buf1.append("        A fi2 = (n2) -> {\n");
    buf1.append("            if (a == b) {\n");
    buf1.append("                System.out.println(n2);\n");
    buf1.append("                return;\n");
    buf1.append("            }\n");
    buf1.append("        };\n");
    buf1.append("        return a + b;\n");
    buf1.append("    }\n");
    buf1.append("\n");
    buf1.append("    A fun2(int a1, int b1) {\n");
    buf1.append("        return (n) -> {\n");
    buf1.append("            if (a1 == b1) {\n");
    buf1.append("                System.out.println(n);\n");
    buf1.append("                return;\n");
    buf1.append("            }\n");
    buf1.append("        };\n");
    buf1.append("    }\n");
    buf1.append("\n");
    buf1.append("    int fun3(int a2, int b2) {\n");
    buf1.append("        B fi3 = (x) -> (n) -> {\n");
    buf1.append("            if (a2 == b2) {\n");
    buf1.append("                System.out.println(a2);\n");
    buf1.append("                return;\n");
    buf1.append("            }\n");
    buf1.append("        };\n");
    buf1.append("        return a2 + b2;\n");
    buf1.append("    }\n");
    buf1.append("}\n");
    String expected1 = buf1.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected1 });
    str = "if (a == b)";
    context = getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0);
    proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 3);
    assertCorrectLabels(proposals);
    buf1 = new StringBuffer();
    buf1.append("package test1;\n");
    buf1.append("@FunctionalInterface\n");
    buf1.append("interface A {\n");
    buf1.append("    void run(int n);\n");
    buf1.append("}\n");
    buf1.append("\n");
    buf1.append("@FunctionalInterface\n");
    buf1.append("interface B {\n");
    buf1.append("    A foo(int x);\n");
    buf1.append("}\n");
    buf1.append("\n");
    buf1.append("public class Test {\n");
    buf1.append("    A fi0 = (n1) -> {\n");
    buf1.append("        if (n1 == 0) {\n");
    buf1.append("            System.out.println(n1);\n");
    buf1.append("            return;\n");
    buf1.append("        }\n");
    buf1.append("    };\n");
    buf1.append("    \n");
    buf1.append("    int fun1(int a, int b) {\n");
    buf1.append("        A fi2 = (n2) -> {\n");
    buf1.append("            if (a != b)\n");
    buf1.append("                return;\n");
    buf1.append("            System.out.println(n2);\n");
    buf1.append("        };\n");
    buf1.append("        return a + b;\n");
    buf1.append("    }\n");
    buf1.append("\n");
    buf1.append("    A fun2(int a1, int b1) {\n");
    buf1.append("        return (n) -> {\n");
    buf1.append("            if (a1 == b1) {\n");
    buf1.append("                System.out.println(n);\n");
    buf1.append("                return;\n");
    buf1.append("            }\n");
    buf1.append("        };\n");
    buf1.append("    }\n");
    buf1.append("\n");
    buf1.append("    int fun3(int a2, int b2) {\n");
    buf1.append("        B fi3 = (x) -> (n) -> {\n");
    buf1.append("            if (a2 == b2) {\n");
    buf1.append("                System.out.println(a2);\n");
    buf1.append("                return;\n");
    buf1.append("            }\n");
    buf1.append("        };\n");
    buf1.append("        return a2 + b2;\n");
    buf1.append("    }\n");
    buf1.append("}\n");
    expected1 = buf1.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected1 });
    str = "if (a1 == b1)";
    context = getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0);
    proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 3);
    assertCorrectLabels(proposals);
    buf1 = new StringBuffer();
    buf1.append("package test1;\n");
    buf1.append("@FunctionalInterface\n");
    buf1.append("interface A {\n");
    buf1.append("    void run(int n);\n");
    buf1.append("}\n");
    buf1.append("\n");
    buf1.append("@FunctionalInterface\n");
    buf1.append("interface B {\n");
    buf1.append("    A foo(int x);\n");
    buf1.append("}\n");
    buf1.append("\n");
    buf1.append("public class Test {\n");
    buf1.append("    A fi0 = (n1) -> {\n");
    buf1.append("        if (n1 == 0) {\n");
    buf1.append("            System.out.println(n1);\n");
    buf1.append("            return;\n");
    buf1.append("        }\n");
    buf1.append("    };\n");
    buf1.append("    \n");
    buf1.append("    int fun1(int a, int b) {\n");
    buf1.append("        A fi2 = (n2) -> {\n");
    buf1.append("            if (a == b) {\n");
    buf1.append("                System.out.println(n2);\n");
    buf1.append("                return;\n");
    buf1.append("            }\n");
    buf1.append("        };\n");
    buf1.append("        return a + b;\n");
    buf1.append("    }\n");
    buf1.append("\n");
    buf1.append("    A fun2(int a1, int b1) {\n");
    buf1.append("        return (n) -> {\n");
    buf1.append("            if (a1 != b1)\n");
    buf1.append("                return;\n");
    buf1.append("            System.out.println(n);\n");
    buf1.append("        };\n");
    buf1.append("    }\n");
    buf1.append("\n");
    buf1.append("    int fun3(int a2, int b2) {\n");
    buf1.append("        B fi3 = (x) -> (n) -> {\n");
    buf1.append("            if (a2 == b2) {\n");
    buf1.append("                System.out.println(a2);\n");
    buf1.append("                return;\n");
    buf1.append("            }\n");
    buf1.append("        };\n");
    buf1.append("        return a2 + b2;\n");
    buf1.append("    }\n");
    buf1.append("}\n");
    expected1 = buf1.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected1 });
    str = "if (a2 == b2)";
    context = getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0);
    proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 3);
    assertCorrectLabels(proposals);
    buf1 = new StringBuffer();
    buf1.append("package test1;\n");
    buf1.append("@FunctionalInterface\n");
    buf1.append("interface A {\n");
    buf1.append("    void run(int n);\n");
    buf1.append("}\n");
    buf1.append("\n");
    buf1.append("@FunctionalInterface\n");
    buf1.append("interface B {\n");
    buf1.append("    A foo(int x);\n");
    buf1.append("}\n");
    buf1.append("\n");
    buf1.append("public class Test {\n");
    buf1.append("    A fi0 = (n1) -> {\n");
    buf1.append("        if (n1 == 0) {\n");
    buf1.append("            System.out.println(n1);\n");
    buf1.append("            return;\n");
    buf1.append("        }\n");
    buf1.append("    };\n");
    buf1.append("    \n");
    buf1.append("    int fun1(int a, int b) {\n");
    buf1.append("        A fi2 = (n2) -> {\n");
    buf1.append("            if (a == b) {\n");
    buf1.append("                System.out.println(n2);\n");
    buf1.append("                return;\n");
    buf1.append("            }\n");
    buf1.append("        };\n");
    buf1.append("        return a + b;\n");
    buf1.append("    }\n");
    buf1.append("\n");
    buf1.append("    A fun2(int a1, int b1) {\n");
    buf1.append("        return (n) -> {\n");
    buf1.append("            if (a1 == b1) {\n");
    buf1.append("                System.out.println(n);\n");
    buf1.append("                return;\n");
    buf1.append("            }\n");
    buf1.append("        };\n");
    buf1.append("    }\n");
    buf1.append("\n");
    buf1.append("    int fun3(int a2, int b2) {\n");
    buf1.append("        B fi3 = (x) -> (n) -> {\n");
    buf1.append("            if (a2 != b2)\n");
    buf1.append("                return;\n");
    buf1.append("            System.out.println(a2);\n");
    buf1.append("        };\n");
    buf1.append("        return a2 + b2;\n");
    buf1.append("    }\n");
    buf1.append("}\n");
    expected1 = buf1.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 87 with AssistContext

use of org.eclipse.jdt.internal.ui.text.correction.AssistContext in project che by eclipse.

the class AssistQuickFixTest method testAssignParamToField4.

@Test
public void testAssignParamToField4() throws Exception {
    IPreferenceStore store = JavaPlugin.getDefault().getPreferenceStore();
    store.setValue(PreferenceConstants.CODEGEN_KEYWORD_THIS, true);
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    private long count;\n");
    buf.append("\n");
    buf.append("    public void foo(int count) {\n");
    buf.append("        count++;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    int offset = buf.toString().indexOf("int count");
    AssistContext context = getCorrectionContext(cu, offset, 0);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 2);
    assertCorrectLabels(proposals);
    CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
    String preview1 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    private long count;\n");
    buf.append("    private int count2;\n");
    buf.append("\n");
    buf.append("    public void foo(int count) {\n");
    buf.append("        this.count2 = count;\n");
    buf.append("        count++;\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    proposal = (CUCorrectionProposal) proposals.get(1);
    String preview2 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    private long count;\n");
    buf.append("\n");
    buf.append("    public void foo(int count) {\n");
    buf.append("        this.count = count;\n");
    buf.append("        count++;\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected2 = buf.toString();
    assertEqualStringsIgnoreOrder(new String[] { preview1, preview2 }, new String[] { expected1, expected2 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) ArrayList(java.util.ArrayList) List(java.util.List) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) Test(org.junit.Test)

Example 88 with AssistContext

use of org.eclipse.jdt.internal.ui.text.correction.AssistContext in project che by eclipse.

the class AssistQuickFixTest method testRemoveIfBlockBug138628.

@Test
public void testRemoveIfBlockBug138628() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        if (true) {\n");
    buf.append("            if (true)\n");
    buf.append("                ;\n");
    buf.append("        } else\n");
    buf.append("            ;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    String str = " (true) {";
    AssistContext context = getCorrectionContext(cu, buf.toString().indexOf(str), 0);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 1);
    assertCorrectLabels(proposals);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        if (false)\n");
    buf.append("            ;\n");
    buf.append("        else {\n");
    buf.append("            if (true)\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) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 89 with AssistContext

use of org.eclipse.jdt.internal.ui.text.correction.AssistContext in project che by eclipse.

the class AdvancedQuickAssistTest method testConvertIfToSwitch6.

@Test
public void testConvertIfToSwitch6() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    private int a= 10;\n");
    buf.append("    public void foo() {\n");
    buf.append("        if (this.a == 1) {\n");
    buf.append("            System.out.println(1);\n");
    buf.append("        } else if (this.a == 2 || this.a == 3 || this.a == 4) {\n");
    buf.append("            System.out.println(2);\n");
    buf.append("        } else if (this.a == 5) {\n");
    buf.append("            System.out.println(4);\n");
    buf.append("        } else {\n");
    buf.append("            System.out.println(-1);\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("if");
    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("public class E {\n");
    buf.append("    private int a= 10;\n");
    buf.append("    public void foo() {\n");
    buf.append("        switch (this.a) {\n");
    buf.append("            case 1 :\n");
    buf.append("                System.out.println(1);\n");
    buf.append("                break;\n");
    buf.append("            case 2 :\n");
    buf.append("            case 3 :\n");
    buf.append("            case 4 :\n");
    buf.append("                System.out.println(2);\n");
    buf.append("                break;\n");
    buf.append("            case 5 :\n");
    buf.append("                System.out.println(4);\n");
    buf.append("                break;\n");
    buf.append("            default :\n");
    buf.append("                System.out.println(-1);\n");
    buf.append("                break;\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) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 90 with AssistContext

use of org.eclipse.jdt.internal.ui.text.correction.AssistContext in project che by eclipse.

the class AdvancedQuickAssistTest method testConvertIfToSwitch8.

@Test
public void testConvertIfToSwitch8() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    private int a= 10;\n");
    buf.append("    public int getA() {\n");
    buf.append("        return a;\n");
    buf.append("    }\n");
    buf.append("    public void foo() {\n");
    buf.append("        if (getA() == 1) {\n");
    buf.append("            System.out.println(1);\n");
    buf.append("        } else if (this.a == 2) {\n");
    buf.append("            System.out.println(2);\n");
    buf.append("        } else if (getA() == 3) {\n");
    buf.append("            System.out.println(3);\n");
    buf.append("        } else {\n");
    buf.append("            System.out.println(-1);\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("if");
    AssistContext context = getCorrectionContext(cu, offset, 0);
    assertNoErrors(context);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 3);
    assertCorrectLabels(proposals);
    assertProposalDoesNotExist(proposals, CorrectionMessages.AdvancedQuickAssistProcessor_convertIfElseToSwitch);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

AssistContext (org.eclipse.jdt.internal.ui.text.correction.AssistContext)390 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)385 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)384 Test (org.junit.Test)384 List (java.util.List)378 ArrayList (java.util.ArrayList)313 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)57 HashMap (java.util.HashMap)15 Map (java.util.Map)15 ProblemLocation (org.eclipse.jdt.internal.ui.text.correction.ProblemLocation)8 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)6 Ignore (org.junit.Ignore)4 IDocument (org.eclipse.jface.text.IDocument)3 Iterator (java.util.Iterator)2 IBuffer (org.eclipse.jdt.core.IBuffer)2 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 Point (org.eclipse.swt.graphics.Point)2 TextViewer (org.eclipse.che.jdt.javaeditor.TextViewer)1 LinkedModeModelImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedModeModelImpl)1