Search in sources :

Example 1 with CodeFactory

use of spoon.reflect.factory.CodeFactory in project spoon by INRIA.

the class CompilationTest method compileTest.

@Test
public void compileTest() throws Exception {
    // contract: the modified version of classes is the one that is compiled to binary code
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/noclasspath/Simple.java");
    File outputBinDirectory = new File("./target/class-simple");
    if (!outputBinDirectory.exists()) {
        outputBinDirectory.mkdirs();
    }
    launcher.setBinaryOutputDirectory(outputBinDirectory);
    launcher.getEnvironment().setShouldCompile(true);
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    CoreFactory core = factory.Core();
    CodeFactory code = factory.Code();
    CtClass simple = factory.Class().get("Simple");
    CtMethod method = core.createMethod();
    method.addModifier(ModifierKind.PUBLIC);
    method.setType(factory.Type().integerPrimitiveType());
    method.setSimpleName("m");
    CtBlock block = core.createBlock();
    CtReturn aReturn = core.createReturn();
    CtBinaryOperator binaryOperator = code.createBinaryOperator(code.createLiteral(10), code.createLiteral(32), BinaryOperatorKind.PLUS);
    aReturn.setReturnedExpression(binaryOperator);
    // return 10 + 32;
    block.addStatement(aReturn);
    method.setBody(block);
    simple.addMethod(method);
    launcher.getModelBuilder().compile();
    final URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { outputBinDirectory.toURL() });
    Class<?> aClass = urlClassLoader.loadClass("Simple");
    Method m = aClass.getMethod("m");
    Assert.assertEquals(42, m.invoke(aClass.newInstance()));
}
Also used : CtBinaryOperator(spoon.reflect.code.CtBinaryOperator) CoreFactory(spoon.reflect.factory.CoreFactory) Factory(spoon.reflect.factory.Factory) CodeFactory(spoon.reflect.factory.CodeFactory) Method(java.lang.reflect.Method) CtMethod(spoon.reflect.declaration.CtMethod) CoreFactory(spoon.reflect.factory.CoreFactory) CtClass(spoon.reflect.declaration.CtClass) CtBlock(spoon.reflect.code.CtBlock) CodeFactory(spoon.reflect.factory.CodeFactory) CtReturn(spoon.reflect.code.CtReturn) URLClassLoader(java.net.URLClassLoader) Launcher(spoon.Launcher) File(java.io.File) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 2 with CodeFactory

use of spoon.reflect.factory.CodeFactory in project spoon by INRIA.

the class LiteralTest method testFactoryLiternal.

@Test
public void testFactoryLiternal() {
    Launcher runLaunch = new Launcher();
    Factory factory = runLaunch.getFactory();
    CodeFactory code = factory.Code();
    CtLiteral literal = code.createLiteral(1);
    assertEquals(1, literal.getValue());
    assertEquals(factory.Type().integerPrimitiveType(), literal.getType());
    literal = code.createLiteral(new Integer(1));
    assertEquals(1, literal.getValue());
    assertEquals(factory.Type().integerPrimitiveType(), literal.getType());
    literal = code.createLiteral(1.0);
    assertEquals(1.0, literal.getValue());
    assertEquals(factory.Type().doublePrimitiveType(), literal.getType());
    literal = code.createLiteral("literal");
    assertEquals("literal", literal.getValue());
    assertEquals(factory.Type().stringType(), literal.getType());
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) CodeFactory(spoon.reflect.factory.CodeFactory) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CodeFactory(spoon.reflect.factory.CodeFactory) TypeFactory(spoon.reflect.factory.TypeFactory) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 Launcher (spoon.Launcher)2 CodeFactory (spoon.reflect.factory.CodeFactory)2 Factory (spoon.reflect.factory.Factory)2 File (java.io.File)1 Method (java.lang.reflect.Method)1 URLClassLoader (java.net.URLClassLoader)1 CtBinaryOperator (spoon.reflect.code.CtBinaryOperator)1 CtBlock (spoon.reflect.code.CtBlock)1 CtLiteral (spoon.reflect.code.CtLiteral)1 CtReturn (spoon.reflect.code.CtReturn)1 CtClass (spoon.reflect.declaration.CtClass)1 CtMethod (spoon.reflect.declaration.CtMethod)1 CoreFactory (spoon.reflect.factory.CoreFactory)1 TypeFactory (spoon.reflect.factory.TypeFactory)1