String toStringCode = getToStringCode(superOidClass != null);
String equalsCode = getEqualsCode(superOidClass != null);
String hashCodeCode = getHashCodeCode(superOidClass != null);
// build the java code
CodeFormat code = newCodeFormat();
if (!isInnerClass() && packageDec.length() > 0)
code.append(packageDec).afterSection();
if (!isInnerClass() && imports.length() > 0)
code.append(imports).afterSection();
code.append("/**").endl().
append(" * ").
append(_loc.get("appid-comment-for", _type.getName())).
endl().
append(" *").endl().
append(" * ").append(_loc.get("appid-comment-gen")).endl().
append(" * ").append(getClass().getName()).endl().
append(" */").endl();
code.append("public ");
if (isInnerClass())
code.append("static ");
code.append("class ").append(className);
if (code.getBraceOnSameLine())
code.append(" ");
else
code.endl().tab();
if (superOidClass != null) {
code.append("extends " + Strings.getClassName(superOidClass));
if (code.getBraceOnSameLine())
code.append(" ");
else
code.endl().tab();
}
code.append("implements Serializable").openBrace(1).endl();
// if we use a byte array we need a static array for encoding to string
if (bytes) {
code.tab().append("private static final char[] HEX = ").
append("new char[] {").endl();
code.tab(2).append("'0', '1', '2', '3', '4', '5', '6', '7',").
endl();
code.tab(2).append("'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'").
endl();
code.tab().append("};").endl(2);
}
// static block to register class
code.tab().append("static").openBrace(2).endl();
code.tab(2).append("// register persistent class in JVM").endl();
code.tab(2).append("try { Class.forName").openParen(true).
append("\"").append(_type.getName()).append("\"").
closeParen().append(";").append(" }").endl();
code.tab(2).append("catch").openParen(true).
append("Exception e").closeParen().append(" {}").endl();
code.closeBrace(2);
// field declarations
if (fieldDecs.length() > 0)
code.endl(2).append(fieldDecs);
// default constructor
code.afterSection().tab().append("public ").append(className).
parens().openBrace(2).endl();
code.closeBrace(2);
// string constructor
code.afterSection().append(constructor);
// properties
if (properties.length() > 0)
code.afterSection().append(properties);
// toString, equals, hashCode methods
if (toStringCode.length() > 0)
code.afterSection().append(toStringCode);
if (hashCodeCode.length() > 0)
code.afterSection().append(hashCodeCode);
if (equalsCode.length() > 0)
code.afterSection().append(equalsCode);
if (fromStringCode.length() > 0)
code.afterSection().append(fromStringCode);
// if we have any byte array fields, we have to add the extra
// methods for handling byte arrays
if (bytes) {
code.afterSection().append(getToBytesByteArrayCode());
code.afterSection().append(getToStringByteArrayCode());
code.afterSection().append(getEqualsByteArrayCode());
code.afterSection().append(getHashCodeByteArrayCode());
}
// base classes might need to define a custom tokenizer
if (superOidClass == null && getTokenizer(false) == TOKENIZER_CUSTOM)
code.afterSection().append(getCustomTokenizerClass());
code.endl();
code.closeBrace(1);
_code = code.toString();
// if this is an inner class, then indent the entire
// code unit one tab level
if (isInnerClass()) {
// indent the entire code block one level to make it
// a propertly indented innder class
_code = code.getTab() + Strings.replace(_code,
J2DoPrivHelper.getLineSeparator(),
J2DoPrivHelper.getLineSeparator() + code.getTab());
}
return true;
}