// 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, 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(new Integer(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, 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 MethodCallExpression(
new MethodCallExpression(
new FieldExpression(values),
"size",
MethodCallExpression.NO_ARGUMENTS
),
"minus",
new ConstantExpression(new Integer(1))
)
)
)
);
code.addStatement(
new ExpressionStatement(
new DeclarationExpression(
new VariableExpression("ordinal"),
assign,
new MethodCallExpression(
new MethodCallExpression(
VariableExpression.THIS_EXPRESSION,
"ordinal",
MethodCallExpression.NO_ARGUMENTS),
"previous",
MethodCallExpression.NO_ARGUMENTS
)
)
)
);
code.addStatement(
new IfStatement(
new BooleanExpression(new BinaryExpression(
new VariableExpression("ordinal"),
lt,
new ConstantExpression(new Integer(0))
)),
ifStatement,
EmptyStatement.INSTANCE
)
);
code.addStatement(
new ReturnStatement(
new MethodCallExpression(new FieldExpression(values), "getAt", new VariableExpression("ordinal"))
)
);
nextMethod.setCode(code);
enumClass.addMethod(nextMethod);
}
{
// create valueOf
Parameter stringParameter = new Parameter(ClassHelper.STRING_TYPE,"name");
MethodNode valueOfMethod = new MethodNode("valueOf",PS,enumClass,new Parameter[]{stringParameter},ClassNode.EMPTY_ARRAY,null);
ArgumentListExpression callArguments = new ArgumentListExpression();
callArguments.addExpression(new ClassExpression(enumClass));
callArguments.addExpression(new VariableExpression("name"));
BlockStatement code = new BlockStatement();
code.addStatement(
new ReturnStatement(
new MethodCallExpression(new ClassExpression(ClassHelper.Enum_Type),"valueOf",callArguments)
)
);
valueOfMethod.setCode(code);
valueOfMethod.setSynthetic(true);
enumClass.addMethod(valueOfMethod);
}
addConstructor(enumClass);
{
// constructor helper
// This method is used instead of calling the constructor as
// calling the constructor may require a table with MetaClass
// selecting the constructor for each enum value. So instead we
// use this method to have a central point for constructor selection
// and only one table. The whole construction is needed because
// Reflection forbids access to the enum constructor.
// code:
// def $INIT(Object[] para) {
// return this(*para)
// }
Parameter[] parameter = new Parameter[]{new Parameter(ClassHelper.OBJECT_TYPE.makeArray(), "para")};
MethodNode initMethod = new MethodNode("$INIT",PRIVATE_FS,enumClass,parameter,ClassNode.EMPTY_ARRAY,null);
initMethod.setSynthetic(true);
ConstructorCallExpression cce = new ConstructorCallExpression(
ClassNode.THIS,
new ArgumentListExpression(
new SpreadExpression(new VariableExpression("para"))
)
);
BlockStatement code = new BlockStatement();
code.addStatement(new ReturnStatement(cce));
initMethod.setCode(code);
enumClass.addMethod(initMethod);
}
{
// static init
List fields = enumClass.getFields();
List arrayInit = new ArrayList();
int value = -1;
Token assign = Token.newSymbol(Types.ASSIGN, -1, -1);
List block = new ArrayList();
FieldNode tempMin = null;
FieldNode tempMax = null;
for (Iterator iterator = fields.iterator(); iterator.hasNext();) {
FieldNode field = (FieldNode) iterator.next();