Author: tchemit Date: 2010-05-08 10:14:32 +0200 (Sat, 08 May 2010) New Revision: 885 Url: http://nuiton.org/repositories/revision/eugene/885 Log: - Evolution #576: Create Code Extensions to allow inserting code into object model - Remove deprecated api - Fix javadoc Added: trunk/eugene/src/main/java/org/nuiton/eugene/java/CodesManager.java trunk/eugene/src/main/java/org/nuiton/eugene/java/CodesManagerExtension.java Removed: trunk/eugene/src/main/java/org/nuiton/eugene/models/object/ObjectModelVisibility.java Modified: trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaBuilder.java Added: trunk/eugene/src/main/java/org/nuiton/eugene/java/CodesManager.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/java/CodesManager.java (rev 0) +++ trunk/eugene/src/main/java/org/nuiton/eugene/java/CodesManager.java 2010-05-08 08:14:32 UTC (rev 885) @@ -0,0 +1,81 @@ +/* + * #%L + * EUGene :: EUGene + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.eugene.java; + +import org.nuiton.eugene.models.object.ObjectModelOperation; + +import java.util.HashMap; +import java.util.Map; + +/** + * To manage some verbatim code to inject in operations. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0.2 + */ +public class CodesManager { + + private static final String EMPTY_STRING = ""; + + /** store of codes associated to operations */ + protected Map<ObjectModelOperation, StringBuilder> codes; + + /** + * Add the {@code annotation} for the given {@code element} of + * the classifier. + * + * @param operation the operation on which add the code + * @param code the code to add + */ + public void addCode(ObjectModelOperation operation, String code) { + Map<ObjectModelOperation, StringBuilder> map = getCodes(); + + StringBuilder buffer = map.get(operation); + if (buffer == null) { + buffer = new StringBuilder(); + map.put(operation, buffer); + } + buffer.append('\n').append(code); + } + + /** + * Obtain the codes registred for a given operation of the classifier. + * + * @param operation the operation where to search for code + * @return the code for the operation (empty if none found) + */ + public String getCode(ObjectModelOperation operation) { + Map<ObjectModelOperation, StringBuilder> map = getCodes(); + StringBuilder buffer = map.get(operation); + return buffer == null ? EMPTY_STRING : buffer.toString(); + } + + protected Map<ObjectModelOperation, StringBuilder> getCodes() { + if (codes == null) { + codes = new HashMap<ObjectModelOperation, StringBuilder>(); + } + return codes; + } +} Property changes on: trunk/eugene/src/main/java/org/nuiton/eugene/java/CodesManager.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: trunk/eugene/src/main/java/org/nuiton/eugene/java/CodesManagerExtension.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/java/CodesManagerExtension.java (rev 0) +++ trunk/eugene/src/main/java/org/nuiton/eugene/java/CodesManagerExtension.java 2010-05-08 08:14:32 UTC (rev 885) @@ -0,0 +1,89 @@ +package org.nuiton.eugene.java; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.eugene.models.object.ObjectModelClassifier; +import org.nuiton.eugene.models.object.ObjectModelOperation; + +import java.util.HashMap; +import java.util.Map; + +/** + * Object model extensions to manage verbatim code to attzach to operations. + * + * @author tchemit <chemit@codelutin.com> + * @see CodesManager + * @since 2.0.2 + */ +public class CodesManagerExtension { + + private static final Log log = LogFactory.getLog(CodesManagerExtension.class); + + /** Extension static used to identify CodesManagerExtension in ObjectModel */ + public static final String OBJECTMODEL_EXTENSION = "codes"; + + /** + * Map of CodesManager with key equals to the classifier qualified name + * associated to the CodesManager + */ + protected Map<String, CodesManager> managers; + + /** + * Add the {@code code} for the given {@code operation} of the + * given {@code classifier}. + * + * @param classifier the classifier container of the operation + * @param operation the operation on which to add the code + * @param code the code to add for the operation + */ + public void addcode(ObjectModelClassifier classifier, + ObjectModelOperation operation, + String code) { + CodesManager codesManager = getManager(classifier); + codesManager.addCode(operation, code); + } + + /** + * Get body code for a operation of the given classifier. + * <p/> + * The CodesManager must be defined in the model. + * + * @param classifier reference for the codes + * @param operation the operation to seek + * @return the body code of the method + */ + public String getCode(ObjectModelClassifier classifier, + ObjectModelOperation operation) { + CodesManager manager = getManager(classifier); + return manager.getCode(operation); + } + + /** + * Get the CodesManager associated to the classifier. + * <p/> + * <b>Note:</b> If not exist, it will be created. + * + * @param classifier reference for the ImportsManager + * @return the codesManager associated to the classifier (never null) + */ + protected CodesManager getManager(ObjectModelClassifier classifier) { + Map<String, CodesManager> managers = getManagers(); + String fqn = classifier.getQualifiedName(); + CodesManager manager = managers.get(fqn); + if (manager == null) { + manager = new CodesManager(); + managers.put(fqn, manager); + if (log.isDebugEnabled()) { + log.debug("Add new codesManager for : " + fqn); + } + } + return manager; + } + + protected Map<String, CodesManager> getManagers() { + if (managers == null) { + managers = new HashMap<String, CodesManager>(); + } + return managers; + } +} Property changes on: trunk/eugene/src/main/java/org/nuiton/eugene/java/CodesManagerExtension.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Modified: trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaBuilder.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaBuilder.java 2010-05-07 22:01:06 UTC (rev 884) +++ trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaBuilder.java 2010-05-08 08:14:32 UTC (rev 885) @@ -25,53 +25,65 @@ package org.nuiton.eugene.java; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.eugene.GeneratorUtil; +import org.nuiton.eugene.models.object.ObjectModel; +import org.nuiton.eugene.models.object.ObjectModelAttribute; +import org.nuiton.eugene.models.object.ObjectModelBuilder; +import org.nuiton.eugene.models.object.ObjectModelClass; +import org.nuiton.eugene.models.object.ObjectModelClassifier; +import org.nuiton.eugene.models.object.ObjectModelElement; +import org.nuiton.eugene.models.object.ObjectModelEnumeration; +import org.nuiton.eugene.models.object.ObjectModelInterface; +import org.nuiton.eugene.models.object.ObjectModelModifier; +import org.nuiton.eugene.models.object.ObjectModelOperation; +import org.nuiton.eugene.models.object.ObjectModelParameter; +import org.nuiton.eugene.models.object.ObjectModelType; +import org.nuiton.eugene.models.object.xml.ObjectModelClassImpl; +import org.nuiton.eugene.models.object.xml.ObjectModelOperationImpl; + import java.util.Arrays; import java.util.HashSet; import java.util.Set; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.eugene.GeneratorUtil; -import org.nuiton.eugene.models.object.*; -import org.nuiton.eugene.models.object.xml.*; - /** * JavaBuilder TODO heritates it from ModelBuilder * <p/> * Created: 29 oct. 2009 + * <p/> + * Builder to fill an empty ObjectModel with java specificities + * (imports, only one inheritance, constructor, ...). + * <p/> + * JavaBuilder uses some extensions to manage imports , constants, annotations, + * ...in the model. + * <p/> + * JavaBuilder is also based on ObjectModelBuilder for the simple filling of + * the model. * * @author fdesbois * @version $Revision$ * <p/> - * Builder to fill an empty ObjectModel with java specificities (imports, only one inheritance, constructor, ...). - * JavaBuilder uses ImportsManagerExtension to manage imports in the model. - * JavaBuilder is also based on ObjectModelBuilder for the simple filling of the model. - * <p/> - * Mise a jour: $Date$ - * par : */ + */ public class JavaBuilder { private static final Log log = LogFactory.getLog(JavaBuilder.class); - /** - * Builder where the filling is based on - */ + /** Builder where the filling is based on */ protected ObjectModelBuilder modelBuilder; - /** - * ObjectModel extension to manage imports : one ImportsManager by classifier - */ + /** ObjectModel extension to manage imports : one ImportsManager by classifier */ protected ImportsManagerExtension importsManagerExtension; - /** - * ObjectModel extension to manage imports : one ImportsManager by classifier - */ + + /** ObjectModel extension to manage imports : one ImportsManager by classifier */ protected AnnotationsManagerExtension annotationsManagerExtension; - /** - * ObjectModel extension to manage constants : one ConstantsManager for all - */ - protected ConstantsManagerExtension constanssManagerExtension; - @SuppressWarnings("unchecked") + /** ObjectModel extension to manage constants : one ConstantsManager for all */ + protected ConstantsManagerExtension constantsManagerExtension; + + /** ObjectModel extension to manage operation boby codes */ + protected CodesManagerExtension codesManagerExtension; + public JavaBuilder(String modelName) { modelBuilder = new ObjectModelBuilder(modelName); @@ -83,9 +95,13 @@ AnnotationsManagerExtension.OBJECTMODEL_EXTENSION, AnnotationsManagerExtension.class); - constanssManagerExtension = getModel().getExtension( + constantsManagerExtension = getModel().getExtension( ConstantsManagerExtension.OBJECTMODEL_EXTENSION, ConstantsManagerExtension.class); + + codesManagerExtension = getModel().getExtension( + CodesManagerExtension.OBJECTMODEL_EXTENSION, + CodesManagerExtension.class); } /** @@ -97,6 +113,12 @@ return modelBuilder.getModel(); } + /** + * Sets the documentation to the given {@code element}. + * + * @param element the element on which add the documentation + * @param documentation the documentation to add + */ public void setDocumentation(ObjectModelElement element, String documentation) { modelBuilder.setDocumentation(element, documentation); @@ -131,16 +153,16 @@ manager.addImport(oneType); if (log.isDebugEnabled()) { log.debug("Add import for : " + classifier.getQualifiedName() + - " _ import: " + oneType); + " _ import: " + oneType); } } } /** * Add an annotation on an element of a classifier. - * + * * @param classifier where the annotation will be added. - * @param element element on which add annotation + * @param element element on which add annotation * @param annotation annotation to add */ public void addAnnotation(ObjectModelClassifier classifier, @@ -156,23 +178,59 @@ manager.addAnnotation(element, annotation); if (log.isDebugEnabled()) { log.debug("Add annotation for <" + classifier.getQualifiedName() + - ":" + element.getName() + "> : " + annotation); + ":" + element.getName() + "> : " + annotation); } } + /** + * Add a body code to the operation of a classifier. + * + * @param classifier where the body code will be added. + * @param operation operation on which add body code + * @param code code to add + */ + public void addBodyCode(ObjectModelClassifier classifier, + ObjectModelOperation operation, + String code) { + if (code == null) { + return; + } + CodesManager manager = + codesManagerExtension.getManager(classifier); + code = code.trim(); + + manager.addCode(operation, code); + if (log.isDebugEnabled()) { + log.debug("Add code for <" + classifier.getQualifiedName() + + ":" + operation.getName() + "> : " + code); + } + } + + /** + * Converts the given {@code propertyName} to a constant name. + * <p/> + * For example : + * <pre> + * "CONSTANT_A" = getConstantName("constantA"); + * </pre> + * + * @param propertyName the name of the property to convert + * @return the constant name + */ public String getConstantName(String propertyName) { - String result = constanssManagerExtension.getConstantName(propertyName); + String result = constantsManagerExtension.getConstantName(propertyName); if (log.isDebugEnabled()) { log.debug("get constant name for <" + propertyName + "> : " + - result); + result); } return result; } + /** * Create a new class in the model. * - * @param name + * @param name the name of the new class to create * @param packageName package's name of the class to create * @return a new ObjectModelClass * @see ObjectModelBuilder#createClass(String, String, ObjectModelModifier...) @@ -184,7 +242,7 @@ /** * Create a new abstract class in the model. * - * @param name + * @param name the name of the abstract class to create * @param packageName package's name of the class to create * @return a new ObjectModelClass * @see ObjectModelBuilder#createClass(String, String, ObjectModelModifier...) @@ -192,13 +250,13 @@ public ObjectModelClass createAbstractClass(String name, String packageName) { return modelBuilder.createClass(name, packageName, - ObjectModelModifier.ABSTRACT); + ObjectModelModifier.ABSTRACT); } /** * Create a new interface in the model * - * @param name + * @param name the name of the interface to create * @param packageName package's name of the interface to create * @return a new ObjectModelInterface * @see ObjectModelBuilder#createInterface(String, String) @@ -211,7 +269,7 @@ /** * Create a new enumration in the model * - * @param name + * @param name the name of the enumeration to create * @param packageName package's name of the enumeration to create * @return a new ObjectModelEnumeration */ @@ -225,15 +283,16 @@ * to the class. * IMPORTS superclassQualifiedName. * - * @param classifier - * @param superclassQualifiedName + * @param classifier the classifier on which to set the super class + * @param superclassQualifiedName fully qualified name of the super class * @see ObjectModelBuilder#addInterface( *ObjectModelClassifier, String) */ public void setSuperClass(ObjectModelClass classifier, String superclassQualifiedName) { ObjectModelClassImpl impl = (ObjectModelClassImpl) classifier; - impl.clearSuperclasses(); // suppress all existing superclass: only one can be set for java + // suppress all existing superclass: only one can be set for java + impl.clearSuperclasses(); modelBuilder.addSuperclass(impl, superclassQualifiedName); addImport(classifier, superclassQualifiedName); } @@ -242,8 +301,8 @@ * Add an interface to a classifier (interface, class, enum). * IMPORTS interfaceQualifiedName. * - * @param classifier - * @param interfaceQualifiedName + * @param classifier the classifier on which to add the interface + * @param interfaceQualifiedName fully qualified name of the interface * @see ObjectModelBuilder#addInterface( *ObjectModelClassifier, String) */ @@ -257,11 +316,11 @@ * Add a new attribute to a classifier. * IMPORTS type, value. * - * @param classifier - * @param name - * @param type - * @param value - * @param modifiers + * @param classifier the classifier on which to add the attribute + * @param name name of attribute + * @param type type of attribute + * @param value initializer value of attribute + * @param modifiers modifiers of the attribute * @return a new ObjectModelAttribute * @see ObjectModelBuilder#addAttribute(ObjectModelClassifier, String, String, String, ObjectModelModifier...) */ @@ -274,8 +333,12 @@ // ANO#474 FD-20100408 : problem with import from defaultValue, will // not be supported from version 2.0.1 //this.addImport(classifier, value); - return modelBuilder.addAttribute(classifier, name, type, value, - modifiers); + return modelBuilder.addAttribute(classifier, + name, + type, + value, + modifiers + ); } /** @@ -283,10 +346,10 @@ * and have a value. * IMPORTS type, value. * - * @param classifier - * @param name - * @param type - * @param value + * @param classifier the classifier on which to add the constant field + * @param name name of the constant field + * @param type type of the constant field + * @param value initializer value of the constant field * @param visibility modifier allowed : PUBLIC, PRIVATE, PROTECTED, PACKAGE * @return a new ObjectModelAttribute * @throws IllegalArgumentException if the modifier is not a visibility @@ -298,13 +361,19 @@ ObjectModelModifier visibility) throws IllegalArgumentException { if (!visibility.isVisibility()) { - throw new IllegalArgumentException("Illegal visibility type : " + - visibility.name() + + throw new IllegalArgumentException( + "Illegal visibility type : " + visibility.name() + " for " + classifier.getQualifiedName()); } - return addAttribute(classifier, name, type, value, visibility, - ObjectModelModifier.STATIC, ObjectModelModifier.FINAL); + return addAttribute(classifier, + name, + type, + value, + visibility, + ObjectModelModifier.STATIC, + ObjectModelModifier.FINAL + ); } /** @@ -312,24 +381,28 @@ * Default visibility is set to PROTECTED. * IMPORTS type. * - * @param classifier - * @param name - * @param type + * @param classifier the classifier on which add the attribute + * @param name name of the attribute + * @param type type of the attribute * @return a new ObjectModelAttribute */ public ObjectModelAttribute addAttribute(ObjectModelClassifier classifier, String name, String type) { - return addAttribute(classifier, name, type, "", - ObjectModelModifier.PROTECTED); + return addAttribute(classifier, + name, + type, + "", + ObjectModelModifier.PROTECTED + ); } /** * Add a new attribute to a classifier from an existing attribute. * IMPORTS attribute.getType() and attribute.getDefaultValue(). * - * @param classifier - * @param attribute + * @param classifier the classifier on which to add the attribute + * @param attribute the attribute to add * @return a new ObjectModelAttribute */ public ObjectModelAttribute addAttribute(ObjectModelClassifier classifier, @@ -353,20 +426,22 @@ modifiers.add(ObjectModelModifier.PACKAGE); } - return addAttribute(classifier, + return addAttribute( + classifier, attribute.getName(), attribute.getType(), attribute.getDefaultValue(), - modifiers.toArray(new ObjectModelModifier[modifiers.size()])); + modifiers.toArray(new ObjectModelModifier[modifiers.size()]) + ); } /** * Add a new operation to a classifier. * - * @param classifier - * @param name - * @param type - * @param modifiers + * @param classifier the classifier on which to add the operation + * @param name the name of the operation + * @param type the return type of the operation + * @param modifiers the modifiers of the operation * @return a new ObjectModelOperation * @see ObjectModelBuilder#addOperation(ObjectModelClassifier, String, String, ObjectModelModifier...) */ @@ -381,13 +456,13 @@ /** * Add a new block to a classifier. * - * @param classifier - * @param modifiers + * @param classifier the classifier on which to add the block + * @param modifiers the modifiers of the operation * @return a new ObjectModelOperation * @see ObjectModelBuilder#addOperation(ObjectModelClassifier, String, String, ObjectModelModifier...) */ - public ObjectModelOperation addBlock(ObjectModelClassifier classifier, - ObjectModelModifier... modifiers) { + public ObjectModelOperation addBlock(ObjectModelClassifier classifier, + ObjectModelModifier... modifiers) { ObjectModelOperationImpl operation = (ObjectModelOperationImpl) modelBuilder.addOperation(classifier, null, null, modifiers); if (Arrays.asList(modifiers).contains(ObjectModelModifier.STATIC)) { @@ -399,17 +474,18 @@ /** * Add a constructor to a class. * - * @param clazz - * @param visibility + * @param clazz the classifier on which to add the constructor + * @param visibility the visibility of the operation * @return a new ObjectModelOperation * @throws IllegalArgumentException if the modifier is not a visibility + * @see ObjectModelBuilder#addOperation(ObjectModelClassifier, String, String, ObjectModelModifier...) */ public ObjectModelOperation addConstructor(ObjectModelClass clazz, ObjectModelModifier visibility) throws IllegalArgumentException { if (!visibility.isVisibility()) { - throw new IllegalArgumentException("Illegal visibility type : " + - visibility.name() + + throw new IllegalArgumentException( + "Illegal visibility type : " + visibility.name() + " for " + clazz.getQualifiedName()); } @@ -419,49 +495,46 @@ /** * Add a new parameter to an existing operation. * - * @param operation - * @param type - * @param name + * @param operation the operation on which to add a parameter + * @param type the type of the parameter + * @param name the name of the parameter * @return a new ObjectModelParameter - * @see ObjectModelBuilder#addParameter( - *ObjectModelOperation, String, String) + * @see ObjectModelBuilder#addParameter(ObjectModelOperation, String, String) */ public ObjectModelParameter addParameter(ObjectModelOperation operation, String type, String name) { addImport((ObjectModelClassifier) operation.getDeclaringElement(), - type); + type); return modelBuilder.addParameter(operation, type, name); } /** * Add an exception to an operation. * - * @param operation - * @param exception - * @see ObjectModelBuilder#addException( - *ObjectModelOperation, String) + * @param operation the operation on which to add a exeception + * @param exception the exception to add + * @see ObjectModelBuilder#addException(ObjectModelOperation, String) */ public void addException(ObjectModelOperation operation, String exception) { addImport((ObjectModelClassifier) operation.getDeclaringElement(), - exception); + exception); modelBuilder.addException(operation, exception); } /** * Set the operation body code. * - * @param operation - * @param body + * @param operation the operation on which to add the body code + * @param body the body code to set on the operation * @throws IllegalArgumentException if operation is abstract - * @see ObjectModelBuilder#setOperationBody( - *ObjectModelOperation, String) + * @see ObjectModelBuilder#setOperationBody(ObjectModelOperation, String) */ public void setOperationBody(ObjectModelOperation operation, String body) throws IllegalArgumentException { if (operation.isAbstract()) { - throw new IllegalArgumentException("Unable to add body for an " + - "abstract method"); + throw new IllegalArgumentException( + "Unable to add body for an abstract method"); } modelBuilder.setOperationBody(operation, body); } @@ -473,18 +546,36 @@ return modelBuilder.addInnerClassifier(clazz, type, name); } + /** + * Add a constructor to a enumeration. + * + * @param enumeration the enumeration on which to add the constructor + * @param visibility the visibility of the constructor (should always to private...) + * @return the new constructor + * @throws IllegalArgumentException if visibility is not ok + */ public ObjectModelOperation addConstructor(ObjectModelEnumeration enumeration, ObjectModelModifier visibility) throws IllegalArgumentException { if (!visibility.isVisibility()) { - throw new IllegalArgumentException("Illegal visibility type : " + - visibility.name() + + throw new IllegalArgumentException( + "Illegal visibility type : " + visibility.name() + " for " + enumeration.getQualifiedName()); } - return addOperation(enumeration, enumeration.getName(), null, visibility); + return addOperation(enumeration, + enumeration.getName(), + null, + visibility + ); } + /** + * Add a literal to the given {@code classifier}. + * + * @param classifier the enumeration on which to add the literal + * @param name the name of the literal to add + */ public void addLiteral(ObjectModelEnumeration classifier, String name) { modelBuilder.addLiteral(classifier, name); } Deleted: trunk/eugene/src/main/java/org/nuiton/eugene/models/object/ObjectModelVisibility.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/models/object/ObjectModelVisibility.java 2010-05-07 22:01:06 UTC (rev 884) +++ trunk/eugene/src/main/java/org/nuiton/eugene/models/object/ObjectModelVisibility.java 2010-05-08 08:14:32 UTC (rev 885) @@ -1,35 +0,0 @@ -/* - * #%L - * EUGene :: EUGene - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2010 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -package org.nuiton.eugene.models.object; - -/** - * - * @author fdesbois - */ -@Deprecated -public enum ObjectModelVisibility { - PUBLIC, PROTECTED, PRIVATE, PACKAGE -}