Search in sources :

Example 21 with CtLiteral

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

the class ExecutableRefTest method methodTest.

@Test
public void methodTest() throws Exception {
    CtAbstractInvocation<?> ctAbstractInvocation = this.getInvocationFromMethod("testMethod");
    Assert.assertTrue(ctAbstractInvocation instanceof CtInvocation<?>);
    CtExecutableReference<?> executableReference = ctAbstractInvocation.getExecutable();
    Assert.assertNotNull(executableReference);
    Method method = executableReference.getActualMethod();
    Assert.assertNotNull(method);
    assertEquals("Hello World", method.invoke(null, ((CtLiteral<?>) ctAbstractInvocation.getArguments().get(0)).getValue()));
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) Method(java.lang.reflect.Method) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 22 with CtLiteral

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

the class ReplaceTest method testReplaceReplace.

@Test
public void testReplaceReplace() throws Exception {
    // bug found by Benoit
    CtClass<?> foo = factory.Package().get("spoon.test.replace.testclasses").getType("Foo");
    CtMethod<?> fooMethod = foo.getElements(new NamedElementFilter<>(CtMethod.class, "foo")).get(0);
    assertEquals("foo", fooMethod.getSimpleName());
    CtMethod<?> barMethod = foo.getElements(new NamedElementFilter<>(CtMethod.class, "bar")).get(0);
    assertEquals("bar", barMethod.getSimpleName());
    CtLocalVariable<?> assignment = (CtLocalVariable<?>) fooMethod.getBody().getStatements().get(0);
    CtLocalVariable<?> newAssignment = barMethod.getBody().getStatement(0);
    assignment.replace(newAssignment);
    assertEquals(fooMethod.getBody(), newAssignment.getParent());
    CtLiteral<Integer> lit = (CtLiteral<Integer>) foo.getElements(new TypeFilter<CtLiteral<?>>(CtLiteral.class)).get(0);
    final CtElement parent = lit.getParent();
    CtLiteral<Integer> newLit = factory.Code().createLiteral(0);
    lit.replace(newLit);
    assertEquals("int y = 0", fooMethod.getBody().getStatement(0).toString());
    assertEquals(parent, newLit.getParent());
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) CtElement(spoon.reflect.declaration.CtElement) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) CtLocalVariable(spoon.reflect.code.CtLocalVariable) Test(org.junit.Test)

Example 23 with CtLiteral

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

the class SignatureTest method testNullSignature.

@Test
public void testNullSignature() throws Exception {
    // bug found by Thomas Vincent et Mathieu Schepens (students at the
    // University of Lille) on Nov 4 2014
    // in their analysis, they put CtExpressions in a Map
    // if one expression has an empty signature, an exception is thrown
    // the solution is to improve the signature of null literals
    Factory factory = new Launcher().createFactory();
    CtClass<?> clazz = factory.Code().createCodeSnippetStatement("" + "class X {" + "public Object foo() {" + " return null;" + "}};").compile();
    CtReturn<?> returnEl = clazz.getElements(new TypeFilter<>(CtReturn.class)).get(0);
    CtExpression<?> lit = returnEl.getReturnedExpression();
    assertTrue(lit instanceof CtLiteral);
    assertEquals("null", lit.toString());
    // since the signature is null, CtElement.equals throws an exception and
    // should not
    CtLiteral<?> lit2 = ((CtLiteral<?>) lit).clone();
    HashSet<CtExpression<?>> s = new HashSet<CtExpression<?>>();
    s.add(lit);
    s.add(lit2);
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) CtExpression(spoon.reflect.code.CtExpression) DefaultCoreFactory(spoon.support.DefaultCoreFactory) Factory(spoon.reflect.factory.Factory) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 24 with CtLiteral

use of spoon.reflect.code.CtLiteral 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 25 with CtLiteral

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

the class VisitorPartialEvaluator method visitCtWhile.

public void visitCtWhile(CtWhile whileLoop) {
    CtWhile w = whileLoop.clone();
    w.setLoopingExpression(evaluate(whileLoop.getLoopingExpression()));
    // If lopping Expression always false
    if ((whileLoop.getLoopingExpression() instanceof CtLiteral) && !((CtLiteral<Boolean>) whileLoop.getLoopingExpression()).getValue()) {
        setResult(null);
        return;
    }
    w.setBody(evaluate(whileLoop.getBody()));
    setResult(w);
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) CtWhile(spoon.reflect.code.CtWhile)

Aggregations

CtLiteral (spoon.reflect.code.CtLiteral)38 Test (org.junit.Test)28 CtMethod (spoon.reflect.declaration.CtMethod)19 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)19 AbstractTest (fr.inria.AbstractTest)13 Factory (spoon.reflect.factory.Factory)10 Launcher (spoon.Launcher)8 CtInvocation (spoon.reflect.code.CtInvocation)6 CtStatement (spoon.reflect.code.CtStatement)6 CtTypeReference (spoon.reflect.reference.CtTypeReference)6 CtExpression (spoon.reflect.code.CtExpression)5 List (java.util.List)4 CtLocalVariable (spoon.reflect.code.CtLocalVariable)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 CtIf (spoon.reflect.code.CtIf)3 CtClass (spoon.reflect.declaration.CtClass)3 CtElement (spoon.reflect.declaration.CtElement)3 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)3 AssertionRemover (fr.inria.diversify.dspot.assertGenerator.AssertionRemover)2