Search in sources :

Example 26 with Factory

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);
}
Also used : Factory(spoon.reflect.factory.Factory) Launcher(spoon.Launcher) CtLocalVariable(spoon.reflect.code.CtLocalVariable) Test(org.junit.Test)

Example 27 with Factory

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());
    }
}
Also used : CtBlock(spoon.reflect.code.CtBlock) CoreFactory(spoon.reflect.factory.CoreFactory) Factory(spoon.reflect.factory.Factory) CodeFactory(spoon.reflect.factory.CodeFactory) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 28 with Factory

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;
        }
    });
}
Also used : CtClass(spoon.reflect.declaration.CtClass) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtNewClass(spoon.reflect.code.CtNewClass) Foo(spoon.test.constructorcallnewclass.testclasses.Foo) Factory(spoon.reflect.factory.Factory) Before(org.junit.Before)

Example 29 with Factory

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) Factory(spoon.reflect.factory.Factory) Foo2(spoon.test.constructorcallnewclass.testclasses.Foo2) Test(org.junit.Test)

Example 30 with Factory

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);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) ClassWithBodies(spoon.test.ctBodyHolder.testclasses.ClassWithBodies) Factory(spoon.reflect.factory.Factory) 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