JavaField field = new JavaField.Static(className, fieldName, tagDCTypeName);
SwitchStatement.SwitchCase sc = new SwitchStatement.IntCaseGroup(dc.getOrdinal(), new ReturnStatement(field));
ordSwitch.addCase(sc);
} else {
// This is a valid ordinal for the data type but does not correspond to a zero arity DC.
LiteralWrapper badValueMessageWrapper = LiteralWrapper.make ("Attempt to treat " + dc.getName() + " as a zero arity data constructor.");
Block block = new Block();
block.addStatement(new JavaStatement.LineComment(dc.getName().getQualifiedName()));
JavaExpression castExpression = new CastExpression(tagDCTypeName, new MethodInvocation.Static(JavaTypeNames.RTVALUE, "badValue", badValueMessageWrapper, JavaTypeName.STRING, JavaTypeNames.RTVALUE));
block.addStatement(new ReturnStatement(castExpression));
ordSwitch.addCase(new SwitchStatement.IntCaseGroup (dc.getOrdinal(), block));
}
}
// Add a default case in the switch to throw an error if an invalid ordinal value is used.
Block defaultBlock = new Block();
LocalVariable bf = new LocalVariable("bf", JavaTypeName.STRING_BUILDER);
defaultBlock.addStatement(new LocalVariableDeclaration (bf, new ClassInstanceCreationExpression(JavaTypeName.STRING_BUILDER)));
LiteralWrapper badValueMessageWrapper1 = LiteralWrapper.make("Invalid ordinal value of ");
JavaExpression message = new MethodInvocation.Instance(bf, "append", badValueMessageWrapper1, JavaTypeName.STRING, JavaTypeName.STRING_BUILDER, MethodInvocation.InvocationType.VIRTUAL);
message = new MethodInvocation.Instance(message, "append", METHODVAR_ORDINAL, JavaTypeName.INT, JavaTypeName.STRING_BUILDER, MethodInvocation.InvocationType.VIRTUAL);
LiteralWrapper badValueMessageWrapper2 = LiteralWrapper.make(" in " + className.toString() + ".getTagDC().");
message = new MethodInvocation.Instance(message, "append", badValueMessageWrapper2, JavaTypeName.STRING, JavaTypeName.STRING_BUILDER, MethodInvocation.InvocationType.VIRTUAL);
defaultBlock.addStatement (new ExpressionStatement(message));
message = new MethodInvocation.Instance(bf, "toString", JavaTypeName.STRING, MethodInvocation.InvocationType.VIRTUAL);
defaultBlock.addStatement (new ReturnStatement(new CastExpression(tagDCTypeName, new MethodInvocation.Static(JavaTypeNames.RTVALUE, "badValue", message, JavaTypeName.STRING, JavaTypeNames.RTVALUE))));
ordSwitch.addCase(new SwitchStatement.DefaultCase (defaultBlock));