org.nuiton.eugene.java.JavaBuilder Maven / Gradle / Ivy
/*
* #%L
* EUGene :: EUGene
*
* $Id: JavaBuilder.java 885 2010-05-08 08:14:32Z tchemit $
* $HeadURL: http://svn.nuiton.org/svn/eugene/tags/eugene-2.0.2/eugene/src/main/java/org/nuiton/eugene/java/JavaBuilder.java $
* %%
* 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
* .
* #L%
*/
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;
/**
* JavaBuilder TODO heritates it from ModelBuilder
*
* Created: 29 oct. 2009
*
* Builder to fill an empty ObjectModel with java specificities
* (imports, only one inheritance, constructor, ...).
*
* JavaBuilder uses some extensions to manage imports , constants, annotations,
* ...in the model.
*
* JavaBuilder is also based on ObjectModelBuilder for the simple filling of
* the model.
*
* @author fdesbois
* @version $Revision: 885 $
*
*/
public class JavaBuilder {
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;
/** ObjectModel extension to manage imports : one ImportsManager by classifier */
protected AnnotationsManagerExtension annotationsManagerExtension;
/** 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);
importsManagerExtension = getModel().getExtension(
ImportsManagerExtension.OBJECTMODEL_EXTENSION,
ImportsManagerExtension.class);
annotationsManagerExtension = getModel().getExtension(
AnnotationsManagerExtension.OBJECTMODEL_EXTENSION,
AnnotationsManagerExtension.class);
constantsManagerExtension = getModel().getExtension(
ConstantsManagerExtension.OBJECTMODEL_EXTENSION,
ConstantsManagerExtension.class);
codesManagerExtension = getModel().getExtension(
CodesManagerExtension.OBJECTMODEL_EXTENSION,
CodesManagerExtension.class);
}
/**
* Get the model which is built
*
* @return an ObjectModel
*/
public ObjectModel getModel() {
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);
}
/**
* Add an import to a classifier. Imports are automatic for lots of
* JavaBuilder's methods except body code of operations.
*
* You can have some complex imports like :
* - new java.util.List()
* --> two imports 'java.util.List' and 'org.chorem.bonzoms.Role'
* - java.util.Collection
* --> two imports 'java.util.Collection' and 'org.nuiton.topia.TopiaEntity'
*
*
* @param classifier where the imports will be added.
* @param imports to add
* @see GeneratorUtil#getTypesList(String)
*/
public void addImport(ObjectModelClassifier classifier, String imports) {
if (imports == null) {
return;
}
ImportsManager manager = importsManagerExtension.getManager(classifier);
imports = imports.trim();
// remove ... operator
if (imports.endsWith("...")) {
imports = imports.substring(0, imports.length() - 3);
}
for (String oneType : GeneratorUtil.getTypesList(imports)) {
manager.addImport(oneType);
if (log.isDebugEnabled()) {
log.debug("Add import for : " + classifier.getQualifiedName() +
" _ 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 annotation annotation to add
*/
public void addAnnotation(ObjectModelClassifier classifier,
ObjectModelElement element,
String annotation) {
if (annotation == null) {
return;
}
AnnotationsManager manager =
annotationsManagerExtension.getManager(classifier);
annotation = annotation.trim();
manager.addAnnotation(element, annotation);
if (log.isDebugEnabled()) {
log.debug("Add annotation for <" + classifier.getQualifiedName() +
":" + 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.
*
* For example :
*
* "CONSTANT_A" = getConstantName("constantA");
*
*
* @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);
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.
*
* @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
*/
public ObjectModelAttribute addConstant(ObjectModelClassifier classifier,
String name,
String type,
String value,
ObjectModelModifier visibility)
throws IllegalArgumentException {
if (!visibility.isVisibility()) {
throw new IllegalArgumentException(
"Illegal visibility type : " + visibility.name() +
" for " + classifier.getQualifiedName());
}
return addAttribute(classifier,
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.
*
* @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
);
}
/**
* Add a new attribute to a classifier from an existing attribute.
* IMPORTS attribute.getType() and attribute.getDefaultValue().
*
* @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,
ObjectModelAttribute attribute) {
Set modifiers = new HashSet();
if (attribute.isStatic()) {
modifiers.add(ObjectModelModifier.STATIC);
}
if (attribute.isFinal()) {
modifiers.add(ObjectModelModifier.FINAL);
}
if (attribute.getVisibility().equals("public")) {
modifiers.add(ObjectModelModifier.PUBLIC);
} else if (attribute.getVisibility().equals("protected")) {
modifiers.add(ObjectModelModifier.PROTECTED);
} else if (attribute.getVisibility().equals("private")) {
modifiers.add(ObjectModelModifier.PRIVATE);
} else if (attribute.getVisibility().equals("")) {
modifiers.add(ObjectModelModifier.PACKAGE);
}
return addAttribute(
classifier,
attribute.getName(),
attribute.getType(),
attribute.getDefaultValue(),
modifiers.toArray(new ObjectModelModifier[modifiers.size()])
);
}
/**
* 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
* @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) {
ObjectModelOperationImpl operation = (ObjectModelOperationImpl)
modelBuilder.addOperation(classifier, null, null, modifiers);
if (Arrays.asList(modifiers).contains(ObjectModelModifier.STATIC)) {
operation.setStatic(true);
}
return operation;
}
/**
* Add a constructor to a class.
*
* @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);
}
/**
* 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)
throws IllegalArgumentException {
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() +
" for " + enumeration.getQualifiedName());
}
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);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy