Search in sources :

Example 86 with ICompilationUnit

use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.

the class AssistQuickFixTest17 method testReplaceMultiCatchClauseWithThrows4.

@Test
public void testReplaceMultiCatchClauseWithThrows4() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.lang.reflect.InvocationTargetException;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        try {\n");
    buf.append("            String.class.getConstructor().newInstance();\n");
    buf.append("        } catch (InstantiationException | IllegalAccessException\n");
    buf.append("                | IllegalArgumentException | InvocationTargetException\n");
    buf.append("                | NoSuchMethodException | SecurityException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    int offset = buf.toString().indexOf("IllegalArgumentException");
    AssistContext context = getCorrectionContext(cu, offset, 0);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 4);
    assertCorrectLabels(proposals);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.lang.reflect.InvocationTargetException;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        try {\n");
    buf.append("            String.class.getConstructor().newInstance();\n");
    buf.append("        } catch (InstantiationException | IllegalAccessException\n");
    buf.append("                | InvocationTargetException\n");
    buf.append("                | NoSuchMethodException | SecurityException e) {\n");
    buf.append("            e.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.lang.reflect.InvocationTargetException;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() throws IllegalArgumentException {\n");
    buf.append("        try {\n");
    buf.append("            String.class.getConstructor().newInstance();\n");
    buf.append("        } catch (InstantiationException | IllegalAccessException\n");
    buf.append("                | InvocationTargetException\n");
    buf.append("                | NoSuchMethodException | SecurityException e) {\n");
    buf.append("            e.printStackTrace();\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.lang.reflect.InvocationTargetException;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        try {\n");
    buf.append("            String.class.getConstructor().newInstance();\n");
    buf.append("        } catch (InstantiationException | IllegalAccessException\n");
    buf.append("                | InvocationTargetException\n");
    buf.append("                | NoSuchMethodException | SecurityException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        } catch (IllegalArgumentException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected3 = buf.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected1, expected2, expected3 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) List(java.util.List) Test(org.junit.Test)

Example 87 with ICompilationUnit

use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.

the class ConvertForLoopQuickFixTest method testExpressionPrecondition05.

@Test
public void testExpressionPrecondition05() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test;\n");
    buf.append("public class E {\n");
    buf.append("    Object[] x;\n");
    buf.append("    public void foo() {\n");
    buf.append("        for (int i = 0; i < x.length; i++) {}\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    assertTrue(satisfiesPrecondition(cu));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Test(org.junit.Test)

Example 88 with ICompilationUnit

use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.

the class ConvertForLoopQuickFixTest method testMatrix2.

@Test
public void testMatrix2() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class A {\n");
    buf.append("    public void foo() {\n");
    buf.append("		int[][] matrix = {{1,2},{3,4}};\n");
    buf.append("		for (int i = 0; i < matrix.length; i++){\n");
    buf.append("			for(int j = 0; j < matrix[i].length; j++){\n");
    buf.append("				System.out.println(matrix[i][j]);\n");
    buf.append("			}\n");
    buf.append("		}\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("A.java", buf.toString(), false, null);
    List proposals = fetchConvertingProposal(buf, cu);
    assertNotNull(fConvertLoopProposal);
    assertCorrectLabels(proposals);
    String preview1 = getPreviewContent(fConvertLoopProposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class A {\n");
    buf.append("    public void foo() {\n");
    buf.append("		int[][] matrix = {{1,2},{3,4}};\n");
    buf.append("		for (int[] element : matrix) {\n");
    buf.append("			for(int j = 0; j < element.length; j++){\n");
    buf.append("				System.out.println(element[j]);\n");
    buf.append("			}\n");
    buf.append("		}\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected = buf.toString();
    assertEqualString(preview1, expected);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) List(java.util.List) Test(org.junit.Test)

Example 89 with ICompilationUnit

use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.

the class ConvertForLoopQuickFixTest method testInferTypeElement.

@Test
public void testInferTypeElement() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class A {\n");
    buf.append("    public void foo() {\n");
    buf.append("		String[] array = {\"1\",\"2\"};\n");
    buf.append("		for (int i = 0; i < array.length; i++){\n");
    buf.append("		}\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("A.java", buf.toString(), false, null);
    List proposals = fetchConvertingProposal(buf, cu);
    assertNotNull(fConvertLoopProposal);
    assertCorrectLabels(proposals);
    String preview1 = getPreviewContent(fConvertLoopProposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class A {\n");
    buf.append("    public void foo() {\n");
    buf.append("		String[] array = {\"1\",\"2\"};\n");
    buf.append("		for (String element : array) {\n");
    buf.append("		}\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected = buf.toString();
    assertEqualString(preview1, expected);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) List(java.util.List) Test(org.junit.Test)

Example 90 with ICompilationUnit

use of org.eclipse.jdt.core.ICompilationUnit in project che by eclipse.

the class ConvertForLoopQuickFixTest method testSimplestClean.

@Test
public void testSimplestClean() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class A {\n");
    buf.append("    public void foo() {\n");
    buf.append("		String[] array = {\"1\",\"2\"};\n");
    buf.append("		for (int i = 0; i < array.length; i++){\n");
    buf.append("			System.out.println(array[i]);\n");
    buf.append("		}\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("A.java", buf.toString(), false, null);
    List proposals = fetchConvertingProposal(buf, cu);
    assertNotNull(fConvertLoopProposal);
    assertCorrectLabels(proposals);
    String preview1 = getPreviewContent(fConvertLoopProposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class A {\n");
    buf.append("    public void foo() {\n");
    buf.append("		String[] array = {\"1\",\"2\"};\n");
    buf.append("		for (String element : array) {\n");
    buf.append("			System.out.println(element);\n");
    buf.append("		}\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected = buf.toString();
    assertEqualString(preview1, expected);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) List(java.util.List) Test(org.junit.Test)

Aggregations

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1368 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1050 Test (org.junit.Test)1046 ArrayList (java.util.ArrayList)799 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)505 List (java.util.List)466 AssistContext (org.eclipse.jdt.internal.ui.text.correction.AssistContext)385 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)284 Hashtable (java.util.Hashtable)142 IType (org.eclipse.jdt.core.IType)98 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)79 OrganizeImportsOperation (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation)70 IChooseImportQuery (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery)70 ASTNode (org.eclipse.jdt.core.dom.ASTNode)69 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)51 IJavaElement (org.eclipse.jdt.core.IJavaElement)47 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)44 Image (org.eclipse.swt.graphics.Image)41 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)38 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)38