use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class ParentExiter method visitCtCase.
@Override
public <E> void visitCtCase(CtCase<E> caseStatement) {
final ASTNode node = jdtTreeBuilder.getContextBuilder().stack.peek().node;
if (node instanceof CaseStatement && ((CaseStatement) node).constantExpression != null && caseStatement.getCaseExpression() == null && child instanceof CtExpression) {
caseStatement.setCaseExpression((CtExpression<E>) child);
return;
} else if (child instanceof CtStatement) {
caseStatement.addStatement((CtStatement) child);
return;
}
super.visitCtCase(caseStatement);
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class SnippetCompilationHelper method createWrapperContent.
private static String createWrapperContent(final CtElement element, final Factory f, final CtTypeReference returnType) {
CtClass<?> w = f.Class().create(WRAPPER_CLASS_NAME);
CtBlock body = f.Core().createBlock();
if (element instanceof CtStatement) {
body.addStatement((CtStatement) element);
} else if (element instanceof CtExpression) {
CtReturn ret = f.Core().createReturn();
ret.setReturnedExpression((CtExpression) element);
body.addStatement(ret);
}
Set<ModifierKind> modifiers = EnumSet.of(ModifierKind.STATIC);
Set<CtTypeReference<? extends Throwable>> thrownTypes = new HashSet<>();
thrownTypes.add(f.Class().<Throwable>get(Throwable.class).getReference());
f.Method().create(w, modifiers, returnType, WRAPPER_METHOD_NAME, CtElementImpl.<CtParameter<?>>emptyList(), thrownTypes, body);
String contents = w.toString();
// Clean up (delete wrapper from factory) after it is printed. The DefaultJavaPrettyPrinter needs w in model to be able to print it correctly
w.getPackage().removeType(w);
return contents;
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class SnippetCompilationHelper method internalCompileStatement.
private static CtStatement internalCompileStatement(CtElement st, CtTypeReference returnType) {
Factory f = st.getFactory();
String contents = createWrapperContent(st, f, returnType);
build(f, contents);
CtType<?> c = f.Type().get(WRAPPER_CLASS_NAME);
// Get the part we want
CtMethod<?> wrapper = c.getMethod(WRAPPER_METHOD_NAME);
List<CtStatement> statements = wrapper.getBody().getStatements();
CtStatement ret = statements.get(statements.size() - 1);
// Clean up
c.getPackage().removeType(c);
if (ret instanceof CtClass) {
CtClass klass = (CtClass) ret;
ret.getFactory().Package().getRootPackage().addType(klass);
klass.setSimpleName(klass.getSimpleName().replaceAll("^[0-9]*", ""));
}
return ret;
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class ContextBuilder method enter.
@SuppressWarnings("unchecked")
void enter(CtElement e, ASTNode node) {
stack.push(new ASTPair(e, node));
if (!(e instanceof CtPackage) || (compilationUnitSpoon.getFile() != null && compilationUnitSpoon.getFile().getName().equals(DefaultJavaPrettyPrinter.JAVA_PACKAGE_DECLARATION))) {
if (compilationunitdeclaration != null && !e.isImplicit()) {
e.setPosition(this.jdtTreeBuilder.getPositionBuilder().buildPositionCtElement(e, node));
}
}
ASTPair pair = stack.peek();
CtElement current = pair.element;
if (current instanceof CtExpression) {
while (!casts.isEmpty()) {
((CtExpression<?>) current).addTypeCast(casts.remove(0));
}
}
if (current instanceof CtStatement && !this.label.isEmpty()) {
((CtStatement) current).setLabel(this.label.pop());
}
try {
if (e instanceof CtTypedElement && !(e instanceof CtConstructorCall) && !(e instanceof CtCatchVariable) && node instanceof Expression) {
if (((CtTypedElement<?>) e).getType() == null) {
((CtTypedElement<Object>) e).setType(this.jdtTreeBuilder.getReferencesBuilder().getTypeReference(((Expression) node).resolvedType));
}
}
} catch (UnsupportedOperationException ignore) {
// For some element, we throw an UnsupportedOperationException when we call setType().
}
}
use of spoon.reflect.code.CtStatement in project spoon by INRIA.
the class CtBlockImpl method setStatements.
@Override
public <T extends CtStatementList> T setStatements(List<CtStatement> statements) {
if (statements == null || statements.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 s : statements) {
addStatement(s);
}
return (T) this;
}
Aggregations