📄 mapgenerator.java
字号:
* @param uid the new candidate UID */ public void addUID( String uid ) { String[] uida = new String[niceKeys.length + 1]; for( int i=0; i<niceKeys.length; i++ ) { uida[i+1] = niceKeys[i]; } uida[0] = uid; niceKeys = uida; // debug for( int i=0; i<niceKeys.length; i++ ) { System.out.print(" "); System.out.print(niceKeys[i]); } } /** used by gui */ public ReflectedClass[] getRoots() { ReflectedClass[] v = new ReflectedClass[roots.size()]; roots.copyInto(v); return v; } private ReflectedClass reallyAdd( Class clazz, boolean verbose ) { ReflectedClass rc = (ReflectedClass) rClasses.get(clazz); Class superclass = clazz.getSuperclass(); if( rc == null ) { // not already added rc = new ReflectedClass( this, clazz ); } else if( rc.isPersistent() ) { if( verbose ) { buf.append("<!-- ") .append(clazz.getName()) .append(" already added -->\n"); } return rc; } rc.setPersistent( true ); if( rc.getUidProp( niceKeys ) != null ) { // it's a root // add properties from superclasses rc.addSuperclassProps(); roots.add( rc ); } else if ( // added by Eric Everman on 5-16-2002 superclass!=null && abstractClasses.containsKey( superclass.getName() ) ) { //The user has requested that class diving go no farther by //marking the next class up the chain as abstract. rc.addSuperclassProps(); //add superclass props if( rc.getUidProp( niceKeys ) != null ) { //Thank goodness, its persistable roots.add( rc ); } else { //User specified an abstract 'root' class which is not persistable rc.setPersistent( false ); return null; //generic can't add message generated by addClass } } else { // add superclasses 'til root is found if( superclass == null ) { // can't add to roots because didn't find UID // rc isn't persistent after all rc.setPersistent( false ); // report!? (addClass will do it when it sees null) return null; } else { ReflectedClass sup = reallyAdd( superclass, false ); // recurse if( sup == null ) { // rc isn't persistent after all rc.setPersistent( false ); return null; } sup.addReflectedClass( rc ); } } return rc; } class PEntity { // we don't make these yet // maybe someday toplevel collections will be supported void getXML( int level, boolean isRoot ) { } } /** used to make unique table and column names * @param best is the desired name * @param h is the uniqifying hashtable * @return the unique name */ protected String nextName( String best, Hashtable h ) { Integer seen = (Integer )h.get( best ); if( seen == null ) { // all set h.put( best, new Integer(1) ); return best; } h.put( best, new Integer(seen.intValue() + 1) ); return (best + "_" + seen); } protected String tableNameFor( String name ) { String best = StringHelper.unqualify( name ); // TO DO check for illegal names? // check for duplicate names... return nextName( best, usedTableNames ); } protected String columnNameFor( String best ) { // TO DO check for illegal names? // check for duplicate names... return nextName( best, usedColumnNames ); } /** adds spaces to the front of lines in buf for indentation * @param n indentation level */ protected void emitPrefix( int n ) { int e = n * 1; // configurable? if( e > prefix.length ) e = prefix.length; buf.append( prefix, 0, e ); } /** after all classes are added * @return the XML */ public String getXML() { buf.append("<hibernate-mapping>\n"); // xml the common entities, e.g., keyed collections int eln = entities.size(); for( int i=0; i<eln; i++ ) { PEntity pet = (PEntity )roots.get(i); usedColumnNames = new Hashtable(); pet.getXML( 1, true ); } // xml the classes int len = roots.size(); for( int i=0; i<len; i++ ) { ReflectedClass rc = (ReflectedClass )roots.get(i); usedColumnNames = new Hashtable(); rc.getXML(1); } buf.append("</hibernate-mapping>\n"); return buf.toString(); } private Class checkClassNamed( String className, boolean v ) { try { Class clazz = classLoader.loadClass( className ); return checkClass( clazz, className, v ); } catch( Exception e ) { if(v) buf.append("<!-- Class ") .append(className) .append(" gave exception ") .append(e) .append(" -->\n"); return null; } } /** this is the factory to make a ReflectedProperty * <br>using this factory will insure that the property * types are inferred consistently * * @param name the property name * @param cls the property class * @return the new ReflectedProperty */ protected ReflectedProperty makeProperty( String name, Class cls ) { String tynm = cls.getName(); Type htyp = TypeFactory.basic(tynm); if ( htyp != null ) { return new ReflectedProperty( name, cls, this, "basic" ); } else if ( cls.isArray() ) { return new ReflectedArrayProperty( name, cls, this ); } /*else if ( Type.class.isAssignableFrom(cls) ) { return new ReflectedProperty( name, cls, this, "custom" ); }*/ else if ( PersistentEnum.class.isAssignableFrom(cls) ) { return new ReflectedProperty( name, cls, this, "enum" ); } // differs from Hibernate.auto... else if ( java.util.List.class.isAssignableFrom(cls) ) { return new ReflectedListProperty( name, cls, this ); } else if ( java.util.Map.class.isAssignableFrom(cls) ) { return new ReflectedMapProperty( name, cls, this ); } else if ( java.util.Set.class.isAssignableFrom(cls) ) { return new ReflectedSetProperty( name, cls, this ); } else if ( java.util.Collection.class.isAssignableFrom(cls) ) { return new ReflectedCollectionProperty( name, cls, this ); } // leave the hard stuff for ReflectedComponent... else { Class comp = checkComponent( cls, tynm, true ); if( comp == null ) { //return null; // hibernate can't handle return new ReflectedProperty( name, cls, this, "custom" ); } ReflectedClass rc = (ReflectedClass )rClasses.get(comp); if( rc == null ) { // not already processed (avoid infinite regress!) rc = new ReflectedClass( this, comp ); } return new ReflectedComponent( name, cls, this, rc ); } } /** verify that a class is <b>hibernate-persistable</b> * * @param clazz the class to ckeck * @param className the name of the class for error reporting * @param v verbose - should be true for supplied classes, * but false for chased superclasses * @return the class clazz if it is hibernate-persistable, else null */ protected Class checkClass( Class clazz, String className, boolean v ) { return checkClCoGuts( clazz, className, v, "<!-- Class " ); } /** verify that a class is <b>hibernate-persistable</b> as a component * * @param clazz the class to ckeck * @param className the name of the class for error reporting * @param v verbose - should probably always be true, * but can be false to ignore the property's status * @return the class clazz if it is hibernate-persistable as a component, else null */ protected Class checkComponent( Class clazz, String className, boolean v ) { return checkClCoGuts( clazz, className, v, "<!-- Component " ); } private Class checkClCoGuts( Class clazz, String className, boolean v, String c ) { if( clazz.isPrimitive() ) { if(v) buf.append(c).append(className).append(" is a primitive! -->\n"); return null; } if( clazz.isArray() ) { if(v) buf.append(c).append(className).append(" is an array! -->\n"); return null; } if( clazz.isInterface() ) { if(v) buf.append(c).append(className).append(" is an interface! -->\n"); return null; } if( clazz.getDeclaringClass() != null ) { if(v) buf.append(c).append(className).append(" is a nested class! -->\n"); return null; } /* per Gavin, abstract is OK int clsmods = clazz.getModifiers(); if( Modifier.isAbstract( clsmods ) ) { if(v) buf.append(c).append(className).append(" is abstract! -->\n"); return null; } */ // must have a default constructor Constructor z; try { // try the public constructor z = clazz.getConstructor(new Class[0]); } catch( NoSuchMethodException e ) { z = null; } catch( SecurityException e ) { // ugh if(v) buf.append(c) .append(className) .append(" cannot be reflected due to a SecurityException! -->\n"); return null; } // no public default constructor, try harder... if( z == null ) { try { Constructor[] cs = clazz.getDeclaredConstructors(); for ( int i=0; i<cs.length; i++ ) { if ( cs[i].getParameterTypes().length==0 ) { z = cs[i]; break; } } } catch( SecurityException e ) { // ugh if(v) buf.append(c) .append(className) .append(" cannot be reflected due to a SecurityException! -->\n"); return null; } } if( z == null ) { if(v) buf.append(c) .append(className) .append(" has no 0-arg constructor! -->\n"); return null; } // the class is OK to perisist return clazz; } public void setClassLoader(ClassLoader classLoader) { this.classLoader = classLoader; }}/* 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 * ... * */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -