Search in sources :

Example 11 with CtReturn

use of spoon.reflect.code.CtReturn in project spoon by INRIA.

the class EqualTest method testEqualsEmptyException.

@Test
public void testEqualsEmptyException() throws Exception {
    Factory factory = new Launcher().createFactory();
    String realParam1 = "\"\"";
    String content = "" + "class X {" + "public Object foo() {" + " Integer.getInteger(" + realParam1 + ");" + " return \"\";" + "}};";
    SpoonModelBuilder builder = new JDTSnippetCompiler(factory, content);
    try {
        builder.build();
    } catch (Exception e) {
        e.printStackTrace();
        fail("Unable create model");
    }
    CtClass<?> clazz1 = (CtClass<?>) factory.Type().getAll().get(0);
    CtMethod<?> method = (CtMethod<?>) clazz1.getMethods().toArray()[0];
    CtInvocation<?> invo = (CtInvocation<?>) method.getBody().getStatement(0);
    CtLiteral<?> argument1 = (CtLiteral<?>) invo.getArguments().get(0);
    assertEquals(realParam1, argument1.toString());
    CtReturn<?> returnStatement = (CtReturn<?>) method.getBody().getStatement(1);
    CtLiteral<?> returnExp = (CtLiteral<?>) returnStatement.getReturnedExpression();
    assertEquals(realParam1, returnExp.toString());
    try {
        assertEquals(argument1, returnExp);
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Factory(spoon.reflect.factory.Factory) CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) CtLiteral(spoon.reflect.code.CtLiteral) CtReturn(spoon.reflect.code.CtReturn) JDTSnippetCompiler(spoon.support.compiler.jdt.JDTSnippetCompiler) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 12 with CtReturn

use of spoon.reflect.code.CtReturn 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)

Aggregations

CtReturn (spoon.reflect.code.CtReturn)12 Test (org.junit.Test)9 Launcher (spoon.Launcher)5 CtConstructorCall (spoon.reflect.code.CtConstructorCall)5 CtClass (spoon.reflect.declaration.CtClass)5 Factory (spoon.reflect.factory.Factory)5 CtMethod (spoon.reflect.declaration.CtMethod)4 CtInvocation (spoon.reflect.code.CtInvocation)3 CtStatement (spoon.reflect.code.CtStatement)3 CtModel (spoon.reflect.CtModel)2 CtBinaryOperator (spoon.reflect.code.CtBinaryOperator)2 CtBlock (spoon.reflect.code.CtBlock)2 CtComment (spoon.reflect.code.CtComment)2 CtDo (spoon.reflect.code.CtDo)2 CtExpression (spoon.reflect.code.CtExpression)2 CtFor (spoon.reflect.code.CtFor)2 CtIf (spoon.reflect.code.CtIf)2 CtLiteral (spoon.reflect.code.CtLiteral)2 CtLocalVariable (spoon.reflect.code.CtLocalVariable)2 CtSwitch (spoon.reflect.code.CtSwitch)2