// Day next() {
// int ordinal = ordinal().next()
// if (ordinal >= values().size()) ordinal = 0
// return values()[ordinal]
// }
Token assign = Token.newSymbol(Types.ASSIGN, -1, -1);
Token ge = Token.newSymbol(Types.COMPARE_GREATER_THAN_EQUAL, -1, -1);
MethodNode nextMethod = new MethodNode("next", Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, enumClass, new Parameter[0], ClassNode.EMPTY_ARRAY, null);
nextMethod.setSynthetic(true);
BlockStatement code = new BlockStatement();
BlockStatement ifStatement = new BlockStatement();
ifStatement.addStatement(
new ExpressionStatement(
new BinaryExpression(new VariableExpression("ordinal"), assign, new ConstantExpression(Integer.valueOf(0)))
)
);
code.addStatement(
new ExpressionStatement(
new DeclarationExpression(
new VariableExpression("ordinal"),
assign,
new MethodCallExpression(
new MethodCallExpression(
VariableExpression.THIS_EXPRESSION,
"ordinal",
MethodCallExpression.NO_ARGUMENTS),
"next",
MethodCallExpression.NO_ARGUMENTS
)
)
)
);
code.addStatement(
new IfStatement(
new BooleanExpression(new BinaryExpression(
new VariableExpression("ordinal"),
ge,
new MethodCallExpression(
new FieldExpression(values),
"size",
MethodCallExpression.NO_ARGUMENTS
)
)),
ifStatement,
EmptyStatement.INSTANCE
)
);
code.addStatement(
new ReturnStatement(
new MethodCallExpression(new FieldExpression(values), "getAt", new VariableExpression("ordinal"))
)
);
nextMethod.setCode(code);
enumClass.addMethod(nextMethod);
}
if (!hasPrevious) {
// create previous() method, code:
// Day previous() {
// int ordinal = ordinal().previous()
// if (ordinal < 0) ordinal = values().size() - 1
// return values()[ordinal]
// }
Token assign = Token.newSymbol(Types.ASSIGN, -1, -1);
Token lt = Token.newSymbol(Types.COMPARE_LESS_THAN, -1, -1);
MethodNode nextMethod = new MethodNode("previous", Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, enumClass, new Parameter[0], ClassNode.EMPTY_ARRAY, null);
nextMethod.setSynthetic(true);
BlockStatement code = new BlockStatement();
BlockStatement ifStatement = new BlockStatement();
ifStatement.addStatement(