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 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 ToStringASTTransformation method appendValue.
private static void appendValue(BlockStatement body, Expression result, VariableExpression first, Expression value, String name, boolean includeNames, boolean ignoreNulls) {
final BlockStatement thenBlock = new BlockStatement();
final Statement appendValue = ignoreNulls ? ifS(notNullX(value), thenBlock) : thenBlock;
appendCommaIfNotFirst(thenBlock, result, first);
appendPrefix(thenBlock, result, name, includeNames);
thenBlock.addStatement(ifElseS(sameX(value, new VariableExpression("this")), appendS(result, constX("(this)")), appendS(result, callX(INVOKER_TYPE, "toString", value))));
body.addStatement(appendValue);
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy-core by groovy.
the class ToStringASTTransformation method toStringPropertyName.
private static Statement toStringPropertyName(Expression result, String fName) {
final BlockStatement body = new BlockStatement();
body.addStatement(appendS(result, constX(fName + ":")));
return body;
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy-core by groovy.
the class TupleConstructorASTTransformation method createConstructor.
public static void createConstructor(ClassNode cNode, boolean includeFields, boolean includeProperties, boolean includeSuperFields, boolean includeSuperProperties, boolean callSuper, boolean force, List<String> excludes, List<String> includes, boolean useSetters) {
// no processing if existing constructors found
List<ConstructorNode> constructors = cNode.getDeclaredConstructors();
if (constructors.size() > 1 && !force)
return;
boolean foundEmpty = constructors.size() == 1 && constructors.get(0).getFirstStatement() == null;
if (constructors.size() == 1 && !foundEmpty && !force)
return;
// HACK: JavaStubGenerator could have snuck in a constructor we don't want
if (foundEmpty)
constructors.remove(0);
List<FieldNode> superList = new ArrayList<FieldNode>();
if (includeSuperProperties) {
superList.addAll(getSuperPropertyFields(cNode.getSuperClass()));
}
if (includeSuperFields) {
superList.addAll(getSuperNonPropertyFields(cNode.getSuperClass()));
}
List<FieldNode> list = new ArrayList<FieldNode>();
if (includeProperties) {
list.addAll(getInstancePropertyFields(cNode));
}
if (includeFields) {
list.addAll(getInstanceNonPropertyFields(cNode));
}
final List<Parameter> params = new ArrayList<Parameter>();
final List<Expression> superParams = new ArrayList<Expression>();
final BlockStatement body = new BlockStatement();
for (FieldNode fNode : superList) {
String name = fNode.getName();
if (shouldSkip(name, excludes, includes))
continue;
params.add(createParam(fNode, name));
boolean hasSetter = cNode.getProperty(name) != null && !fNode.isFinal();
if (callSuper) {
superParams.add(varX(name));
} else {
if (useSetters && hasSetter) {
body.addStatement(stmt(callThisX(getSetterName(name), varX(name))));
} else {
body.addStatement(assignS(propX(varX("this"), name), varX(name)));
}
}
}
if (callSuper) {
body.addStatement(stmt(ctorX(ClassNode.SUPER, args(superParams))));
}
for (FieldNode fNode : list) {
String name = fNode.getName();
if (shouldSkip(name, excludes, includes))
continue;
Parameter nextParam = createParam(fNode, name);
params.add(nextParam);
boolean hasSetter = cNode.getProperty(name) != null && !fNode.isFinal();
if (useSetters && hasSetter) {
body.addStatement(stmt(callThisX(getSetterName(name), varX(nextParam))));
} else {
body.addStatement(assignS(propX(varX("this"), name), varX(nextParam)));
}
}
cNode.addConstructor(new ConstructorNode(ACC_PUBLIC, params.toArray(new Parameter[params.size()]), ClassNode.EMPTY_ARRAY, body));
// or if there is only one Map property (for backwards compatibility)
if (params.size() > 0) {
ClassNode firstParam = params.get(0).getType();
if (params.size() > 1 || firstParam.equals(ClassHelper.OBJECT_TYPE)) {
if (firstParam.equals(ClassHelper.MAP_TYPE)) {
addMapConstructors(cNode, true, "The class " + cNode.getName() + " was incorrectly initialized via the map constructor with null.");
} else {
ClassNode candidate = HMAP_TYPE;
while (candidate != null) {
if (candidate.equals(firstParam)) {
addMapConstructors(cNode, true, "The class " + cNode.getName() + " was incorrectly initialized via the map constructor with null.");
break;
}
candidate = candidate.getSuperClass();
}
}
}
}
}
Aggregations