public static BlockStatement block(Statement... stmts) { BlockStatement block = new BlockStatement(); for (Statement stmt : stmts) block.addStatement(stmt); return block; }
public static BlockStatement block(VariableScope varScope, List<Statement> stmts) { BlockStatement block = new BlockStatement(); block.setVariableScope(varScope); for (Statement stmt : stmts) block.addStatement(stmt); return block; }
private BlockStatement getBlockStatement(MethodNode targetMethod, Statement code) { BlockStatement toBlock; if (code instanceof BlockStatement) { toBlock = (BlockStatement) code; } else { toBlock = new BlockStatement(); toBlock.addStatement(code); targetMethod.setCode(toBlock); } return toBlock; }
public static BlockStatement block(VariableScope varScope, Statement... stmts) { BlockStatement block = new BlockStatement(); block.setVariableScope(varScope); for (Statement stmt : stmts) block.addStatement(stmt); return block; }
private static Statement toStringPropertyName(Expression result, String fName) { final BlockStatement body = new BlockStatement(); body.addStatement(appendS(result, constX(fName + ":"))); return body; }
private BlockStatement siblingsToBlockStatement(AST firstSiblingNode) { BlockStatement block = new BlockStatement(); for (AST node = firstSiblingNode; node != null; node = node.getNextSibling()) { block.addStatement(statement(node)); } return block; }
private static void appendCommaIfNotFirst(BlockStatement body, Expression result, VariableExpression first) { // if ($toStringFirst) $toStringFirst = false else result.append(", ") body.addStatement(ifElseS( first, assignS(first, ConstantExpression.FALSE), appendS(result, constX(", ")))); }
@Override public boolean validateProperties(AbstractASTTransformation xform, BlockStatement body, ClassNode cNode, List<PropertyNode> props) { if (xform instanceof MapConstructorASTTransformation) { body.addStatement(ifS(equalsNullX(varX("args")), assignS(varX("args"), new MapExpression()))); body.addStatement(stmt(callX(SELF_TYPE, "checkPropNames", args("this", "args")))); } return super.validateProperties(xform, body, cNode, props); }
@Override public boolean validateProperties(AbstractASTTransformation xform, BlockStatement body, ClassNode cNode, List<PropertyNode> props) { if (xform instanceof MapConstructorASTTransformation) { body.addStatement(ifS(equalsNullX(varX("args")), assignS(varX("args"), new MapExpression()))); body.addStatement(stmt(callX(IMMUTABLE_XFORM_TYPE, "checkPropNames", args("this", "args")))); } return super.validateProperties(xform, body, cNode, props); }
private static MethodNode createBuilderMethod(AnnotationNode anno, ClassNode builder) { String builderMethodName = getMemberStringValue(anno, "builderMethodName", "builder"); final BlockStatement body = new BlockStatement(); body.addStatement(returnS(ctorX(builder))); return new MethodNode(builderMethodName, PUBLIC_STATIC, builder, NO_PARAMS, NO_EXCEPTIONS, body); }
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); }
private static Expression initializeInstance(ClassNode sourceClass, List<PropertyInfo> props, BlockStatement body) { Expression instance = varX("_the" + sourceClass.getNameWithoutPackage(), sourceClass); body.addStatement(declS(instance, ctorX(sourceClass))); for (PropertyInfo prop : props) { body.addStatement(stmt(assignX(propX(instance, prop.getName()), varX(prop.getName().equals("class") ? "clazz" : prop.getName(), newClass(prop.getType()))))); } return instance; }
private static Expression initializeInstance(ClassNode buildee, List<PropertyInfo> props, BlockStatement body) { Expression instance = varX("_the" + buildee.getNameWithoutPackage(), buildee); body.addStatement(declS(instance, ctorX(buildee))); for (PropertyInfo pi : props) { body.addStatement(stmt(assignX(propX(instance, pi.getName()), varX(pi.getName(), pi.getType())))); } return instance; } }
private static MethodNode createBuildMethod(AnnotationNode anno, ClassNode buildee, List<PropertyInfo> props) { String buildMethodName = getMemberStringValue(anno, "buildMethodName", "build"); final BlockStatement body = new BlockStatement(); body.addStatement(returnS(initializeInstance(buildee, props, body))); return new MethodNode(buildMethodName, ACC_PUBLIC, newClass(buildee), NO_PARAMS, NO_EXCEPTIONS, body); }
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); }
private static void createBuilderConstructors(ClassNode builder, ClassNode buildee, List<FieldNode> fields) { builder.addConstructor(ACC_PRIVATE, NO_PARAMS, NO_EXCEPTIONS, block(ctorSuperS())); final BlockStatement body = new BlockStatement(); body.addStatement(ctorSuperS()); initializeFields(fields, body, false); builder.addConstructor(ACC_PRIVATE, getParams(fields, buildee), NO_EXCEPTIONS, body); }
private static MethodNode createBuildMethodForMethod(AnnotationNode anno, ClassNode buildee, MethodNode mNode, Parameter[] params) { String buildMethodName = getMemberStringValue(anno, "buildMethodName", "build"); final BlockStatement body = new BlockStatement(); ClassNode returnType; if (mNode instanceof ConstructorNode) { returnType = newClass(buildee); body.addStatement(returnS(ctorX(newClass(mNode.getDeclaringClass()), args(params)))); } else { body.addStatement(returnS(callX(newClass(mNode.getDeclaringClass()), mNode.getName(), args(params)))); returnType = newClass(mNode.getReturnType()); } return new MethodNode(buildMethodName, ACC_PUBLIC, returnType, NO_PARAMS, NO_EXCEPTIONS, body); }
private static void createWriteExternal(ClassNode cNode, List<String> excludes, List<FieldNode> list) { final BlockStatement body = new BlockStatement(); Parameter out = param(OBJECTOUTPUT_TYPE, "out"); for (FieldNode fNode : list) { if (excludes != null && excludes.contains(fNode.getName())) continue; if ((fNode.getModifiers() & ACC_TRANSIENT) != 0) continue; MethodCallExpression writeObject = callX(varX(out), "write" + suffixForField(fNode), varX(fNode)); writeObject.setImplicitThis(false); body.addStatement(stmt(writeObject)); } ClassNode[] exceptions = {make(IOException.class)}; addGeneratedMethod(cNode, "writeExternal", ACC_PUBLIC, ClassHelper.VOID_TYPE, params(out), exceptions, body); }
private static void addGetter(FieldNode fNode, ClassNode componentType) { ClassNode cNode = fNode.getDeclaringClass(); BlockStatement body = new BlockStatement(); Parameter[] params = new Parameter[1]; params[0] = new Parameter(ClassHelper.int_TYPE, "index"); body.addStatement(stmt(indexX(varX(fNode), varX(params[0])))); addGeneratedMethod(cNode, makeName(fNode, "get"), getModifiers(fNode), componentType, params, null, body); }
private static void initializeFields(List<FieldNode> fields, BlockStatement body, boolean useSetters) { for (FieldNode field : fields) { String name = field.getName(); body.addStatement( stmt(useSetters && !field.isFinal() ? callThisX(getSetterName(name), varX(param(field.getType(), name))) : assignX(propX(varX("this"), field.getName()), varX(param(field.getType(), name))) ) ); } }