From: <max...@us...> - 2003-02-16 12:49:05
|
Update of /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/tool/hbm2java In directory sc8-pr-cvs1:/tmp/cvs-serv15397/src/net/sf/hibernate/tool/hbm2java Modified Files: ClassMapping.java BasicRenderer.java ClassName.java Log Message: Applied patch:685440 hbm2java generate Beans style property events by Klaus Zimmermann with small bugfix changes. Index: ClassMapping.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/tool/hbm2java/ClassMapping.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** ClassMapping.java 2 Feb 2003 17:34:09 -0000 1.11 --- ClassMapping.java 16 Feb 2003 12:49:02 -0000 1.12 *************** *** 581,584 **** --- 581,585 ---- if ( hibernateType.equals("binary") ) { cn.setFullyQualifiedName("byte[]",true); + cn.setIsArray(true); return cn; } Index: BasicRenderer.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/tool/hbm2java/BasicRenderer.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** BasicRenderer.java 2 Feb 2003 17:34:09 -0000 1.9 --- BasicRenderer.java 16 Feb 2003 12:49:02 -0000 1.10 *************** *** 5,8 **** --- 5,9 ---- import java.io.StringWriter; import java.util.Collection; + import java.util.HashMap; import java.util.Iterator; import java.util.List; *************** *** 17,20 **** --- 18,25 ---- public class BasicRenderer implements Renderer { + + static final protected int ORDINARY = 0; + static final protected int BOUND = 1; + static final protected int CONSTRAINT = 3;//any constraint properties are bound as well static private Log log = LogFactory.getLog(BasicRenderer.class); *************** *** 41,44 **** --- 46,60 ---- } + private String makeSupportField(String fieldName, List fieldList) { + String suffix=""; + boolean needSuffix=false; + for( Iterator fields = fieldList.iterator(); fields.hasNext();) { + String name = ((Field)fields.next()).getName(); + if(name.equals(fieldName)) needSuffix=true; + suffix+=name; + } + return needSuffix ? fieldName+"_"+suffix : fieldName; + } + String getTrueTypeName(ClassName cn, Map class2classmap) { String name = cn.getFullyQualifiedName(); *************** *** 117,133 **** writer.println(" {"); writer.println(); - - doFields(classMapping, writer); ! doConstructors(classMapping, class2classmap, writer); ! doFieldAccessors(classMapping, class2classmap, writer); ! doToString(classMapping, writer); ! doEqualsAndHashCode(classMapping, writer); ! writer.println("}"); // finally write the imports --- 133,161 ---- writer.println(" {"); writer.println(); ! // switch to another writer to be able to insert the ! // veto- and changeSupport fields ! StringWriter strPropWriter = new StringWriter(); ! PrintWriter propWriter = new PrintWriter(strPropWriter); ! ! doFields(classMapping, propWriter); ! ! doConstructors(classMapping, class2classmap, propWriter); ! String vetoSupport = makeSupportField("vetos", classMapping.getAllFields()); ! String changeSupport = makeSupportField("changes", classMapping.getAllFields()); ! int fieldTypes = doFieldAccessors(classMapping, class2classmap, propWriter, vetoSupport, changeSupport); ! doSupportMethods(fieldTypes, vetoSupport, changeSupport, propWriter); ! doToString(classMapping, propWriter); ! doEqualsAndHashCode(classMapping, propWriter); ! propWriter.println("}"); + //insert change and VetoSupport + doSupports(fieldTypes, classMapping, vetoSupport, changeSupport, writer); + + writer.print(strPropWriter.toString()); // finally write the imports *************** *** 138,141 **** --- 166,230 ---- } + /** + * Method doSupportMethods. + * @param fieldTypes + * @param vetoSupport + * @param changeSupport + * @param propWriter + */ + private void doSupportMethods( + int fieldTypes, + String vetoSupport, + String changeSupport, + PrintWriter writer) { + if((fieldTypes&CONSTRAINT)==CONSTRAINT) { + writer.println(" public void addVetoableChangeListener( VetoableChangeListener l ) {"); + writer.println(" "+vetoSupport+".addVetoableChangeListener(l);"); + writer.println(" }"); + writer.println(" public void removeVetoableChangeListener( VetoableChangeListener l ) {"); + writer.println(" "+vetoSupport+".removeVetoableChangeListener(l);"); + writer.println(" }"); + writer.println(); + } + if((fieldTypes&BOUND)==BOUND) { + writer.println(" public void addPropertyChangeListener( PropertyChangeListener l ) {"); + writer.println(" "+changeSupport+".addPropertyChangeListener(l);"); + writer.println(" }"); + writer.println(" public void removePropertyChangeListener( PropertyChangeListener l ) {"); + writer.println(" "+changeSupport+".removePropertyChangeListener(l);"); + writer.println(" }"); + writer.println(); + } + } + + + /** + * Method doSupports. + * @param vetoSupport + * @param changeSupport + * @param writer + */ + private void doSupports( int fieldTypes, + ClassMapping classMapping, + String vetoSupport, + String changeSupport, + PrintWriter writer) { + if((fieldTypes&CONSTRAINT)==CONSTRAINT) { + writer.println( " private VetoableChangeSupport "+vetoSupport+ + " = new VetoableChangeSupport(this);" ); + classMapping.getImports().add("java.beans.VetoableChangeSupport"); + classMapping.getImports().add("java.beans.PropertyVetoException"); + classMapping.getImports().add("java.beans.VetoableChangeListener"); + } + if((fieldTypes&BOUND)==BOUND) { + writer.println( " private PropertyChangeSupport "+changeSupport+ + " = new PropertyChangeSupport(this);" ); + writer.println(); + classMapping.getImports().add("java.beans.PropertyChangeSupport"); + classMapping.getImports().add("java.beans.PropertyChangeListener"); + } + } + + public void doConstructors(ClassMapping classMapping, Map class2classmap, PrintWriter writer) { // full constructor *************** *** 293,298 **** } ! public void doFieldAccessors(ClassMapping classMapping, Map class2classmap, PrintWriter writer) { // field accessors for (Iterator fields = classMapping.getFields().iterator(); fields.hasNext();) { --- 382,418 ---- } + + static Map primitiveToObject = new HashMap(); + { + primitiveToObject.put("char", "Character"); + + primitiveToObject.put("byte", "Byte"); + primitiveToObject.put("short", "Short"); + primitiveToObject.put("int", "Integer"); + primitiveToObject.put("long", "Long"); + + primitiveToObject.put("boolean", "Boolean"); + + primitiveToObject.put("float", "Float"); + primitiveToObject.put("double", "Double"); + + } + private String getFieldAsObject(boolean prependThis, Field field) { + ClassName type = field.getClassType(); + if(type != null && type.isPrimitive() && !type.isArray()) { + String typeName = (String) primitiveToObject.get(type.getName()); + typeName = "new "+typeName+"( "; + typeName += prependThis ? "this." : ""; + return typeName+field.getName()+" )"; + } + return field.getName(); + } ! public int doFieldAccessors(ClassMapping classMapping, ! Map class2classmap, ! PrintWriter writer, ! String vetoSupport, ! String changeSupport) { ! int fieldTypes=ORDINARY; // field accessors for (Iterator fields = classMapping.getFields().iterator(); fields.hasNext();) { *************** *** 312,318 **** // setter String setAccessScope = getFieldScope(field, "scope-set", "public"); ! writer.println(" " + setAccessScope + " void set" + field.getAsSuffix() + StringHelper.OPEN_PAREN + getTrueTypeName(field, class2classmap) + " " + field.getName() + ") {"); writer.println(" this." + field.getName() + " = " + field.getName() + ";"); writer.println(" }"); writer.println(); --- 432,461 ---- // setter + int fieldType=0; + if(field.getMeta("beans-property-type")!=null) { + String beansPropertyType = field.getMetaAsString("beans-property-type").trim().toLowerCase(); + if(beansPropertyType.equals("constraint") ) { + fieldTypes = (fieldTypes | CONSTRAINT); + fieldType = CONSTRAINT; + } + else if(beansPropertyType.equals("bound") ) { + fieldTypes = (fieldTypes | BOUND); + fieldType = BOUND; + } + } String setAccessScope = getFieldScope(field, "scope-set", "public"); ! writer.print(" " + setAccessScope + " void set" + field.getAsSuffix() + StringHelper.OPEN_PAREN + getTrueTypeName(field, class2classmap) + " " + field.getName() + ")"); ! writer.println((fieldType&CONSTRAINT)==CONSTRAINT ? " throws PropertyVetoException {" : " {"); ! if((fieldType&CONSTRAINT)==CONSTRAINT) { ! writer.println(" "+vetoSupport+".fireVetoableChange(\""+field.getName()+"\","); ! writer.println(" "+getFieldAsObject(true, field)+","); ! writer.println(" "+getFieldAsObject(false, field)+");"); ! } writer.println(" this." + field.getName() + " = " + field.getName() + ";"); + if((fieldType&BOUND)==BOUND) { + writer.println(" "+changeSupport+".firePropertyChange(\""+field.getName()+"\","); + writer.println(" "+getFieldAsObject(true, field)+","); + writer.println(" "+getFieldAsObject(false, field)+");"); + } writer.println(" }"); writer.println(); *************** *** 353,356 **** --- 496,500 ---- */ } + return fieldTypes; } Index: ClassName.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/tool/hbm2java/ClassName.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** ClassName.java 16 Jan 2003 20:52:38 -0000 1.4 --- ClassName.java 16 Feb 2003 12:49:02 -0000 1.5 *************** *** 2,6 **** package net.sf.hibernate.tool.hbm2java; - public class ClassName { private String fullyQualifiedName = null; --- 2,5 ---- *************** *** 9,62 **** 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() { return this.fullyQualifiedName; } ! public String getPackageName() { return this.packageName; } ! public String getName() { return this.name; } ! public boolean inJavaLang() { return "java.lang".equals(packageName); } ! public boolean inSamePackage(ClassName other) { ! return other.packageName==this.packageName || ! ( other.packageName!=null && other.packageName.equals(this.packageName) ); } ! public boolean equals(Object other) { ClassName otherClassName = (ClassName) other; --- 8,63 ---- 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; } + } else { + name = fullyQualifiedName; + packageName = null; } + } ! public String getFullyQualifiedName() { return this.fullyQualifiedName; } ! public String getPackageName() { return this.packageName; } ! public String getName() { return this.name; } ! public boolean inJavaLang() { return "java.lang".equals(packageName); } ! public boolean inSamePackage(ClassName other) { ! return other.packageName == this.packageName ! || (other.packageName != null && other.packageName.equals(this.packageName)); } ! public boolean equals(Object other) { ClassName otherClassName = (ClassName) other; *************** *** 75,79 **** */ public void setIsArray(boolean b) { ! isArray=b; } /** --- 76,80 ---- */ public void setIsArray(boolean b) { ! isArray = b; } /** *************** *** 85,97 **** } ! public String toString() { ! return getFullyQualifiedName(); ! } ! ! } ! ! ! ! ! --- 86,92 ---- } ! public String toString() { ! return getFullyQualifiedName(); ! } + } |