Search in sources :

Example 76 with IPackageFragment

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

the class ModifierCorrectionsQuickFixTest method testStaticOverridesMethod.

@Test
public void testStaticOverridesMethod() throws Exception {
    IPackageFragment pack2 = fSourceFolder.createPackageFragment("test2", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test2;\n");
    buf.append("public class C {\n");
    buf.append("    public void foo() {\n");
    buf.append("    }\n");
    buf.append("}\n");
    pack2.createCompilationUnit("C.java", buf.toString(), false, null);
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import test2.C;\n");
    buf.append("public class E extends C {\n");
    buf.append("    public static void 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, 1);
    assertCorrectLabels(proposals);
    CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
    String preview = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import test2.C;\n");
    buf.append("public class E extends C {\n");
    buf.append("    public void foo() {\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) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 77 with IPackageFragment

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

the class LocalCorrectionsQuickFixTest method testUncaughtExceptionDuplicate.

@Test
public void testUncaughtExceptionDuplicate() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class MyException extends Exception {\n");
    buf.append("}\n");
    pack1.createCompilationUnit("MyException.java", buf.toString(), false, null);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("import java.text.ParseException;\n");
    buf.append("public class E {\n");
    buf.append("    public void m1() throws IOException {\n");
    buf.append("        m2();\n");
    buf.append("    }\n");
    buf.append("    public void m2() throws IOException, ParseException, MyException {\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    // 2 uncaught exceptions
    ArrayList proposals = collectCorrections(cu, astRoot, 2);
    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("import java.text.ParseException;\n");
    buf.append("public class E {\n");
    buf.append("    public void m1() throws IOException, ParseException, MyException {\n");
    buf.append("        m2();\n");
    buf.append("    }\n");
    buf.append("    public void m2() throws IOException, ParseException, MyException {\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("import java.text.ParseException;\n");
    buf.append("public class E {\n");
    buf.append("    public void m1() throws IOException {\n");
    buf.append("        try {\n");
    buf.append("            m2();\n");
    buf.append("        } catch (ParseException e) {\n");
    buf.append("        } catch (MyException e) {\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("    public void m2() throws IOException, ParseException, MyException {\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 78 with IPackageFragment

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

the class LocalCorrectionsQuickFixTest method testUnimplementedMethods_bug122906.

@Test
public void testUnimplementedMethods_bug122906() throws Exception {
    IPackageFragment pack2 = fSourceFolder.createPackageFragment("test2", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test2;\n");
    buf.append("import java.util.Properties;\n");
    buf.append("public interface F {\n");
    buf.append("    public void b(Properties p);\n");
    buf.append("    public void g(test2.Properties p);\n");
    buf.append("}\n");
    pack2.createCompilationUnit("F.java", buf.toString(), false, null);
    buf = new StringBuffer();
    buf.append("package test2;\n");
    buf.append("public class Properties {}\n");
    pack2.createCompilationUnit("Properties.java", buf.toString(), false, null);
    buf = new StringBuffer();
    buf.append("package test2;\n");
    buf.append("public class A implements F {\n");
    buf.append("}\n");
    ICompilationUnit cu = pack2.createCompilationUnit("A.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    ArrayList proposals = collectCorrections(cu, astRoot, 2);
    assertNumberOfProposals(proposals, 2);
    assertCorrectLabels(proposals);
    CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
    String preview1 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test2;\n");
    buf.append("\n");
    buf.append("import java.util.Properties;\n");
    buf.append("\n");
    buf.append("public class A implements F {\n");
    buf.append("\n");
    buf.append("    public void b(Properties p) {\n");
    buf.append("    }\n");
    buf.append("\n");
    buf.append("    public void g(test2.Properties p) {\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 test2;\n");
    buf.append("public abstract class A implements F {\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 79 with IPackageFragment

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

the class LocalCorrectionsQuickFixTest method testUnnecessaryInstanceof2.

@Test
public void testUnnecessaryInstanceof2() 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(String b) {\n");
    buf.append("        if  (b instanceof String && b.getClass() != null) {\n");
    buf.append("            System.out.println();\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(String b) {\n");
    buf.append("        if  (b != null && b.getClass() != null) {\n");
    buf.append("            System.out.println();\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)

Example 80 with IPackageFragment

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

the class LocalCorrectionsQuickFixTest method testUnusedVariableBug120579.

@Test
public void testUnusedVariableBug120579() throws Exception {
    Hashtable hashtable = JavaCore.getOptions();
    hashtable.put(JavaCore.COMPILER_PB_UNUSED_LOCAL, JavaCore.ERROR);
    hashtable.put(JavaCore.COMPILER_PB_UNUSED_PARAMETER, 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() {\n");
    buf.append("        char[] array= new char[0];\n");
    buf.append("        for (char element: array) {}\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, 0);
    assertCorrectLabels(proposals);
}
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)

Aggregations

IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1147 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1052 Test (org.junit.Test)1039 ArrayList (java.util.ArrayList)755 List (java.util.List)453 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)435 AssistContext (org.eclipse.jdt.internal.ui.text.correction.AssistContext)384 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)284 Hashtable (java.util.Hashtable)136 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)100 OrganizeImportsOperation (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation)67 IChooseImportQuery (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery)67 Ignore (org.junit.Ignore)43 IType (org.eclipse.jdt.core.IType)37 IJavaElement (org.eclipse.jdt.core.IJavaElement)32 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)24 HashMap (java.util.HashMap)23 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)23 BaseTest (org.eclipse.che.plugin.java.server.che.BaseTest)21 Map (java.util.Map)19