r1333 - in topia/branches/generators-refactoring: . topia-persistence topia-persistence/src/main/java/org/codelutin/topia/generator
Author: tchemit Date: 2009-01-29 15:09:31 +0000 (Thu, 29 Jan 2009) New Revision: 1333 Modified: topia/branches/generators-refactoring/changelog.txt topia/branches/generators-refactoring/pom.xml topia/branches/generators-refactoring/topia-persistence/changelog.txt topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityAbstractGenerator.java topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityHibernateMappingGenerator.java topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaGeneratorUtil.java Log: - use lutinproject 3.4 - add methods in TopiaGeneratorUtil class to optimize imports - optimize imports on EntityAbstractGenerator - fix missing space on HibernateMappingGenerator Modified: topia/branches/generators-refactoring/changelog.txt =================================================================== --- topia/branches/generators-refactoring/changelog.txt 2009-01-29 14:22:19 UTC (rev 1332) +++ topia/branches/generators-refactoring/changelog.txt 2009-01-29 15:09:31 UTC (rev 1333) @@ -1,5 +1,6 @@ 2.1.3 ??? 200902?? * 20090126 [chemit] - refactor poms (all dependencies in parent-pom dependencyManagment) + * 20090129 [chemit] - use lutinproject 3.4 2.1.2 chemit 20090115 * 20090114 [chemit] - using lutinproject 3.3 Modified: topia/branches/generators-refactoring/pom.xml =================================================================== --- topia/branches/generators-refactoring/pom.xml 2009-01-29 14:22:19 UTC (rev 1332) +++ topia/branches/generators-refactoring/pom.xml 2009-01-29 15:09:31 UTC (rev 1333) @@ -10,7 +10,7 @@ <parent> <groupId>org.codelutin</groupId> <artifactId>lutinproject</artifactId> - <version>3.3</version> + <version>3.4-SNAPSHOT</version> </parent> <artifactId>topia</artifactId> @@ -170,7 +170,8 @@ <lutinutil.version>1.0</lutinutil.version> <xmlrpc.version>3.1</xmlrpc.version> <hibernate.version>3.3.1.GA</hibernate.version> - + <!-- todo pour la montee de version voir la doc --> + <topia.version>${project.version}</topia.version> </properties> <build> Modified: topia/branches/generators-refactoring/topia-persistence/changelog.txt =================================================================== --- topia/branches/generators-refactoring/topia-persistence/changelog.txt 2009-01-29 14:22:19 UTC (rev 1332) +++ topia/branches/generators-refactoring/topia-persistence/changelog.txt 2009-01-29 15:09:31 UTC (rev 1333) @@ -1,4 +1,7 @@ 2.1.3 ??? 200902?? +* 20090129 [chemit] - add methods in TopiaGeneratorUtil class to optimize imports +* 20090129 [thimel] - ??? +* 20090128 [bpoussin] - introduce Visitor design pattern on generated TopiaEntityAbstract * 20090126 [chemit] - refactor poms (all dependencies in parent-pom dependencyManagment) * 20090126 [chemit] - add somes junit tests to check none-regression when modify ToPIA's templates. - fix generation bug (notNull could ne null (line 226 EntityHibernateMappingGenerator) Modified: topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityAbstractGenerator.java =================================================================== --- topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityAbstractGenerator.java 2009-01-29 14:22:19 UTC (rev 1332) +++ topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityAbstractGenerator.java 2009-01-29 15:09:31 UTC (rev 1333) @@ -45,6 +45,7 @@ import java.io.IOException; import java.io.Writer; import java.util.Iterator; +import java.util.List; /** * Generateur d'entites abstraites. Il s'agit de l'implatation par defaut d'une @@ -89,21 +90,33 @@ }*/ } String clazzName = clazz.getName(); - String clazzFQN = clazz.getQualifiedName(); + String clazzFQN = TopiaGeneratorUtil.getSimpleName(clazz.getQualifiedName()); /*{package <%=clazz.getPackageName()%>; - -import org.apache.commons.lang.builder.ToStringBuilder; -import org.codelutin.topia.TopiaException; -import org.codelutin.topia.framework.TopiaContextImplementor; -import org.codelutin.topia.persistence.TopiaEntity; -import org.codelutin.topia.persistence.EntityVisitor; -import java.util.List; -import java.util.ArrayList; -import java.util.Collection; -import java.io.Serializable; - +}*/ + List<String> imports = TopiaGeneratorUtil.getImports(clazz, + org.apache.commons.lang.builder.ToStringBuilder.class.getName(), + org.codelutin.topia.TopiaException.class.getName(), + org.codelutin.topia.framework.TopiaContextImplementor.class.getName(), + org.codelutin.topia.persistence.TopiaEntity.class.getName(), + TopiaEntityAbstract.class.getName(), + org.codelutin.topia.persistence.EntityVisitor.class.getName(), + java.util.List.class.getName(), + java.util.ArrayList.class.getName(), + java.io.Serializable.class.getName() + ); + if (log.isDebugEnabled()) { + log.debug("imports for class <" + clazzFQN + ">"); + } + for (String anImport : imports) { + if (log.isDebugEnabled()) { + log.debug("import " + anImport); + } +/*{import <%=anImport%>; +}*/ + } +/*{ /** - * Implantation POJO pour l'entité <%=GeneratorUtil.capitalize(clazzName)%>. + * Implantation POJO pour l'entité {@link <%=TopiaGeneratorUtil.capitalize(clazzFQN)%>}. }*/ { String dbName = clazz.getTagValue(TopiaGeneratorUtil.TAG_DB_NAME); @@ -123,7 +136,7 @@ // impl ne sera pas créé boolean abstractParent = false; if (parent instanceof ObjectModelClass) { - abstractParent = shouldBeAbstract((ObjectModelClass) parent); + abstractParent = shouldBeAbstract(parent); } if (parent.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_ENTITY)) { if (abstractParent) { @@ -137,10 +150,10 @@ } } if (extendClass.length() == 0) { - extendClass += TopiaEntityAbstract.class.getName(); + extendClass += TopiaEntityAbstract.class.getSimpleName(); } -/*{public abstract class <%=clazzName%>Abstract extends <%=extendClass%> implements Serializable, <%=clazzFQN%> { +/*{public abstract class <%=clazzName%>Abstract extends <%=extendClass%> implements Serializable, <%=clazzName%> { }*/ @@ -161,7 +174,6 @@ || attr.hasAssociationClass())) { continue; } - ; if (TopiaGeneratorUtil.hasDocumentation(attr) || attr.hasTagValue(TopiaGeneratorUtil.TAG_DB_NAME)) { /*{ /** }*/ @@ -187,11 +199,11 @@ String attrVisibility = attr.getVisibility(); // In case attribute is NMultiplicity - String collectionType = TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr); + String collectionType = TopiaGeneratorUtil.getSimpleName(TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr)); if (!attr.hasAssociationClass()) { String attrName = attr.getName(); - String attrType = attr.getType(); + String attrType = TopiaGeneratorUtil.getSimpleName(attr.getType()); if (!GeneratorUtil.isNMultiplicity(attr)) { /*{ <%=attrVisibility%> <%=attrType%> <%=attrName%>; @@ -203,7 +215,7 @@ } } else { String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); - String assocClassFQN = attr.getAssociationClass().getQualifiedName(); + String assocClassFQN = TopiaGeneratorUtil.getSimpleName(attr.getAssociationClass().getQualifiedName()); if (!GeneratorUtil.isNMultiplicity(attr)) { //TODO THIMEL : Je pense que les GeneratorUtil.toLowerCaseFirstLetter sont inutiles ici, ou alors il faudrait le faire partout /*{ <%=attrVisibility%> <%=assocClassFQN%> <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; @@ -223,7 +235,7 @@ for (ObjectModelAttribute attr : assoc.getParticipantsAttributes()) { if (attr != null) { String attrVisibility = attr.getVisibility(); - String attrType = attr.getType(); + String attrType = TopiaGeneratorUtil.getSimpleName(attr.getType()); String attrName = attr.getName(); /*{ <%=attrVisibility%> <%=attrType%> <%=GeneratorUtil.toLowerCaseFirstLetter(attrName)%>; @@ -250,7 +262,9 @@ * Envoi via les methodes du visitor l'ensemble des champs de l'entity * avec leur type et leur valeur. * - * @param visitor : le visiteur de l'entite. + * @param visitor : le visiteur de l'entite. + * + * @throws TopiaException if any pb while visit *) public void accept(EntityVisitor visitor) throws TopiaException { visitor.start(this); @@ -269,14 +283,14 @@ } if (!attr.hasAssociationClass()) { - String attrType = attr.getType(); + String attrType = TopiaGeneratorUtil.getSimpleName(attr.getType()); String attrName = attr.getName(); if (!GeneratorUtil.isNMultiplicity(attr)) { /*{ visitor.visit(this, <%=attrType%>.class, <%=attrName%>); }*/ } else { - String collectionType = TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr); + String collectionType = TopiaGeneratorUtil.getSimpleName(TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr)); /*{ visitor.visit(this, <%=collectionType%>.class, <%=attrType%>.class, <%=attrName%>); }*/ @@ -284,14 +298,14 @@ } else { if (!GeneratorUtil.isNMultiplicity(attr)) { String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); - String assocClassFQN = attr.getAssociationClass().getQualifiedName(); + String assocClassFQN = TopiaGeneratorUtil.getSimpleName(attr.getAssociationClass().getQualifiedName()); /*{ visitor.visit(this, <%=assocClassFQN%>.class, <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>); }*/ } else { String collectionType = TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr); String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); - String assocClassFQN = attr.getAssociationClass().getQualifiedName(); + String assocClassFQN = TopiaGeneratorUtil.getSimpleName(attr.getAssociationClass().getQualifiedName()); /*{ visitor.visit(this, <%=collectionType%>.class, <%=assocClassFQN%>.class, <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>); }*/ @@ -304,7 +318,7 @@ ObjectModelAssociationClass assoc = (ObjectModelAssociationClass) clazz; for (ObjectModelAttribute attr : assoc.getParticipantsAttributes()) { if (attr != null) { - String attrType = attr.getType(); + String attrType = TopiaGeneratorUtil.getSimpleName(attr.getType()); String attrName = attr.getName(); /*{ visitor.visit(this, <%=attrType%>.class, <%=GeneratorUtil.toLowerCaseFirstLetter(attrName)%>); @@ -367,7 +381,7 @@ } } else if (attr.hasAssociationClass()) { String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); - String assocClassFQN = attr.getAssociationClass().getQualifiedName(); + String assocClassFQN = TopiaGeneratorUtil.getSimpleName(attr.getAssociationClass().getQualifiedName()); if (!GeneratorUtil.isNMultiplicity(attr)) { /*{ if (this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> != null) { @@ -416,7 +430,7 @@ ObjectModelAttribute reverse = attr.getReverseAttribute(); String attrName = attr.getName(); - String attrType = attr.getType(); + String attrType = TopiaGeneratorUtil.getSimpleName(attr.getType()); if (!(attr.isNavigable() || hasUnidirectionalRelationOnAbstractType(reverse, model))) { @@ -447,7 +461,7 @@ }*/ } else { String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); - String assocClassFQN = attr.getAssociationClass().getQualifiedName(); + String assocClassFQN = TopiaGeneratorUtil.getSimpleName(attr.getAssociationClass().getQualifiedName()); /*{ /* (non-Javadoc) * @see <%=clazzFQN%>#set<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=assocClassFQN%>) *) @@ -468,8 +482,8 @@ }*/ } } else { //NMultiplicity - String collectionInterface = TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr); - String collectionObject = TopiaGeneratorUtil.getNMultiplicityObjectType(attr); + String collectionInterface = TopiaGeneratorUtil.getSimpleName(TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr)); + String collectionObject = TopiaGeneratorUtil.getSimpleName(TopiaGeneratorUtil.getNMultiplicityObjectType(attr)); if (!attr.hasAssociationClass()) { //Méthodes remplacées par des accesseurs sur les classes d'assoc /*{ /* (non-Javadoc) * @see <%=clazzFQN%>#add<%=GeneratorUtil.capitalize(attrName)%>(<%=attrType%>) @@ -483,7 +497,7 @@ if (reverse != null && (reverse.isNavigable() || hasUnidirectionalRelationOnAbstractType(attr, model))) { String reverseAttrName = reverse.getName(); - String reverseAttrType = reverse.getType(); + String reverseAttrType = TopiaGeneratorUtil.getSimpleName(reverse.getType()); if (!GeneratorUtil.isNMultiplicity(reverse)) { /*{ <%=attrName%>.set<%=GeneratorUtil.capitalize(reverseAttrName)%>(this); }*/ @@ -583,7 +597,7 @@ }*/ } else { String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); - String assocClassFQN = attr.getAssociationClass().getQualifiedName(); + String assocClassFQN = TopiaGeneratorUtil.getSimpleName(attr.getAssociationClass().getQualifiedName()); /*{ /* (non-Javadoc) * @see <%=clazzFQN%>#add<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=assocClassFQN%>) *) @@ -698,7 +712,7 @@ }*/ } else { String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); - String assocClassFQN = attr.getAssociationClass().getQualifiedName(); + String assocClassFQN = TopiaGeneratorUtil.getSimpleName(attr.getAssociationClass().getQualifiedName()); /*{ /* (non-Javadoc) * @see <%=clazzFQN%>#get<%=GeneratorUtil.capitalize(assocAttrName)%>() *) @@ -741,7 +755,7 @@ ObjectModelAssociationClass assoc = (ObjectModelAssociationClass) clazz; for (ObjectModelAttribute attr : assoc.getParticipantsAttributes()) { if (attr != null) { - String attrType = attr.getType(); + String attrType = TopiaGeneratorUtil.getSimpleName(attr.getType()); String attrName = attr.getName(); generateAssociationAccessors(output, attrName, attrType); // //Ne sert plus à rien normalement avec la navigabilité Modified: topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityHibernateMappingGenerator.java =================================================================== --- topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityHibernateMappingGenerator.java 2009-01-29 14:22:19 UTC (rev 1332) +++ topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityHibernateMappingGenerator.java 2009-01-29 15:09:31 UTC (rev 1333) @@ -211,11 +211,11 @@ // / *{ <one-to-one name="<%=getName(attr, true)%>" class="<%=getType(attr, true)%>"<%=(TopiaGeneratorUtil.notEmpty(attr.getTagValue(TopiaGeneratorUtil.TAG_LENGTH))?(" length=\"" + attr.getTagValue(TopiaGeneratorUtil.TAG_LENGTH) + "\""):"")%><%=(attr.isComposite()?" cascade=\"delete\"":"")%>/> // } */ // } else { - String notNull = generateFromTagValue(attr, TopiaGeneratorUtil.TAG_NOT_NULL, "not-null"); + String notNull = " " + generateFromTagValue(attr, TopiaGeneratorUtil.TAG_NOT_NULL, "not-null"); String attrName = getName(attr, true); String attrType = getType(attr, true); String attrColumn = TopiaGeneratorUtil.getDBName(attr); -/*{<%=prefix%> <many-to-one name="<%=attrName%>" class="<%=attrType%>" column="<%=attrColumn%>" node="<%=attrName%>/@topiaId" embed-xml="false"<%=notNull%>/> +/*{<%=prefix%> <many-to-one name="<%=attrName%>" class="<%=attrType%>" column="<%=attrColumn%>" node="<%=attrName%>/@topiaId" embed-xml="false" <%=notNull%>/> }*/ // } //Ne sert plus grâce à l'utilisation de la navigabilité @@ -312,16 +312,19 @@ String optionalAttributes = ""; if (attr.isIndexed()) { String indexName = tableName + "_idx"; - optionalAttributes += "index=\"" + indexName + "\" "; + optionalAttributes += "index=\"" + indexName + "\""; } optionalAttributes += generateFromTagValue(attr, TopiaGeneratorUtil.TAG_NOT_NULL, "not-null"); /*{<%=prefix%> <property name="<%=attrName%>" type="<%=attrType%>" access="<%=accessField%>" }*/ - + optionalAttributes = optionalAttributes.trim(); String[] columnNames = this.columnNamesMap.get(attrType); if (columnNames == null || columnNames.length == 0) { optionalAttributes += generateFromTagValue(attr, TopiaGeneratorUtil.TAG_LENGTH, "length"); String attrColumn = TopiaGeneratorUtil.getDBName(attr); -/*{column="<%=attrColumn%>" node="<%=attrName%>" <%=optionalAttributes%>/> + optionalAttributes = optionalAttributes.trim(); + if (!optionalAttributes.isEmpty()) + optionalAttributes = " " + optionalAttributes; +/*{column="<%=attrColumn%>" node="<%=attrName%>"<%=optionalAttributes%>/> }*/ } else { /*{<%=optionalAttributes%>> @@ -453,7 +456,7 @@ // vérifier si le tag lazy est defini par defaut dans le fichier de proprietes String lazy = generateFromTagValue(attr, TopiaGeneratorUtil.TAG_LAZY, "lazy"); /*{<%=lazy%>}*/ - String notNull = generateFromTagValue(attr, TopiaGeneratorUtil.TAG_NOT_NULL, "not-null"); + String notNull = " " + generateFromTagValue(attr, TopiaGeneratorUtil.TAG_NOT_NULL, "not-null"); /*{<%=notNull%>}*/ /*{/> }*/ Modified: topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaGeneratorUtil.java =================================================================== --- topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaGeneratorUtil.java 2009-01-29 14:22:19 UTC (rev 1332) +++ topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaGeneratorUtil.java 2009-01-29 15:09:31 UTC (rev 1333) @@ -39,6 +39,8 @@ import org.codelutin.generator.models.object.ObjectModelElement; import org.codelutin.generator.models.object.ObjectModelInterface; import org.codelutin.generator.models.object.ObjectModelOperation; +import org.codelutin.generator.models.object.ObjectModelAssociationClass; +import org.codelutin.generator.models.object.ObjectModelParameter; import java.util.ArrayList; import java.util.Collection; @@ -47,6 +49,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Set; +import java.util.Arrays; /** Classe regroupant divers méthodes utiles pour la génération des entités */ public class TopiaGeneratorUtil extends GeneratorUtil { @@ -437,7 +440,11 @@ return type; } - private static Set<String> numberTypes = new HashSet<String>(); + private static final Set<String> numberTypes = new HashSet<String>(); + private static final Set<String> textTypes = new HashSet<String>(); + private static final Set<String> booleanTypes =new HashSet<String>(); + private static final Set<String> primitiveTypes =new HashSet<String>(); + private static final String VOID_TYPE = "void"; static { numberTypes.add("byte"); @@ -452,20 +459,23 @@ numberTypes.add("java.lang.Float"); numberTypes.add("double"); numberTypes.add("java.lang.Double"); + + textTypes.add("char"); + textTypes.add("java.lang.Char"); + textTypes.add("java.lang.String"); + + booleanTypes.add("boolean"); + booleanTypes.add("java.lang.Boolean"); + + primitiveTypes.addAll(numberTypes); + primitiveTypes.addAll(textTypes); + primitiveTypes.addAll(booleanTypes); } public static boolean isNumericType(ObjectModelAttribute attr) { return numberTypes.contains(attr.getType()); } - private static Set<String> textTypes = new HashSet<String>(); - - static { - textTypes.add("char"); - textTypes.add("java.lang.Char"); - textTypes.add("java.lang.String"); - } - public static boolean isTextType(ObjectModelAttribute attr) { return textTypes.contains(attr.getType()); } @@ -475,10 +485,13 @@ } public static boolean isBooleanType(ObjectModelAttribute attr) { - return ("boolean".equals(attr.getType()) || "java.lang.Boolean" - .equals(attr.getType())); + return booleanTypes.contains(attr.getType()); } + public static boolean isPrimitiveType(ObjectModelAttribute attr) { + return primitiveTypes.contains(attr.getType()); + } + /** * Indique si la classe specifiee n'a aucune ou que des methodes abstraites * @@ -694,4 +707,166 @@ return false; } } + + + /** + * Obtain the list of fqn of object involed in the given class. + * + * @param aClass the clazz to inspect + * @return the list of fqn of attributes + */ + public static List<String> getImports(ObjectModelClass aClass, String... incomingFqns) { + Set<String> tmp = new HashSet<String>(); + tmp.addAll(Arrays.asList(incomingFqns)); + getImports(aClass, tmp); + List<String> result = cleanImports(aClass.getPackageName(),tmp); + return result; + } + + /** + * Obtain the list of fqn of object involed in the given interface. + * + * @param anInterface the interface to inspect + * @return the list of fqn of attributes + */ + public static List<String> getImports(ObjectModelInterface anInterface, String... incomingFqns) { + Set<String> tmp = new HashSet<String>(); + tmp.addAll(Arrays.asList(incomingFqns)); + getImports(anInterface, tmp); + List<String> result = cleanImports(anInterface.getPackageName(),tmp); + return result; + } + + + public static String getSimpleName(String fqn) { + int lasIndex = fqn.lastIndexOf("."); + if (lasIndex==1) { + // primitive type + return fqn; + } + return fqn.substring(lasIndex + 1); + /*if (lasIndex == aClass.getPackageName().length()) { + // same package + return fqn.substring(lasIndex + 1); + } + + return fqn;*/ + } + /** + * Obtain the list of fqn of object involed in the given class. + * + * @param aClass the class to inspect + * @param fqns where to store found fqns + */ + protected static void getImports(ObjectModelClass aClass, Set<String> fqns) { + // scan attributes + for (ObjectModelAttribute attr : aClass.getAttributes()) { + fqns.add(attr.getType()); + if (isNMultiplicity(attr)) { + String collectionType = getNMultiplicityInterfaceType(attr); + fqns.add(collectionType); + String collectionObject = getNMultiplicityObjectType(attr); + fqns.add(collectionObject); + } + } + for (ObjectModelAttribute attribute : aClass.getAllOtherAttributes()) { + fqns.add(attribute.getType()); + } + // scan associations + if (aClass instanceof ObjectModelAssociationClass) { + ObjectModelAssociationClass assoc = (ObjectModelAssociationClass) aClass; + for (ObjectModelAttribute attr : assoc.getParticipantsAttributes()) { + if (attr == null) { + continue; + } + fqns.add(attr.getType()); + if (isNMultiplicity(attr)) { + String collectionType = getNMultiplicityInterfaceType(attr); + fqns.add(collectionType); + String collectionObject = getNMultiplicityObjectType(attr); + fqns.add(collectionObject); + } + } + } + // scan operations + for (ObjectModelOperation operation : aClass.getOperations()) { + getImports(operation, fqns); + } + // scan super interfaces + for (ObjectModelInterface modelInterface : aClass.getInterfaces()) { + fqns.add(modelInterface.getQualifiedName()); + getImports(modelInterface, fqns); + } + // scan super classes + for (ObjectModelClass modelClass : aClass.getSuperclasses()) { + fqns.add(modelClass.getQualifiedName()); + getImports(modelClass); + } + } + + /** + * Obtain the list of fqn of object involed in the given interface. + * + * @param anInterface the interface to inspect + * @param fqns where to store found fqns + */ + protected static void getImports(ObjectModelInterface anInterface, Set<String> fqns) { + // scan operations + for (ObjectModelOperation operation : anInterface.getOperations()) { + getImports(operation, fqns); + } + // scan super interfaces + for (ObjectModelInterface modelInterface : anInterface.getInterfaces()) { + fqns.add(modelInterface.getQualifiedName()); + getImports(modelInterface, fqns); + } + } + + /** + * Obtain the fqn's list of all involed type in a givne operation. + * + * @param operation operation to inspect + * @param fqns where to store found fqns + */ + protected static void getImports(ObjectModelOperation operation, Set<String> fqns) { + String fqn = operation.getReturnType(); + fqns.add(fqn); + for (ObjectModelParameter parameter : operation.getParameters()) { + fqns.add(parameter.getType()); + } + } + + /** + * Clean a set of fqns, transform it into a {@link List} and sort it. + * + * @param packageName the current package name + * @param fqns the dirty set of fqns + * @return the sorted cleaned list of fqns. + */ + protected static List<String> cleanImports(String packageName, Set<String> fqns) { + fqns.removeAll(primitiveTypes); + fqns.remove(VOID_TYPE); + int packageLength = packageName.length(); + List<String> genericType = new ArrayList<String>(); + for (Iterator<String> it = fqns.iterator(); it.hasNext();) { + String fqn = it.next(); + int lastIndex = fqn.lastIndexOf("."); + if (lastIndex == packageLength) { + // same package + it.remove(); + continue; + } + int genericIndex = fqn.indexOf('<'); + if (genericIndex!=-1) { + genericType.add(fqn.substring(0,genericIndex)); + it.remove(); + } + } + fqns.addAll(genericType); + + ArrayList<String> result = new ArrayList<String>(fqns); + java.util.Collections.sort(result); + return result; + } + } // GeneratorUtil
participants (1)
-
tchemit@users.labs.libre-entreprise.org