use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class EqualTest method testEqualsActualTypeRef.
@Test
public void testEqualsActualTypeRef() throws Exception {
// contract: actual type refs are part of the identity
Factory factory = new Launcher().createFactory();
CtLocalVariable var = factory.Code().createCodeSnippetStatement("java.util.List<String> l ").compile();
CtLocalVariable var2 = factory.Code().createCodeSnippetStatement("java.util.List<Object> l ").compile();
assertNotEquals(var2, var);
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class CompilationTest method testNewInstance.
@Test
public void testNewInstance() throws Exception {
// contract: a ctclass can be instantiated, and each modification results in a new valid object
Factory factory = new Launcher().getFactory();
CtClass<Ifoo> c = factory.Code().createCodeSnippetStatement("class X implements spoon.test.compilation.Ifoo { public int foo() {int i=0; return i;} }").compile();
// required otherwise java.lang.IllegalAccessException at runtime when instantiating
c.addModifier(ModifierKind.PUBLIC);
CtBlock body = c.getElements(new TypeFilter<>(CtBlock.class)).get(1);
Ifoo o = c.newInstance();
assertEquals(0, o.foo());
for (int i = 1; i <= 10; i++) {
body.getStatement(0).replace(factory.Code().createCodeSnippetStatement("int i = " + i + ";"));
o = c.newInstance();
// each time this is a new class
// each time the behavior has changed!
assertEquals(i, o.foo());
}
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class NewClassTest method setUp.
@Before
public void setUp() throws Exception {
final Factory build = build(Foo.class);
final CtClass<?> foo = (CtClass<?>) build.Type().get(Foo.class);
newClasses = foo.getElements(new AbstractFilter<CtNewClass<?>>(CtNewClass.class) {
@Override
public boolean matches(CtNewClass<?> element) {
return true;
}
});
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class NewClassTest method testMoreThan9NewClass.
@Test
public void testMoreThan9NewClass() throws Exception {
final Factory build = build(Foo2.class);
final CtClass<?> foo = (CtClass<?>) build.Type().get(Foo2.class);
List<CtNewClass<?>> elements = foo.getElements(new AbstractFilter<CtNewClass<?>>(CtNewClass.class) {
@Override
public boolean matches(CtNewClass<?> element) {
return true;
}
});
assertEquals(13, elements.size());
assertEquals(Foo2.class.getCanonicalName() + "$12", elements.get(11).getAnonymousClass().getQualifiedName());
assertEquals(Foo2.class.getCanonicalName() + "$12$1", elements.get(12).getAnonymousClass().getQualifiedName());
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class CtBodyHolderTest method testMethod.
@Test
public void testMethod() throws Exception {
Factory factory = build(ClassWithBodies.class, CWBStatementTemplate.class);
CtClass<?> cwbClass = (CtClass<?>) factory.Type().get(ClassWithBodies.class);
assertEquals(2, cwbClass.getMethods().size());
CtMethod<?> method = cwbClass.getMethod("method");
checkCtBody(method, "method_body", 0);
}
Aggregations