Search in sources :

Example 21 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class DeleteTest method testDeleteStatementInCase.

@Test
public void testDeleteStatementInCase() throws Exception {
    final Factory factory = build(Adobada.class);
    final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
    final CtMethod method = adobada.getMethod("m3");
    final CtCase aCase = method.getElements(new TypeFilter<>(CtCase.class)).get(0);
    assertEquals(2, aCase.getStatements().size());
    final CtStatement statement = aCase.getStatements().get(1);
    statement.delete();
    assertEquals(1, aCase.getStatements().size());
    assertFalse(aCase.getStatements().contains(statement));
}
Also used : CtStatement(spoon.reflect.code.CtStatement) CtCase(spoon.reflect.code.CtCase) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Adobada(spoon.test.delete.testclasses.Adobada) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 22 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class DeleteTest method testDeleteACaseOfASwitch.

@Test
public void testDeleteACaseOfASwitch() throws Exception {
    final Factory factory = build(Adobada.class);
    final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
    final CtMethod method = adobada.getMethod("m3");
    final CtSwitch aSwitch = method.getElements(new TypeFilter<>(CtSwitch.class)).get(0);
    final CtCase aCase = (CtCase) aSwitch.getCases().get(1);
    assertEquals(2, aSwitch.getCases().size());
    aCase.delete();
    assertEquals(1, aSwitch.getCases().size());
    assertFalse(aSwitch.getCases().contains(aCase));
}
Also used : CtCase(spoon.reflect.code.CtCase) CtSwitch(spoon.reflect.code.CtSwitch) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Adobada(spoon.test.delete.testclasses.Adobada) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 23 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class DeleteTest method testDeleteReturn.

@Test
public void testDeleteReturn() throws Exception {
    final Factory factory = build(Adobada.class);
    final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
    final CtMethod method = adobada.getMethod("m2");
    assertEquals(1, method.getBody().getStatements().size());
    final CtStatement statement = method.getBody().getStatement(0);
    statement.delete();
    assertEquals(0, method.getBody().getStatements().size());
    assertFalse(method.getBody().getStatements().contains(statement));
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Factory(spoon.reflect.factory.Factory) Adobada(spoon.test.delete.testclasses.Adobada) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 24 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class EqualTest method testEqualsComment.

@Test
public void testEqualsComment() throws Exception {
    Factory factory = new Launcher().createFactory();
    CtLocalVariable<?> var = factory.Code().createCodeSnippetStatement("int i=0").compile();
    CtLocalVariable<?> var2 = var.clone();
    var2.addComment(factory.Code().createComment("foo", CtComment.CommentType.INLINE));
    assertNotEquals(1, var.getComments().size());
    assertNotEquals(var2, var);
}
Also used : Factory(spoon.reflect.factory.Factory) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 25 with Factory

use of spoon.reflect.factory.Factory in project spoon by INRIA.

the class EqualTest method testEqualsMultitype.

@Test
public void testEqualsMultitype() throws Exception {
    Factory factory = new Launcher().createFactory();
    CtTry var = factory.Code().createCodeSnippetStatement("try{}catch(RuntimeException | AssertionError e){}").compile();
    CtTry var2 = var.clone();
    assertEquals(2, var2.getCatchers().get(0).getParameter().getMultiTypes().size());
    // removing a multitype
    var2.getCatchers().get(0).getParameter().getMultiTypes().remove(0);
    assertEquals(1, var2.getCatchers().get(0).getParameter().getMultiTypes().size());
    assertNotEquals(var2, var);
}
Also used : Factory(spoon.reflect.factory.Factory) Launcher(spoon.Launcher) CtTry(spoon.reflect.code.CtTry) Test(org.junit.Test)

Aggregations

Factory (spoon.reflect.factory.Factory)364 Test (org.junit.Test)322 Launcher (spoon.Launcher)154 CtClass (spoon.reflect.declaration.CtClass)87 CtMethod (spoon.reflect.declaration.CtMethod)73 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)65 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)53 File (java.io.File)41 CtStatement (spoon.reflect.code.CtStatement)36 CtTypeReference (spoon.reflect.reference.CtTypeReference)32 CtAnnotation (spoon.reflect.declaration.CtAnnotation)31 CtInvocation (spoon.reflect.code.CtInvocation)30 SpoonModelBuilder (spoon.SpoonModelBuilder)29 DefaultCoreFactory (spoon.support.DefaultCoreFactory)29 List (java.util.List)25 FileSystemFile (spoon.support.compiler.FileSystemFile)25 ArrayList (java.util.ArrayList)24 CtExpression (spoon.reflect.code.CtExpression)20 Annotation (java.lang.annotation.Annotation)19 MainTest (spoon.test.main.MainTest)19