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()));
}
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());
}
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);
}
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());
}
}
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);
}
Aggregations