Search in sources :

Example 1 with DefaultCoreFactory

use of spoon.support.DefaultCoreFactory in project spoon by INRIA.

the class FactoryImpl method Core.

/**
 * The core factory.
 */
@Override
public CoreFactory Core() {
    if (core == null) {
        // During deserialization, the transient field core, is null
        core = new DefaultCoreFactory();
        core.setMainFactory(this);
    }
    return core;
}
Also used : DefaultCoreFactory(spoon.support.DefaultCoreFactory)

Example 2 with DefaultCoreFactory

use of spoon.support.DefaultCoreFactory in project spoon by INRIA.

the class CommentTest method testAddCommentsToSnippet.

@Test
public void testAddCommentsToSnippet() {
    Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
    factory.getEnvironment().setNoClasspath(true);
    factory.getEnvironment().setCommentEnabled(true);
    CtStatement statement = factory.Code().createCodeSnippetStatement("System.out.println(\"Caenorhabditis\")");
    CtComment comment = factory.createComment("My comment on my statement", CtComment.CommentType.INLINE);
    statement.addComment(comment);
    CtExpression expression = factory.Code().createCodeSnippetExpression("\"Caenorhabditis\" + \"Caenorhabditis\"");
    CtComment commentExpression = factory.createComment("My comment on my expression", CtComment.CommentType.INLINE);
    expression.addComment(commentExpression);
    assertEquals("// My comment on my statement" + newLine + "System.out.println(\"Caenorhabditis\")", statement.toString());
    assertEquals("// My comment on my expression" + newLine + "\"Caenorhabditis\" + \"Caenorhabditis\"", expression.toString());
}
Also used : CtComment(spoon.reflect.code.CtComment) DefaultCoreFactory(spoon.support.DefaultCoreFactory) CtStatement(spoon.reflect.code.CtStatement) CtExpression(spoon.reflect.code.CtExpression) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) FactoryImpl(spoon.reflect.factory.FactoryImpl) StandardEnvironment(spoon.support.StandardEnvironment) Test(org.junit.Test)

Example 3 with DefaultCoreFactory

use of spoon.support.DefaultCoreFactory 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)

Example 4 with DefaultCoreFactory

use of spoon.support.DefaultCoreFactory in project spoon by INRIA.

the class FactoryTest method testFactoryOverriding.

@Test
public void testFactoryOverriding() throws Exception {
    @SuppressWarnings("serial")
    class MyCtMethod<T> extends CtMethodImpl<T> {
    }
    ;
    @SuppressWarnings("serial") final CoreFactory specialCoreFactory = new DefaultCoreFactory() {

        @Override
        public <T> CtMethod<T> createMethod() {
            MyCtMethod<T> m = new MyCtMethod<T>();
            m.setFactory(getMainFactory());
            return m;
        }
    };
    Launcher launcher = new Launcher() {

        @Override
        public Factory createFactory() {
            return new FactoryImpl(specialCoreFactory, new StandardEnvironment());
        }
    };
    CtClass<?> type = build("spoon.test.testclasses", "SampleClass", launcher.getFactory());
    CtMethod<?> m = type.getMethodsByName("method3").get(0);
    assertTrue(m instanceof MyCtMethod);
}
Also used : DefaultCoreFactory(spoon.support.DefaultCoreFactory) CtMethodImpl(spoon.support.reflect.declaration.CtMethodImpl) Launcher(spoon.Launcher) DefaultCoreFactory(spoon.support.DefaultCoreFactory) CoreFactory(spoon.reflect.factory.CoreFactory) FactoryImpl(spoon.reflect.factory.FactoryImpl) StandardEnvironment(spoon.support.StandardEnvironment) Test(org.junit.Test)

Example 5 with DefaultCoreFactory

use of spoon.support.DefaultCoreFactory in project spoon by INRIA.

the class SerializableTest method testSerialCtStatement.

@Test
public void testSerialCtStatement() throws Exception {
    Factory factory = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());
    CtStatement sta2 = (factory).Code().createCodeSnippetStatement("String hello =\"t1\"; System.out.println(hello)").compile();
    byte[] ser = ByteSerialization.serialize(sta2);
    CtStatement des = (CtStatement) ByteSerialization.deserialize(ser);
    String sigBef = sta2.getShortRepresentation();
    String sigAf = des.getShortRepresentation();
    CtType<?> typeBef = sta2.getParent(CtType.class);
    assertNotNull(typeBef);
    assertEquals(sigBef, sigAf);
    des.setFactory(factory);
    String toSBef = sta2.toString();
    String toSgAf = des.toString();
    assertEquals(toSBef, toSgAf);
    CtType<?> typeDes = des.getParent(CtType.class);
    assertNotNull(typeDes);
    // After deserialization, getDeclaringType throws an exception
    CtType<?> decl = typeDes.getDeclaringType();
    assertNull(decl);
    CtPackage parentOriginal = (CtPackage) typeBef.getParent();
    CtPackage parentDeser = (CtPackage) typeDes.getParent();
    assertEquals(CtPackage.TOP_LEVEL_PACKAGE_NAME, parentOriginal.getSimpleName());
    assertEquals(CtPackage.TOP_LEVEL_PACKAGE_NAME, parentDeser.getSimpleName());
}
Also used : DefaultCoreFactory(spoon.support.DefaultCoreFactory) CtStatement(spoon.reflect.code.CtStatement) Factory(spoon.reflect.factory.Factory) DefaultCoreFactory(spoon.support.DefaultCoreFactory) CtPackage(spoon.reflect.declaration.CtPackage) FactoryImpl(spoon.reflect.factory.FactoryImpl) StandardEnvironment(spoon.support.StandardEnvironment) Test(org.junit.Test)

Aggregations

DefaultCoreFactory (spoon.support.DefaultCoreFactory)12 FactoryImpl (spoon.reflect.factory.FactoryImpl)11 StandardEnvironment (spoon.support.StandardEnvironment)11 Test (org.junit.Test)10 Factory (spoon.reflect.factory.Factory)10 CtStatement (spoon.reflect.code.CtStatement)4 CtClass (spoon.reflect.declaration.CtClass)3 CtMethod (spoon.reflect.declaration.CtMethod)3 JDTSnippetCompiler (spoon.support.compiler.jdt.JDTSnippetCompiler)3 SpoonModelBuilder (spoon.SpoonModelBuilder)2 CtInvocation (spoon.reflect.code.CtInvocation)2 CoreFactory (spoon.reflect.factory.CoreFactory)2 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1 Launcher (spoon.Launcher)1 FactoryAccessor (spoon.processing.FactoryAccessor)1 CtAssignment (spoon.reflect.code.CtAssignment)1 CtBlock (spoon.reflect.code.CtBlock)1 CtComment (spoon.reflect.code.CtComment)1 CtExpression (spoon.reflect.code.CtExpression)1