Search in sources :

Example 41 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.

the class LocalCorrectionsQuickFixTest17 method testRemoveRedundantTypeArguments3.

@Test
public void testRemoveRedundantTypeArguments3() 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("import java.util.Map;\n");
    buf.append("public class E {\n");
    buf.append("    void foo() {\n");
    buf.append("        List<Map<String, String>> a = new ArrayList<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.ArrayList;\n");
    buf.append("import java.util.List;\n");
    buf.append("import java.util.Map;\n");
    buf.append("public class E {\n");
    buf.append("    void foo() {\n");
    buf.append("        List<Map<String, 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)

Example 42 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.

the class LocalCorrectionsQuickFixTest17 method testUnneededCatchBlockTryWithResources.

@Test
public void testUnneededCatchBlockTryWithResources() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileReader;\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 foo() throws Exception {\n");
    buf.append("        try (FileReader reader1 = new FileReader(\"file\")) {\n");
    buf.append("            int ch;\n");
    buf.append("            while ((ch = reader1.read()) != -1) {\n");
    buf.append("                System.out.println(ch);\n");
    buf.append("            }\n");
    buf.append("        } catch (MyException e) {\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, 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.FileReader;\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 foo() throws Exception {\n");
    buf.append("        try (FileReader reader1 = new FileReader(\"file\")) {\n");
    buf.append("            int ch;\n");
    buf.append("            while ((ch = reader1.read()) != -1) {\n");
    buf.append("                System.out.println(ch);\n");
    buf.append("            }\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.FileReader;\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 foo() throws Exception {\n");
    buf.append("        try (FileReader reader1 = new FileReader(\"file\")) {\n");
    buf.append("            int ch;\n");
    buf.append("            while ((ch = reader1.read()) != -1) {\n");
    buf.append("                System.out.println(ch);\n");
    buf.append("            }\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) 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 43 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.

the class ModifierCorrectionsQuickFixTest method testSuppressWarningsForFieldVariables.

@Test
public void testSuppressWarningsForFieldVariables() throws Exception {
    Hashtable options = JavaCore.getOptions();
    options.put(JavaCore.COMPILER_PB_UNCHECKED_TYPE_OPERATION, JavaCore.WARNING);
    JavaCore.setOptions(options);
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1; \n");
    buf.append("import java.util.*;\n");
    buf.append("\n");
    buf.append("public class A {\n");
    buf.append("    List<String> myList = new ArrayList();\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("A.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    ArrayList proposals = collectCorrections(cu, astRoot, 1);
    assertCorrectLabels(proposals);
    assertNumberOfProposals(proposals, 3);
    String[] expected = new String[1];
    buf = new StringBuffer();
    buf.append("package test1; \n");
    buf.append("import java.util.*;\n");
    buf.append("\n");
    buf.append("public class A {\n");
    buf.append("    @SuppressWarnings(\"unchecked\")\n");
    buf.append("    List<String> myList = new ArrayList();\n");
    buf.append("}\n");
    expected[0] = buf.toString();
    assertExpectedExistInProposals(proposals, expected);
}
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) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 44 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.

the class ModifierCorrectionsQuickFixTest method testMethodRequiresBody2.

@Test
public void testMethodRequiresBody2() 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 int foo();\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, 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("    public int foo() {\n");
    buf.append("        return 0;\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("    public abstract int foo();\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) 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 45 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.

the class ModifierCorrectionsQuickFixTest method testInvalidVisabilityOverrideMethod.

/**
	 * Quick Fix proposes wrong visibility for overriding/overridden method.
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=216898#c1
	 * 
	 * @throws Exception if anything goes wrong
	 * @since 3.9
	 */
@Test
public void testInvalidVisabilityOverrideMethod() throws Exception {
    // No simple solution to this problemID IProblem.AbstractMethodCannotBeOverridden
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    IPackageFragment pack2 = fSourceFolder.createPackageFragment("test2", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public abstract class Abs {\n");
    buf.append("  abstract String getName();\n");
    buf.append("}\n");
    pack1.createCompilationUnit("Abs.java", buf.toString(), false, null);
    buf = new StringBuffer();
    buf.append("package test2;\n");
    buf.append("public class AbsImpl extends test1.Abs {\n");
    buf.append("  String getName() {\n");
    buf.append("    return \"name\";\n");
    buf.append("  }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack2.createCompilationUnit("AbsImpl.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    ArrayList proposals = collectCorrections(cu, astRoot, 2);
    assertCorrectLabels(proposals);
    assertNumberOfProposals(proposals, 0);
}
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