use of spoon.reflect.declaration.CtAnonymousExecutable in project spoon by INRIA.
the class CtTypeImpl method addTypeMemberAt.
@Override
public <C extends CtType<T>> C addTypeMemberAt(int position, CtTypeMember member) {
if (member == null) {
return (C) this;
}
if (this.typeMembers == CtElementImpl.<CtTypeMember>emptyList()) {
this.typeMembers = new ArrayList<>();
}
if (!this.typeMembers.stream().anyMatch(m -> m == member)) {
member.setParent(this);
CtRole role;
if (member instanceof CtMethod) {
role = METHOD;
} else if (member instanceof CtConstructor) {
role = CONSTRUCTOR;
} else if (member instanceof CtField) {
role = FIELD;
} else if (member instanceof CtAnonymousExecutable) {
role = ANNONYMOUS_EXECUTABLE;
} else {
role = NESTED_TYPE;
}
getFactory().getEnvironment().getModelChangeListener().onListAdd(this, role, this.typeMembers, position, member);
if (position < typeMembers.size()) {
this.typeMembers.add(position, member);
} else {
this.typeMembers.add(member);
}
}
return (C) this;
}
use of spoon.reflect.declaration.CtAnonymousExecutable in project spoon by INRIA.
the class InitializerTest method testModelBuildingStaticInitializer.
@Test
public void testModelBuildingStaticInitializer() throws Exception {
CtClass<?> type = build("spoon.test.initializers", "InternalClassStaticFieldInit");
assertEquals("InternalClassStaticFieldInit", type.getSimpleName());
CtClass<?> InternalClass = type.getNestedType("InternalClass");
assertTrue(InternalClass.getModifiers().contains(ModifierKind.STATIC));
CtAnonymousExecutable staticBlock = type.getElements(new TypeFilter<CtAnonymousExecutable>(CtAnonymousExecutable.class)).get(0);
assertTrue(staticBlock.getModifiers().contains(ModifierKind.STATIC));
assertEquals(1, staticBlock.getBody().getStatements().size());
// this fails: regression or known bug?
// RP: this look OK. Spoon adds the full path
// assertEquals("InternalClass.tmp = \"nop\"",
// staticBlock.getBody().getStatements().get(0).toString());
}
use of spoon.reflect.declaration.CtAnonymousExecutable in project spoon by INRIA.
the class InitializerTest method testModelBuildingInitializer.
@Test
public void testModelBuildingInitializer() throws Exception {
CtClass<?> type = build("spoon.test.initializers", "InstanceInitializers");
assertEquals("InstanceInitializers", type.getSimpleName());
CtField<?> k = type.getElements(new NamedElementFilter<>(CtField.class, "k")).get(0);
assertTrue(k.getDefaultExpression() instanceof CtConstructorCall);
CtField<?> l = type.getElements(new NamedElementFilter<>(CtField.class, "l")).get(0);
assertTrue(l.getDefaultExpression() instanceof CtConstructorCall);
CtField<?> x = type.getElements(new NamedElementFilter<>(CtField.class, "x")).get(0);
assertTrue(x.getDefaultExpression() == null);
CtField<?> y = type.getElements(new NamedElementFilter<>(CtField.class, "y")).get(0);
assertTrue(y.getDefaultExpression() instanceof CtLiteral);
CtField<?> z = type.getElements(new NamedElementFilter<>(CtField.class, "z")).get(0);
assertTrue(z.getDefaultExpression().toString().equals("5"));
// static initializer
CtAnonymousExecutable ex = type.getElements(new TypeFilter<CtAnonymousExecutable>(CtAnonymousExecutable.class)).get(0);
assertEquals("x = 3", ex.getBody().getStatements().get(0).toString());
}
use of spoon.reflect.declaration.CtAnonymousExecutable in project spoon by INRIA.
the class ExecutableFactory method createAnonymous.
/**
* Creates an anonymous executable (initializer block) in a target class).
*/
public CtAnonymousExecutable createAnonymous(CtClass<?> target, CtBlock<Void> body) {
CtAnonymousExecutable a = factory.Core().createAnonymousExecutable();
target.addAnonymousExecutable(a);
a.setBody(body);
return a;
}
use of spoon.reflect.declaration.CtAnonymousExecutable in project spoon by INRIA.
the class DeleteTest method testDeleteAStatementInAnonymousExecutable.
@Test
public void testDeleteAStatementInAnonymousExecutable() throws Exception {
final Factory factory = build(Adobada.class);
final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
final List<CtAnonymousExecutable> anonymousExecutables = adobada.getAnonymousExecutables();
final CtAnonymousExecutable instanceExec = anonymousExecutables.get(0);
assertEquals(2, instanceExec.getBody().getStatements().size());
final CtStatement statement = instanceExec.getBody().getStatement(1);
statement.delete();
assertEquals(1, instanceExec.getBody().getStatements().size());
assertFalse(instanceExec.getBody().getStatements().contains(statement));
}
Aggregations