use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy-core by groovy.
the class BindableASTTransformation method wrapSetterMethod.
/*
* Wrap an existing setter.
*/
private void wrapSetterMethod(ClassNode classNode, String propertyName) {
String getterName = "get" + MetaClassHelper.capitalize(propertyName);
MethodNode setter = classNode.getSetterMethod("set" + MetaClassHelper.capitalize(propertyName));
if (setter != null) {
// Get the existing code block
Statement code = setter.getCode();
Expression oldValue = varX("$oldValue");
Expression newValue = varX("$newValue");
BlockStatement block = new BlockStatement();
// create a local variable to hold the old value from the getter
block.addStatement(declS(oldValue, callThisX(getterName)));
// call the existing block, which will presumably set the value properly
block.addStatement(code);
// get the new value to emit in the event
block.addStatement(declS(newValue, callThisX(getterName)));
// add the firePropertyChange method call
block.addStatement(stmt(callThisX("firePropertyChange", args(constX(propertyName), oldValue, newValue))));
// replace the existing code block with our new one
setter.setCode(block);
}
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy-core by groovy.
the class InitializerStrategy method createBuilderMethod.
private static MethodNode createBuilderMethod(String buildMethodName, ClassNode builder, int numFields, String builderMethodName) {
final BlockStatement body = new BlockStatement();
body.addStatement(returnS(callX(builder, buildMethodName)));
ClassNode returnType = makeClassSafeWithGenerics(builder, unsetGenTypes(numFields));
return new MethodNode(builderMethodName, PUBLIC_STATIC, returnType, NO_PARAMS, NO_EXCEPTIONS, body);
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy-core by groovy.
the class ExternalStrategy method createBuildMethod.
private static MethodNode createBuildMethod(BuilderASTTransformation transform, AnnotationNode anno, ClassNode sourceClass, List<PropertyInfo> fields) {
String buildMethodName = transform.getMemberStringValue(anno, "buildMethodName", "build");
final BlockStatement body = new BlockStatement();
Expression sourceClassInstance = initializeInstance(sourceClass, fields, body);
body.addStatement(returnS(sourceClassInstance));
return new MethodNode(buildMethodName, ACC_PUBLIC, sourceClass, NO_PARAMS, NO_EXCEPTIONS, body);
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy-core by groovy.
the class ClassNode method positionStmtsAfterEnumInitStmts.
public void positionStmtsAfterEnumInitStmts(List<Statement> staticFieldStatements) {
MethodNode method = getOrAddStaticConstructorNode();
Statement statement = method.getCode();
if (statement instanceof BlockStatement) {
BlockStatement block = (BlockStatement) statement;
// add given statements for explicitly declared static fields just after enum-special fields
// are found - the $VALUES binary expression marks the end of such fields.
List<Statement> blockStatements = block.getStatements();
ListIterator<Statement> litr = blockStatements.listIterator();
while (litr.hasNext()) {
Statement stmt = litr.next();
if (stmt instanceof ExpressionStatement && ((ExpressionStatement) stmt).getExpression() instanceof BinaryExpression) {
BinaryExpression bExp = (BinaryExpression) ((ExpressionStatement) stmt).getExpression();
if (bExp.getLeftExpression() instanceof FieldExpression) {
FieldExpression fExp = (FieldExpression) bExp.getLeftExpression();
if (fExp.getFieldName().equals("$VALUES")) {
for (Statement tmpStmt : staticFieldStatements) {
litr.add(tmpStmt);
}
}
}
}
}
}
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy-core by groovy.
the class ClassNode method addStaticInitializerStatements.
public void addStaticInitializerStatements(List<Statement> staticStatements, boolean fieldInit) {
MethodNode method = getOrAddStaticConstructorNode();
BlockStatement block = null;
Statement statement = method.getCode();
if (statement == null) {
block = new BlockStatement();
} else if (statement instanceof BlockStatement) {
block = (BlockStatement) statement;
} else {
block = new BlockStatement();
block.addStatement(statement);
}
// before the other statements
if (!fieldInit) {
block.addStatements(staticStatements);
} else {
List<Statement> blockStatements = block.getStatements();
staticStatements.addAll(blockStatements);
blockStatements.clear();
blockStatements.addAll(staticStatements);
}
}
Aggregations