|
From: <one...@us...> - 2002-12-29 01:30:59
|
Update of /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/codegen
In directory sc8-pr-cvs1:/tmp/cvs-serv29617/cirrus/hibernate/tools/codegen
Modified Files:
Generator.java Field.java ClassMapping.java BasicRenderer.java
Log Message:
* CodeGenerator Improvements:
- fixed some problems with composite-ids
- composite-id classes now implement equals(), hashCCode()
- all generated classes implement toString()
* Use ObjectUtil.equals() in a bunch of places
Index: Generator.java
===================================================================
RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/codegen/Generator.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** Generator.java 21 Oct 2002 03:45:04 -0000 1.8
--- Generator.java 29 Dec 2002 01:30:55 -0000 1.9
***************
*** 59,67 ****
// set lowerFirstLetter
value = generateElement.getAttributeValue("lowerFirstLetter");
! if ((value == null) || (value.equals("false"))) {
this.lowerFirstLetter = false;
! } else if (value.equals("true")) {
this.lowerFirstLetter = true;
! } else {
throw new Exception("invalid value for element lowerFirstLeter. should be true or false.");
}
--- 59,69 ----
// set lowerFirstLetter
value = generateElement.getAttributeValue("lowerFirstLetter");
! if ( (value == null) || ( value.equals("false") ) ) {
this.lowerFirstLetter = false;
! }
! else if ( value.equals("true") ) {
this.lowerFirstLetter = true;
! }
! else {
throw new Exception("invalid value for element lowerFirstLeter. should be true or false.");
}
Index: Field.java
===================================================================
RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/codegen/Field.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** Field.java 21 Oct 2002 03:45:04 -0000 1.5
--- Field.java 29 Dec 2002 01:30:55 -0000 1.6
***************
*** 21,28 ****
}
! public Field(String name, String type, boolean id, boolean generated) {
this.name = name;
this.type = type;
this.id = id;
this.generated = generated;
this.asSuffix = name.substring(0, 1).toUpperCase() + name.substring(1);
--- 21,29 ----
}
! public Field(String name, String type, boolean nullable, boolean id, boolean generated) {
this.name = name;
this.type = type;
this.id = id;
+ this.nullable = nullable; //?
this.generated = generated;
this.asSuffix = name.substring(0, 1).toUpperCase() + name.substring(1);
Index: ClassMapping.java
===================================================================
RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/codegen/ClassMapping.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -C2 -d -r1.20 -r1.21
*** ClassMapping.java 28 Dec 2002 02:25:48 -0000 1.20
--- ClassMapping.java 29 Dec 2002 01:30:55 -0000 1.21
***************
*** 26,30 ****
private List subclasses = new ArrayList();
private static final Map components = new HashMap();
!
public ClassMapping(ClassName superClass, Element classElement) throws Exception {
--- 26,30 ----
private List subclasses = new ArrayList();
private static final Map components = new HashMap();
! private boolean mustImplementEquals = false;
public ClassMapping(ClassName superClass, Element classElement) throws Exception {
***************
*** 53,56 ****
--- 53,62 ----
propertyList.addAll( classElement.getChildren("version") );
propertyList.addAll( classElement.getChildren("timestamp") );
+ propertyList.addAll( classElement.getChildren("key-property") );
+
+ // get all many-to-one associations defined for the class
+ List manyToOneList = new ArrayList();
+ manyToOneList.addAll( classElement.getChildren("many-to-one") );
+ manyToOneList.addAll( classElement.getChildren("key-many-to-one") );
Attribute att = classElement.getAttribute("proxy");
***************
*** 68,90 ****
String cmpclass = cmpid.getAttributeValue("class");
if ( cmpclass==null || cmpclass.equals("") ) {
! propertyList.addAll( cmpid.getChildren("key-property") );
! for ( Iterator it = cmpid.getChildren("key-many-to-one").iterator(); it.hasNext(); ) {
! Element manyToOne = (Element) it.next();
!
! ClassName classType = new ClassName();
! classType.setFullyQualifiedName(manyToOne.getAttributeValue("class"));
! // add an import and field for this property
! addImport(classType);
! fields.add(new Field(manyToOne.getAttributeValue("name"), classType.getName(), false));
! }
}
else {
ClassMapping mapping = new ClassMapping(cmpid, true);
!
ClassName classType = new ClassName();
classType.setFullyQualifiedName(cmpclass);
// add an import and field for this property
addImport(classType);
! fields.add(new Field(cmpname, classType.getName(), false));
components.put( mapping.getCanonicalName(), mapping );
}
--- 74,91 ----
String cmpclass = cmpid.getAttributeValue("class");
if ( cmpclass==null || cmpclass.equals("") ) {
! //Embedded composite id
! implementEquals();
! propertyList.addAll(0, cmpid.getChildren("key-property") );
! manyToOneList.addAll(0, cmpid.getChildren("key-many-to-one") );
}
else {
+ //Composite id class
ClassMapping mapping = new ClassMapping(cmpid, true);
! mapping.implementEquals();
ClassName classType = new ClassName();
classType.setFullyQualifiedName(cmpclass);
// add an import and field for this property
addImport(classType);
! fields.add( new Field(cmpname, classType.getName(), false, true, false) );
components.put( mapping.getCanonicalName(), mapping );
}
***************
*** 113,125 ****
// ids may be generated and may need to be of object type in order to support
// the unsaved-value "null" value.
! // Properties may be nullable (whilst ids can not)
! if(property == id) {
Element generator = property.getChild("generator");
String unsavedValue = property.getAttributeValue("unsaved-value");
! boolean needObject = (unsavedValue != null && unsavedValue.equals("null"));
boolean generated = !generator.getAttributeValue("class").equals("assigned");
! Field idField = new Field(name, getFieldType(type, needObject), true, generated);
fields.add(idField);
! } else {
String notnull = property.getAttributeValue("not-null");
// if not-null property is missing lets see if it has been
--- 114,127 ----
// ids may be generated and may need to be of object type in order to support
// the unsaved-value "null" value.
! // Properties may be nullable (ids may not)
! if (property == id) {
Element generator = property.getChild("generator");
String unsavedValue = property.getAttributeValue("unsaved-value");
! boolean needObject = ( unsavedValue != null && unsavedValue.equals("null") );
boolean generated = !generator.getAttributeValue("class").equals("assigned");
! Field idField = new Field( name, getFieldType(type, needObject), false, true, generated );
fields.add(idField);
! }
! else {
String notnull = property.getAttributeValue("not-null");
// if not-null property is missing lets see if it has been
***************
*** 130,135 ****
notnull = column.getAttributeValue("not-null");
}
! boolean nullable = (notnull == null || notnull.equals("false"));
! fields.add(new Field(name, getFieldType(type), nullable));
}
}
--- 132,138 ----
notnull = column.getAttributeValue("not-null");
}
! boolean nullable = ( notnull == null || notnull.equals("false") );
! boolean key = property.getName().startsWith("key-"); //a composite id property
! fields.add( new Field(name, getFieldType(type), nullable && !key, key, false) );
}
}
***************
*** 137,141 ****
// one to ones
List onetooneList = classElement.getChildren("one-to-one");
! for (Iterator onetoones = onetooneList.iterator(); onetoones.hasNext();) {
Element onetoone = (Element) onetoones.next();
--- 140,144 ----
// one to ones
List onetooneList = classElement.getChildren("one-to-one");
! for ( Iterator onetoones = onetooneList.iterator(); onetoones.hasNext(); ) {
Element onetoone = (Element) onetoones.next();
***************
*** 144,158 ****
// ensure that the class is specified
String clazz = onetoone.getAttributeValue("class");
! if(StringUtils.isEmpty(clazz)) {
System.out.println("one-to-one \"" + name + "\" in class " + getName() + " is missing a class attribute");
continue;
}
! fields.add(new Field(name, getFieldType(clazz), true));
}
// many to ones - TODO: consolidate with code above
! for (Iterator manytoOnes = classElement.getChildren("many-to-one").iterator(); manytoOnes.hasNext();) {
Element manyToOne = (Element) manytoOnes.next();
--- 147,161 ----
// ensure that the class is specified
String clazz = onetoone.getAttributeValue("class");
! if( StringUtils.isEmpty(clazz) ) {
System.out.println("one-to-one \"" + name + "\" in class " + getName() + " is missing a class attribute");
continue;
}
! fields.add( new Field(name, getFieldType(clazz), true) );
}
// many to ones - TODO: consolidate with code above
! for ( Iterator manytoOnes = manyToOneList.iterator(); manytoOnes.hasNext(); ) {
Element manyToOne = (Element) manytoOnes.next();
***************
*** 170,178 ****
// is it nullable?
String notnull = manyToOne.getAttributeValue("not-null");
! boolean nullable = (notnull == null || notnull.equals("false"));
!
// add an import and field for this property
addImport(classType);
! Field f = new Field( name, classType.getName(), nullable );
f.setClassType(classType);
fields.add(f);
--- 173,182 ----
// is it nullable?
String notnull = manyToOne.getAttributeValue("not-null");
! boolean nullable = ( notnull == null || notnull.equals("false") );
! boolean key = manyToOne.getName().startsWith("key-"); //a composite id property
!
// add an import and field for this property
addImport(classType);
! Field f = new Field( name, classType.getName(), nullable && !key, key, false );
f.setClassType(classType);
fields.add(f);
***************
*** 218,226 ****
// add an import and field for this property
addImport(classType);
! fields.add(new Field(cmpname, classType.getName(), false));
components.put( mapping.getCanonicalName(), mapping );
}
}
public List getFields() {
return fields;
--- 222,238 ----
// add an import and field for this property
addImport(classType);
! fields.add( new Field(cmpname, classType.getName(), false) );
components.put( mapping.getCanonicalName(), mapping );
}
}
+ public void implementEquals() {
+ mustImplementEquals = true;
+ }
+
+ public boolean mustImplementEquals() {
+ return mustImplementEquals;
+ }
+
public List getFields() {
return fields;
***************
*** 264,280 ****
boolean missingId = true;
int countNull = 0;
! for(Iterator it = fields.iterator(); it.hasNext(); ) {
Field f = (Field) it.next();
! if(f.isIdentifier()) {
generatedId = f.isGenerated();
missingId = false;
}
else
! if(f.isNullable()) countNull++;
}
! return !(countNull == 0 ||
! ((countNull == (fields.size() - 1)) && generatedId) ||
! ((countNull == (fields.size()) && missingId)));
}
--- 276,294 ----
boolean missingId = true;
int countNull = 0;
! for( Iterator it = fields.iterator(); it.hasNext(); ) {
Field f = (Field) it.next();
! if( f.isIdentifier() ) {
generatedId = f.isGenerated();
missingId = false;
}
else
! if( f.isNullable() ) countNull++;
}
! return !(
! countNull == 0 ||
! ( ( countNull == fields.size()-1 ) && generatedId ) ||
! (countNull == fields.size() && missingId)
! );
}
***************
*** 330,334 ****
private void doArrays(Element classElement, String type) {
! for (Iterator arrays = classElement.getChildren(type).iterator(); arrays.hasNext();) {
Element array = (Element) arrays.next();
String role = array.getAttributeValue("role");
--- 344,348 ----
private void doArrays(Element classElement, String type) {
! for ( Iterator arrays = classElement.getChildren(type).iterator(); arrays.hasNext(); ) {
Element array = (Element) arrays.next();
String role = array.getAttributeValue("role");
***************
*** 365,369 ****
if (
(basicType instanceof PrimitiveType) &&
! !hibernateType.trim().equals( basicType.returnedClass().getName()) &&
!needObject
) {
--- 379,383 ----
if (
(basicType instanceof PrimitiveType) &&
! !hibernateType.trim().equals( basicType.returnedClass().getName() ) &&
!needObject
) {
Index: BasicRenderer.java
===================================================================
RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/codegen/BasicRenderer.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -C2 -d -r1.13 -r1.14
*** BasicRenderer.java 8 Nov 2002 23:49:47 -0000 1.13
--- BasicRenderer.java 29 Dec 2002 01:30:55 -0000 1.14
***************
*** 38,46 ****
// imports
classMapping.getImports().add("java.io.Serializable");
! String item = null;
! for (Iterator imports = classMapping.getImports().iterator(); imports.hasNext();) {
! item = (String) imports.next();
! writer.println("import " + item + ";");
}
writer.println();
--- 38,49 ----
// imports
classMapping.getImports().add("java.io.Serializable");
! classMapping.getImports().add("org.apache.commons.lang.builder.ToStringBuilder");
! if ( classMapping.mustImplementEquals() ) {
! classMapping.getImports().add("org.apache.commons.lang.builder.EqualsBuilder");
! classMapping.getImports().add("org.apache.commons.lang.builder.HashCodeBuilder");
! }
! for ( Iterator imports = classMapping.getImports().iterator(); imports.hasNext(); ) {
! writer.println("import " + imports.next() + ";");
}
writer.println();
***************
*** 66,75 ****
}
writer.println(" {");
// fields
- Field field = null;
-
for (Iterator fields = classMapping.getFields().iterator(); fields.hasNext();) {
! field = (Field) fields.next();
writer.println(" private " + field.getType() + ' ' + field.getName() +
( field.isIdentifier() ? "; //identifier" : "; //persistent")
--- 69,77 ----
}
writer.println(" {");
+ writer.println();
// fields
for (Iterator fields = classMapping.getFields().iterator(); fields.hasNext();) {
! Field field = (Field) fields.next();
writer.println(" private " + field.getType() + ' ' + field.getName() +
( field.isIdentifier() ? "; //identifier" : "; //persistent")
***************
*** 82,86 ****
String fullCons = " public " + classMapping.getName() + "(";
for(Iterator fields = classMapping.getFields().iterator(); fields.hasNext();) {
! field = (Field) fields.next();
if(!field.isIdentifier() || (field.isIdentifier() && !field.isGenerated())) {
fullCons = fullCons + field.getType() + " " + field.getName();
--- 84,88 ----
String fullCons = " public " + classMapping.getName() + "(";
for(Iterator fields = classMapping.getFields().iterator(); fields.hasNext();) {
! Field field = (Field) fields.next();
if(!field.isIdentifier() || (field.isIdentifier() && !field.isGenerated())) {
fullCons = fullCons + field.getType() + " " + field.getName();
***************
*** 93,97 ****
writer.println(fullCons + ") {");
for(Iterator fields = classMapping.getFields().iterator(); fields.hasNext();) {
! field = (Field) fields.next();
if(!field.isIdentifier() || (field.isIdentifier() && !field.isGenerated())) {
writer.println(" this." + field.getName() + " = " + field.getName() + ";");
--- 95,99 ----
writer.println(fullCons + ") {");
for(Iterator fields = classMapping.getFields().iterator(); fields.hasNext();) {
! Field field = (Field) fields.next();
if(!field.isIdentifier() || (field.isIdentifier() && !field.isGenerated())) {
writer.println(" this." + field.getName() + " = " + field.getName() + ";");
***************
*** 111,144 ****
if(fullConsArgCount>0) {
! if(classMapping.needsMinimalConstructor()) {
! String minCons = " public " + classMapping.getName() + "(";
! for(Iterator fields = classMapping.getFields().iterator(); fields.hasNext();) {
! field = (Field) fields.next();
! if((!field.isIdentifier() && !field.isNullable()) ||
! (field.isIdentifier() && !field.isGenerated())) {
! minCons = minCons + field.getType() + " " + field.getName();
! minCons = minCons + ", ";
! }
! }
! if ( minCons.endsWith(", ") ) minCons = minCons.substring(0, minCons.length()-2);
!
! writer.println(minCons + ") {");
! for(Iterator fields = classMapping.getFields().iterator(); fields.hasNext();) {
! field = (Field) fields.next();
! if((!field.isIdentifier() && !field.isNullable()) ||
! (field.isIdentifier() && !field.isGenerated())) {
! writer.println(" this." + field.getName() + " = " + field.getName() + ";");
! }
! }
! writer.println(" }");
! writer.println();
! }
}
// field accessors
for (Iterator fields = classMapping.getFields().iterator(); fields.hasNext();) {
! field = (Field) fields.next();
! String getterType = (field.getType().toLowerCase().equals("boolean")) ? " is" : " get";
// getter
--- 113,146 ----
if(fullConsArgCount>0) {
! if( classMapping.needsMinimalConstructor() ) {
! String minCons = " public " + classMapping.getName() + "(";
! for(Iterator fields = classMapping.getFields().iterator(); fields.hasNext();) {
! Field field = (Field) fields.next();
! if((!field.isIdentifier() && !field.isNullable()) ||
! (field.isIdentifier() && !field.isGenerated())) {
! minCons = minCons + field.getType() + " " + field.getName();
! minCons = minCons + ", ";
! }
! }
! if ( minCons.endsWith(", ") ) minCons = minCons.substring(0, minCons.length()-2);
!
! writer.println(minCons + ") {");
! for(Iterator fields = classMapping.getFields().iterator(); fields.hasNext();) {
! Field field = (Field) fields.next();
! if((!field.isIdentifier() && !field.isNullable()) ||
! (field.isIdentifier() && !field.isGenerated())) {
! writer.println(" this." + field.getName() + " = " + field.getName() + ";");
! }
! }
! writer.println(" }");
! writer.println();
! }
}
// field accessors
for (Iterator fields = classMapping.getFields().iterator(); fields.hasNext();) {
! Field field = (Field) fields.next();
! String getterType = ( field.getType().toLowerCase().equals("boolean") ) ? " is" : " get";
// getter
***************
*** 153,157 ****
writer.println(" }");
}
!
writer.println("}");
}
--- 155,194 ----
writer.println(" }");
}
!
! writer.println();
! writer.println(" public String toString() {");
! //easier to use reflectionToString() than worry about superclasses
! writer.println(" return ToStringBuilder.reflectionToString(this);");
! writer.println(" }");
! writer.println();
!
! if ( classMapping.mustImplementEquals() ) {
! writer.println(" public boolean equals(Object other) {");
! writer.println(" if ( !(other instanceof " + className + ") ) return false;");
! writer.println(" " + className + " castOther = (" + className + ") other;");
! writer.println(" return new EqualsBuilder()");
! for (Iterator fields = classMapping.getFields().iterator(); fields.hasNext();) {
! Field field = (Field) fields.next();
! if ( field.isIdentifier() ) {
! writer.println(" .append(this." + field.getName() + ", castOther." + field.getName() + ")");
! }
! }
! writer.println(" .isEquals();");
! writer.println(" }");
! writer.println();
!
! writer.println(" public int hashCode() {");
! writer.println(" return new HashCodeBuilder()");
! for (Iterator fields = classMapping.getFields().iterator(); fields.hasNext();) {
! Field field = (Field) fields.next();
! if ( field.isIdentifier() ) {
! writer.println(" .append(" + field.getName() + ")");
! }
! }
! writer.println(" .toHashCode();");
! writer.println(" }");
! writer.println();
! }
!
writer.println("}");
}
|