|
From: <one...@us...> - 2002-12-30 11:32:53
|
Update of /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/codegen
In directory sc8-pr-cvs1:/tmp/cvs-serv21178
Modified Files:
BasicRenderer.java ClassMapping.java ClassName.java Field.java
Log Message:
integrated Max Andersen's patch to fix subclass constructors
improved generated comments
Index: BasicRenderer.java
===================================================================
RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/codegen/BasicRenderer.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -C2 -d -r1.14 -r1.15
*** BasicRenderer.java 29 Dec 2002 01:30:55 -0000 1.14
--- BasicRenderer.java 30 Dec 2002 11:32:49 -0000 1.15
***************
*** 4,8 ****
--- 4,10 ----
import java.io.PrintWriter;
import java.util.Iterator;
+ import java.util.List;
import java.util.Map;
+ import java.util.TreeSet;
public class BasicRenderer implements Renderer {
***************
*** 17,21 ****
*/
String getTrueTypeName(Field field, Map class2classmap) {
! String name=field.getClassType()!=null?field.getClassType().getFullyQualifiedName():field.getType();
ClassMapping cmap = (ClassMapping) class2classmap.get(name);
--- 19,25 ----
*/
String getTrueTypeName(Field field, Map class2classmap) {
! String name = ( field.getClassType()!=null ) ?
! field.getClassType().getFullyQualifiedName():
! field.getType();
ClassMapping cmap = (ClassMapping) class2classmap.get(name);
***************
*** 32,36 ****
}
else {
! writer.println("//warning: default package");
}
writer.println();
--- 36,40 ----
}
else {
! writer.println("// default package");
}
writer.println();
***************
*** 50,58 ****
// class declaration
! writer.print("public class " + classMapping.getName());
// subclass
if (classMapping.getSuperClass() != null) {
! writer.print(" extends " + classMapping.getSuperClass());
}
--- 54,63 ----
// class declaration
! writer.println("/** @author Hibernate CodeGenerator */");
! writer.print( "public class " + classMapping.getName() );
// subclass
if (classMapping.getSuperClass() != null) {
! writer.print( " extends " + classMapping.getSuperClass() );
}
***************
*** 72,102 ****
// 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")
);
}
- writer.println();
// full constructor
! int fullConsArgCount = 0;
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();
! fullCons = fullCons + ", ";
! fullConsArgCount++;
! }
}
! if ( fullCons.endsWith(", ") ) fullCons = fullCons.substring(0, fullCons.length()-2);
!
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() + ";");
}
}
writer.println(" }");
--- 77,131 ----
// fields
! for ( Iterator fields = classMapping.getFields().iterator(); fields.hasNext(); ) {
Field field = (Field) fields.next();
! writer.println(
! " /** " +
! ( field.isNullable() && !field.isIdentifier() ? "nullable " : "" ) +
! ( field.isIdentifier() ? "identifier" : "persistent" )
! + " field */");
! writer.println(
! " private " +
! shortenType( field.getType(), classMapping.getImports() ) +
! ' ' +
! field.getName() +
! ';'
);
+ writer.println();
}
// full constructor
! List allFieldsForFullConstructor = classMapping.getAllFieldsForFullConstructor();
!
! writer.println(" /** full constructor */");
String fullCons = " public " + classMapping.getName() + "(";
!
!
! for(Iterator fields = allFieldsForFullConstructor.iterator(); fields.hasNext();) {
Field field = (Field) fields.next();
! fullCons = fullCons + shortenType(getTrueTypeName(field, class2classmap), classMapping.getImports()) + " " + field.getName();
! if(fields.hasNext()) {
! fullCons = fullCons + ", ";
! }
}
!
writer.println(fullCons + ") {");
! //invoke super to initialize superclass...
! List supersConstructorFields = classMapping.getFieldsForSupersFullConstructor();
! if (!supersConstructorFields.isEmpty()) {
! writer.print(" super(");
! for (Iterator fields = supersConstructorFields.iterator(); fields.hasNext();) {
! Field field = (Field) fields.next();
! writer.print(field.getName());
! if(fields.hasNext()) {
! writer.print(", ");
! }
}
+ writer.println(");");
+ }
+
+ // initialisation of localfields
+ for(Iterator fields = classMapping.getLocalFieldsForFullConstructor().iterator(); fields.hasNext();) {
+ Field field = (Field) fields.next();
+ writer.println(" this." + field.getName() + " = " + field.getName() + ";");
}
writer.println(" }");
***************
*** 104,108 ****
// no args constructor (if fullconstructor had any arguments!)
! if (fullConsArgCount > 0) {
writer.println(" public " + classMapping.getName() + "() {");
writer.println(" }");
--- 133,138 ----
// no args constructor (if fullconstructor had any arguments!)
! if (allFieldsForFullConstructor.size() > 0) {
! writer.println(" /** default constructor */");
writer.println(" public " + classMapping.getName() + "() {");
writer.println(" }");
***************
*** 110,139 ****
}
! // minimal constructor (only if the fullconstructor had any arguments)
! 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();
! }
}
--- 140,180 ----
}
! // minimal constructor (only if the fullconstructor had any arguments)
! if ((allFieldsForFullConstructor.size() > 0) && classMapping.needsMinimalConstructor()) {
!
! List allFieldsForMinimalConstructor = classMapping.getAllFieldsForMinimalConstructor();
! writer.println(" /** minimal constructor */");
! String minCons = " public " + classMapping.getName() + "(";
! for (Iterator fields = allFieldsForMinimalConstructor.iterator(); fields.hasNext();) {
! Field field = (Field) fields.next();
! minCons = minCons + shortenType(getTrueTypeName(field, class2classmap), classMapping.getImports()) + " " + field.getName();
! if (fields.hasNext()) {
! minCons = minCons + ", ";
! }
! }
!
! writer.println(minCons + ") {");
! // invoke super to initialize superclass...
! List supersMinConstructorFields = classMapping.getFieldsForSupersMinimalConstructor();
! if (!supersMinConstructorFields.isEmpty()) {
! writer.print(" super(");
! for (Iterator fields = supersMinConstructorFields.iterator(); fields.hasNext();) {
! Field field = (Field) fields.next();
! writer.print(field.getName());
! if(fields.hasNext()) {
! writer.print(", ");
! }
! }
! writer.println(");");
! }
!
! // initialisation of localfields
! for (Iterator fields = classMapping.getLocalFieldsForMinimalConstructor().iterator(); fields.hasNext();) {
! Field field = (Field) fields.next();
! writer.println(" this." + field.getName() + " = " + field.getName() + ";");
! }
! writer.println(" }");
! writer.println();
}
***************
*** 192,195 ****
--- 233,258 ----
writer.println("}");
+ }
+ /**
+ * Returns the last part of type if it is in the set of imports.
+ * e.g. java.util.Date would become Date, if imports contains
+ * java.util.Date.
+ *
+ * @param type
+ * @param imports
+ * @return String
+ */
+ private String shortenType(String type, TreeSet imports) {
+ if( imports.contains(type) ) {
+ return type.substring( type.lastIndexOf('.')+1 );
+ }
+ else {
+ if( type.endsWith("[]") ) {
+ return shortenType( type.substring(0, type.length()-2), imports ) + "[]";
+ }
+ else {
+ return type;
+ }
+ }
}
Index: ClassMapping.java
===================================================================
RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/codegen/ClassMapping.java,v
retrieving revision 1.21
retrieving revision 1.22
diff -C2 -d -r1.21 -r1.22
*** ClassMapping.java 29 Dec 2002 01:30:55 -0000 1.21
--- ClassMapping.java 30 Dec 2002 11:32:49 -0000 1.22
***************
*** 21,24 ****
--- 21,25 ----
private ClassName name = null;
private String superClass = null;
+ private ClassMapping superClassMapping = null;
private String proxyClass = null;
private List fields = new ArrayList();
***************
*** 28,31 ****
--- 29,53 ----
private boolean mustImplementEquals = false;
+ public ClassMapping(ClassName superClass, ClassMapping superClassMapping, Element classElement) throws Exception {
+ this(superClass, classElement);
+
+ this.superClassMapping = superClassMapping;
+
+ if(this.superClassMapping!=null) {
+ List l = this.superClassMapping.getAllFieldsForFullConstructor();
+ for (Iterator iter = l.iterator(); iter.hasNext();) {
+ Field element = (Field) iter.next();
+ ClassName ct = element.getClassType();
+ if(ct!=null) { // add imports for superclasses possible fields.
+ addImport(ct);
+ } else {
+ addImport(element.getType());
+ }
+ }
+
+
+ }
+ }
+
public ClassMapping(ClassName superClass, Element classElement) throws Exception {
this(classElement);
***************
*** 87,91 ****
// 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 );
}
--- 109,113 ----
// add an import and field for this property
addImport(classType);
! fields.add( new Field(cmpname, classType, false, true, false) );
components.put( mapping.getCanonicalName(), mapping );
}
***************
*** 178,183 ****
// 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);
}
--- 200,204 ----
// add an import and field for this property
addImport(classType);
! Field f = new Field( name, classType, nullable && !key, key, false );
fields.add(f);
}
***************
*** 192,208 ****
- // subclasses
- for ( Iterator iter = classElement.getChildren("subclass").iterator(); iter.hasNext(); ) {
- Element subclass = (Element) iter.next();
- ClassMapping subclassMapping = new ClassMapping(name, subclass);
- subclasses.add(subclassMapping);
- }
-
- for ( Iterator iter = classElement.getChildren("joined-subclass").iterator(); iter.hasNext(); ) {
- Element subclass = (Element) iter.next();
- ClassMapping subclassMapping = new ClassMapping(name, subclass);
- subclasses.add(subclassMapping);
- }
//components
--- 213,217 ----
***************
*** 222,228 ****
// add an import and field for this property
addImport(classType);
! fields.add( new Field(cmpname, classType.getName(), false) );
components.put( mapping.getCanonicalName(), mapping );
}
}
--- 231,251 ----
// add an import and field for this property
addImport(classType);
! fields.add( new Field(cmpname, classType, false) );
components.put( mapping.getCanonicalName(), mapping );
}
+
+ // subclasses (done last so they can access this superclass for info)
+
+ for ( Iterator iter = classElement.getChildren("subclass").iterator(); iter.hasNext(); ) {
+ Element subclass = (Element) iter.next();
+ ClassMapping subclassMapping = new ClassMapping(name, this,subclass);
+ subclasses.add(subclassMapping);
+ }
+
+ for ( Iterator iter = classElement.getChildren("joined-subclass").iterator(); iter.hasNext(); ) {
+ Element subclass = (Element) iter.next();
+ ClassMapping subclassMapping = new ClassMapping(name, this, subclass);
+ subclasses.add(subclassMapping);
+ }
}
***************
*** 294,304 ****
}
private void addImport(ClassName className) {
// if the package is java.lang or our own package don't add
! if ( !className.inJavaLang() && !className.inSamePackage(name) ) {
! imports.add( className.getFullyQualifiedName() );
}
}
public static Iterator getComponents() {
return components.values().iterator();
--- 317,402 ----
}
+ public List getLocalFieldsForFullConstructor() {
+ List result = new ArrayList();
+ for(Iterator fields = getFields().iterator(); fields.hasNext();) {
+ Field field = (Field) fields.next();
+ if(!field.isIdentifier() || (field.isIdentifier() && !field.isGenerated())) {
+ result.add(field);
+ }
+ }
+
+ return result;
+ }
+
+ public List getFieldsForSupersFullConstructor() {
+ List result = new ArrayList();
+ if(getSuperClassMapping()!=null) {
+ // The correct sequence is vital here, as the subclass should be
+ // able to invoke the fullconstructor based on the sequence returned
+ // by this method!
+ result.addAll(getSuperClassMapping().getFieldsForSupersFullConstructor());
+ result.addAll(getSuperClassMapping().getLocalFieldsForFullConstructor());
+ }
+
+ return result;
+ }
+
+ public List getLocalFieldsForMinimalConstructor() {
+ List result = new ArrayList();
+ for (Iterator fields = getFields().iterator(); fields.hasNext();) {
+ Field field = (Field) fields.next();
+ if ((!field.isIdentifier() && !field.isNullable()) ||
+ (field.isIdentifier() && !field.isGenerated())) {
+ result.add(field);
+ }
+ }
+ return result;
+ }
+ public List getAllFieldsForFullConstructor() {
+
+ List result = getFieldsForSupersFullConstructor();
+ result.addAll(getLocalFieldsForFullConstructor());
+ return result;
+ }
+
+ public List getFieldsForSupersMinimalConstructor() {
+ List result = new ArrayList();
+ if(getSuperClassMapping()!=null) {
+ // The correct sequence is vital here, as the subclass should be
+ // able to invoke the fullconstructor based on the sequence returned
+ // by this method!
+ result.addAll(getSuperClassMapping().getFieldsForSupersMinimalConstructor());
+ result.addAll(getSuperClassMapping().getLocalFieldsForMinimalConstructor());
+ }
+
+ return result;
+ }
+
+ public List getAllFieldsForMinimalConstructor() {
+
+ List result = getFieldsForSupersMinimalConstructor();
+ result.addAll(getLocalFieldsForMinimalConstructor());
+ return result;
+ }
+
private void addImport(ClassName className) {
// if the package is java.lang or our own package don't add
! if ( !className.inJavaLang() && !className.inSamePackage(name) && !className.isPrimitive()) {
! if(className.isArray()) {
! imports.add( className.getFullyQualifiedName().substring(0,className.getFullyQualifiedName().length()-2) ); // remove []
! } else {
! imports.add( className.getFullyQualifiedName() );
! }
}
}
+ private void addImport(String className) {
+ ClassName cn = new ClassName();
+ cn.setFullyQualifiedName(className);
+ addImport(cn);
+
+ }
+
+
public static Iterator getComponents() {
return components.values().iterator();
***************
*** 320,326 ****
// add an import and field for this collection
addImport(interfaceClassName);
! addImport(implementingClassName);
! fields.add(new Field(name, interfaceClassName.getName(), "new " + implementingClassName.getName() + "()", false) );
if (collection.getChildren("composite-element") != null) {
for (Iterator compositeElements = collection.getChildren("composite-element").iterator(); compositeElements.hasNext(); ) {
--- 418,427 ----
// add an import and field for this collection
addImport(interfaceClassName);
! // import implementingClassName should only be
! // added if the initialisaiton code of the field
! // is actually used - and currently it isn't!
! //addImport(implementingClassName);
! fields.add(new Field(name, interfaceClassName, "new " + implementingClassName.getName() + "()", false) );
if (collection.getChildren("composite-element") != null) {
for (Iterator compositeElements = collection.getChildren("composite-element").iterator(); compositeElements.hasNext(); ) {
***************
*** 360,375 ****
if (elementClass==null) elementClass=elt.getAttributeValue("class");
}
! fields.add( new Field( role, getFieldType(elementClass) + "[]", false ) );
}
}
! private String getFieldType(String hibernateType) {
return getFieldType(hibernateType, false);
}
! private String getFieldType(String hibernateType, boolean needObject) {
// deal with hibernate binary type
if ( hibernateType.equals("binary") ) {
! return "byte[]";
}
else {
--- 461,481 ----
if (elementClass==null) elementClass=elt.getAttributeValue("class");
}
! ClassName cn = getFieldType(elementClass);
! cn.setFullyQualifiedName(cn.getFullyQualifiedName() + "[]",cn.isPrimitive());
! cn.setIsArray(true);
! fields.add( new Field( role, cn, false ) );
}
}
! private ClassName getFieldType(String hibernateType) {
return getFieldType(hibernateType, false);
}
! private ClassName getFieldType(String hibernateType, boolean needObject) {
// deal with hibernate binary type
+ ClassName cn = new ClassName();
if ( hibernateType.equals("binary") ) {
! cn.setFullyQualifiedName("byte[]",true);
! return cn;
}
else {
***************
*** 382,389 ****
!needObject
) {
! return ( (PrimitiveType) basicType ).primitiveClass().getName();
}
else {
! return basicType.returnedClass().getName();
}
--- 488,497 ----
!needObject
) {
! cn.setFullyQualifiedName(( (PrimitiveType) basicType ).primitiveClass().getName(),true);
! return cn;
}
else {
! cn.setFullyQualifiedName(basicType.returnedClass().getName());
! return cn;
}
***************
*** 394,403 ****
// add an import and field for this property
addImport(classType);
! return classType.getName();
}
}
}
!
}
--- 502,530 ----
// add an import and field for this property
addImport(classType);
! return classType;
}
}
}
+
! /**
! * Returns the superClassMapping.
! * @return ClassMapping
! */
! public ClassMapping getSuperClassMapping() {
! return superClassMapping;
! }
!
! /**
! * Sets the superClassMapping.
! * @param superClassMapping The superClassMapping to set
! */
! public void setSuperClassMapping(ClassMapping superClassMapping) {
! this.superClassMapping = superClassMapping;
}
+
+ }
+
+
+
Index: ClassName.java
===================================================================
RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/codegen/ClassName.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** ClassName.java 9 Oct 2002 03:52:10 -0000 1.3
--- ClassName.java 30 Dec 2002 11:32:49 -0000 1.4
***************
*** 7,23 ****
private String packageName = null;
private String name = null;
!
public void setFullyQualifiedName(String fullyQualifiedName) {
! this.fullyQualifiedName = fullyQualifiedName;
! int lastDot = fullyQualifiedName.lastIndexOf(".");
! if (lastDot<0 ) {
! name = fullyQualifiedName;
! packageName = null;
! }
! else {
! name = fullyQualifiedName.substring(lastDot + 1);
! packageName = fullyQualifiedName.substring(0, lastDot);
! }
}
public String getFullyQualifiedName() {
--- 7,38 ----
private String packageName = null;
private String name = null;
! private boolean primitive = false;
! private boolean isArray = false;
!
public void setFullyQualifiedName(String fullyQualifiedName) {
! setFullyQualifiedName(fullyQualifiedName,false);
}
+
+ public void setFullyQualifiedName(String fullyQualifiedName, boolean isPrimitive) {
+ this.fullyQualifiedName = fullyQualifiedName;
+ primitive = isPrimitive;
+ if (!isPrimitive) {
+
+ if (fullyQualifiedName != null) {
+
+ int lastDot = fullyQualifiedName.lastIndexOf(".");
+ if (lastDot < 0) {
+ name = fullyQualifiedName;
+ packageName = null;
+ } else {
+ name = fullyQualifiedName.substring(lastDot + 1);
+ packageName = fullyQualifiedName.substring(0, lastDot);
+ }
+ } else {
+ name = fullyQualifiedName;
+ packageName = null;
+ }
+ }
+ }
public String getFullyQualifiedName() {
***************
*** 46,48 ****
--- 61,85 ----
return otherClassName.fullyQualifiedName.equals(fullyQualifiedName);
}
+ /**
+ * Method isPrimitive.
+ * @return boolean
+ */
+ public boolean isPrimitive() {
+ return primitive;
+ }
+ /**
+ * Method setIsArray.
+ * @param b
+ */
+ public void setIsArray(boolean b) {
+ isArray=b;
+ }
+ /**
+ * Method isArray.
+ * @return boolean
+ */
+ public boolean isArray() {
+ return isArray;
+ }
+
}
Index: Field.java
===================================================================
RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/codegen/Field.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** Field.java 29 Dec 2002 01:30:55 -0000 1.6
--- Field.java 30 Dec 2002 11:32:49 -0000 1.7
***************
*** 5,9 ****
public class Field {
private String name = null;
! private String type = null;
private String initialisation = null;
private String asSuffix = null;
--- 5,9 ----
public class Field {
private String name = null;
! // private String type = null;
private String initialisation = null;
private String asSuffix = null;
***************
*** 13,19 ****
private ClassName classType;
! public Field(String name, String type, boolean nullable) {
this.name = name;
! this.type = type;
this.nullable = nullable;
--- 13,19 ----
private ClassName classType;
! public Field(String name, ClassName type, boolean nullable) {
this.name = name;
! setType(type);
this.nullable = nullable;
***************
*** 21,27 ****
}
! public Field(String name, String type, boolean nullable, boolean id, boolean generated) {
this.name = name;
! this.type = type;
this.id = id;
this.nullable = nullable; //?
--- 21,27 ----
}
! public Field(String name, ClassName type, boolean nullable, boolean id, boolean generated) {
this.name = name;
! setType(type);
this.id = id;
this.nullable = nullable; //?
***************
*** 30,34 ****
}
! public Field(String name, String type, String initialisation, boolean nullable) {
this(name, type, nullable);
--- 30,34 ----
}
! public Field(String name, ClassName type, String initialisation, boolean nullable) {
this(name, type, nullable);
***************
*** 49,52 ****
--- 49,53 ----
public String getType() {
+ String type = classType.getFullyQualifiedName();
int loc = type.indexOf("java.lang.");
if ( loc<0 ) {
***************
*** 73,83 ****
return getType() + ":" + getName();
}
! /**
! * Method setClassType.
! * @param classType
! */
! public void setClassType(ClassName classType) {
! this.classType = classType;
! }
/**
* Returns the classType. Can be null as it is not always possible to get
--- 74,78 ----
return getType() + ":" + getName();
}
!
/**
* Returns the classType. Can be null as it is not always possible to get
***************
*** 88,91 ****
--- 83,92 ----
return classType;
}
+
+ private void setType(ClassName type) {
+ this.classType = type;
+ }
+
+
}
|