Search in sources :

Example 36 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit 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 37 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit 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 38 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit 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 39 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit 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 40 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit 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

CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)632 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)557 ArrayList (java.util.ArrayList)453 Test (org.junit.Test)441 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)435 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)228 Hashtable (java.util.Hashtable)137 ASTNode (org.eclipse.jdt.core.dom.ASTNode)90 ASTParser (org.eclipse.jdt.core.dom.ASTParser)44 SimpleName (org.eclipse.jdt.core.dom.SimpleName)36 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)35 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)35 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)35 Ignore (org.junit.Ignore)34 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)29 Type (org.eclipse.jdt.core.dom.Type)27 AST (org.eclipse.jdt.core.dom.AST)23 Expression (org.eclipse.jdt.core.dom.Expression)21 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)19 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)19