📄 classmapping.java
字号:
// the full constructor or the no-arg constructor. // A minimal construtor is one that lets // you specify only the required fields. public boolean needsMinimalConstructor() { return (getAllFieldsForFullConstructor().size()!=getAllFieldsForMinimalConstructor().size()) && getAllFieldsForMinimalConstructor().size()>0; } 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 getAllFields() { List result = new ArrayList(); if(getSuperClassMapping()!=null) { result.addAll(getSuperClassMapping().getAllFields()); } else { result.addAll(getFields()); } 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; } public void addImport(ClassName className) { // if the package is java.lang or our own package don't add if ( !className.inJavaLang() && !className.inSamePackage(generatedName) && !className.isPrimitive()) { if(className.isArray()) { imports.add( className.getFullyQualifiedName().substring(0,className.getFullyQualifiedName().length()-2) ); // remove [] } else { imports.add( className.getFullyQualifiedName() ); } } } public void addImport(String className) { ClassName cn = new ClassName(); cn.setFullyQualifiedName(className); addImport(cn); } public static Iterator getComponents() { return components.values().iterator(); } private void doCollections(Element classElement, String xmlName, String interfaceClass, String implementingClass, MultiMap inheritedMeta) { for (Iterator collections = classElement.getChildren(xmlName).iterator(); collections.hasNext();) { Element collection = (Element) collections.next(); MultiMap metaForCollection = MetaAttributeHelper.loadAndMergeMetaMap(collection, inheritedMeta); String name = collection.getAttributeValue("name"); // Small hack to switch over to sortedSet/sortedMap if sort is specified. (that is sort != unsorted) if (!"unsorted".equals(collection.getAttributeValue("sort"))) { if("map".equals(xmlName)) { interfaceClass = SortedMap.class.getName(); implementingClass = TreeMap.class.getName(); } else if("set".equals(xmlName)) { interfaceClass = SortedSet.class.getName(); implementingClass = TreeSet.class.getName(); } } ClassName interfaceClassName = new ClassName(); ClassName implementingClassName = new ClassName(); interfaceClassName.setFullyQualifiedName(interfaceClass); implementingClassName.setFullyQualifiedName(implementingClass); // 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); ClassName foreignClass = null; Set foreignKeys = null; // Collect bidirectional data if(collection.getChildren("one-to-many").size()!=0) { foreignClass = new ClassName(); foreignClass.setFullyQualifiedName(collection.getChild("one-to-many").getAttributeValue("class")); } else if (collection.getChildren("many-to-many").size()!=0) { foreignClass = new ClassName(); foreignClass.setFullyQualifiedName(collection.getChild("many-to-many").getAttributeValue("class")); } // Do the foreign keys and import if (foreignClass != null) { // Collect the keys foreignKeys = new HashSet(); foreignKeys.add(collection.getChild("key").getAttributeValue("column")); for(Iterator iter = collection.getChild("key").getChildren("column").iterator(); iter.hasNext();) { foreignKeys.add(((Element) iter.next()).getAttributeValue("name")); } //addImport(foreignClass); } Field cf = new Field(name, interfaceClassName, "new " + implementingClassName.getName() + "()", false, foreignClass, foreignKeys, metaForCollection); fields.add(cf); if (collection.getChildren("composite-element") != null) { for (Iterator compositeElements = collection.getChildren("composite-element").iterator(); compositeElements.hasNext(); ) { Element compositeElement = (Element) compositeElements.next(); String compClass = compositeElement.getAttributeValue("class"); try { ClassMapping mapping = new ClassMapping(compositeElement, true, metaattribs); ClassName classType = new ClassName(); classType.setFullyQualifiedName(compClass); // add an import and field for this property addImport(classType); components.put( mapping.getCanonicalName(), mapping ); } catch (Exception e) { log.error("Error building composite-element " + compClass,e); } } } } } private void doArrays(Element classElement, String type, MultiMap inheritedMeta) { for ( Iterator arrays = classElement.getChildren(type).iterator(); arrays.hasNext(); ) { Element array = (Element) arrays.next(); MultiMap metaForArray = MetaAttributeHelper.loadAndMergeMetaMap(array,inheritedMeta); String role = array.getAttributeValue("name"); String elementClass = array.getAttributeValue("element-class"); if (elementClass==null) { Element elt = array.getChild("element"); if (elt==null) elt = array.getChild("one-to-many"); if (elt==null) elt = array.getChild("many-to-many"); if (elt==null) elt = array.getChild("composite-element"); if (elt==null) { log.warn("skipping collection with subcollections"); continue; } elementClass = elt.getAttributeValue("type"); if (elementClass==null) elementClass=elt.getAttributeValue("class"); } ClassName cn = getFieldType(elementClass); cn.setFullyQualifiedName(cn.getFullyQualifiedName() + "[]"); addImport(cn); Field af = new Field( role, cn, false, metaForArray); fields.add(af); } } 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 { Type basicType = TypeFactory.basic(hibernateType); if ( basicType!=null ) { if ( (basicType instanceof PrimitiveType) && !hibernateType.trim().equals( basicType.getReturnedClass().getName() ) && !needObject ) { cn.setFullyQualifiedName(( (PrimitiveType) basicType ).getPrimitiveClass().getName(),true); return cn; } else { cn.setFullyQualifiedName(basicType.getReturnedClass().getName()); return cn; } } else { // check and resolve correct type if it is an usertype hibernateType = getTypeForUserType(hibernateType); ClassName classType = new ClassName(); classType.setFullyQualifiedName(hibernateType); // add an import and field for this property addImport(classType); return classType; } } } /** Returns name of returnedclass if type is an UserType **/ private String getTypeForUserType(String type) { Class clazz = null; try { clazz = ReflectHelper.classForName(type); if (UserType.class.isAssignableFrom(clazz)) { UserType ut = (UserType) clazz.newInstance(); log.debug("Resolved usertype: " + type + " to " + ut.returnedClass().getName()); String t= clazzToName(ut.returnedClass()); return t; } if (CompositeUserType.class.isAssignableFrom(clazz)) { CompositeUserType ut = (CompositeUserType) clazz.newInstance(); log.debug("Resolved composite usertype: " + type + " to " + ut.returnedClass().getName()); String t= clazzToName(ut.returnedClass()); return t; } } catch (ClassNotFoundException e) { log.warn("Could not find UserType: " + type + ". Using the type '" + type + "' directly instead. (" + e.toString() +")"); } catch (IllegalAccessException iae) { log.warn("Error while trying to resolve UserType. Using the type '" + type + "' directly instead. (" + iae.toString() + ")"); } catch (InstantiationException e) { log.warn("Error while trying to resolve UserType. Using the type '" + type + "' directly instead. (" + e.toString() + ")"); } return type; } private String clazzToName(Class cl) { String s = null; if(cl.isArray()) { s = clazzToName(cl.getComponentType()) + "[]"; } else { s = cl.getName(); } return s; } /** * Returns the superClassMapping. * @return ClassMapping */public ClassMapping getSuperClassMapping() { return superClassMapping;} Collection getMeta(String attribute) { return (Collection) metaattribs.get(attribute); }/** * Method getMetaAsString. * @param string * @return String */public String getMetaAsString(String attribute) { Collection c= getMeta(attribute); return MetaAttributeHelper.getMetaAsString(c); }/** * Method shouldBeAbstract. * @return boolean */public boolean shouldBeAbstract() { return shouldBeAbstract;}// Based on some raw heuristics the following method validates the provided metaattribs.void validateMetaAttribs() { // Inform that "extends" is not used if this one is a genuine subclass if(getSuperClass()!=null && getMeta("extends")!=null) { log.warn("Warning: meta attribute extends='" + getMetaAsString("extends") + "' will be ignored for subclass " + name); } } /** * @see java.lang.Object#toString() */ public String toString() { return "ClassMapping: " + name.getFullyQualifiedName(); } public boolean getMetaAsBool(String attribute) { return getMetaAsBool(attribute,false); } public boolean getMetaAsBool(String attribute, boolean defaultValue) { Collection c= getMeta(attribute); return MetaAttributeHelper.getMetaAsBool(c,defaultValue); } public boolean isInterface() { return getMetaAsBool("interface"); } /** * @param subclassMapping */ public void addSubClass(ClassMapping subclassMapping) { subclasses.add(subclassMapping); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -