use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class ImmutableASTTransformation method createConstructorStatement.
private Statement createConstructorStatement(ClassNode cNode, PropertyNode pNode, List<String> knownImmutableClasses, List<String> knownImmutables) {
FieldNode fNode = pNode.getField();
final ClassNode fieldType = fNode.getType();
Statement statement = null;
if (fieldType.isArray() || isOrImplements(fieldType, CLONEABLE_TYPE)) {
statement = createConstructorStatementArrayOrCloneable(fNode);
} else if (isKnownImmutableClass(fieldType, knownImmutableClasses) || isKnownImmutable(pNode.getName(), knownImmutables)) {
statement = createConstructorStatementDefault(fNode);
} else if (fieldType.isDerivedFrom(DATE_TYPE)) {
statement = createConstructorStatementDate(fNode);
} else if (isOrImplements(fieldType, COLLECTION_TYPE) || fieldType.isDerivedFrom(COLLECTION_TYPE) || isOrImplements(fieldType, MAP_TYPE) || fieldType.isDerivedFrom(MAP_TYPE)) {
statement = createConstructorStatementCollection(fNode);
} else if (fieldType.isResolved()) {
addError(createErrorMessage(cNode.getName(), fNode.getName(), fieldType.getName(), "compiling"), fNode);
statement = EmptyStatement.INSTANCE;
} else {
statement = createConstructorStatementGuarded(cNode, fNode);
}
return statement;
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class AutoNewLineTransformer method visitClosureExpression.
@Override
public void visitClosureExpression(final ClosureExpression expression) {
super.visitClosureExpression(expression);
if (inBuilderMethod) {
Statement oldCode = expression.getCode();
BlockStatement block = oldCode instanceof BlockStatement ? ((BlockStatement) oldCode) : new BlockStatement(Arrays.asList(oldCode), new VariableScope());
List<Statement> statements = block.getStatements();
if (!statements.isEmpty()) {
Statement first = statements.get(0);
Statement last = statements.get(statements.size() - 1);
if (expression.getLineNumber() < first.getLineNumber()) {
// there's a new line between { -> ... and the first statement
statements.add(0, createNewLine(expression));
}
if (expression.getLastLineNumber() > last.getLastLineNumber()) {
// there's a new line between { -> ... and the first statement
statements.add(createNewLine(expression));
}
}
expression.setCode(block);
}
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class NotYetImplementedASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
throw new RuntimeException("Internal error: expecting [AnnotationNode, AnnotatedNode] but got: " + Arrays.asList(nodes));
}
AnnotationNode annotationNode = (AnnotationNode) nodes[0];
ASTNode node = nodes[1];
if (!(node instanceof MethodNode)) {
addError("@NotYetImplemented must only be applied on test methods!", node);
return;
}
MethodNode methodNode = (MethodNode) node;
ArrayList<Statement> statements = new ArrayList<Statement>();
Statement statement = methodNode.getCode();
if (statement instanceof BlockStatement) {
statements.addAll(((BlockStatement) statement).getStatements());
}
if (statements.isEmpty())
return;
BlockStatement rewrittenMethodCode = new BlockStatement();
rewrittenMethodCode.addStatement(tryCatchAssertionFailedError(annotationNode, methodNode, statements));
rewrittenMethodCode.addStatement(throwAssertionFailedError(annotationNode));
methodNode.setCode(rewrittenMethodCode);
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class StatementWriter method visitExpressionOrStatement.
private void visitExpressionOrStatement(Object o) {
if (o == EmptyExpression.INSTANCE)
return;
if (o instanceof Expression) {
Expression expr = (Expression) o;
int mark = controller.getOperandStack().getStackLength();
expr.visit(controller.getAcg());
controller.getOperandStack().popDownTo(mark);
} else {
((Statement) o).visit(controller.getAcg());
}
}
use of org.codehaus.groovy.ast.stmt.Statement in project groovy by apache.
the class Verifier method addInitialization.
protected void addInitialization(ClassNode node, ConstructorNode constructorNode) {
Statement firstStatement = constructorNode.getFirstStatement();
// if some transformation decided to generate constructor then it probably knows who it does
if (firstStatement instanceof BytecodeSequence)
return;
ConstructorCallExpression first = getFirstIfSpecialConstructorCall(firstStatement);
// in case of this(...) let the other constructor do the init
if (first != null && (first.isThisCall()))
return;
List<Statement> statements = new ArrayList<Statement>();
List<Statement> staticStatements = new ArrayList<Statement>();
final boolean isEnum = node.isEnum();
List<Statement> initStmtsAfterEnumValuesInit = new ArrayList<Statement>();
Set<String> explicitStaticPropsInEnum = new HashSet<String>();
if (isEnum) {
for (PropertyNode propNode : node.getProperties()) {
if (!propNode.isSynthetic() && propNode.getField().isStatic()) {
explicitStaticPropsInEnum.add(propNode.getField().getName());
}
}
for (FieldNode fieldNode : node.getFields()) {
if (!fieldNode.isSynthetic() && fieldNode.isStatic() && fieldNode.getType() != node) {
explicitStaticPropsInEnum.add(fieldNode.getName());
}
}
}
if (!Traits.isTrait(node)) {
for (FieldNode fn : node.getFields()) {
addFieldInitialization(statements, staticStatements, fn, isEnum, initStmtsAfterEnumValuesInit, explicitStaticPropsInEnum);
}
}
statements.addAll(node.getObjectInitializerStatements());
Statement code = constructorNode.getCode();
BlockStatement block = new BlockStatement();
List<Statement> otherStatements = block.getStatements();
if (code instanceof BlockStatement) {
block = (BlockStatement) code;
otherStatements = block.getStatements();
} else if (code != null) {
otherStatements.add(code);
}
if (!otherStatements.isEmpty()) {
if (first != null) {
// it is super(..) since this(..) is already covered
otherStatements.remove(0);
statements.add(0, firstStatement);
}
Statement stmtThis$0 = getImplicitThis$0StmtIfInnerClass(otherStatements);
if (stmtThis$0 != null) {
// since there can be field init statements that depend on method/property dispatching
// that uses this$0, it needs to bubble up before the super call itself (GROOVY-4471)
statements.add(0, stmtThis$0);
}
statements.addAll(otherStatements);
}
BlockStatement newBlock = new BlockStatement(statements, block.getVariableScope());
newBlock.setSourcePosition(block);
constructorNode.setCode(newBlock);
if (!staticStatements.isEmpty()) {
if (isEnum) {
/*
* GROOVY-3161: initialize statements for explicitly declared static fields
* inside an enum should come after enum values are initialized
*/
staticStatements.removeAll(initStmtsAfterEnumValuesInit);
node.addStaticInitializerStatements(staticStatements, true);
if (!initStmtsAfterEnumValuesInit.isEmpty()) {
node.positionStmtsAfterEnumInitStmts(initStmtsAfterEnumValuesInit);
}
} else {
node.addStaticInitializerStatements(staticStatements, true);
}
}
}
Aggregations