📄 classmapping.java
字号:
//$Id: ClassMapping.java,v 1.9 2003/09/27 11:17:10 maxcsaucdk Exp $package net.sf.hibernate.tool.hbm2java;import java.util.*;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.TreeSet;import net.sf.hibernate.CompositeUserType;import net.sf.hibernate.UserType;import net.sf.hibernate.type.TypeFactory;import net.sf.hibernate.type.PrimitiveType;import net.sf.hibernate.type.Type;import net.sf.hibernate.util.ReflectHelper;import net.sf.hibernate.util.StringHelper;import org.apache.commons.collections.MultiMap;import org.apache.commons.lang.StringUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.jdom.Attribute;import org.jdom.Element;public class ClassMapping { static private Log log = LogFactory.getLog(ClassMapping.class); private ClassName name = null; private ClassName generatedName = null; private String superClass = null; private ClassMapping superClassMapping = null; private String proxyClass = null; private List fields = new ArrayList(); private TreeSet imports = new TreeSet(); private List subclasses = new ArrayList(); private static final Map components = new HashMap(); private boolean mustImplementEquals = false; private MultiMap metaattribs; private boolean shouldBeAbstract = false; public ClassMapping(ClassName superClass, ClassMapping superClassMapping, Element classElement, MultiMap inheritedMeta) { this(superClass, classElement, inheritedMeta); 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, MultiMap inheritedMeta) { initWith(superClass, classElement, false, inheritedMeta); } public ClassMapping(Element classElement, MultiMap inheritedMeta) { initWith(null, classElement, false, inheritedMeta); } public ClassMapping(Element classElement, boolean component, MultiMap inheritedMeta) { initWith(null, classElement, component, inheritedMeta); }protected void initWith(ClassName superClass, Element classElement, boolean component, MultiMap inheritedMeta) { String fullyQualifiedName = classElement.getAttributeValue(component?"class":"name"); log.debug("Processing mapping for class: " + fullyQualifiedName); setMetaAttribs(MetaAttributeHelper.loadAndMergeMetaMap(classElement, inheritedMeta)); // class & package names name = new ClassName(); name.setFullyQualifiedName(fullyQualifiedName); if(getMeta("generated-class")!=null) { generatedName = new ClassName(); generatedName.setFullyQualifiedName(getMetaAsString("generated-class").trim()); shouldBeAbstract = true; log.warn("Generating " + generatedName + " instead of " + name); } else { generatedName = name; } if(superClass!=null) { this.superClass = superClass.getName(); addImport(superClass); // can only be done AFTER this class gets its own name. } // get the properties defined for this class List propertyList = new ArrayList(); propertyList.addAll( classElement.getChildren("property") ); propertyList.addAll( classElement.getChildren("version") ); propertyList.addAll( classElement.getChildren("timestamp") ); propertyList.addAll( classElement.getChildren("key-property") ); propertyList.addAll( classElement.getChildren("any")); // get all many-to-one associations defined for the class List manyToOneList = new ArrayList(); manyToOneList.addAll( classElement.getChildren("many-to-one") ); manyToOneList.addAll( classElement.getChildren("key-many-to-one") ); Attribute att = classElement.getAttribute("proxy"); if (att!=null) proxyClass = att.getValue(); Element id = classElement.getChild("id"); if (id != null) { propertyList.add(0, id); implementEquals(); } // composite id Element cmpid = classElement.getChild("composite-id"); if (cmpid != null) { implementEquals(); String cmpname = cmpid.getAttributeValue("name"); String cmpclass = cmpid.getAttributeValue("class"); if ( cmpclass==null || cmpclass.equals(StringHelper.EMPTY_STRING) ) { //Embedded composite id //implementEquals(); propertyList.addAll(0, cmpid.getChildren("key-property") ); manyToOneList.addAll(0, cmpid.getChildren("key-many-to-one") ); } else { //Composite id class ClassMapping mapping = new ClassMapping(cmpid, true, metaattribs); MultiMap metaForCompositeid = MetaAttributeHelper.loadAndMergeMetaMap(cmpid, metaattribs); mapping.implementEquals(); ClassName classType = new ClassName(); classType.setFullyQualifiedName(cmpclass); // add an import and field for this property addImport(classType); Field cmpidfield = new Field(cmpname, classType, false, true, false, metaForCompositeid); fields.add(cmpidfield); components.put( mapping.getCanonicalName(), mapping); } } // derive the class imports and fields from the properties for (Iterator properties = propertyList.iterator(); properties.hasNext();) { Element property = (Element) properties.next(); MultiMap metaForProperty = MetaAttributeHelper.loadAndMergeMetaMap(property, metaattribs); String name = property.getAttributeValue("name"); if ( name == null || name.trim().equals(StringHelper.EMPTY_STRING) ) { continue; //since an id doesn't necessarily need a name } // ensure that the type is specified String type = property.getAttributeValue("type"); if (type == null && cmpid != null) { // for composite-keys type = property.getAttributeValue("class"); } if("timestamp".equals(property.getName())){ type = "java.util.Date"; } if("any".equals(property.getName())) { type = "java.lang.Object"; } if ( type == null || type.trim().equals(StringHelper.EMPTY_STRING) ) { log.warn("property \"" + name + "\" in class " + getName() + " is missing a type attribute"); continue; } // handle in a different way id and properties... // ids may be generated and may need to be of object type in order to support // the unsaved-value "null" value. // Properties may be nullable (ids may not) if (property == id) { Element generator = property.getChild("generator"); String unsavedValue = property.getAttributeValue("unsaved-value"); boolean needObject = ( unsavedValue != null && unsavedValue.equals("null") ); boolean generated = !generator.getAttributeValue("class").equals("assigned"); ClassName rtype = getFieldType(type, needObject); addImport(rtype); Field idField = new Field( name, rtype, false, true, generated, metaForProperty); fields.add(idField); } else { String notnull = property.getAttributeValue("not-null"); // if not-null property is missing lets see if it has been // defined at column level if(notnull == null) { Element column = property.getChild("column"); if(column != null) notnull = column.getAttributeValue("not-null"); } boolean nullable = ( notnull == null || notnull.equals("false") ); boolean key = property.getName().startsWith("key-"); //a composite id property ClassName t = getFieldType(type); addImport(t); Field stdField =new Field(name, t, nullable && !key, key, false, metaForProperty); fields.add(stdField ); } } // one to ones List onetooneList = classElement.getChildren("one-to-one"); for ( Iterator onetoones = onetooneList.iterator(); onetoones.hasNext(); ) { Element onetoone = (Element) onetoones.next(); MultiMap metaForOneToOne = MetaAttributeHelper.loadAndMergeMetaMap(onetoone,metaattribs); String name = onetoone.getAttributeValue("name"); // ensure that the class is specified String clazz = onetoone.getAttributeValue("class"); if( StringUtils.isEmpty(clazz) ) { log.warn("one-to-one \"" + name + "\" in class " + getName() + " is missing a class attribute"); continue; } ClassName cn = getFieldType(clazz); addImport(cn); Field fm = new Field(name, cn, true, metaForOneToOne); fields.add(fm); } // many to ones - TODO: consolidate with code above for ( Iterator manytoOnes = manyToOneList.iterator(); manytoOnes.hasNext(); ) { Element manyToOne = (Element) manytoOnes.next(); MultiMap metaForManyToOne = MetaAttributeHelper.loadAndMergeMetaMap(manyToOne,metaattribs); String name = manyToOne.getAttributeValue("name"); // ensure that the type is specified String type = manyToOne.getAttributeValue("class"); if ( StringUtils.isEmpty(type) ) { log.warn("many-to-one \"" + name + "\" in class " + getName() + " is missing a class attribute"); continue; } ClassName classType = new ClassName(); classType.setFullyQualifiedName(type); // is it nullable? String notnull = manyToOne.getAttributeValue("not-null"); boolean nullable = ( notnull == null || notnull.equals("false") ); boolean key = manyToOne.getName().startsWith("key-"); //a composite id property // add an import and field for this property addImport(classType); Field f = new Field( name, classType, nullable && !key, key, false, metaForManyToOne); fields.add(f); } // collections doCollections(classElement, "list", "java.util.List", "java.util.ArrayList", metaattribs); doCollections(classElement, "map", "java.util.Map", "java.util.HashMap", metaattribs); doCollections(classElement, "set", "java.util.Set", "java.util.HashSet", metaattribs); doCollections(classElement, "bag", System.getProperty("hbm2java.bag.interface","java.util.List"), "java.util.ArrayList", metaattribs); doArrays(classElement, "array", metaattribs); doArrays(classElement, "primitive-array", metaattribs); //components for ( Iterator iter = classElement.getChildren("component").iterator(); iter.hasNext(); ) { Element cmpe = (Element) iter.next(); MultiMap metaForComponent = MetaAttributeHelper.loadAndMergeMetaMap(cmpe, metaattribs); String cmpname = cmpe.getAttributeValue("name"); String cmpclass = cmpe.getAttributeValue("class"); if ( cmpclass==null || cmpclass.equals(StringHelper.EMPTY_STRING) ) { log.warn("component \"" + cmpname + "\" in class " + getName() + " does not specify a class"); continue; } ClassMapping mapping = new ClassMapping(cmpe, true, metaattribs); ClassName classType = new ClassName(); classType.setFullyQualifiedName(cmpclass); // add an import and field for this property addImport(classType); Field ff = new Field(cmpname, classType, false, metaForComponent); fields.add(ff); 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, metaattribs); addSubClass(subclassMapping); } for ( Iterator iter = classElement.getChildren("joined-subclass").iterator(); iter.hasNext(); ) { Element subclass = (Element) iter.next(); ClassMapping subclassMapping = new ClassMapping(name, this, subclass, metaattribs); addSubClass(subclassMapping); } validateMetaAttribs();}/** * Method setMetaAttribs. * @param multiMap */private void setMetaAttribs(MultiMap multiMap) { metaattribs = multiMap;} public void implementEquals() { mustImplementEquals = true; } public boolean mustImplementEquals() { return (!isInterface()) && mustImplementEquals; } public List getFields() { return fields; } public TreeSet getImports() { return imports; } public String getCanonicalName() { return name.getFullyQualifiedName(); } public String getName() { return name.getName(); } public ClassName getClassName() { return name; } public String getGeneratedName() { return generatedName.getName(); } public String getProxy() { return proxyClass; } public String getPackageName() { return name.getPackageName(); } public String getGeneratedPackageName() { return generatedName.getPackageName(); } public List getSubclasses() { return subclasses; } public String getSuperClass() { return superClass; } // We need a minimal constructor only if it's different from
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -