private void addConstructor(ClassNode enumClass) {
// first look if there are declared constructors
List ctors = new ArrayList(enumClass.getDeclaredConstructors());
if (ctors.size()==0) {
// add default constructor
ConstructorNode init = new ConstructorNode(Opcodes.ACC_PUBLIC,new Parameter[0],ClassNode.EMPTY_ARRAY,new BlockStatement());
enumClass.addConstructor(init);
ctors.add(init);
}
// for each constructor:
// if constructor does not define a call to super, then transform constructor
// to get String,int parameters at beginning and add call super(String,int)
for (Iterator iterator = ctors.iterator(); iterator.hasNext();) {
boolean chainedThisConstructorCall = false;
ConstructorNode ctor = (ConstructorNode) iterator.next();
ConstructorCallExpression cce = null;
if (ctor.firstStatementIsSpecialConstructorCall()) {
Statement code = ctor.getFirstStatement();
cce = (ConstructorCallExpression) ((ExpressionStatement) code).getExpression();
if(cce.isSuperCall()) {
continue;
} else {
chainedThisConstructorCall = true;
}
}
// we need to add parameters
Parameter[] oldP = ctor.getParameters();
Parameter[] newP = new Parameter[oldP.length+2];
String stringParameterName = getUniqueVariableName("__str",ctor.getCode());
newP[0] = new Parameter(ClassHelper.STRING_TYPE,stringParameterName);
String intParameterName = getUniqueVariableName("__int",ctor.getCode());
newP[1] = new Parameter(ClassHelper.int_TYPE,intParameterName);
System.arraycopy(oldP, 0, newP, 2, oldP.length);
ctor.setParameters(newP);
if(chainedThisConstructorCall) {
TupleExpression args = (TupleExpression) cce.getArguments();
List<Expression> argsExprs = args.getExpressions();
argsExprs.add(0, new VariableExpression(stringParameterName));
argsExprs.add(1, new VariableExpression(intParameterName));
} else {
// and a super call
cce = new ConstructorCallExpression(
ClassNode.SUPER,
new ArgumentListExpression(
new VariableExpression(stringParameterName),
new VariableExpression(intParameterName)
)
);
BlockStatement code = new BlockStatement();
code.addStatement(new ExpressionStatement(cce));
Statement oldCode = ctor.getCode();
if (oldCode!=null) code.addStatement(oldCode);
ctor.setCode(code);
}
}
}