Search in sources :

Example 41 with CUCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal in project che by eclipse.

the class LocalCorrectionsQuickFixTest method testUnnecessaryCastBug335173_2.

@Test
public void testUnnecessaryCastBug335173_2() throws Exception {
    Hashtable hashtable = JavaCore.getOptions();
    hashtable.put(JavaCore.COMPILER_PB_UNNECESSARY_TYPE_CHECK, JavaCore.ERROR);
    JavaCore.setOptions(hashtable);
    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 void foo(Integer n) {\n");
    buf.append("        int i = ((Integer) (n)).intValue();\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, 1);
    assertCorrectLabels(proposals);
    CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
    String preview = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(Integer n) {\n");
    buf.append("        int i = (n).intValue();\n");
    buf.append("    }\n");
    buf.append("}\n");
    assertEqualString(preview, buf.toString());
}
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) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 42 with CUCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal in project che by eclipse.

the class LocalCorrectionsQuickFixTest method testUnnecessaryElse3.

@Test
public void testUnnecessaryElse3() throws Exception {
    Hashtable hashtable = JavaCore.getOptions();
    hashtable.put(JavaCore.COMPILER_PB_UNNECESSARY_ELSE, JavaCore.ERROR);
    JavaCore.setOptions(hashtable);
    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 boolean foo(int x) {\n");
    buf.append("        if (x == 9) {\n");
    buf.append("            return true;\n");
    buf.append("        } else\n");
    buf.append("            return false;\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, 1);
    assertCorrectLabels(proposals);
    CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
    String preview = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public boolean foo(int x) {\n");
    buf.append("        if (x == 9) {\n");
    buf.append("            return true;\n");
    buf.append("        }\n");
    buf.append("        return false;\n");
    buf.append("    }\n");
    buf.append("}\n");
    assertEqualString(preview, buf.toString());
}
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) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 43 with CUCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal in project che by eclipse.

the class LocalCorrectionsQuickFixTest method testRemoveDeadCodeIfThen.

@Test
public void testRemoveDeadCodeIfThen() throws Exception {
    Hashtable options = JavaCore.getOptions();
    options.put(JavaCore.COMPILER_PB_DEAD_CODE, JavaCore.WARNING);
    JavaCore.setOptions(options);
    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 void foo() {\n");
    buf.append("        if (false) {\n");
    buf.append("            System.out.println(\"a\");\n");
    buf.append("        } else {\n");
    buf.append("            System.out.println(\"b\");\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("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        System.out.println(\"b\");\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("    @SuppressWarnings(\"unused\")\n");
    buf.append("    public void foo() {\n");
    buf.append("        if (false) {\n");
    buf.append("            System.out.println(\"a\");\n");
    buf.append("        } else {\n");
    buf.append("            System.out.println(\"b\");\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) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 44 with CUCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal in project che by eclipse.

the class LocalCorrectionsQuickFixTest method testUndefinedConstructorInDefaultConstructor2.

@Test
public void testUndefinedConstructorInDefaultConstructor2() 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 F {\n");
    buf.append("    public F(Runnable runnable) throws IOException {\n");
    buf.append("    }\n");
    buf.append("\n");
    buf.append("    public F(int i, Runnable runnable) {\n");
    buf.append("    }\n");
    buf.append("}\n");
    pack1.createCompilationUnit("F.java", buf.toString(), false, null);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E extends F {\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 extends F {\n");
    buf.append("\n");
    buf.append("    public E(int i, Runnable runnable) {\n");
    buf.append("        super(i, runnable);\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("\n");
    buf.append("import java.io.IOException;\n");
    buf.append("\n");
    buf.append("public class E extends F {\n");
    buf.append("\n");
    buf.append("    public E(Runnable runnable) throws IOException {\n");
    buf.append("        super(runnable);\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 45 with CUCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal in project che by eclipse.

the class LocalCorrectionsQuickFixTest method testInheritedAccessOnStaticInGeneric.

@Test
public void testInheritedAccessOnStaticInGeneric() throws Exception {
    IPackageFragment pack0 = fSourceFolder.createPackageFragment("pack", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package pack;\n");
    buf.append("public class A<T> {\n");
    buf.append("    public static void foo() {\n");
    buf.append("    }\n");
    buf.append("}\n");
    pack0.createCompilationUnit("A.java", buf.toString(), false, null);
    buf = new StringBuffer();
    buf.append("package pack;\n");
    buf.append("public class B<T> extends A<String> {\n");
    buf.append("}\n");
    pack0.createCompilationUnit("B.java", buf.toString(), false, null);
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import pack.B;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(B<Number> b) {\n");
    buf.append("        b.foo();\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);
    CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
    String preview1 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import pack.B;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(B<Number> b) {\n");
    buf.append("        B.foo();\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 pack.A;\n");
    buf.append("import pack.B;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(B<Number> b) {\n");
    buf.append("        A.foo();\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 pack;\n");
    buf.append("public class A<T> {\n");
    buf.append("    public void foo() {\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)

Aggregations

CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)290 ArrayList (java.util.ArrayList)285 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)285 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)284 Test (org.junit.Test)284 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)228 AssistContext (org.eclipse.jdt.internal.ui.text.correction.AssistContext)57 List (java.util.List)48 Hashtable (java.util.Hashtable)47 ProblemLocation (org.eclipse.jdt.internal.ui.text.correction.ProblemLocation)7 Ignore (org.junit.Ignore)7 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)6 ChangeCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal)6 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)5 CorrectPackageDeclarationProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectPackageDeclarationProposal)4 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)3 Iterator (java.util.Iterator)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)2 IDocument (org.eclipse.jface.text.IDocument)2