Author: fdesbois Date: 2010-06-24 23:52:01 +0200 (Thu, 24 Jun 2010) New Revision: 924 Url: http://nuiton.org/repositories/revision/eugene/924 Log: Evo #711 : use inheritance between JavaBuilder and ObjectModelBuilder Modified: trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaBuilder.java Modified: trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaBuilder.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaBuilder.java 2010-06-24 18:24:23 UTC (rev 923) +++ trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaBuilder.java 2010-06-24 21:52:01 UTC (rev 924) @@ -48,7 +48,7 @@ import java.util.Set; /** - * JavaBuilder TODO heritates it from ModelBuilder + * JavaBuilder * <p/> * Created: 29 oct. 2009 * <p/> @@ -61,17 +61,14 @@ * JavaBuilder is also based on ObjectModelBuilder for the simple filling of * the model. * - * @author fdesbois - * @version $Revision$ - * <p/> + * @author fdesbois <fdesbois@codelutin.com> + * @author tchemit <tchemit@codelutin.com> + * @version $Id$ */ -public class JavaBuilder { +public class JavaBuilder extends ObjectModelBuilder { private static final Log log = LogFactory.getLog(JavaBuilder.class); - /** Builder where the filling is based on */ - protected ObjectModelBuilder modelBuilder; - /** ObjectModel extension to manage imports : one ImportsManager by classifier */ protected ImportsManagerExtension importsManagerExtension; @@ -85,7 +82,7 @@ protected CodesManagerExtension codesManagerExtension; public JavaBuilder(String modelName) { - modelBuilder = new ObjectModelBuilder(modelName); + super(modelName); importsManagerExtension = getModel().getExtension( ImportsManagerExtension.OBJECTMODEL_EXTENSION, @@ -105,26 +102,162 @@ } /** - * Get the model which is built + * Add an interface to a classifier (interface, class, enum). + * IMPORTS interfaceQualifiedName. * - * @return an ObjectModel + * @param classifier the classifier on which to add the interface + * @param interfaceQualifiedName fully qualified name of the interface + * @see ObjectModelBuilder#addInterface(ObjectModelClassifier, String) */ - public ObjectModel getModel() { - return modelBuilder.getModel(); + @Override + public void addInterface(ObjectModelClassifier classifier, + String interfaceQualifiedName) { + super.addInterface(classifier, interfaceQualifiedName); + addImport(classifier, interfaceQualifiedName); } + @Override + public void addSuperclass(ObjectModelClass clazz, String superclassQualifiedName) + throws UnsupportedOperationException { + throw new UnsupportedOperationException("This operation can't be used" + + " to generate Java, use setSuperClass method instead"); + } + /** - * Sets the documentation to the given {@code element}. + * Add a new attribute to a classifier. + * IMPORTS type. * - * @param element the element on which add the documentation - * @param documentation the documentation to add + * @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...) */ - public void setDocumentation(ObjectModelElement element, - String documentation) { - modelBuilder.setDocumentation(element, documentation); + @Override + public ObjectModelAttribute addAttribute(ObjectModelClassifier classifier, + String name, + String type, + String value, + ObjectModelModifier... modifiers) { + addImport(classifier, type); + // ANO#474 FD-20100408 : problem with import from defaultValue, will + // not be supported from version 2.0.1 + //this.addImport(classifier, value); + return super.addAttribute(classifier, + name, + type, + value, + modifiers + ); } /** + * Add a new attribute to a classifier with no default value. + * Default visibility is set to PROTECTED. + * IMPORTS 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 + */ + @Override + public ObjectModelAttribute addAttribute(ObjectModelClassifier classifier, + String name, + String type) { + return addAttribute(classifier, + name, + type, + "", + ObjectModelModifier.PROTECTED + ); + } + + /** + * Add a new operation to a classifier. + * + * @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...) + */ + @Override + public ObjectModelOperation addOperation(ObjectModelClassifier classifier, + String name, + String type, + ObjectModelModifier... modifiers) { + addImport(classifier, type); + return super.addOperation(classifier, name, type, modifiers); + } + + /** + * Add a new parameter to an existing operation. + * + * @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) + */ + @Override + public ObjectModelParameter addParameter(ObjectModelOperation operation, + String type, + String name) { + addImport((ObjectModelClassifier) operation.getDeclaringElement(), + type); + return super.addParameter(operation, type, name); + } + + /** + * Add an exception to an operation. + * + * @param operation the operation on which to add a exeception + * @param exception the exception to add + * @see ObjectModelBuilder#addException(ObjectModelOperation, String) + */ + @Override + public void addException(ObjectModelOperation operation, String exception) { + addImport((ObjectModelClassifier) operation.getDeclaringElement(), + exception); + super.addException(operation, exception); + } + + /** + * Set the operation body code. + * + * @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) + */ + @Override + public void setOperationBody(ObjectModelOperation operation, String body) + throws IllegalArgumentException { + if (operation.isAbstract()) { + throw new IllegalArgumentException( + "Unable to add body for an abstract method"); + } + super.setOperationBody(operation, body); + } + + /** + * Create a new abstract class in the model. + * + * @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...) + */ + public ObjectModelClass createAbstractClass(String name, + String packageName) { + return createClass(name, packageName, ObjectModelModifier.ABSTRACT); + } + + /** * Add an import to a classifier. Imports are automatic for lots of * JavaBuilder's methods except body code of operations. * <pre> @@ -153,7 +286,7 @@ manager.addImport(oneType); if (log.isDebugEnabled()) { log.debug("Add import for : " + classifier.getQualifiedName() + - " _ import: " + oneType); + " _ import: " + oneType); } } } @@ -178,170 +311,28 @@ 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 = constantsManagerExtension.getConstantName(propertyName); - if (log.isDebugEnabled()) { - log.debug("get constant name for <" + propertyName + "> : " + - result); - } - return result; - } - - - /** - * Create a new class in the model. - * - * @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...) - */ - public ObjectModelClass createClass(String name, String packageName) { - return modelBuilder.createClass(name, packageName); - } - - /** - * Create a new abstract class in the model. - * - * @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...) - */ - public ObjectModelClass createAbstractClass(String name, - String packageName) { - return modelBuilder.createClass(name, packageName, - ObjectModelModifier.ABSTRACT); - } - - /** - * Create a new interface in the model - * - * @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) - */ - public ObjectModelInterface createInterface(String name, - String packageName) { - return modelBuilder.createInterface(name, packageName); - } - - /** - * Create a new enumration in the model - * - * @param name the name of the enumeration to create - * @param packageName package's name of the enumeration to create - * @return a new ObjectModelEnumeration - */ - public ObjectModelEnumeration createEnumeration(String name, - String packageName) { - return modelBuilder.createEnumeration(name, packageName); - } - - /** * Set the superclass of an other class. Only one superclass can be set * to the class. * IMPORTS 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; // suppress all existing superclass: only one can be set for java impl.clearSuperclasses(); - modelBuilder.addSuperclass(impl, superclassQualifiedName); + super.addSuperclass(impl, superclassQualifiedName); addImport(classifier, superclassQualifiedName); } /** - * Add an interface to a classifier (interface, class, enum). - * IMPORTS interfaceQualifiedName. - * - * @param classifier the classifier on which to add the interface - * @param interfaceQualifiedName fully qualified name of the interface - * @see ObjectModelBuilder#addInterface( - *ObjectModelClassifier, String) - */ - public void addInterface(ObjectModelClassifier classifier, - String interfaceQualifiedName) { - modelBuilder.addInterface(classifier, interfaceQualifiedName); - addImport(classifier, interfaceQualifiedName); - } - - /** - * Add a new attribute to a classifier. - * IMPORTS type, value. - * - * @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...) - */ - public ObjectModelAttribute addAttribute(ObjectModelClassifier classifier, - String name, - String type, - String value, - ObjectModelModifier... modifiers) { - addImport(classifier, type); - // 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 - ); - } - - /** * Add a new constant to a classifier. A constant is static and final * and have a value. * IMPORTS type, value. @@ -363,41 +354,64 @@ if (!visibility.isVisibility()) { throw new IllegalArgumentException( "Illegal visibility type : " + visibility.name() + - " for " + classifier.getQualifiedName()); + " for " + classifier.getQualifiedName()); } return addAttribute(classifier, - name, - type, - value, - visibility, - ObjectModelModifier.STATIC, - ObjectModelModifier.FINAL + name, + type, + value, + visibility, + ObjectModelModifier.STATIC, + ObjectModelModifier.FINAL ); } /** - * Add a new attribute to a classifier with no default value. - * Default visibility is set to PROTECTED. - * IMPORTS type. + * Add a body code to the operation of a classifier. * - * @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 + * @param classifier where the body code will be added. + * @param operation operation on which add body code + * @param code code to add */ - public ObjectModelAttribute addAttribute(ObjectModelClassifier classifier, - String name, - String type) { - return addAttribute(classifier, - name, - type, - "", - ObjectModelModifier.PROTECTED - ); + 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 = constantsManagerExtension.getConstantName(propertyName); + if (log.isDebugEnabled()) { + log.debug("get constant name for <" + propertyName + "> : " + + result); + } + return result; + } + + /** * Add a new attribute to a classifier from an existing attribute. * IMPORTS attribute.getType() and attribute.getDefaultValue(). * @@ -436,24 +450,6 @@ } /** - * Add a new operation to a classifier. - * - * @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...) - */ - public ObjectModelOperation addOperation(ObjectModelClassifier classifier, - String name, - String type, - ObjectModelModifier... modifiers) { - addImport(classifier, type); - return modelBuilder.addOperation(classifier, name, type, modifiers); - } - - /** * Add a new block to a classifier. * * @param classifier the classifier on which to add the block @@ -464,7 +460,7 @@ public ObjectModelOperation addBlock(ObjectModelClassifier classifier, ObjectModelModifier... modifiers) { ObjectModelOperationImpl operation = (ObjectModelOperationImpl) - modelBuilder.addOperation(classifier, null, null, modifiers); + addOperation(classifier, null, null, modifiers); if (Arrays.asList(modifiers).contains(ObjectModelModifier.STATIC)) { operation.setStatic(true); } @@ -473,82 +469,22 @@ /** * Add a constructor to a class. + * FIXME-fdesbois-2010-06-23 : need to take care of generic case * * @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() + - " for " + clazz.getQualifiedName()); - } - - return addOperation(clazz, clazz.getName(), null, visibility); + return addConstructorForClassifier(clazz, visibility); } /** - * Add a new parameter to an existing operation. - * - * @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) - */ - public ObjectModelParameter addParameter(ObjectModelOperation operation, - String type, - String name) { - addImport((ObjectModelClassifier) operation.getDeclaringElement(), - type); - return modelBuilder.addParameter(operation, type, name); - } - - /** - * Add an exception to an operation. - * - * @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); - modelBuilder.addException(operation, exception); - } - - /** - * Set the operation body code. - * - * @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) - */ - public void setOperationBody(ObjectModelOperation operation, String body) - throws IllegalArgumentException { - if (operation.isAbstract()) { - throw new IllegalArgumentException( - "Unable to add body for an abstract method"); - } - modelBuilder.setOperationBody(operation, body); - } - - public ObjectModelClassifier addInnerClassifier(ObjectModelClass clazz, - ObjectModelType type, - String name, - ObjectModelModifier... modifiers) - throws IllegalArgumentException { - return modelBuilder.addInnerClassifier(clazz, type, name, modifiers); - } - - /** * Add a constructor to a enumeration. + * FIXME-fdesbois-2010-06-23 : need to take care of generic case * * @param enumeration the enumeration on which to add the constructor * @param visibility the visibility of the constructor (should always to private...) @@ -558,26 +494,17 @@ public ObjectModelOperation addConstructor(ObjectModelEnumeration enumeration, ObjectModelModifier visibility) throws IllegalArgumentException { + return addConstructorForClassifier(enumeration, visibility); + } + + protected ObjectModelOperation addConstructorForClassifier( + ObjectModelClassifier classifier, ObjectModelModifier visibility) { + if (!visibility.isVisibility()) { throw new IllegalArgumentException( "Illegal visibility type : " + visibility.name() + - " for " + enumeration.getQualifiedName()); + " for " + classifier.getQualifiedName()); } - - return addOperation(enumeration, - enumeration.getName(), - null, - visibility - ); + return addOperation(classifier, classifier.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); - } }
participants (1)
-
fdesbois@users.nuiton.org