JavaTypeName superClassTypeName = JavaTypeName.OBJECT;
// No interfaces are implemented
JavaTypeName[] interfaces = IO_Source_Generator.EMPTY_TYPE_NAME_ARRAY;
JavaClassRep javaClassRep =
new JavaClassRep(
typeConstructorClassName,
superClassTypeName,
classModifiers,
interfaces);
// Add a comment for the top of the source file.
MultiLineComment mlc = new MultiLineComment("<!--");
mlc.addLine(" ");
mlc.addLine("**************************************************************");
mlc.addLine("This Java source has been automatically generated.");
mlc.addLine("MODIFICATIONS TO THIS SOURCE MAY BE OVERWRITTEN - DO NOT MODIFY THIS FILE");
mlc.addLine("**************************************************************");
mlc.addLine(" ");
mlc.addLine(" ");
mlc.addLine("This file (" + javaClassRep.getClassName().getUnqualifiedJavaSourceName() + ".java)");
mlc.addLine("was generated from CAL type constructor: " + typeConstructorInfo.typeConstructor.getName() + ".");
mlc.addLine(" ");
mlc.addLine("Creation date: " + new Date());
mlc.addLine("--!>");
mlc.addLine(" ");
javaClassRep.setComment(mlc);
// We want to insert the CALDoc comment for the type as a JavaDoc comment for the class.
JavaDocComment jdc =
new JavaDocComment ("This class (" + javaClassRep.getClassName().getUnqualifiedJavaSourceName() + ") provides a Java data class corresponding to");
jdc.addLine("the CAL type constructor " + typeConstructorInfo.typeConstructor.getName() + ".");
jdc.addLine("");
if (typeConstructorInfo.isEnumerationType) {
jdc.addLine("This type constructor is an enumeration. (i.e. all data constructors have no fields)");
jdc.addLine("The individual data constructors are represented by instances of " + javaClassRep.getClassName().getUnqualifiedJavaSourceName() + " held ");
jdc.addLine("in static final fields.");
} else {
jdc.addLine("Because the type constructor has only one data constructor, with the same name");
jdc.addLine("as the type constructor this class also represents instances of the data constructor.");
}
javaClassRep.setJavaDoc(jdc);
// Generate fields in this class for any fields which are common to all data constructors.
createFields(javaClassRep, typeConstructorInfo);
if (typeConstructorInfo.isEnumerationType) {
// Add instance fields for name and ordinal
// Add static final fields for each data constructor
createFields_Enumeration(typeConstructorInfo.typeConstructor, javaClassRep, typeConstructorClassName);
// Add a function which takes an int value for the
// ordinal and returns the corresponding enum instance.
javaClassRep.addMethod(createMethod_fromOrdinal (typeConstructorClassName));
}
if (typeConstructorInfo.commonFieldNames.size() > 0) {
javaClassRep.addConstructor(createConstructor(typeConstructorInfo, typeConstructorClassName));
} else {
javaClassRep.addConstructor(createConstructor_default(typeConstructorClassName, typeConstructorInfo.isEnumerationType));
}
// Create a get_FieldName method for each unique field name in the set of
// data constructors.
// If the field is common to all DCs it will be implemented at this
// level. Otherwise the implementation at this level throws an error
// and the DC classes are expected to override it.
for (FieldName fieldName : typeConstructorInfo.commonFieldNames) {
String javaFieldName = (String)typeConstructorInfo.fieldJavaNames.get(fieldName);
Set<JavaTypeName> fieldTypes = typeConstructorInfo.allFieldTypeNames.get(fieldName);
for (JavaTypeName type : fieldTypes) {
String accessorMethodName = (String)typeConstructorInfo.fieldJavaAccessorMethodNames.get(fieldName);
JavaMethod getter = createMethod_getField (
accessorMethodName,
javaFieldName,
type,
true);
javaClassRep.addMethod(getter);
getter.setJavaDocComment(new JavaDocComment ("@return " + fieldName));
}
}
javaClassRep.addMethod(createMethod_getDCName(typeConstructorInfo));
javaClassRep.addMethod(createMethod_getDCOrdinal(typeConstructorInfo));
JavaMethod toString = createMethod_toString();
if (toString != null) {
javaClassRep.addMethod (toString);
}
if (typeConstructorInfo.isEnumerationType) {
JavaMethod equals = JavaDataClassGenerator.this.createMethod_equals(
typeConstructorClassName,
typeConstructorInfo.commonFieldNames,
typeConstructorInfo.commonFieldTypes);
javaClassRep.addMethod (equals);
}
createMethod_hashCode (javaClassRep);
return javaClassRep;