Search in sources :

Example 96 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.

the class LocalCorrectionsQuickFixTest method testUnimplementedMethodsInEnumConstant2.

@Test
public void testUnimplementedMethodsInEnumConstant2() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test;\n");
    buf.append("enum TestEnum {\n");
    buf.append("    A {\n");
    buf.append("    };\n");
    buf.append("    public abstract boolean foo();\n");
    buf.append("    public abstract void bar();\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("TestEnum.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    ArrayList proposals = collectCorrections(cu, astRoot, 2);
    assertCorrectLabels(proposals);
    assertNumberOfProposals(proposals, 1);
    String[] expected = new String[1];
    buf = new StringBuffer();
    buf.append("package test;\n");
    buf.append("enum TestEnum {\n");
    buf.append("    A {\n");
    buf.append("\n");
    buf.append("        @Override\n");
    buf.append("        public boolean foo() {\n");
    buf.append("            return false;\n");
    buf.append("        }\n");
    buf.append("\n");
    buf.append("        @Override\n");
    buf.append("        public void bar() {\n");
    buf.append("        }\n");
    buf.append("    };\n");
    buf.append("    public abstract boolean foo();\n");
    buf.append("    public abstract void bar();\n");
    buf.append("}\n");
    expected[0] = buf.toString();
    assertExpectedExistInProposals(proposals, expected);
}
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) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 97 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit 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)

Example 98 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.

the class LocalCorrectionsQuickFixTest method testUndefinedConstructorWithLineBreaks.

@Test
public void testUndefinedConstructorWithLineBreaks() throws Exception {
    Hashtable hashtable = JavaCore.getOptions();
    hashtable.put(DefaultCodeFormatterConstants.FORMATTER_LINE_SPLIT, "30");
    String optionValue = DefaultCodeFormatterConstants.createAlignmentValue(false, DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE, DefaultCodeFormatterConstants.INDENT_DEFAULT);
    hashtable.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_CONSTRUCTOR_DECLARATION, optionValue);
    hashtable.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_EXPLICIT_CONSTRUCTOR_CALL, optionValue);
    JavaCore.setOptions(hashtable);
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class F {\n");
    buf.append("    public F(Runnable runnable, boolean isGreen, boolean isBlue, boolean isRed) {\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, 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 extends F {\n");
    buf.append("\n");
    buf.append("    public E(\n");
    buf.append("            Runnable runnable,\n");
    buf.append("            boolean isGreen,\n");
    buf.append("            boolean isBlue,\n");
    buf.append("            boolean isRed) {\n");
    buf.append("        super(\n");
    buf.append("                runnable,\n");
    buf.append("                isGreen,\n");
    buf.append("                isBlue,\n");
    buf.append("                isRed);\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 99 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.

the class LocalCorrectionsQuickFixTest method testUnimplementedMethodsWithSubsignature3.

@Test
public void testUnimplementedMethodsWithSubsignature3() throws Exception {
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=272657 , https://bugs.eclipse.org/bugs/show_bug.cgi?id=424509#c6
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test;\n");
    buf.append("import java.math.BigInteger;\n");
    buf.append("\n");
    buf.append("interface A { Object m(Class c); }\n");
    buf.append("interface B<S extends Number> { Object m(Class<S> c); }\n");
    buf.append("interface D<S,T> extends B<BigInteger>, A {}\n");
    buf.append("\n");
    buf.append("class M implements D<BigInteger,BigInteger> {\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("A.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    ArrayList proposals = collectCorrections(cu, astRoot);
    assertCorrectLabels(proposals);
    assertNumberOfProposals(proposals, 2);
    String[] expected = new String[1];
    buf = new StringBuffer();
    buf.append("package test;\n");
    buf.append("import java.math.BigInteger;\n");
    buf.append("\n");
    buf.append("interface A { Object m(Class c); }\n");
    buf.append("interface B<S extends Number> { Object m(Class<S> c); }\n");
    buf.append("interface D<S,T> extends B<BigInteger>, A {}\n");
    buf.append("\n");
    buf.append("class M implements D<BigInteger,BigInteger> {\n");
    buf.append("\n");
    buf.append("    public Object m(Class c) {\n");
    buf.append("        return null;\n");
    buf.append("    }\n");
    buf.append("}\n");
    expected[0] = buf.toString();
    assertExpectedExistInProposals(proposals, expected);
}
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) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 100 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.

the class LocalCorrectionsQuickFixTest method testUnimplementedMethods_bug113665.

@Test
public void testUnimplementedMethods_bug113665() throws Exception {
    IPackageFragment pack2 = fSourceFolder.createPackageFragment("test2", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test2;\n");
    buf.append("public interface F {\n");
    buf.append("      public void c() throws Exception;\n");
    buf.append("      public void e();\n");
    buf.append("}\n");
    pack2.createCompilationUnit("F.java", buf.toString(), false, null);
    buf = new StringBuffer();
    buf.append("package test2;\n");
    buf.append("public class A implements F {\n");
    buf.append("    public void c() throws Exception, RuntimeException { }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack2.createCompilationUnit("A.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 test2;\n");
    buf.append("public class A implements F {\n");
    buf.append("    public void c() throws Exception, RuntimeException { }\n");
    buf.append("\n");
    buf.append("    public void e() {\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("    public void c() throws Exception, RuntimeException { }\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

CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)632 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)557 ArrayList (java.util.ArrayList)453 Test (org.junit.Test)441 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)435 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)228 Hashtable (java.util.Hashtable)137 ASTNode (org.eclipse.jdt.core.dom.ASTNode)90 ASTParser (org.eclipse.jdt.core.dom.ASTParser)44 SimpleName (org.eclipse.jdt.core.dom.SimpleName)36 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)35 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)35 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)35 Ignore (org.junit.Ignore)34 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)29 Type (org.eclipse.jdt.core.dom.Type)27 AST (org.eclipse.jdt.core.dom.AST)23 Expression (org.eclipse.jdt.core.dom.Expression)21 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)19 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)19