Search in sources :

Example 91 with CUCorrectionProposal

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

the class TypeMismatchQuickFixTests method testCastOnCastExpression.

@Test
public void testCastOnCastExpression() 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("    public void foo(List list) {\n");
    buf.append("        ArrayList a= (Cloneable) list;\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.ArrayList;\n");
    buf.append("import java.util.List;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(List list) {\n");
    buf.append("        ArrayList a= (ArrayList) list;\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.ArrayList;\n");
    buf.append("import java.util.List;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(List list) {\n");
    buf.append("        Cloneable a= (Cloneable) list;\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 92 with CUCorrectionProposal

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

the class TypeMismatchQuickFixTests method testTypeMismatchInFieldDeclNoImport.

@Test
public void testTypeMismatchInFieldDeclNoImport() 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("    private class StringBuffer { }\n");
    buf.append("    private final StringBuffer sb;\n");
    buf.append("    public E() {\n");
    buf.append("        sb= new java.lang.StringBuffer();\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("public class E {\n");
    buf.append("    private class StringBuffer { }\n");
    buf.append("    private final java.lang.StringBuffer sb;\n");
    buf.append("    public E() {\n");
    buf.append("        sb= new java.lang.StringBuffer();\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    assertEqualString(preview1, 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) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 93 with CUCorrectionProposal

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

the class TypeMismatchQuickFixTests method testTypeMismatchInAssignment2.

@Test
public void testTypeMismatchInAssignment2() 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, str2;\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, str2;\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("        String str2;\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 94 with CUCorrectionProposal

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

the class LocalCorrectionsQuickFixTest method testUnneededCatchBlockWithFinally.

@Test
public void testUnneededCatchBlockWithFinally() 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 E {\n");
    buf.append("    public void goo() {\n");
    buf.append("    }\n");
    buf.append("    public void foo() {\n");
    buf.append("        try {\n");
    buf.append("            goo();\n");
    buf.append("        } catch (IOException e) {\n");
    buf.append("        } finally {\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.IOException;\n");
    buf.append("public class E {\n");
    buf.append("    public void goo() {\n");
    buf.append("    }\n");
    buf.append("    public void foo() {\n");
    buf.append("        try {\n");
    buf.append("            goo();\n");
    buf.append("        } finally {\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.IOException;\n");
    buf.append("public class E {\n");
    buf.append("    public void goo() {\n");
    buf.append("    }\n");
    buf.append("    public void foo() throws IOException {\n");
    buf.append("        try {\n");
    buf.append("            goo();\n");
    buf.append("        } finally {\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 95 with CUCorrectionProposal

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

the class LocalCorrectionsQuickFixTest method testSetParenteses1.

//    @Test
//	public void testHidingVariable1() throws Exception {
//		Hashtable hashtable= JavaCore.getOptions();
//		hashtable.put(JavaCore.COMPILER_PB_LOCAL_VARIABLE_HIDING, JavaCore.ERROR);
//		hashtable.put(JavaCore.COMPILER_PB_FIELD_HIDING, 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("    private int count;\n");
//		buf.append("    public void foo() {\n");
//		buf.append("       int count= 1;\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);
//		assertTrue(proposals.get(0) instanceof LinkedNamesAssistProposal);
//	}
//    @Test
//	public void testHidingVariable2() throws Exception {
//		Hashtable hashtable= JavaCore.getOptions();
//		hashtable.put(JavaCore.COMPILER_PB_LOCAL_VARIABLE_HIDING, JavaCore.ERROR);
//		hashtable.put(JavaCore.COMPILER_PB_FIELD_HIDING, 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("    private int count;\n");
//		buf.append("    public void foo(int count) {\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);
//		assertTrue(proposals.get(0) instanceof LinkedNamesAssistProposal);
//	}
//
//    @Test
//	public void testHidingVariable3() throws Exception {
//		Hashtable hashtable= JavaCore.getOptions();
//		hashtable.put(JavaCore.COMPILER_PB_LOCAL_VARIABLE_HIDING, JavaCore.ERROR);
//		hashtable.put(JavaCore.COMPILER_PB_FIELD_HIDING, 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(int count) {\n");
//		buf.append("        class Inner {\n");
//		buf.append("            private int count;\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);
//		assertTrue(proposals.get(0) instanceof LinkedNamesAssistProposal);
//	}
//
//	public void testHidingVariable4() throws Exception {
//		Hashtable hashtable= JavaCore.getOptions();
//		hashtable.put(JavaCore.COMPILER_PB_LOCAL_VARIABLE_HIDING, JavaCore.ERROR);
//		hashtable.put(JavaCore.COMPILER_PB_FIELD_HIDING, 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("    private int count;\n");
//		buf.append("    public void foo() {\n");
//		buf.append("        class Inner {\n");
//		buf.append("            private int count;\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);
//		assertTrue(proposals.get(0) instanceof LinkedNamesAssistProposal);
//	}
//
//	public void testHidingVariable5() throws Exception {
//		Hashtable hashtable= JavaCore.getOptions();
//		hashtable.put(JavaCore.COMPILER_PB_LOCAL_VARIABLE_HIDING, JavaCore.ERROR);
//		hashtable.put(JavaCore.COMPILER_PB_FIELD_HIDING, 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(int count) {\n");
//		buf.append("        class Inner {\n");
//		buf.append("            public void foo() {\n");
//		buf.append("                 int count;\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);
//		assertTrue(proposals.get(0) instanceof LinkedNamesAssistProposal);
//	}
//
//	public void testHidingVariable6() throws Exception {
//		Hashtable hashtable= JavaCore.getOptions();
//		hashtable.put(JavaCore.COMPILER_PB_LOCAL_VARIABLE_HIDING, JavaCore.ERROR);
//		hashtable.put(JavaCore.COMPILER_PB_FIELD_HIDING, 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(int count) {\n");
//		buf.append("        class Inner {\n");
//		buf.append("            public void foo(int count) {\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);
//		assertTrue(proposals.get(0) instanceof LinkedNamesAssistProposal);
//	}
@Test
public void testSetParenteses1() throws Exception {
    Hashtable hashtable = JavaCore.getOptions();
    hashtable.put(JavaCore.COMPILER_PB_LOCAL_VARIABLE_HIDING, JavaCore.ERROR);
    hashtable.put(JavaCore.COMPILER_PB_FIELD_HIDING, 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(Object x) {\n");
    buf.append("        if (!x instanceof Runnable) {\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 preview = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(Object x) {\n");
    buf.append("        if (!(x instanceof Runnable)) {\n");
    buf.append("        }\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)

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