Search in sources :

Example 31 with Factory

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());
}
Also used : CtAssignment(spoon.reflect.code.CtAssignment) ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) CtBlock(spoon.reflect.code.CtBlock) CtType(spoon.reflect.declaration.CtType) CtStatement(spoon.reflect.code.CtStatement) Factory(spoon.reflect.factory.Factory) CWBStatementTemplate(spoon.test.ctBodyHolder.testclasses.CWBStatementTemplate)

Example 32 with Factory

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

Example 33 with Factory

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));
}
Also used : CtClass(spoon.reflect.declaration.CtClass) Foo(spoon.test.ctClass.testclasses.Foo) Factory(spoon.reflect.factory.Factory) CtConstructor(spoon.reflect.declaration.CtConstructor) Test(org.junit.Test)

Example 34 with Factory

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());
}
Also used : Factory(spoon.reflect.factory.Factory) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) CtTypeAccess(spoon.reflect.code.CtTypeAccess) Test(org.junit.Test)

Example 35 with Factory

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());
}
Also used : ClassFactory(spoon.reflect.factory.ClassFactory) DefaultCoreFactory(spoon.support.DefaultCoreFactory) CoreFactory(spoon.reflect.factory.CoreFactory) ConstructorFactory(spoon.reflect.factory.ConstructorFactory) ClassFactory(spoon.reflect.factory.ClassFactory) Factory(spoon.reflect.factory.Factory) DefaultCoreFactory(spoon.support.DefaultCoreFactory) ConstructorFactory(spoon.reflect.factory.ConstructorFactory) FactoryImpl(spoon.reflect.factory.FactoryImpl) StandardEnvironment(spoon.support.StandardEnvironment) 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