Search in sources :

Example 26 with AssistContext

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

the class AssistQuickFixTest18 method testConvertToAnonymousClassCreation3.

@Test
public void testConvertToAnonymousClassCreation3() 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(() -> System.out.println());\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, 5);
    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("            }\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 27 with AssistContext

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

the class AssistQuickFixTest method testReplaceCatchClauseWithThrowsWithFinally.

@Test
public void testReplaceCatchClauseWithThrowsWithFinally() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        try {\n");
    buf.append("            goo();\n");
    buf.append("        } catch (IOException e) {\n");
    buf.append("        } finally {\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    String str = "(IOException e)";
    AssistContext context = getCorrectionContext(cu, buf.toString().indexOf(str), 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("import java.io.IOException;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() throws IOException {\n");
    buf.append("        try {\n");
    buf.append("            goo();\n");
    buf.append("        } finally {\n");
    buf.append("        }\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("import java.io.IOException;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        try {\n");
    buf.append("            goo();\n");
    buf.append("        } finally {\n");
    buf.append("        }\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) Test(org.junit.Test)

Example 28 with AssistContext

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

the class AssistQuickFixTest method testExtractToMethod2.

@Test
public void testExtractToMethod2() throws Exception {
    //https://bugs.eclipse.org/bugs/show_bug.cgi?id=41302
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    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 b = 1;");
    int offset2 = buf.toString().indexOf("a + b;") + 6;
    AssistContext context = getCorrectionContext(cu, offset1, offset2 - offset1);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 2);
    assertCorrectLabels(proposals);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    void foo() {\n");
    buf.append("        int a = 1;\n");
    buf.append("        extracted(a);\n");
    buf.append("    }\n");
    buf.append("\n");
    buf.append("    private void extracted(int a) {\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 StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    void foo() {\n");
    buf.append("        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();
    buf = new StringBuffer();
    // Wrap in buf.append() (to clipboard)
    String ex3 = null;
    assertExpectedExistInProposals(proposals, new String[] { ex1, ex2, ex3 });
}
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 29 with AssistContext

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

the class AssistQuickFixTest method testInvertEquals6.

@Test
public void testInvertEquals6() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    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 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 StringBuffer();
    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 StringBuffer();
    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());
}
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) Test(org.junit.Test)

Example 30 with AssistContext

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

the class AssistQuickFixTest17 method testConvertToMultiCatch2.

@Test
public void testConvertToMultiCatch2() 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("    void foo() {\n");
    buf.append("        try {\n");
    buf.append("            System.out.println(\"foo\");\n");
    buf.append("        } catch (IllegalArgumentException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        } catch (NullPointerException 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);
    int offset = buf.toString().indexOf("catch");
    AssistContext context = getCorrectionContext(cu, offset, 0);
    assertNoErrors(context);
    List proposals = collectAssists(context, false);
    assertCorrectLabels(proposals);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    void foo() {\n");
    buf.append("        try {\n");
    buf.append("            System.out.println(\"foo\");\n");
    buf.append("        } catch (IllegalArgumentException | NullPointerException e) {\n");
    buf.append("            e.printStackTrace();\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

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