Search in sources :

Example 61 with IPackageFragment

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

the class LocalCorrectionsQuickFixTest17 method testRemoveRedundantTypeArguments4.

@Test
public void testRemoveRedundantTypeArguments4() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.util.HashMap;\n");
    buf.append("import java.util.Map;\n");
    buf.append("public class E {\n");
    buf.append("    void foo() {\n");
    buf.append("        Map<Map<String, String>, Map<String, String>> a = new HashMap<Map<String, String>, Map<String, String>>();;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    ArrayList proposals = collectCorrections(cu, astRoot);
    assertNumberOfProposals(proposals, 3);
    assertCorrectLabels(proposals);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.util.HashMap;\n");
    buf.append("import java.util.Map;\n");
    buf.append("public class E {\n");
    buf.append("    void foo() {\n");
    buf.append("        Map<Map<String, String>, Map<String, String>> a = new HashMap<>();;\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected1 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 62 with IPackageFragment

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

the class LocalCorrectionsQuickFixTest17 method testUncaughtExceptionUnionType.

@Test
public void testUncaughtExceptionUnionType() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileNotFoundException;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("import java.io.InterruptedIOException;\n");
    buf.append("public class E {\n");
    buf.append("    void foo(int a) {\n");
    buf.append("        try {\n");
    buf.append("            if (a < 10)\n");
    buf.append("                throw new FileNotFoundException();\n");
    buf.append("            else if (a < 20)\n");
    buf.append("                throw new InterruptedIOException();\n");
    buf.append("            else\n");
    buf.append("                throw new IOException();\n");
    buf.append("        } catch (FileNotFoundException | InterruptedIOException ex) {\n");
    buf.append("            ex.printStackTrace();\n");
    buf.append("        } \n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    ArrayList proposals = collectCorrections(cu, astRoot);
    assertNumberOfProposals(proposals, 4);
    assertCorrectLabels(proposals);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileNotFoundException;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("import java.io.InterruptedIOException;\n");
    buf.append("public class E {\n");
    buf.append("    void foo(int a) throws IOException {\n");
    buf.append("        try {\n");
    buf.append("            if (a < 10)\n");
    buf.append("                throw new FileNotFoundException();\n");
    buf.append("            else if (a < 20)\n");
    buf.append("                throw new InterruptedIOException();\n");
    buf.append("            else\n");
    buf.append("                throw new IOException();\n");
    buf.append("        } catch (FileNotFoundException | InterruptedIOException ex) {\n");
    buf.append("            ex.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.io.FileNotFoundException;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("import java.io.InterruptedIOException;\n");
    buf.append("public class E {\n");
    buf.append("    void foo(int a) {\n");
    buf.append("        try {\n");
    buf.append("            if (a < 10)\n");
    buf.append("                throw new FileNotFoundException();\n");
    buf.append("            else if (a < 20)\n");
    buf.append("                throw new InterruptedIOException();\n");
    buf.append("            else\n");
    buf.append("                throw new IOException();\n");
    buf.append("        } catch (FileNotFoundException | InterruptedIOException ex) {\n");
    buf.append("            ex.printStackTrace();\n");
    buf.append("        } catch (IOException e) {\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.io.FileNotFoundException;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("import java.io.InterruptedIOException;\n");
    buf.append("public class E {\n");
    buf.append("    void foo(int a) {\n");
    buf.append("        try {\n");
    buf.append("            if (a < 10)\n");
    buf.append("                throw new FileNotFoundException();\n");
    buf.append("            else if (a < 20)\n");
    buf.append("                throw new InterruptedIOException();\n");
    buf.append("            else\n");
    buf.append("                throw new IOException();\n");
    buf.append("        } catch (FileNotFoundException | InterruptedIOException | IOException ex) {\n");
    buf.append("            ex.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) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 63 with IPackageFragment

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

the class LocalCorrectionsQuickFixTest17 method testUncaughtExceptionTryWithResources3.

@Test
public void testUncaughtExceptionTryWithResources3() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileInputStream;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar(int n) throws IllegalArgumentException, MyException {\n");
    buf.append("        if (n == 1)\n");
    buf.append("            throw new IllegalArgumentException();\n");
    buf.append("        else\n");
    buf.append("            throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            bar(1);\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    //quick fix on 3rd problem
    ArrayList proposals = collectCorrections(cu, astRoot, 3, 2);
    assertNumberOfProposals(proposals, 5);
    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.FileInputStream;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar(int n) throws IllegalArgumentException, MyException {\n");
    buf.append("        if (n == 1)\n");
    buf.append("            throw new IllegalArgumentException();\n");
    buf.append("        else\n");
    buf.append("            throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) throws IllegalArgumentException, MyException {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            bar(1);\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.FileInputStream;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar(int n) throws IllegalArgumentException, MyException {\n");
    buf.append("        if (n == 1)\n");
    buf.append("            throw new IllegalArgumentException();\n");
    buf.append("        else\n");
    buf.append("            throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            bar(1);\n");
    buf.append("        } catch (IllegalArgumentException e) {\n");
    buf.append("        } catch (MyException e) {\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected2 = buf.toString();
    proposal = (CUCorrectionProposal) proposals.get(2);
    String preview3 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileInputStream;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar(int n) throws IllegalArgumentException, MyException {\n");
    buf.append("        if (n == 1)\n");
    buf.append("            throw new IllegalArgumentException();\n");
    buf.append("        else\n");
    buf.append("            throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            bar(1);\n");
    buf.append("        } catch (IllegalArgumentException | MyException e) {\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected3 = buf.toString();
    proposal = (CUCorrectionProposal) proposals.get(3);
    String preview4 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileInputStream;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar(int n) throws IllegalArgumentException, MyException {\n");
    buf.append("        if (n == 1)\n");
    buf.append("            throw new IllegalArgumentException();\n");
    buf.append("        else\n");
    buf.append("            throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            try {\n");
    buf.append("                bar(1);\n");
    buf.append("            } catch (IllegalArgumentException e) {\n");
    buf.append("            } catch (MyException e) {\n");
    buf.append("            }\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected4 = buf.toString();
    proposal = (CUCorrectionProposal) proposals.get(4);
    String preview5 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileInputStream;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar(int n) throws IllegalArgumentException, MyException {\n");
    buf.append("        if (n == 1)\n");
    buf.append("            throw new IllegalArgumentException();\n");
    buf.append("        else\n");
    buf.append("            throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            try {\n");
    buf.append("                bar(1);\n");
    buf.append("            } catch (IllegalArgumentException | MyException e) {\n");
    buf.append("            }\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected5 = buf.toString();
    assertEqualStringsIgnoreOrder(new String[] { preview1, preview2, preview3, preview4, preview5 }, new String[] { expected1, expected2, expected3, expected4, expected5 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 64 with IPackageFragment

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

the class LocalCorrectionsQuickFixTest17 method testUncaughtExceptionTryWithResources5.

@Test
public void testUncaughtExceptionTryWithResources5() throws Exception {
    //https://bugs.eclipse.org/bugs/show_bug.cgi?id=139231
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileInputStream;\n");
    buf.append("public class E {\n");
    buf.append("    void foo(String name, boolean b) {\n");
    buf.append("        String e;\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    //quick fix on 1st problem
    ArrayList proposals = collectCorrections(cu, astRoot, 2, 0);
    assertNumberOfProposals(proposals, 3);
    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.FileInputStream;\n");
    buf.append("import java.io.FileNotFoundException;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("public class E {\n");
    buf.append("    void foo(String name, boolean b) throws FileNotFoundException, IOException {\n");
    buf.append("        String e;\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\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.FileInputStream;\n");
    buf.append("import java.io.FileNotFoundException;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("public class E {\n");
    buf.append("    void foo(String name, boolean b) {\n");
    buf.append("        String e;\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("        } catch (FileNotFoundException e1) {\n");
    buf.append("        } catch (IOException e1) {\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected2 = buf.toString();
    proposal = (CUCorrectionProposal) proposals.get(2);
    String preview3 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileInputStream;\n");
    buf.append("import java.io.FileNotFoundException;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("public class E {\n");
    buf.append("    void foo(String name, boolean b) {\n");
    buf.append("        String e;\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("        } catch (FileNotFoundException | IOException e1) {\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected3 = buf.toString();
    assertEqualStringsIgnoreOrder(new String[] { preview1, preview2, preview3 }, new String[] { expected1, expected2, expected3 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 65 with IPackageFragment

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

the class LocalCorrectionsQuickFixTest17 method testRemoveRedundantTypeArguments1.

@Test
public void testRemoveRedundantTypeArguments1() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    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("    void foo() {\n");
    buf.append("        List<String> a = new ArrayList<java.lang.String>();\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    ArrayList proposals = collectCorrections(cu, astRoot);
    assertNumberOfProposals(proposals, 3);
    assertCorrectLabels(proposals);
    buf = new StringBuffer();
    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("    void foo() {\n");
    buf.append("        List<String> a = new ArrayList<>();\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected1 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1147 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1052 Test (org.junit.Test)1039 ArrayList (java.util.ArrayList)755 List (java.util.List)453 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)435 AssistContext (org.eclipse.jdt.internal.ui.text.correction.AssistContext)384 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)284 Hashtable (java.util.Hashtable)136 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)100 OrganizeImportsOperation (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation)67 IChooseImportQuery (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery)67 Ignore (org.junit.Ignore)43 IType (org.eclipse.jdt.core.IType)37 IJavaElement (org.eclipse.jdt.core.IJavaElement)32 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)24 HashMap (java.util.HashMap)23 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)23 BaseTest (org.eclipse.che.plugin.java.server.che.BaseTest)21 Map (java.util.Map)19