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 });
}
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 });
}
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 });
}
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 });
}
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 });
}
Aggregations