use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class VisitorPartialEvaluator method visitCtInvocation.
public <T> void visitCtInvocation(CtInvocation<T> invocation) {
CtInvocation<T> i = invocation.getFactory().Core().createInvocation();
i.setExecutable(invocation.getExecutable());
i.setTypeCasts(invocation.getTypeCasts());
boolean constant = true;
i.setTarget(evaluate(invocation.getTarget()));
if ((i.getTarget() != null) && !(i.getTarget() instanceof CtLiteral)) {
constant = false;
}
for (CtExpression<?> e : invocation.getArguments()) {
CtExpression<?> re = evaluate(e);
if (!(re instanceof CtLiteral)) {
constant = false;
}
i.addArgument(re);
}
// do not partially evaluate super(...)
if (i.getExecutable().getSimpleName().equals(CtExecutableReference.CONSTRUCTOR_NAME)) {
setResult(i);
return;
}
if (constant) {
CtExecutable<?> executable = invocation.getExecutable().getDeclaration();
CtType<?> aType = invocation.getParent(CtType.class);
CtTypeReference<?> execDeclaringType = invocation.getExecutable().getDeclaringType();
// (including to superclasses)
if (executable != null && aType != null && invocation.getType() != null && execDeclaringType != null && execDeclaringType.isSubtypeOf(aType.getReference())) {
CtBlock<?> b = evaluate(executable.getBody());
flowEnded = false;
CtStatement last = b.getStatements().get(b.getStatements().size() - 1);
if ((last != null) && (last instanceof CtReturn)) {
if (((CtReturn<?>) last).getReturnedExpression() instanceof CtLiteral) {
setResult(((CtReturn<?>) last).getReturnedExpression());
return;
}
}
} else {
// try to completely evaluate
T r = null;
try {
// System.err.println("invocking "+i);
r = RtHelper.invoke(i);
if (isLiteralType(r)) {
CtLiteral<T> l = invocation.getFactory().Core().createLiteral();
l.setValue(r);
setResult(l);
return;
}
} catch (Exception e) {
}
}
}
setResult(i);
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class EqualsChecker method scanCtStatement.
@Override
public void scanCtStatement(CtStatement s) {
final CtStatement peek = (CtStatement) this.other;
final String leftLabel = s.getLabel();
final String rightLabel = peek.getLabel();
if (leftLabel == null && rightLabel == null) {
super.scanCtStatement(s);
return;
}
if (leftLabel == null || !leftLabel.equals(rightLabel)) {
isNotEqual = true;
return;
}
super.scanCtStatement(s);
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class CtStatementListImpl method insertBegin.
@Override
public <T extends CtStatementList> T insertBegin(CtStatementList statements) {
ensureModifiableStatementsList();
for (CtStatement statement : statements.getStatements()) {
statement.setParent(this);
this.addStatement(0, statement);
}
if (isImplicit() && this.statements.size() > 1) {
setImplicit(false);
}
return (T) this;
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class CtStatementListImpl method setStatements.
@Override
public <T extends CtStatementList> T setStatements(List<CtStatement> stmts) {
if (stmts == null || stmts.isEmpty()) {
this.statements = CtElementImpl.emptyList();
return (T) this;
}
getFactory().getEnvironment().getModelChangeListener().onListDeleteAll(this, STATEMENT, this.statements, new ArrayList<>(this.statements));
this.statements.clear();
for (CtStatement stmt : stmts) {
addStatement(stmt);
}
return (T) this;
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class AnnotationTest method testAnnotationIntrospection.
@Test
public void testAnnotationIntrospection() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationIntrospection.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
CtClass<Object> aClass = factory.Class().get(AnnotationIntrospection.class);
CtMethod<?> mMethod = aClass.getMethod("m");
CtStatement statement = mMethod.getBody().getStatement(1);
assertEquals("annotation.equals(null)", statement.toString());
}
Aggregations