📄 classmapping.java
字号:
Set foreignKeys = null; // Collect bidirectional data if(collection.getChildren("one-to-many").size()!=0) { foreignClass = new ClassName(collection.getChild("one-to-many").getAttributeValue("class")); } else if (collection.getChildren("many-to-many").size()!=0) { foreignClass = new ClassName(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); } FieldProperty cf = new FieldProperty(collection, this, propertyName, interfaceClassName, implementationClassName, false, foreignClass, foreignKeys, metaForCollection); addFieldProperty(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(classPackage, compositeElement, this, true, getMetaAttribs()); ClassName classType = new ClassName(compClass); // add an import and field for this property addImport(classType); components.put( mapping.getFullyQualifiedName(), 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, false, true); addImport(cn); FieldProperty af = new FieldProperty( array, this, role, cn, false, metaForArray); addFieldProperty(af); } } private ClassName getFieldType(String hibernateType) { return getFieldType(hibernateType, false, false); } /** * Return a ClassName for a hibernatetype. * * @param hibernateType Name of the hibernatetype (e.g. "binary") * @param needObject * @param isArray if the type should be postfixed with array brackes ("[]") * @return */ private ClassName getFieldType(String hibernateType, boolean mustBeNullable, boolean isArray) { String postfix = isArray ? "[]" : ""; // deal with hibernate binary type ClassName cn = null; if ( hibernateType.equals("binary") ) { cn = new ClassName("byte[]" + postfix); return cn; } else { Type basicType = TypeFactory.basic(hibernateType); if ( basicType!=null ) { if ( (basicType instanceof PrimitiveType) && !hibernateType.trim().equals( basicType.getReturnedClass().getName() ) && !mustBeNullable ) { cn = new ClassName(( (PrimitiveType) basicType ).getPrimitiveClass().getName() + postfix); return cn; } else { cn = new ClassName(basicType.getReturnedClass().getName() + postfix); return cn; } } else { // check and resolve correct type if it is an usertype hibernateType = getTypeForUserType(hibernateType); cn = new ClassName(hibernateType + postfix); // add an import and field for this property addImport(cn); return cn; } } } /** 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; } /** * 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 isInterface() { return getMetaAsBool("interface"); } /** * @param subclassMapping */ public void addSubClass(ClassMapping subclassMapping) { subclasses.add(subclassMapping); } /** * @return */ public String getScope() { String classScope = "public"; if(getMeta("scope-class")!=null) { classScope = getMetaAsString("scope-class").trim(); } return classScope; } /** * @return */ public String getDeclarationType() { if(isInterface()) { return "interface"; } else { return "class"; } } /** * Return the modifers for this class. * Adds "abstract" if class should be abstract (but not if scope contains abstract) * TODO: deprecate/remove scope-class and introduce class-modifier instead * @return */ public String getModifiers() { if(shouldBeAbstract() && (getScope().indexOf("abstract")==-1)) { return "abstract"; } else { return ""; } } public void addImport(Class clazz) { addImport(clazz.getName()); } /** * @return */ public boolean isSuperInterface() { return getSuperClassMapping()==null?false:getSuperClassMapping().isInterface(); } public void storeIn(Map cmap) { cmap.put(getFullyQualifiedName(), this); Iterator it = getSubclasses().iterator(); while (it.hasNext()) { ClassMapping element = (ClassMapping) it.next(); element.storeIn(cmap); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -