Search in sources :

Example 86 with CUCorrectionProposal

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

the class TypeMismatchQuickFixTests method testTypeMismatchForInterfaceInGeneric.

@Test
public void testTypeMismatchForInterfaceInGeneric() throws Exception {
    IPackageFragment pack0 = fSourceFolder.createPackageFragment("test0", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test0;\n");
    buf.append("public interface PrimaryContainer<A> {\n");
    buf.append("}\n");
    pack0.createCompilationUnit("PrimaryContainer.java", buf.toString(), false, null);
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class Container<A> {\n");
    buf.append("    public Container<A> getContainer() {\n");
    buf.append("        return null;\n");
    buf.append("    }\n");
    buf.append("}\n");
    pack1.createCompilationUnit("Container.java", buf.toString(), false, null);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import test0.PrimaryContainer;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(Container<String> c) {\n");
    buf.append("         PrimaryContainer<String> list= c.getContainer();\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);
    CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
    String preview1 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import test0.PrimaryContainer;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(Container<String> c) {\n");
    buf.append("         Container<String> list= c.getContainer();\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 test0.PrimaryContainer;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(Container<String> c) {\n");
    buf.append("         PrimaryContainer<String> list= (PrimaryContainer<String>) c.getContainer();\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("\n");
    buf.append("import test0.PrimaryContainer;\n");
    buf.append("\n");
    buf.append("public class Container<A> {\n");
    buf.append("    public PrimaryContainer<String> getContainer() {\n");
    buf.append("        return null;\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("\n");
    buf.append("import test0.PrimaryContainer;\n");
    buf.append("\n");
    buf.append("public class Container<A> implements PrimaryContainer<String> {\n");
    buf.append("    public Container<A> getContainer() {\n");
    buf.append("        return null;\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected4 = buf.toString();
    assertEqualStringsIgnoreOrder(new String[] { preview1, preview2, preview3, preview4 }, new String[] { expected1, expected2, expected3, expected4 });
}
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 87 with CUCorrectionProposal

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

the class TypeMismatchQuickFixTests method testTypeMismatchInAssignment.

@Test
public void testTypeMismatchInAssignment() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.util.Iterator;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(Iterator iter) {\n");
    buf.append("        String str;\n");
    buf.append("        str= iter.next();\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.util.Iterator;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(Iterator iter) {\n");
    buf.append("        String str;\n");
    buf.append("        str= (String) iter.next();\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.util.Iterator;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(Iterator iter) {\n");
    buf.append("        Object str;\n");
    buf.append("        str= iter.next();\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 88 with CUCorrectionProposal

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

the class TypeMismatchQuickFixTests method testMismatchingReturnTypeOnGenericMethod14.

@Test
public void testMismatchingReturnTypeOnGenericMethod14() throws Exception {
    Map<String, String> options = fJProject1.getOptions(false);
    try {
        Map<String, String> options14 = new HashMap<String, String>(options);
        JavaModelUtil.setComplianceOptions(options14, JavaCore.VERSION_1_4);
        fJProject1.setOptions(options14);
        IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
        StringBuffer buf = new StringBuffer();
        buf.append("package test1;\n");
        buf.append("import java.lang.reflect.AccessibleObject;\n");
        buf.append("public class E {\n");
        buf.append("    void m() {\n");
        buf.append("        new AccessibleObject() {\n");
        buf.append("            public void getAnnotation(Class annotationClass) {\n");
        buf.append("            }\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, 1);
        assertCorrectLabels(proposals);
        CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
        String preview1 = getPreviewContent(proposal);
        buf = new StringBuffer();
        buf.append("package test1;\n");
        buf.append("import java.lang.annotation.Annotation;\n");
        buf.append("import java.lang.reflect.AccessibleObject;\n");
        buf.append("public class E {\n");
        buf.append("    void m() {\n");
        buf.append("        new AccessibleObject() {\n");
        buf.append("            public Annotation getAnnotation(Class annotationClass) {\n");
        buf.append("            }\n");
        buf.append("        };\n");
        buf.append("    }\n");
        buf.append("}\n");
        String expected1 = buf.toString();
        assertEqualStringsIgnoreOrder(new String[] { preview1 }, new String[] { expected1 });
    } finally {
        fJProject1.setOptions(options);
    }
}
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) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 89 with CUCorrectionProposal

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

the class TypeMismatchQuickFixTests method testMismatchingExceptionsOnGeneric.

@Test
public void testMismatchingExceptionsOnGeneric() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public interface IBase<T> {\n");
    buf.append("    T[] getValues();\n");
    buf.append("}\n");
    pack1.createCompilationUnit("IBase.java", buf.toString(), false, null);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("public class E implements IBase<String> {\n");
    buf.append("    public String[] getValues() throws IOException {\n");
    buf.append("        return null;\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("\n");
    buf.append("import java.io.IOException;\n");
    buf.append("\n");
    buf.append("public interface IBase<T> {\n");
    buf.append("    T[] getValues() throws IOException;\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.IOException;\n");
    buf.append("public class E implements IBase<String> {\n");
    buf.append("    public String[] getValues() {\n");
    buf.append("        return null;\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 90 with CUCorrectionProposal

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

the class TypeMismatchQuickFixTests method testTypeMismatchInAssignment3.

@Test
public void testTypeMismatchInAssignment3() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.util.Iterator;\n");
    buf.append("public enum E {\n");
    buf.append("    A, B;\n");
    buf.append("    String str, str2;\n");
    buf.append("    public void foo(Iterator iter) {\n");
    buf.append("        str2= iter.next();\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.util.Iterator;\n");
    buf.append("public enum E {\n");
    buf.append("    A, B;\n");
    buf.append("    String str, str2;\n");
    buf.append("    public void foo(Iterator iter) {\n");
    buf.append("        str2= (String) iter.next();\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.util.Iterator;\n");
    buf.append("public enum E {\n");
    buf.append("    A, B;\n");
    buf.append("    String str;\n");
    buf.append("    Object str2;\n");
    buf.append("    public void foo(Iterator iter) {\n");
    buf.append("        str2= iter.next();\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)

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