From: <one...@us...> - 2002-11-26 03:35:49
|
Update of /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/reflect In directory sc8-pr-cvs1:/tmp/cvs-serv20166/cirrus/hibernate/tools/reflect Modified Files: MappingByReflection.java ReflectedArrayProperty.java ReflectedClass.java ReflectedCollectionProperty.java ReflectedComponent.java ReflectedListProperty.java ReflectedMapProperty.java ReflectedProperty.java ReflectedSetProperty.java Log Message: fixed broken line-endings and added a test Index: MappingByReflection.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/reflect/MappingByReflection.java,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** MappingByReflection.java 11 Nov 2002 01:25:07 -0000 1.15 --- MappingByReflection.java 26 Nov 2002 03:35:45 -0000 1.16 *************** *** 1,672 **** ! //$Id$ ! package cirrus.hibernate.tools.reflect; ! ! import java.lang.reflect.*; ! import java.util.Hashtable; ! import java.util.Vector; ! import java.io.InputStreamReader; ! import java.io.BufferedReader; ! import java.io.Reader; ! import java.io.StreamTokenizer; [...1315 lines suppressed...] ! } ! ! ! } ! ! /* TO DO ! * new dtd accommodated? ! * length property for string/blob/binary? ! * top level collections (see PEntity and entities) ! * (nice)... ! * maybe also give access to ReflectedClass so callers can set... ! * UID property name and generator ! * (or heuristically?)... ! * get real UID, index, and data colummns for collections ! * nesting into collections ! * optimistic locking ! * ... ! * ! */ ! Index: ReflectedArrayProperty.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/reflect/ReflectedArrayProperty.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ReflectedArrayProperty.java 9 Feb 2002 23:08:18 -0000 1.3 --- ReflectedArrayProperty.java 26 Nov 2002 03:35:45 -0000 1.4 *************** *** 1,65 **** ! package cirrus.hibernate.tools.reflect; ! ! /** ! * @version 1.x ! * @author <a href="mailto:dou...@al...">e</a> ! */ ! class ReflectedArrayProperty extends ReflectedProperty { ! ! protected ReflectedProperty element; ! ! /** ! * Constructor for ReflectedArrayProperty. ! * @param name ! * @param type ! * @param comment ! */ ! ReflectedArrayProperty( String name, Class type, MappingByReflection map ) { ! super(name, type, map, "array"); ! this.element = null; ! Class elmcls = type.getComponentType(); ! if( elmcls == null ) { ! // huh! not an array after all!? ! // use type.getName() in case tynm modified ! emitPCommentStr( 1,"NOT an array!?", name, type.getName() ); ! return; ! } ! String roleName = map.tableNameFor(elmcls.getName()); ! this.element = map.makeProperty( roleName, elmcls ); ! // TO DO ! // if elmcls is a collection, it would be nice to make ! // the top level collection template here with role name roleName ! } ! ! protected void getXMLinArray( int level, StringBuffer buf ) { ! this.buf = buf; ! emitPCommentStr(level, "BUG!?: array of array ", name, element.name); ! } ! ! protected void getXMLinComposite( int level, StringBuffer buf ) { ! this.buf = buf; ! emitCollectionInCompositeStr(level); ! } ! ! //<!ELEMENT array (key, index, (element|one-to-many|many-to-many|subcollection|composite-element))> ! // <!ATTLIST array role CDATA #REQUIRED> ! // <!ATTLIST array table CDATA #IMPLIED> ! ! protected void getXML( int level, StringBuffer buf ) { ! this.buf = buf; ! if( comment != null ) { ! emitPCommentStr(level, comment, name, type.getName()); ! } ! if( element == null ) { ! emitPCommentStr(level, "BUG array of unknown kind!", name, element.name); ! } ! else { ! boolean p = element.type.isPrimitive(); ! emitCollectionStrL(level, p ? "primitive-array" : "array", name, false); ! emitCollectionStrK(level+1, "uid"); ! emitCollectionStrI(level+1, "idx"); ! element.getXMLinArray( level+1, buf ); ! emitCollectionStrR(level, p ? "primitive-array" : "array"); ! } ! } ! } --- 1,65 ---- ! package cirrus.hibernate.tools.reflect; ! ! /** ! * @version 1.x ! * @author <a href="mailto:dou...@al...">e</a> ! */ ! class ReflectedArrayProperty extends ReflectedProperty { ! ! protected ReflectedProperty element; ! ! /** ! * Constructor for ReflectedArrayProperty. ! * @param name ! * @param type ! * @param comment ! */ ! ReflectedArrayProperty( String name, Class type, MappingByReflection map ) { ! super(name, type, map, "array"); ! this.element = null; ! Class elmcls = type.getComponentType(); ! if( elmcls == null ) { ! // huh! not an array after all!? ! // use type.getName() in case tynm modified ! emitPCommentStr( 1,"NOT an array!?", name, type.getName() ); ! return; ! } ! String roleName = map.tableNameFor(elmcls.getName()); ! this.element = map.makeProperty( roleName, elmcls ); ! // TO DO ! // if elmcls is a collection, it would be nice to make ! // the top level collection template here with role name roleName ! } ! ! protected void getXMLinArray( int level, StringBuffer buf ) { ! this.buf = buf; ! emitPCommentStr(level, "BUG!?: array of array ", name, element.name); ! } ! ! protected void getXMLinComposite( int level, StringBuffer buf ) { ! this.buf = buf; ! emitCollectionInCompositeStr(level); ! } ! ! //<!ELEMENT array (key, index, (element|one-to-many|many-to-many|subcollection|composite-element))> ! // <!ATTLIST array role CDATA #REQUIRED> ! // <!ATTLIST array table CDATA #IMPLIED> ! ! protected void getXML( int level, StringBuffer buf ) { ! this.buf = buf; ! if( comment != null ) { ! emitPCommentStr(level, comment, name, type.getName()); ! } ! if( element == null ) { ! emitPCommentStr(level, "BUG array of unknown kind!", name, element.name); ! } ! else { ! boolean p = element.type.isPrimitive(); ! emitCollectionStrL(level, p ? "primitive-array" : "array", name, false); ! emitCollectionStrK(level+1, "uid"); ! emitCollectionStrI(level+1, "idx"); ! element.getXMLinArray( level+1, buf ); ! emitCollectionStrR(level, p ? "primitive-array" : "array"); ! } ! } ! } Index: ReflectedClass.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/reflect/ReflectedClass.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** ReflectedClass.java 11 Nov 2002 01:25:07 -0000 1.8 --- ReflectedClass.java 26 Nov 2002 03:35:45 -0000 1.9 *************** *** 1,401 **** ! package cirrus.hibernate.tools.reflect; ! ! import java.lang.reflect.*; ! import java.beans.Introspector; ! import java.util.Enumeration; ! import java.util.Hashtable; ! import java.util.Vector; ! ! import cirrus.hibernate.helpers.StringHelper; ! ! /** a ReflectedClass is created by MappingByReflection instances for ! * every class encountered, from map.addClass(), while chasing the ! * superclass chain, or grokking properties ! * ! * @version 1.x ! * @author <a href="mailto:dou...@al...">e</a> ! */ ! public class ReflectedClass { ! ! private MappingByReflection map; // my containing map ! private StringBuffer buf;// shared buffer provided by MappingByReflection ! private Class clazz; // my class ! private Vector subs; // my ReflectedClass subclasses ! private Hashtable props;// my ReflectedProperty(s) keyed by property name ! private ReflectedProperty uid; ! private boolean persistent; // true => persistent class or subclass ! // false => component or serializable ! ! private Hashtable setprops; ! private Hashtable getprops; ! ! protected ReflectedClass( MappingByReflection map, Class cls ) { ! this.clazz = cls; ! this.map = map; ! this.subs = new Vector(); ! this.uid = null; ! this.persistent = false; // default to component until told otherwise ! map.rClasses.put( cls, this ); // this must happen to avoid infinite regress... ! this.reflect(); // here ! } ! ! /** used by gui */ ! public String getName() { ! return clazz.getName(); ! } ! ! /** used by gui */ ! public ReflectedProperty[] getProperties() { ! ReflectedProperty[] rp = new ReflectedProperty[props.size()]; ! Enumeration e = props.elements(); ! int i = 0; ! while( e.hasMoreElements() ) { ! rp[i++] = (ReflectedProperty )e.nextElement(); ! } ! return rp; ! } ! ! /** used by gui */ ! public ReflectedClass[] getSubclasses() { ! ReflectedClass[] v = new ReflectedClass[subs.size()]; ! subs.copyInto(v); ! return v; ! } ! ! protected void addReflectedClass( ReflectedClass rc ) { ! subs.add( rc ); ! } ! ! /* no longer used; see getUidProp ! protected boolean isRoot() { ! return ( uid != null ); ! }*/ ! ! /** has the class been deemed hibernate-persistable? <br> ! * if not, it may still be useful as a component or may be serializable ! */ ! protected boolean isPersistent() { ! return this.persistent; ! } ! ! /** called by MappinByReflection when this class has been ! * deemed hibernate-persistable ! */ ! protected void setPersistent( boolean p ) { ! this.persistent = p; ! } ! ! /** does this class have any properties? <br> this is not reliable ! * until addSuperclassProps() is called once first ! */ ! protected boolean hasProperties() { ! return ( props.size() != 0 ); ! } ! ! /** add to this class the properties of all its superclasses ! */ ! protected void addSuperclassProps() { ! // add properties from superclasses ! addSuperclassProps( clazz.getSuperclass() ); ! } // with helper... ! private void addSuperclassProps( Class supz ) { ! if( supz != null ) { ! reflectClass( supz ); ! addSuperclassProps( supz.getSuperclass() ); // recurse ! } ! } ! ! /** dump the OR-Mapping XML for a root class and all of its subclasses ! * that share a table and UID ! */ ! protected void getXML(int level) { ! // dumps in map.buf ! this.buf = map.buf; ! String name = clazz.getName(); ! String shortName = StringHelper.unqualify(name); ! // debug... ! map.emitPrefix(level);; ! buf.append("<!-- ") ! .append(name) ! .append(" root -->\n"); ! // a root class -- polymorphic style ! map.emitPrefix(level);; ! buf.append("<class name=\"") ! .append(name) ! .append("\" table=\"") ! .append(map.tableNameFor(name)) ! .append((subs.size() > 0) ? "\" discriminator-value=\"" + shortName : "") ! .append("\">\n"); ! // first find the uid ! if( uid == null ) { ! // report error ! map.emitPrefix(level); ! buf.append("<!-- NO UID!? class=\"") ! .append(name) ! .append("\" -->\n"); ! } else { ! uid.getUidXML(level+1, buf); ! } ! //the discriminator element ! if ( subs.size() > 0 ) { ! map.emitPrefix(level+1); ! buf.append("<discriminator column=\"subclass\" type=\"string\"/>\n"); ! } ! // now the properties already classified by heuristic ! Enumeration ps = props.elements(); ! while( ps.hasMoreElements() ) { ! ReflectedProperty prop = (ReflectedProperty )ps.nextElement(); ! if( prop.isUid ) continue; // done above ! // we're root and top component level ! map.cycleBuster = new Hashtable(); ! prop.getXML( level+1, buf ); ! } ! // now the subclasses ! int len = subs.size(); ! for( int i=0; i<len; i++ ) { ! ReflectedClass rsc = (ReflectedClass )subs.get(i); ! rsc.getXMLasSubclass(level+1); ! } ! map.emitPrefix(level);; ! buf.append("</class>\n"); ! } ! ! /** dump the OR-Mapping XML for a non-root class and all of its ! * subclasses that share its table and UID ! */ ! protected void getXMLasSubclass(int level) { ! // dumps in map.buf ! this.buf = map.buf; ! String name = clazz.getName(); ! // debug... ! map.emitPrefix(level);; ! buf.append("<!-- ") ! .append(name) ! .append(" -->\n"); ! // a sub class -- polymorphic style ! map.emitPrefix(level);; ! buf.append("<subclass name=\"") ! .append(name) ! .append("\">\n"); ! // now the properties already classified by heuristic ! Enumeration ps = props.elements(); ! while( ps.hasMoreElements() ) { ! ReflectedProperty prop = (ReflectedProperty )ps.nextElement(); ! if( prop.isUid ) continue; // done above ! prop.getXML( level+1, buf ); ! } ! // now the subclasses ! int len = subs.size(); ! for( int i=0; i<len; i++ ) { ! ReflectedClass rsc = (ReflectedClass )subs.get(i); ! rsc.getXMLasSubclass(level+1); ! } ! map.emitPrefix(level);; ! buf.append("</subclass>\n"); ! } ! ! /** dump the OR-Mapping XML for a class used as a component ! * ! * @param propName the property name of the property containing this component ! */ ! protected void getXMLasComponent( int level, String propName ) { ! // dumps in buf ! this.buf = map.buf; ! String name = clazz.getName(); ! /* debug... ! map.emitPrefix(level);; ! buf.append("<!-- ") ! .append(name) ! .append(" -->\n");*/ ! // a component ! map.emitPrefix(level);; ! buf.append("<component name=\"") ! .append(propName) ! .append("\" class=\"") ! .append(name) ! .append("\">\n"); ! // now the properties already classified by heuristic ! Enumeration ps = props.elements(); ! while( ps.hasMoreElements() ) { ! ReflectedProperty prop = (ReflectedProperty )ps.nextElement(); ! prop.getXML( level+1, buf ); ! } ! // NOT the subclasses ! map.emitPrefix(level);; ! buf.append("</component>\n"); ! } ! ! protected void getXMLasComposite(int level) { ! // dumps in buf ! this.buf = map.buf; ! String name = clazz.getName(); ! // a composite-element ! map.emitPrefix(level);; ! buf.append("<composite-element class=\"") ! .append(name) ! .append("\">\n"); ! // now the properties already classified by heuristic ! Enumeration ps = props.elements(); ! while( ps.hasMoreElements() ) { ! ReflectedProperty prop = (ReflectedProperty )ps.nextElement(); ! // call a restricted form of getXML that disallows inline ! // collections and uses composite-element instead of component ! prop.getXMLinComposite( level, buf ); ! } ! // NOT the subclasses ! map.emitPrefix(level);; ! buf.append("</composite-element>\n"); ! } ! ! protected void getXMLasMany2Many(int level) { ! // dumps in buf ! this.buf = map.buf; ! String name = clazz.getName(); ! // a many-to-many ! map.emitPrefix(level);; ! buf.append("<many-to-many class=\"" ) ! .append(name) ! .append("\" column=\"") ! .append(map.columnNameFor(name)) ! .append("\"/>\n"); ! } ! ! private String capitalize( String s ) { ! char c = s.charAt(0); ! if( Character.isUpperCase(c) ) return s; ! char[] ca = {Character.toUpperCase(c)}; ! return new String(ca).concat(s.substring(1)); ! } ! ! /** called to determine if the class has a UID property ! * ! * @param niceKeys an array of candidate UID names ! * @return the UID ReflectedProperty ! */ ! protected ReflectedProperty getUidProp( String[] niceKeys ) { ! if( uid != null ) return uid; // cache ! // ugh; we'll try some things... ! ReflectedProperty p; ! // try a prop named identically to a niceKey[] ! for( int i=0; i<niceKeys.length; i++ ) { ! p = (ReflectedProperty )props.get(niceKeys[i]); ! // TO DO is isUidOK OK? ! if( p != null && p.isUidOK() ) { ! uid = p; ! break; ! } ! } ! /* now that we're using this for finding roots this tries too hard?... ! if( uid != null ) return uid; ! // try a prop whose name starts with a niceKey[] ! for( int i=0; i<niceKeys.length; i++ ) { ! Enumeration ps = props.elements(); ! while( ps.hasMoreElements() ) { ! p = (ReflectedProperty )ps.nextElement(); ! if( p.isUidOK() && p.name.startsWith(niceKeys[i])) { ! uid = p; ! break; ! } ! } ! } ! if( uid != null ) return uid; ! // try a prop whose name ends with (capitalized) niceKey[] ! for( int i=0; i<niceKeys.length; i++ ) { ! String candidate = capitalize( niceKeys[i] ); ! Enumeration ps = props.elements(); ! while( ps.hasMoreElements() ) { ! p = (ReflectedProperty )ps.nextElement(); ! if( p.isUidOK() && p.name.endsWith(candidate)) { ! uid = p; ! break; ! } ! } ! } ! if( uid != null ) return uid; ! // more to try ? ! // use anything with an appropriate type ! Enumeration ps = props.elements(); ! while( ps.hasMoreElements() ) { ! p = (ReflectedProperty )ps.nextElement(); ! if( p.isUidOK() ) { ! uid = p; ! break; ! } ! }*/ ! return uid; ! } ! ! private void maybeAddProp( String name, Class type ! , Hashtable thisprops, Hashtable thatprops ) { ! Object ty = thatprops.get( name ); ! if( ty == type ) { ! // a winner! ! thatprops.remove( name ); ! ReflectedProperty prop = map.makeProperty( name, type ); ! if (prop!=null) props.put( name, prop ); ! } ! else { ! // maybe later ! thisprops.put( name, type ); ! } ! } ! ! private void addSetter( String name, Class type ) { ! maybeAddProp( name, type, setprops, getprops ); ! } ! ! private void addGetter( String name, Class type ) { ! maybeAddProp( name, type, getprops, setprops ); ! } ! ! /** called by the constructor ! */ ! private void reflect() { ! props = new Hashtable(); ! setprops = new Hashtable(); ! getprops = new Hashtable(); ! reflectClass( clazz ); ! } ! /** called to get the properties of a class, ! * or add properties from a superclass ! */ ! private void reflectClass( Class cls ) { ! // empty getprops and setprops if a getter and setter pair ! // should come from one class and not a class and its superclass ! try { ! Method[] ms = cls.getDeclaredMethods(); ! for( int i=0; i<ms.length; i++ ) { ! Method m = ms[i]; ! if( m == null ) continue; ! int mmod = m.getModifiers(); ! if( Modifier.isStatic( mmod ) ) continue; ! Class argtyp[] = m.getParameterTypes(); ! if( argtyp.length > 1 ) continue; ! String name = m.getName(); ! Class restyp = m.getReturnType(); ! if( argtyp.length == 1 ) { ! if( restyp == void.class && name.startsWith("set") ) { ! addSetter( Introspector.decapitalize(name.substring(3)) ! , argtyp[0] ); ! } ! } ! else { // argtyp.length == 0 ! if( name.startsWith("get") ) { ! addGetter( Introspector.decapitalize(name.substring(3)) ! , restyp ); ! } ! else if( restyp == boolean.class && name.startsWith("is") ) { ! addGetter( Introspector.decapitalize(name.substring(2)) ! , restyp ); ! } ! } ! } ! } ! catch( SecurityException e ) { ! // ignore!? ! } ! } ! ! ! } ! --- 1,401 ---- ! package cirrus.hibernate.tools.reflect; ! ! import java.lang.reflect.*; ! import java.beans.Introspector; ! import java.util.Enumeration; ! import java.util.Hashtable; ! import java.util.Vector; ! ! import cirrus.hibernate.helpers.StringHelper; ! ! /** a ReflectedClass is created by MappingByReflection instances for ! * every class encountered, from map.addClass(), while chasing the ! * superclass chain, or grokking properties ! * ! * @version 1.x ! * @author <a href="mailto:dou...@al...">e</a> ! */ ! public class ReflectedClass { ! ! private MappingByReflection map; // my containing map ! private StringBuffer buf;// shared buffer provided by MappingByReflection ! private Class clazz; // my class ! private Vector subs; // my ReflectedClass subclasses ! private Hashtable props;// my ReflectedProperty(s) keyed by property name ! private ReflectedProperty uid; ! private boolean persistent; // true => persistent class or subclass ! // false => component or serializable ! ! private Hashtable setprops; ! private Hashtable getprops; ! ! protected ReflectedClass( MappingByReflection map, Class cls ) { ! this.clazz = cls; ! this.map = map; ! this.subs = new Vector(); ! this.uid = null; ! this.persistent = false; // default to component until told otherwise ! map.rClasses.put( cls, this ); // this must happen to avoid infinite regress... ! this.reflect(); // here ! } ! ! /** used by gui */ ! public String getName() { ! return clazz.getName(); ! } ! ! /** used by gui */ ! public ReflectedProperty[] getProperties() { ! ReflectedProperty[] rp = new ReflectedProperty[props.size()]; ! Enumeration e = props.elements(); ! int i = 0; ! while( e.hasMoreElements() ) { ! rp[i++] = (ReflectedProperty )e.nextElement(); ! } ! return rp; ! } ! ! /** used by gui */ ! public ReflectedClass[] getSubclasses() { ! ReflectedClass[] v = new ReflectedClass[subs.size()]; ! subs.copyInto(v); ! return v; ! } ! ! protected void addReflectedClass( ReflectedClass rc ) { ! subs.add( rc ); ! } ! ! /* no longer used; see getUidProp ! protected boolean isRoot() { ! return ( uid != null ); ! }*/ ! ! /** has the class been deemed hibernate-persistable? <br> ! * if not, it may still be useful as a component or may be serializable ! */ ! protected boolean isPersistent() { ! return this.persistent; ! } ! ! /** called by MappinByReflection when this class has been ! * deemed hibernate-persistable ! */ ! protected void setPersistent( boolean p ) { ! this.persistent = p; ! } ! ! /** does this class have any properties? <br> this is not reliable ! * until addSuperclassProps() is called once first ! */ ! protected boolean hasProperties() { ! return ( props.size() != 0 ); ! } ! ! /** add to this class the properties of all its superclasses ! */ ! protected void addSuperclassProps() { ! // add properties from superclasses ! addSuperclassProps( clazz.getSuperclass() ); ! } // with helper... ! private void addSuperclassProps( Class supz ) { ! if( supz != null ) { ! reflectClass( supz ); ! addSuperclassProps( supz.getSuperclass() ); // recurse ! } ! } ! ! /** dump the OR-Mapping XML for a root class and all of its subclasses ! * that share a table and UID ! */ ! protected void getXML(int level) { ! // dumps in map.buf ! this.buf = map.buf; ! String name = clazz.getName(); ! String shortName = StringHelper.unqualify(name); ! // debug... ! map.emitPrefix(level);; ! buf.append("<!-- ") ! .append(name) ! .append(" root -->\n"); ! // a root class -- polymorphic style ! map.emitPrefix(level);; ! buf.append("<class name=\"") ! .append(name) ! .append("\" table=\"") ! .append(map.tableNameFor(name)) ! .append((subs.size() > 0) ? "\" discriminator-value=\"" + shortName : "") ! .append("\">\n"); ! // first find the uid ! if( uid == null ) { ! // report error ! map.emitPrefix(level); ! buf.append("<!-- NO UID!? class=\"") ! .append(name) ! .append("\" -->\n"); ! } else { ! uid.getUidXML(level+1, buf); ! } ! //the discriminator element ! if ( subs.size() > 0 ) { ! map.emitPrefix(level+1); ! buf.append("<discriminator column=\"subclass\" type=\"string\"/>\n"); ! } ! // now the properties already classified by heuristic ! Enumeration ps = props.elements(); ! while( ps.hasMoreElements() ) { ! ReflectedProperty prop = (ReflectedProperty )ps.nextElement(); ! if( prop.isUid ) continue; // done above ! // we're root and top component level ! map.cycleBuster = new Hashtable(); ! prop.getXML( level+1, buf ); ! } ! // now the subclasses ! int len = subs.size(); ! for( int i=0; i<len; i++ ) { ! ReflectedClass rsc = (ReflectedClass )subs.get(i); ! rsc.getXMLasSubclass(level+1); ! } ! map.emitPrefix(level);; ! buf.append("</class>\n"); ! } ! ! /** dump the OR-Mapping XML for a non-root class and all of its ! * subclasses that share its table and UID ! */ ! protected void getXMLasSubclass(int level) { ! // dumps in map.buf ! this.buf = map.buf; ! String name = clazz.getName(); ! // debug... ! map.emitPrefix(level);; ! buf.append("<!-- ") ! .append(name) ! .append(" -->\n"); ! // a sub class -- polymorphic style ! map.emitPrefix(level);; ! buf.append("<subclass name=\"") ! .append(name) ! .append("\">\n"); ! // now the properties already classified by heuristic ! Enumeration ps = props.elements(); ! while( ps.hasMoreElements() ) { ! ReflectedProperty prop = (ReflectedProperty )ps.nextElement(); ! if( prop.isUid ) continue; // done above ! prop.getXML( level+1, buf ); ! } ! // now the subclasses ! int len = subs.size(); ! for( int i=0; i<len; i++ ) { ! ReflectedClass rsc = (ReflectedClass )subs.get(i); ! rsc.getXMLasSubclass(level+1); ! } ! map.emitPrefix(level);; ! buf.append("</subclass>\n"); ! } ! ! /** dump the OR-Mapping XML for a class used as a component ! * ! * @param propName the property name of the property containing this component ! */ ! protected void getXMLasComponent( int level, String propName ) { ! // dumps in buf ! this.buf = map.buf; ! String name = clazz.getName(); ! /* debug... ! map.emitPrefix(level);; ! buf.append("<!-- ") ! .append(name) ! .append(" -->\n");*/ ! // a component ! map.emitPrefix(level);; ! buf.append("<component name=\"") ! .append(propName) ! .append("\" class=\"") ! .append(name) ! .append("\">\n"); ! // now the properties already classified by heuristic ! Enumeration ps = props.elements(); ! while( ps.hasMoreElements() ) { ! ReflectedProperty prop = (ReflectedProperty )ps.nextElement(); ! prop.getXML( level+1, buf ); ! } ! // NOT the subclasses ! map.emitPrefix(level);; ! buf.append("</component>\n"); ! } ! ! protected void getXMLasComposite(int level) { ! // dumps in buf ! this.buf = map.buf; ! String name = clazz.getName(); ! // a composite-element ! map.emitPrefix(level);; ! buf.append("<composite-element class=\"") ! .append(name) ! .append("\">\n"); ! // now the properties already classified by heuristic ! Enumeration ps = props.elements(); ! while( ps.hasMoreElements() ) { ! ReflectedProperty prop = (ReflectedProperty )ps.nextElement(); ! // call a restricted form of getXML that disallows inline ! // collections and uses composite-element instead of component ! prop.getXMLinComposite( level, buf ); ! } ! // NOT the subclasses ! map.emitPrefix(level);; ! buf.append("</composite-element>\n"); ! } ! ! protected void getXMLasMany2Many(int level) { ! // dumps in buf ! this.buf = map.buf; ! String name = clazz.getName(); ! // a many-to-many ! map.emitPrefix(level);; ! buf.append("<many-to-many class=\"" ) ! .append(name) ! .append("\" column=\"") ! .append(map.columnNameFor(name)) ! .append("\"/>\n"); ! } ! ! private String capitalize( String s ) { ! char c = s.charAt(0); ! if( Character.isUpperCase(c) ) return s; ! char[] ca = {Character.toUpperCase(c)}; ! return new String(ca).concat(s.substring(1)); ! } ! ! /** called to determine if the class has a UID property ! * ! * @param niceKeys an array of candidate UID names ! * @return the UID ReflectedProperty ! */ ! protected ReflectedProperty getUidProp( String[] niceKeys ) { ! if( uid != null ) return uid; // cache ! // ugh; we'll try some things... ! ReflectedProperty p; ! // try a prop named identically to a niceKey[] ! for( int i=0; i<niceKeys.length; i++ ) { ! p = (ReflectedProperty )props.get(niceKeys[i]); ! // TO DO is isUidOK OK? ! if( p != null && p.isUidOK() ) { ! uid = p; ! break; ! } ! } ! /* now that we're using this for finding roots this tries too hard?... ! if( uid != null ) return uid; ! // try a prop whose name starts with a niceKey[] ! for( int i=0; i<niceKeys.length; i++ ) { ! Enumeration ps = props.elements(); ! while( ps.hasMoreElements() ) { ! p = (ReflectedProperty )ps.nextElement(); ! if( p.isUidOK() && p.name.startsWith(niceKeys[i])) { ! uid = p; ! break; ! } ! } ! } ! if( uid != null ) return uid; ! // try a prop whose name ends with (capitalized) niceKey[] ! for( int i=0; i<niceKeys.length; i++ ) { ! String candidate = capitalize( niceKeys[i] ); ! Enumeration ps = props.elements(); ! while( ps.hasMoreElements() ) { ! p = (ReflectedProperty )ps.nextElement(); ! if( p.isUidOK() && p.name.endsWith(candidate)) { ! uid = p; ! break; ! } ! } ! } ! if( uid != null ) return uid; ! // more to try ? ! // use anything with an appropriate type ! Enumeration ps = props.elements(); ! while( ps.hasMoreElements() ) { ! p = (ReflectedProperty )ps.nextElement(); ! if( p.isUidOK() ) { ! uid = p; ! break; ! } ! }*/ ! return uid; ! } ! ! private void maybeAddProp( String name, Class type ! , Hashtable thisprops, Hashtable thatprops ) { ! Object ty = thatprops.get( name ); ! if( ty == type ) { ! // a winner! ! thatprops.remove( name ); ! ReflectedProperty prop = map.makeProperty( name, type ); ! if (prop!=null) props.put( name, prop ); ! } ! else { ! // maybe later ! thisprops.put( name, type ); ! } ! } ! ! private void addSetter( String name, Class type ) { ! maybeAddProp( name, type, setprops, getprops ); ! } ! ! private void addGetter( String name, Class type ) { ! maybeAddProp( name, type, getprops, setprops ); ! } ! ! /** called by the constructor ! */ ! private void reflect() { ! props = new Hashtable(); ! setprops = new Hashtable(); ! getprops = new Hashtable(); ! reflectClass( clazz ); ! } ! /** called to get the properties of a class, ! * or add properties from a superclass ! */ ! private void reflectClass( Class cls ) { ! // empty getprops and setprops if a getter and setter pair ! // should come from one class and not a class and its superclass ! try { ! Method[] ms = cls.getDeclaredMethods(); ! for( int i=0; i<ms.length; i++ ) { ! Method m = ms[i]; ! if( m == null ) continue; ! int mmod = m.getModifiers(); ! if( Modifier.isStatic( mmod ) ) continue; ! Class argtyp[] = m.getParameterTypes(); ! if( argtyp.length > 1 ) continue; ! String name = m.getName(); ! Class restyp = m.getReturnType(); ! if( argtyp.length == 1 ) { ! if( restyp == void.class && name.startsWith("set") ) { ! addSetter( Introspector.decapitalize(name.substring(3)) ! , argtyp[0] ); ! } ! } ! else { // argtyp.length == 0 ! if( name.startsWith("get") ) { ! addGetter( Introspector.decapitalize(name.substring(3)) ! , restyp ); ! } ! else if( restyp == boolean.class && name.startsWith("is") ) { ! addGetter( Introspector.decapitalize(name.substring(2)) ! , restyp ); ! } ! } ! } ! } ! catch( SecurityException e ) { ! // ignore!? ! } ! } ! ! ! } ! Index: ReflectedCollectionProperty.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/reflect/ReflectedCollectionProperty.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** ReflectedCollectionProperty.java 19 Sep 2002 08:27:41 -0000 1.1 --- ReflectedCollectionProperty.java 26 Nov 2002 03:35:45 -0000 1.2 *************** *** 1,38 **** ! package cirrus.hibernate.tools.reflect; ! ! /** ! * @version 1.x ! * @author <a href="mailto:dou...@al...">e</a> ! */ ! class ReflectedCollectionProperty extends ReflectedProperty { ! ! /** ! * Constructor for ReflectedListProperty. ! * @param name ! * @param type ! */ ! ReflectedCollectionProperty(String name, Class type, MappingByReflection map) { ! super(name, type, map, "bag"); ! } ! ! protected void getXMLinArray( int level, StringBuffer buf ) { ! this.buf = buf; ! emitSubCollectionStr(level, name); ! } ! ! protected void getXMLinComposite( int level, StringBuffer buf ) { ! this.buf = buf; ! emitCollectionInCompositeStr(level); ! } ! ! protected void getXML( int level, StringBuffer buf ) { ! this.buf = buf; ! if( comment != null ) { ! emitPCommentStr(level, comment, name, type.getName()); ! } ! emitCollectionStrL(level, "bag", name, true); ! emitCollectionStrK(level+1, "uid"); ! emitCollectionStrET(level+1, "elm", "string"); ! emitCollectionStrR(level, "bag"); ! } ! } --- 1,38 ---- ! package cirrus.hibernate.tools.reflect; ! ! /** ! * @version 1.x ! * @author <a href="mailto:dou...@al...">e</a> ! */ ! class ReflectedCollectionProperty extends ReflectedProperty { ! ! /** ! * Constructor for ReflectedListProperty. ! * @param name ! * @param type ! */ ! ReflectedCollectionProperty(String name, Class type, MappingByReflection map) { ! super(name, type, map, "bag"); ! } ! ! protected void getXMLinArray( int level, StringBuffer buf ) { ! this.buf = buf; ! emitSubCollectionStr(level, name); ! } ! ! protected void getXMLinComposite( int level, StringBuffer buf ) { ! this.buf = buf; ! emitCollectionInCompositeStr(level); ! } ! ! protected void getXML( int level, StringBuffer buf ) { ! this.buf = buf; ! if( comment != null ) { ! emitPCommentStr(level, comment, name, type.getName()); ! } ! emitCollectionStrL(level, "bag", name, true); ! emitCollectionStrK(level+1, "uid"); ! emitCollectionStrET(level+1, "elm", "string"); ! emitCollectionStrR(level, "bag"); ! } ! } Index: ReflectedComponent.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/reflect/ReflectedComponent.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** ReflectedComponent.java 10 Jun 2002 15:31:26 -0000 1.5 --- ReflectedComponent.java 26 Nov 2002 03:35:45 -0000 1.6 *************** *** 1,147 **** ! package cirrus.hibernate.tools.reflect; ! ! import java.io.Serializable; ! ! /** ! * @version 1.x ! * @author <a href="mailto:dou...@al...">e</a> ! */ ! class ReflectedComponent extends ReflectedProperty { ! ! private static final int KIND_UNKNOWN = 0; ! private static final int KIND_BASIC = 1; // nothing or serializable ! private static final int KIND_ASSOCIATION = 2; ! private static final int KIND_COMPONENT = 3; ! ! private ReflectedClass component; // my component ! private int kind = KIND_UNKNOWN; ! ! protected ReflectedComponent( String name, Class type ! , MappingByReflection map, ReflectedClass comp ) ! { ! super( name, type, map, "prospective-component" ); ! this.component = comp; ! } ! ! private int determineKind() { ! // this decision has to be delayed to avoid circularities ! // e.g., a component which includes itself will not have ! // access to this info until it is fully processed ! // but we cache it here... ! if( kind != KIND_UNKNOWN ) return kind; ! // is the prospective component a persistent class ! if( component.isPersistent() ) { ! // make it an association ! comment = "association"; ! kind = KIND_ASSOCIATION; ! return KIND_ASSOCIATION; ! } ! // does the prospective component have any properties? ! // first let it look in superclasses ! component.addSuperclassProps(); ! if( component.hasProperties() ) { ! // make it a component ! comment = "component"; ! kind = KIND_COMPONENT; ! return KIND_COMPONENT; ! } ! if ( Serializable.class.isAssignableFrom(type) ) { ! // make it a serializable ! tynm = "serializable"; ! comment = "serializable"; ! kind = KIND_BASIC; ! return KIND_BASIC; ! } ! else { ! buf.append("<!-- BUG unknown kind! name=\"") ! .append(name) ! .append("\" type=\"") ! .append(type.getName()) ! .append("\" -->\n"); ! kind = KIND_BASIC; ! return KIND_BASIC; ! } ! } ! ! protected void getXMLinArray( int level, StringBuffer buf ) { ! this.buf = buf; ! switch( determineKind() ) { ! case KIND_BASIC: ! super.getXML( level, buf ); ! break; ! case KIND_COMPONENT: ! getXMLas(level, true); ! break; ! case KIND_ASSOCIATION: ! component.getXMLasMany2Many(level); ! break; ! default: ! emitPCommentStr(level, "BUG! unknown component kind", name, type.getName()); ! break; ! } ! } ! ! protected void getXMLinComposite( int level, StringBuffer buf ) { ! //this.buf = buf; ! getXMLinArray( level, buf ); ! } ! ! protected void emitMany2OneStr(int level) { ! map.emitPrefix(level); ! buf.append("<many-to-one name=\"" ) ! .append(name) ! .append("\" column=\"") ! .append(map.columnNameFor(name)) ! .append("\" class=\"") ! .append(tynm) ! .append("\"/>\n"); ! } ! ! protected void getXML( int level, StringBuffer buf ) { ! this.buf = buf; ! switch( determineKind() ) { ! case KIND_BASIC: ! super.getXML( level, buf ); ! return; ! case KIND_COMPONENT: ! getXMLas(level, false); //go on... ! return; ! case KIND_ASSOCIATION: ! emitMany2OneStr(level); ! return; ! default: ! emitPCommentStr(level, "BUG! unknown component kind", name, type.getName()); ! return; ! } ! } ! ! private void getXMLas( int level, boolean isComposite ) { ! if( component != null ) { ! Integer x = (Integer )map.cycleBuster.get( component ); ! if( x != null ) { ! // we're nested ! int i = x.intValue(); ! if( i >= map.maxDepth ) { ! emitPCommentStr(level, "component too deeply nested", name, type.getName()); ! return; // bail! ! } ! else { ! map.cycleBuster.put( component, new Integer( i + 1 ) ); ! } ! } ! else { ! map.cycleBuster.put( component, new Integer( 1 ) ); ! } ! if( isComposite ) { ! component.getXMLasComposite(level); ! } ! else { ! component.getXMLasComponent( level, name ); ! } ! } ! else { ! emitPCommentStr(level, "not component?", name, type.getName()); ! } ! } ! ! } --- 1,147 ---- ! package cirrus.hibernate.tools.reflect; ! ! import java.io.Serializable; ! ! /** ! * @version 1.x ! * @author <a href="mailto:dou...@al...">e</a> ! */ ! class ReflectedComponent extends ReflectedProperty { ! ! private static final int KIND_UNKNOWN = 0; ! private static final int KIND_BASIC = 1; // nothing or serializable ! private static final int KIND_ASSOCIATION = 2; ! private static final int KIND_COMPONENT = 3; ! ! private ReflectedClass component; // my component ! private int kind = KIND_UNKNOWN; ! ! protected ReflectedComponent( String name, Class type ! , MappingByReflection map, ReflectedClass comp ) ! { ! super( name, type, map, "prospective-component" ); ! this.component = comp; ! } ! ! private int determineKind() { ! // this decision has to be delayed to avoid circularities ! // e.g., a component which includes itself will not have ! // access to this info until it is fully processed ! // but we cache it here... ! if( kind != KIND_UNKNOWN ) return kind; ! // is the prospective component a persistent class ! if( component.isPersistent() ) { ! // make it an association ! comment = "association"; ! kind = KIND_ASSOCIATION; ! return KIND_ASSOCIATION; ! } ! // does the prospective component have any properties? ! // first let it look in superclasses ! component.addSuperclassProps(); ! if( component.hasProperties() ) { ! // make it a component ! comment = "component"; ! kind = KIND_COMPONENT; ! return KIND_COMPONENT; ! } ! if ( Serializable.class.isAssignableFrom(type) ) { ! // make it a serializable ! tynm = "serializable"; ! comment = "serializable"; ! kind = KIND_BASIC; ! return KIND_BASIC; ! } ! else { ! buf.append("<!-- BUG unknown kind! name=\"") ! .append(name) ! .append("\" type=\"") ! .append(type.getName()) ! .append("\" -->\n"); ! kind = KIND_BASIC; ! return KIND_BASIC; ! } ! } ! ! protected void getXMLinArray( int level, StringBuffer buf ) { ! this.buf = buf; ! switch( determineKind() ) { ! case KIND_BASIC: ! super.getXML( level, buf ); ! break; ! case KIND_COMPONENT: ! getXMLas(level, true); ! break; ! case KIND_ASSOCIATION: ! component.getXMLasMany2Many(level); ! break; ! default: ! emitPCommentStr(level, "BUG! unknown component kind", name, type.getName()); ! break; ! } ! } ! ! protected void getXMLinComposite( int level, StringBuffer buf ) { ! //this.buf = buf; ! getXMLinArray( level, buf ); ! } ! ! protected void emitMany2OneStr(int level) { ! map.emitPrefix(level); ! buf.append("<many-to-one name=\"" ) ! .append(name) ! .append("\" column=\"") ! .append(map.columnNameFor(name)) ! .append("\" class=\"") ! .append(tynm) ! .append("\"/>\n"); ! } ! ! protected void getXML( int level, StringBuffer buf ) { ! this.buf = buf; ! switch( determineKind() ) { ! case KIND_BASIC: ! super.getXML( level, buf ); ! return; ! case KIND_COMPONENT: ! getXMLas(level, false); //go on... ! return; ! case KIND_ASSOCIATION: ! emitMany2OneStr(level); ! return; ! default: ! emitPCommentStr(level, "BUG! unknown component kind", name, type.getName()); ! return; ! } ! } ! ! private void getXMLas( int level, boolean isComposite ) { ! if( component != null ) { ! Integer x = (Integer )map.cycleBuster.get( component ); ! if( x != null ) { ! // we're nested ! int i = x.intValue(); ! if( i >= map.maxDepth ) { ! emitPCommentStr(level, "component too deeply nested", name, type.getName()); ! return; // bail! ! } ! else { ! map.cycleBuster.put( component, new Integer( i + 1 ) ); ! } ! } ! else { ! map.cycleBuster.put( component, new Integer( 1 ) ); ! } ! if( isComposite ) { ! component.getXMLasComposite(level); ! } ! else { ! component.getXMLasComponent( level, name ); ! } ! } ! else { ! emitPCommentStr(level, "not component?", name, type.getName()); ! } ! } ! ! } Index: ReflectedListProperty.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/reflect/ReflectedListProperty.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ReflectedListProperty.java 9 Feb 2002 23:08:18 -0000 1.3 --- ReflectedListProperty.java 26 Nov 2002 03:35:45 -0000 1.4 *************** *** 1,39 **** ! package cirrus.hibernate.tools.reflect; ! ! /** ! * @version 1.x ! * @author <a href="mailto:dou...@al...">e</a> ! */ ! class ReflectedListProperty extends ReflectedProperty { ! ! /** ! * Constructor for ReflectedListProperty. ! * @param name ! * @param type ! */ ! ReflectedListProperty(String name, Class type, MappingByReflection map ) { ! super(name, type, map, "list"); ! } ! ! protected void getXMLinArray( int level, StringBuffer buf ) { ! this.buf = buf; ! emitSubCollectionStr(level, name); ! } ! ! protected void getXMLinComposite( int level, StringBuffer buf ) { ! this.buf = buf; ! emitCollectionInCompositeStr(level); ! } ! ! protected void getXML( int level, StringBuffer buf ) { ! this.buf = buf; ! if( comment != null ) { ! emitPCommentStr(level, comment, name, type.getName()); ! } ! emitCollectionStrL(level, "list", name, true); ! emitCollectionStrK(level+1, "uid"); ! emitCollectionStrI(level+1, "i"); ! emitCollectionStrE(level+1, "elm"); ! emitCollectionStrR(level, "list"); ! } ! } --- 1,39 ---- ! package cirrus.hibernate.tools.reflect; ! ! /** ! * @version 1.x ! * @author <a href="mailto:dou...@al...">e</a> ! */ ! class ReflectedListProperty extends ReflectedProperty { ! ! /** ! * Constructor for ReflectedListProperty. ! * @param name ! * @param type ! */ ! ReflectedListProperty(String name, Class type, MappingByReflection map ) { ! super(name, type, map, "list"); ! } ! ! protected void getXMLinArray( int level, StringBuffer buf ) { ! this.buf = buf; ! emitSubCollectionStr(level, name); ! } ! ! protected void getXMLinComposite( int level, StringBuffer buf ) { ! this.buf = buf; ! emitCollectionInCompositeStr(level); ! } ! ! protected void getXML( int level, StringBuffer buf ) { ! this.buf = buf; ! if( comment != null ) { ! emitPCommentStr(level, comment, name, type.getName()); ! } ! emitCollectionStrL(level, "list", name, true); ! emitCollectionStrK(level+1, "uid"); ! emitCollectionStrI(level+1, "i"); ! emitCollectionStrE(level+1, "elm"); ! emitCollectionStrR(level, "list"); ! } ! } Index: ReflectedMapProperty.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/reflect/ReflectedMapProperty.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** ReflectedMapProperty.java 19 Sep 2002 08:26:45 -0000 1.4 --- ReflectedMapProperty.java 26 Nov 2002 03:35:45 -0000 1.5 *************** *** 1,39 **** ! package cirrus.hibernate.tools.reflect; ! ! /** ! * @version 1.x ! * @author <a href="mailto:dou...@al...">e</a> ! */ ! class ReflectedMapProperty extends ReflectedProperty { ! ! /** ! * Constructor for ReflectedMapProperty. ! * @param name ! * @param type ! */ ! ReflectedMapProperty(String name, Class type, MappingByReflection map) { ! super(name, type, map, "map"); ! } ! ! protected void getXMLinArray( int level, StringBuffer buf ) { ! this.buf = buf; ! emitSubCollectionStr(level, name); ! } ! ! protected void getXMLinComposite( int level, StringBuffer buf ) { ! this.buf = buf; ! emitCollectionInCompositeStr(level); ! } ! ! protected void getXML( int level, StringBuffer buf ) { ! this.buf = buf; ! if( comment != null ) { ! emitPCommentStr(level, comment, name, type.getName()); ! } ! emitCollectionStrL(level, "map", name, true); ! emitCollectionStrK(level+1, "uid"); ! emitCollectionStrIT(level+1, "key", "string"); ! emitCollectionStrE(level+1, "elm"); ! emitCollectionStrR(level, "map"); ! } ! } --- 1,39 ---- ! package cirrus.hibernate.tools.reflect; ! ! /** ! * @version 1.x ! * @author <a href="mailto:dou...@al...">e</a> ! */ ! class ReflectedMapProperty extends ReflectedProperty { ! ! /** ! * Constructor for ReflectedMapProperty. ! * @param name ! * @param type ! */ ! ReflectedMapProperty(String name, Class type, MappingByReflection map) { ! super(name, type, map, "map"); ! } ! ! protected void getXMLinArray( int level, StringBuffer buf ) { ! this.buf = buf; ! emitSubCollectionStr(level, name); ! } ! ! protected void getXMLinComposite( int level, StringBuffer buf ) { ! this.buf = buf; ! emitCollectionInCompositeStr(level); ! } ! ! protected void getXML( int level, StringBuffer buf ) { ! this.buf = buf; ! if( comment != null ) { ! emitPCommentStr(level, comment, name, type.getName()); ! } ! emitCollectionStrL(level, "map", name, true); ! emitCollectionStrK(level+1, "uid"); ! emitCollectionStrIT(level+1, "key", "string"); ! emitCollectionStrE(level+1, "elm"); ! emitCollectionStrR(level, "map"); ! } ! } Index: ReflectedProperty.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/tools/reflect/ReflectedProperty.java,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** ReflectedProperty.java 21 Nov 2002 09:03:14 -0000 1.16 --- ReflectedProperty.java 26 Nov 2002 03:35:45 -0000 1.17 *************** *** 1,264 **** ! package cirrus.hibernate.tools.reflect; ! ! import java.math.BigDecimal; ! import java.sql.Time; ! import java.sql.Timestamp; ! import java.util.Calendar; ! import java.util.HashMap; ! import java.util.Locale; ! import java.util.TimeZone; ! ! import cirrus.hibernate.Hibernate; ! import cirrus.hibernate.type.CurrencyType; ! ! /** a ReflectedProperty is made by an instance of MappingByReflection ! * to model properties of Reflected classes;<br> ! * basic properties are handled here: primitive, enum, custom;<br> ! * subclasses handle more involved cases ! * ! * @version 1.x ! * @author <a href="mailto:dou...@al...">e</a> ! */ ! public class ReflectedProperty { ! ! protected boolean isUid; ! protected Class type; ! protected String tynm; ! protected String name; ! protected String comment; ! protected StringBuffer buf; ! protected MappingByReflection map; ! ! private static HashMap shortTypeNames = new HashMap(); ! static { ! shortTypeNames.put( Long.class.getName(), Hibernate.LONG.getName() ); ! shortTypeNames.put( Integer.class.getName(), Hibernate.INTEGER.getName() ); ! shortTypeNames.put( Boolean.class.getName(), Hibernate.BOOLEAN.getName() ); ! shortTypeNames.put( Short.class.getName(), Hibernate.SHORT.getName() ); ! shortTypeNames.put( Byte.class.getName(), Hibernate.BYTE.getName()); ! shortTypeNames.put( Float.class.getName(), Hibernate.FLOAT.getName() ); ! shortTypeNames.put( Double.class.getName(), Hibernate.DOUBLE.getName() ); ! shortTypeNames.put( Character.class.getName(), Hibernate.CHARACTER.getName() ); ! shortTypeNames.put( String.class.getName(), Hibernate.STRING.getName() ); ! shortTypeNames.put( Locale.class.getName(), Hibernate.LOCALE.getName() ); ! shortTypeNames.put( Calendar.class.getName(), Hibernate.CALENDAR.getName() ); ! if ( CurrencyType.CURRENCY_CLASS!=null) shortTypeNames.put( CurrencyType.CURRENCY_CLASS.getName(), Hibernate.CURRENCY.getName() ); ! shortTypeNames.put( TimeZone.class.getName(), Hibernate.TIMEZONE.getName() ); ! shortTypeNames.put( BigDecimal.class.getName(), Hibernate.BIG_DECIMAL.getName() ); ! shortTypeNames.put( Class.class.getName(), Hibernate.CLASS.getName() ); ! shortTypeNames.put( Object.class.getName(), Hibernate.OBJECT.getName() ); ! shortTypeNames.put( Time.class.getName(), Hibernate.TIME.getName() ); ! shortTypeNames.put( Timestamp.class.getName(), Hibernate.TIMESTAMP.getName() ); ! shortTypeNames.put( java.util.Date.class.getName(), Hibernate.TIMESTAMP.getName() ); ! shortTypeNames.put( java.sql.Date.class.getName(), Hibernate.DATE.getName() ); ! shortTypeNames.put( byte[].class.getName(), Hibernate.BINARY.getName() ); // not shorter, but nicer ! } ! ! protected static String abbreviateType(String typeName) { ! String abbreviated = (String) shortTypeNames.get(typeName); ! return (abbreviated==null) ? typeName : abbreviated; ! } ! ! ReflectedProperty( String name, Class type ! , MappingByReflection map, String comment ) { ! this.name = name; ! this.type = type; ! this.tynm = abbreviateType( type.getName() ); // default; may be changed later ! this.comment = comment; ! this.map = map; ! this.isUid = false; ! } ! ! /** used by gui */ ! public String getName() { ! return name; ! } ! ! /** used by gui */ ! public String getTypeName() { ! return tynm; ! } ! ! protected boolean isUidOK() { ! /*if( long.class == type ) return true; ! if( Long.class == type ) return true; ! if( String.class == type ) return true; ! // more? ! return false;*/ ! return ( !type.isArray() ); // since arrays never implement equals properly ! } ! ! protected void emitIntUidStr(int level) { ! map.emitPrefix(level); ! buf.append("<id name=\"") ! .append(name) ! .append("\" type=\"") ! .append(tynm) // or just use "long" ? ! .append("\" column=\"") ! .append(map.columnNameFor(name)) ! .append("\">\n"); ! map.emitPrefix(level+1); ! buf.append("<generator class=\"hilo.long\"/>\n"); ! map.emitPrefix(level); ! buf.append("</id>\n"); ! } ! ! protected void emitStrUidStr(int level) { ! map.emitPrefix(level); ! buf.append("<id name=\"" ) ! .append(name) ! .append("\" type=\"") ! .append(tynm) // or just use "string" ? ! .append("\">\n"); ! map.emitPrefix(level+1); ! buf.append("<column name=\"") ! .append(map.columnNameFor(name)) ! .append("\" length=\"16\"/>\n"); ! map.emitPrefix(level+1); ! buf.append("<generator class=\"hilo.hex\"/>\n"); ! map.emitPrefix(level); ! buf.append("</id>\n"); ! } ! ! protected void emitPropertyStr(int level) { ! map.emitPrefix(level); ! buf.append("<property name=\"" ) ! .append(name) ! .append("\" column=\"") ! .append(map.columnNameFor(name)) ! .append("\" type=\"") ! .append(tynm) ! .append("\"/>\n"); ! } ! ! protected void emitPCommentStr( int level, String comment, String name, String tynm ) { ! /*map.emitPrefix(level); ! buf.append("<!-- ") ! .append(comment) ! .append(" name=\"") ! .append(name) ! .append("\" type=\"") ! .append(tynm) ! .append("\" -->\n");*/ ! } ! ! protected void emitCollectionStrL( int level, String collection ! , String name, boolean lazy ) { ! map.emitPrefix(level); ! buf.append("<") ! .append(collection) ! .append(" role=\"" ) ! .append(name) ! .append("\" table=\"") ! .append(map.tableNameFor(name)); ! if( lazy ) { ! buf.append("\" lazy=\"true"); ! } ! buf.append("\">\n"); ! } ! protected void emitCollectionStrK( int level, String key ) { ! map.emitPrefix(level); ! buf.append("<key column=\"") ! .append(map.columnNameFor(key)) ! .append("\"/>\n"); ! } ! protected void emitCollectionStrI( int level, String idx ) { ! map.emitPrefix(level); ! buf.append("<index column=\"") ! .append(map.columnNameFor(idx)) ! .append("\"/>\n"); ! } ! protected void emitCollectionStrIT( int level, String idx, String type ) { ! map.emitPrefix(level); ! buf.append("<index column=\"") ! .append(map.columnNameFor(idx)) ! .append("\" type =\"") ! .append(type) ! .append("\"/>\n"); ! } ! protected void emitCollectionStrE( int level, String elm ) { ! map.emitPrefix(level); ! buf.append("<element column=\"") ! .append(map.columnNameFor(elm)) ! .append("\"/>\n"); ! } ! protected void emitCollectionStrET( int level, String elm, String type ) { ! map.emitPrefix(level); ! buf.append("<element column=\"") ! .append(map.columnNameFor(elm)) ! .append("\" type =\"") ! .append(type) ! .append("\"/>\n"); ! } ! protected void emitCollectionStrR( int level, String collection ) { ! map.emitPrefix(level); ! buf.append("</") ! .append(collection) ! .append(">\n"); ! } ! //protected void emitCollectionStrLazy(int level) { ! // map.emitPrefix(level); ! // buf.append("<lazy=\"true\">\n"); ! //} ! ! //<!ELEMENT subcollection (column*)> ! // <!ATTLIST subcollection column CDATA #IMPLIED> ! // <!ATTLIST subcollection role CDATA #REQUIRED> ! // <!ATTLIST subcollection length CDATA #IMPLIED> ! // <!ATTLIST subcollection not-null CDATA #IMPLIED> ! ! protected void emitSubCollectionStr( int level, String name ) { ! map.emitPrefix(level); ! buf.append("<subcollection role=\"" ) ! .append(name) ! .append("\" column=\"") ! .append(map.columnNameFor(name)) ! .append("\">\n"); ! } ! ! //<!ELEMENT collection (column*)> ! // <!ATTLIST collection name CDATA #REQUIRED> ! // <!ATTLIST collection role CDATA #IMPLIED> ! // <!ATTLIST collection column CDATA #IMPLIED> ! // <!ATTLIST collection length CDATA #IMPLIED> ! // <!ATTLIST collection not-null CDATA #IMPLIED> ! ! protected void emitCollectionInCompositeStr(int level) { ! map.emitPrefix(level); ! buf.append("<collection name=\"" ) ! .append(name) ! .append("\" column=\"") ! .append(map.columnNameFor(name)) ! .append("\">\n"); ! } ! ! protected void getUidXML(int level, StringBuffer buf) { ! this.buf = buf; ! isUid = true; // to prevent being emitted twice ! if( type == String.class ) { ! emitStrUidStr(level); ! } ! else { ! emitIntUidStr(level); ! } ! } ! ! protected void getXMLinComposite( int level, StringBuffer buf ) { ! this.buf = buf; ! // basic types are OK in composite-elements ! emitPropertyStr(level); ! } ! ! protected void getXMLinArray( int level, StringBuffer buf ) { ! this.buf = buf; ! emitCollectionStrET(level, "elm", tynm); ! } ! ! protected void getXML( int level, StringBuffer buf ) { ! this.buf = buf; ! if( comment != null ) { ! emitPCommentStr(level, comment, name, type.getName()); ! } ! emitPropertyStr(level); ! } ! } --- 1,264 ---- ! package cirrus.hibernate.tools.reflect; ! ! import java.math.BigDecimal; ! import java.sql.Time; ! import java.sql.Timestamp; ! import java.util.Calendar; ! import java.util.HashMap; ! import java.util.Locale; ! import java.util.TimeZone; ! ! import cirrus.hibernate.Hibernate; ! import cirrus.hibernate.type.CurrencyType; ! ! /** a ReflectedProperty is made by an instance of MappingByReflection ! * to model properties of Reflected classes;<br> ! * basic properties are handled here: primitive, enum, custom;<br> ! * subclasses handle more involved cases ! * ! * @version 1.x ! * @author <a href="mailto:dou...@al...">e</a> ! */ ! public class ReflectedProperty { ! ! protected boolean isUid; ! protected Class type; ! protected String tynm; ! protected String name; ! protected String comment; ! protected StringBuffer buf; ! protected MappingByReflection map; ! ! private static HashMap shortTypeNames = new HashMap(); ! static { ! shortTypeNames.put( Long.class.getName(), Hibernate.LONG.getName() ); ! shortTypeNames.put( Integer.class.getName(), Hibernate.INTEGER.getName() ); ! shortTypeNames.put( Boolean.class.getName(), Hibernate.BOOLEAN.getName() ); ! shortTypeNames.put( Short.class.getName(), Hibernate.SHORT.getName() ); ! shortTypeNames.put( Byte.class.getName(), Hibernate.BYTE.getName()); ! shortTypeNames.put( Float.class.getName(), Hibernate.FLOAT.getName() ); ! shortTypeNames.put( Double.class.getName(), Hibernate.DOUBLE.getName() ); ! shortTypeNames.put( Character.class.getName(), Hibernate.CHARACTER.getName() ); ! shortTypeNames.put( String.class.getName(), Hibernate.STRING.getName() ); ! shortTypeNames.put( Locale.class.getName(), Hibernate.LOCALE.getName() ); ! shortTypeNames.put( Calendar.class.getName(), Hibernate.CALENDAR.getName() ); ! if ( CurrencyType.CURRENCY_CLASS!=null) shortTypeNames.put( CurrencyType.CURRENCY_CLASS.getName(), Hibernate.CURRENCY.getName() ); ! shortTypeNames.put( TimeZone.class.getName(), Hibernate.TIMEZONE.getName() ); ! shortTypeNames.put( BigDecimal.class.getName(), Hibernate.BIG_DECIMAL.getName() ); ! shortTypeNames.put( Class.class.getName(), Hibernate.CLASS.getName() ); ! shortTypeNames.put( Object.class.getName(), Hibernate.OBJECT.getName() ); ! shortTypeNames.put( Time.class.getName(), Hibernate.TIME.getName() ); ! shortTypeNames.put( Timestamp.class.getName(), Hibernate.TIMESTAMP.getName() ); ! shortTypeNames.put( java.util.Date.class.getName(), Hibernate.TIMESTAMP.getName() ); ! shortTypeNames.put( java.sql.Date.class.getName(), Hibernate.DATE.getName() ); ! shortTypeNames.put( byte[].class.getName(), Hibernate.BINARY.getName() ); // not shorter, but nicer ! } ! ! protected static String abbreviateType(String typeName) { ! String abbreviated = (String) shortTypeNames.get(typeName); ! return (abbreviated==null) ? typeName : abbreviated; ! } ! ! ReflectedProperty( String name, Class type ! , MappingByReflection map, String comment ) { ! this.name = name; ! this.type = type; ! this.tynm = abbreviateType( type.getName() ); // default; may be changed later ! this.comment = comment; ! this.map = map; ! this.isUid = false; ! } ! ! /** used by gui */ ! public String getName() { ! return name; ! } ! ! /** used by gui */ ! public String getTypeName() { ! return tynm; ! } ! ! protected boolean isUidOK() { ! /*if( long.class == type ) return true; ! if( Long.class == type ) return true; ! if( String.class == type ) return true; ! // more? ! return false;*/ ! return ( !type.isArray() ); // since arrays never implement equals properly ! } ! ! protected void emitIntUidStr(int level... [truncated message content] |