use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class CtBodyHolderTest method checkCtBody.
private void checkCtBody(CtBodyHolder p_bodyHolder, String p_constant, int off) {
CtStatement body = p_bodyHolder.getBody();
assertTrue(body instanceof CtBlock<?>);
CtBlock<?> block = (CtBlock) body;
assertEquals(1 + off, block.getStatements().size());
assertTrue(block.getStatement(off) instanceof CtAssignment);
CtAssignment assignment = block.getStatement(off);
assertEquals(p_constant, ((CtLiteral<String>) assignment.getAssignment().partiallyEvaluate()).getValue());
Factory f = body.getFactory();
CtStatement newStat = new CWBStatementTemplate("xx").apply(body.getParent(CtType.class));
try {
newStat.getParent();
fail();
} catch (ParentNotInitializedException e) {
// expected exception
}
// try to set statement and get CtBlock
p_bodyHolder.setBody(newStat);
CtBlock newBlock = (CtBlock) p_bodyHolder.getBody();
assertSame(p_bodyHolder, newBlock.getParent());
assertSame(newBlock, newStat.getParent());
// try to set CtBlock and get the same CtBlock
CtStatement newStat2 = newStat.clone();
try {
newStat2.getParent();
fail();
} catch (ParentNotInitializedException e) {
// expected exception
}
CtBlock newBlock2 = f.Code().createCtBlock(newStat2);
assertSame(newBlock2, newStat2.getParent());
try {
newBlock2.getParent();
fail();
} catch (ParentNotInitializedException e) {
// expected exception
}
p_bodyHolder.setBody(newBlock2);
assertSame(newBlock2, p_bodyHolder.getBody());
assertSame(p_bodyHolder, newBlock2.getParent());
assertSame(newBlock2, newStat2.getParent());
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class SwitchCaseTest method insertAfterStatementInSwitchCaseWithoutException.
@Test
public void insertAfterStatementInSwitchCaseWithoutException() throws Exception {
String packageName = "spoon.test.ctCase";
String className = "ClassWithSwitchExample";
Factory factory = factoryFor(packageName, className);
List<CtCase> elements = elementsOfType(CtCase.class, factory);
assertEquals(3, elements.size());
CtCase firstCase = elements.get(0);
List<CtStatement> statements = firstCase.getStatements();
assertEquals(2, statements.size());
CtStatement newStatement = factory.Code().createCodeSnippetStatement("result = 0");
statements.get(0).insertAfter(newStatement);
statements = firstCase.getStatements();
assertEquals(3, statements.size());
assertTrue(statements.get(1) == newStatement);
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class CtClassTest method getConstructor.
@Test
public void getConstructor() throws Exception {
final Factory build = build(Foo.class);
final CtClass<?> foo = (CtClass<?>) build.Type().get(Foo.class);
assertEquals(3, foo.getConstructors().size());
CtTypeReference<Object> typeString = build.Code().createCtTypeReference(String.class);
CtConstructor<?> constructor = foo.getConstructor(typeString);
assertEquals(typeString, constructor.getParameters().get(0).getType());
CtArrayTypeReference<Object> typeStringArray = build.Core().createArrayTypeReference();
typeStringArray.setComponentType(typeString);
constructor = foo.getConstructor(typeStringArray);
assertEquals(typeStringArray, constructor.getParameters().get(0).getType());
CtArrayTypeReference<Object> typeStringArrayArray = build.Core().createArrayTypeReference();
typeStringArrayArray.setComponentType(typeStringArray);
constructor = foo.getConstructor(typeStringArrayArray);
assertEquals(typeStringArrayArray, constructor.getParameters().get(0).getType());
// contract: one could add a type member that already exists (equals but not same) and modify it afterwards
// this adds some flexibility for client code
// see https://github.com/INRIA/spoon/issues/1862
CtConstructor cons = foo.getConstructors().toArray(new CtConstructor[0])[0].clone();
foo.addConstructor(cons);
// as long as we have not changed the signature, getConstructors, which is based on signatures,
// thinks there is one single constructor (and that's OK)
assertEquals(3, foo.getConstructors().size());
cons.addParameter(cons.getFactory().createParameter().setType(cons.getFactory().Type().OBJECT));
// now that we have changed the signature we can call getConstructors safely
assertEquals(4, foo.getConstructors().size());
// we cloned the first constructor, so it has the same position, and comes before the 2nd and 3rd constructor
assertSame(cons, foo.getTypeMembers().get(1));
// the parent is set (the core problem described in the issue has been fixed)
assertSame(foo, cons.getParent());
// now we clone and reset the position
CtConstructor cons2 = foo.getConstructors().toArray(new CtConstructor[0])[0].clone();
cons2.setPosition(null);
// adding the constructor, this time, without a position
foo.addConstructor(cons2);
// without position, it has been addded at the end
assertSame(cons2, foo.getTypeMembers().get(4));
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class CodeFactoryTest method testThisAccess.
@Test
public void testThisAccess() throws Exception {
final Factory factory = createFactory();
final CtTypeReference<Object> type = factory.Type().createReference("fr.inria.Test");
final CtThisAccess<Object> thisAccess = factory.Code().createThisAccess(type);
assertNotNull(thisAccess.getTarget());
assertTrue(thisAccess.getTarget() instanceof CtTypeAccess);
assertEquals(type, ((CtTypeAccess) thisAccess.getTarget()).getAccessedType());
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class ConstructorFactoryTest method testCreateDefault.
@Test
public void testCreateDefault() {
Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
ClassFactory classf = factory.Class();
ConstructorFactory ctorf = factory.Constructor();
CtClass<?> ctclass = classf.create("Sample");
ctorf.createDefault(ctclass);
CtConstructor<?> c = ctclass.getConstructor();
Assert.assertEquals(0, c.getParameters().size());
}
Aggregations