// Write the copyright statement.
GenerateUtilities.writeCopyright (out);
// Write the fields.
for (Iterator i = identityFields.iterator (); i.hasNext ();) {
FieldInfo field = (FieldInfo) i.next ();
String fieldName = field.getName ();
GenerateUtilities.writeJavaDocComment (out, " ", field.getComment ());
out.println (" private " + field.getTypeName ()
+ " " + fieldName + ";");
}
// Write the initialisation of the base class.
List baseConstructors = identityBaseClassInfo.getConstructors();
System.err.println("base constructors = " + baseConstructors);
// Based on two constructors in the absolute base class of all assets
if (baseConstructors.size () != 2) {
throw new IllegalStateException ("Identity base class has wrong number"
+ " of constructors\n" +
"identityBaseClassInfo = " + identityBaseClassInfo.getName());
}
// Iterate over all constructors and replicate them in this class
for (Iterator i = baseConstructors.iterator(); i.hasNext(); /* No ++ */) {
ConstructorInfo baseConstructor = (ConstructorInfo)i.next();
// Write the constructor comment.
GenerateUtilities.openJavaDocComment(out, " ");
GenerateUtilities.addJavaDocComment(out, " ", "Create a new <code>"
+ identityClassName + "</code>.");
List constructorParameters = baseConstructor.getParameters();
for (Iterator innerI = constructorParameters.iterator();
innerI.hasNext(); /* No ++ */) {
ParameterInfo param = (ParameterInfo)innerI.next();
String paramName = param.getName();
comment = "@param " + paramName + " " +
identityBaseClassInfo.getField(paramName, true).getComment();
GenerateUtilities.addJavaDocComment(out, " ", comment);
}
GenerateUtilities.closeJavaDocComment(out, " ");
// Write the constructor method declaration.
String startLine = " public " + identityClassName + " (";
out.print(startLine);
separator = null;
for (Iterator innerI = constructorParameters.iterator();
innerI.hasNext(); /* No ++ */) {
ParameterInfo param = (ParameterInfo)innerI.next();
// Output a parameter declaration.
if (separator == null) {
separator = ",\n" +
GenerateUtilities.getIndent(startLine.length());
} else {
out.print(separator);
}
out.print (param.getTypeName() + " " + param.getName());
}
if (identityFields.size () != 0) {
for (Iterator fieldsIterator = identityFields.iterator();
fieldsIterator.hasNext(); /**/) {
FieldInfo field = (FieldInfo)fieldsIterator.next();
if (separator == null){
separator = ",\n" +
GenerateUtilities.getIndent(startLine.length());
} else {
out.print(separator);
}
out.print (field.getTypeName() + " " + field.getName());
}
}
out.println(") {");
List parameters = baseConstructor.getParameters();
ParameterInfo firstParam = (ParameterInfo) parameters.get(0);
// If the Project is the first parameter, call the superclass
// constructor. If it isn't, call the superclass constructor with
// null for the project.
if (!GenerateUtilities.isProjectField(firstParam)) {
out.print(" super(null, ");
} else {
// Write the call to the superclass
out.print(" super(");
}
separator = "";
for (Iterator innerI = parameters.iterator();
innerI.hasNext(); /* No ++ */) {
ParameterInfo parameter = (ParameterInfo)innerI.next();
out.print(separator);
out.print(parameter.getName());
separator = ", ";
}
out.println (");");
// Write the initialisation of the fields.
if (identityFields.size () != 0) {
out.println ();
for (Iterator innerI = identityFields.iterator ();
innerI.hasNext ();) {
FieldInfo field = (FieldInfo) innerI.next ();
String fieldName = field.getName ();
/* Uncomment this to validate arguments.
if (typeName.equals ("String")) {
out.println (" if (" + fieldName + " == null) {");
out.println (" throw new IllegalArgumentException (\""
+ fieldName + " is null\");");
out.println (" }");
}
*/
out.println (" this." + fieldName + " = " + fieldName + ";");
}
}
out.println (" }");
}
// Write out the getter methods if any.
for (Iterator i = identityFields.iterator (); i.hasNext ();) {
FieldInfo field = (FieldInfo) i.next ();
String fieldName = field.getName ();
MethodInfo method = objectClassInfo.getGetterMethod (fieldName, true);
// Make sure that the method has no extra parameters.
List parameters = method.getParameters ();
if (parameters.size () != 0) {
throw new IllegalStateException ("Getter method has wrong number"
+ " of parameters");
}
// Write the getter method comment.
GenerateUtilities.writeJavaDocComment (out, " ", method.getComment ());
// Write the getter method declaration.
out.println (" public " + method.getReturnTypeName () + " "
+ method.getName () + " () {");
// Write the method body.
out.println (" return " + fieldName + ";");
// Write the method close.
out.println (" }");
}
// Write the getObjectClass method.
out.println ();
out.println (" // Javadoc inherited from super class.");
out.println (" public Class getObjectClass () {");
out.println (" return " + objectClassName + ".class;");
out.println (" }");
if (identityFields.size () != 0) {
// Write the equals method.
out.println ();
out.println (" // Javadoc inherited from super class.");
out.println (" public boolean equals (Object object) {");
out.println ();
GenerateUtilities.formatParagraph (out, " // ",
"Call the super class to check"
+ " whether this object and the other"
+ " object are of the same type and"
+ " have the same name.");
out.println (" if (!super.equals (object)) {");
out.println (" return false;");
out.println (" }");
out.println ();
out.println (" " + identityClassName
+ " identity = (" + identityClassName + ") object;");
out.print (" return ");
separator = "";
for (Iterator i = identityFields.iterator (); i.hasNext ();) {
FieldInfo field = (FieldInfo) i.next ();
String fieldName = field.getName ();
String typeName = field.getTypeName ();
out.print (separator);
if (typeName.equals ("String")) {
out.print ("equals (" + fieldName + ", identity." + fieldName + ")");
} else {
out.print (fieldName + " == identity." + fieldName);
}
separator = " && ";
if (i.hasNext ()) {
out.println ();
} else {
out.println (";");
}
}
out.println (" }");
// Write a hashCode method.
out.println ();
out.println (" // Javadoc inherited from super class.");
out.println (" public int hashCode () {");
out.println (" return super.hashCode ()");
for (Iterator i = identityFields.iterator (); i.hasNext ();) {
FieldInfo field = (FieldInfo) i.next ();
String fieldName = field.getName ();
String typeName = field.getTypeName ();
out.print (" + ");
if (typeName.equals ("boolean")) {
out.println ("(" + fieldName + " ? 1 : 0)");
} else if (typeName.equals ("int")) {
out.println (fieldName);
} else {
out.println ("hashCode (" + fieldName + ")");
}
if (i.hasNext ()) {
out.println ();
} else {
out.println (";");
}
}
out.println (" }");
// Write the compareTo method.
out.println ();
out.println (" // Javadoc inherited from super class.");
out.println (" public int compareTo (Object object) {");
out.println ();
GenerateUtilities.formatParagraph (out, " // ",
"Call the super class to check"
+ " whether this object and the other"
+ " object are of the same type and"
+ " have the same name.");
out.println (" int result;");
out.println (" if ((result = super.compareTo (object)) != 0) {");
out.println (" return result;");
out.println (" }");
out.println ();
out.println (" " + identityClassName
+ " identity = (" + identityClassName + ") object;");
for (Iterator i = identityFields.iterator (); i.hasNext ();) {
FieldInfo field = (FieldInfo) i.next ();
String fieldName = field.getName ();
out.println ();
out.println (" // Compare the " + fieldName);
out.println (" if ((result = compare (" + fieldName
+ ", identity." + fieldName + ")) != 0) {");
out.println (" return result;");
out.println (" }");
}
out.println ();
out.println (" // The identities are equal.");
out.println (" return 0;");
out.println (" }");
// Write a paramString method.
out.println ();
out.println (" // Javadoc inherited from super class.");
out.println (" protected String paramString () {");
out.println (" return super.paramString ()");
for (Iterator i = identityFields.iterator (); i.hasNext ();) {
FieldInfo field = (FieldInfo) i.next ();
String fieldName = field.getName ();
out.print (" + \", " + fieldName + "=\" + " + fieldName);
if (i.hasNext ()) {
out.println ();
} else {