Author: tchemit Date: 2012-12-10 01:29:12 +0100 (Mon, 10 Dec 2012) New Revision: 1216 Url: http://nuiton.org/projects/eugene/repository/revisions/1216 Log: fixes #2468: Introduce a SimpleJavaBeanTransformer fixes #2467: Eugene does not generate Bean Impl as it should Added: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/AbstractJavaBeanTransformer.java trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanTransformer.java Modified: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java Added: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/AbstractJavaBeanTransformer.java =================================================================== --- trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/AbstractJavaBeanTransformer.java (rev 0) +++ trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/AbstractJavaBeanTransformer.java 2012-12-10 00:29:12 UTC (rev 1216) @@ -0,0 +1,687 @@ +package org.nuiton.eugene.java; + +/* + * #%L + * EUGene :: Java templates + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 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% + */ + + +/*{generator option: parentheses = false}*/ +/*{generator option: writeString = +}*/ + +import org.nuiton.eugene.models.object.ObjectModelAttribute; +import org.nuiton.eugene.models.object.ObjectModelClass; +import org.nuiton.eugene.models.object.ObjectModelInterface; +import org.nuiton.eugene.models.object.ObjectModelJavaModifier; +import org.nuiton.eugene.models.object.ObjectModelOperation; + +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Set; + +/** + * Common class form javabean like transformer. + * + * @author tchemit <chemit@codelutin.com> + * @see SimpleJavaBeanTransformer + * @see JavaBeanTransformer + * @since 2.6 + */ +public abstract class AbstractJavaBeanTransformer extends ObjectModelTransformerToJava { + + public static final String DEFAULT_CONSTANT_PREFIX = "PROPERTY_"; + + protected void createPropertyConstant(ObjectModelClass output, + ObjectModelAttribute attr, + String prefix, + Set<String> constantNames) { + + String attrName = getAttributeName(attr); + + String constantName = prefix + builder.getConstantName(attrName); + + if (constantNames.contains(constantName)) { + + // already generated + return; + } + + addConstant(output, + constantName, + String.class, + "\"" + attrName + "\"", + ObjectModelJavaModifier.PUBLIC + ); + } + + protected String getAttributeName(ObjectModelAttribute attr) { + String attrName = attr.getName(); + if (attr.hasAssociationClass()) { + String assocAttrName = JavaGeneratorUtil.getAssocAttrName(attr); + attrName = JavaGeneratorUtil.toLowerCaseFirstLetter(assocAttrName); + } + return attrName; + } + + protected String getAttributeType(ObjectModelAttribute attr) { + String attrType = attr.getType(); + if (attr.hasAssociationClass()) { + attrType = attr.getAssociationClass().getName(); + } + return attrType; + } + + protected boolean containsMutiple(List<ObjectModelAttribute> attributes) { + + boolean result = false; + + for (ObjectModelAttribute attr : attributes) { + + if (JavaGeneratorUtil.isNMultiplicity(attr)) { + result = true; + + break; + } + + } + return result; + } + + protected void createProperty(ObjectModelClass output, + ObjectModelAttribute attr, + boolean usePCS, + boolean generateBooleanGetMethods) { + + String attrName = getAttributeName(attr); + String attrType = getAttributeType(attr); + + boolean multiple = JavaGeneratorUtil.isNMultiplicity(attr); + + String constantName = getConstantName(attrName); + String simpleType = JavaGeneratorUtil.getSimpleName(attrType); + + if (multiple) { + + createGetChildMethod(output, + attrName, + attrType, + simpleType + ); + + createIsEmptyMethod(output, + attrName + ); + + createSizeMethod(output, + attrName + ); + + createAddChildMethod(output, + attrName, + attrType, + constantName, + usePCS + ); + + createAddAllChildrenMethod(output, + attrName, + attrType, + constantName, + usePCS + ); + + createRemoveChildMethod(output, + attrName, + attrType, + constantName, + usePCS + ); + + createRemoveAllChildrenMethod(output, + attrName, + attrType, + constantName, + usePCS + ); + + createContainsChildMethod(output, + attrName, + attrType, + constantName, + usePCS + ); + + createContainsAllChildrenMethod(output, + attrName, + attrType, + constantName, + usePCS + ); + + // Change type for Multiple attribute + if (attr.isOrdered()) { + attrType = List.class.getName() + "<" + attrType + ">"; + } else { + attrType = Collection.class.getName() + "<" + attrType + ">"; + } + + simpleType = JavaGeneratorUtil.getSimpleName(attrType); + } + + boolean booleanProperty = JavaGeneratorUtil.isBooleanPrimitive(attr); + + if (booleanProperty && !multiple) { + + // creates a isXXX method + createGetMethod(output, + attrName, + attrType, + JavaGeneratorUtil.OPERATION_GETTER_BOOLEAN_PREFIX + ); + } + + if (multiple || !booleanProperty || generateBooleanGetMethods) { + + createGetMethod(output, + attrName, + attrType, + JavaGeneratorUtil.OPERATION_GETTER_DEFAULT_PREFIX + ); + + } + createSetMethod(output, + attrName, + attrType, + simpleType, + constantName, + usePCS + ); + + // Add attribute to the class + addAttribute(output, + attrName, + attrType, + "", + ObjectModelJavaModifier.PROTECTED + ); + + } + + protected List<ObjectModelAttribute> getProperties(ObjectModelClass input) { + List<ObjectModelAttribute> attributes = + (List<ObjectModelAttribute>) input.getAttributes(); + + List<ObjectModelAttribute> attrs = + new ArrayList<ObjectModelAttribute>(); + for (ObjectModelAttribute attr : attributes) { + if (attr.isNavigable()) { + + // only keep navigable attributes + attrs.add(attr); + } + } + return attrs; + } + + protected void createGetMethod(ObjectModelClass output, + String attrName, + String attrType, + String methodPrefix) { + + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName(methodPrefix , attrName), + attrType, + ObjectModelJavaModifier.PUBLIC + ); + setOperationBody(operation, "" + /*{ + return <%=attrName%>; + }*/ + ); + } + + protected void createGetChildMethod(ObjectModelClass output, + String attrName, + String attrType, + String simpleType) { + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("get", attrName), + attrType, + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, "int", "index"); + setOperationBody(operation, "" + /*{ + <%=simpleType%> o = getChild(<%=attrName%>, index); + return o; + }*/ + ); + } + + protected void createIsEmptyMethod(ObjectModelClass output, + String attrName) { + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("is", attrName)+"Empty", + boolean.class, + ObjectModelJavaModifier.PUBLIC + ); + setOperationBody(operation, "" + /*{ + return <%=attrName%> == null || <%=attrName%>.isEmpty(); + }*/ + ); + } + + protected void createSizeMethod(ObjectModelClass output, + String attrName) { + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("size", attrName), + int.class, + ObjectModelJavaModifier.PUBLIC + ); + setOperationBody(operation, "" + /*{ + return <%=attrName%> == null ? 0 : <%=attrName%>.size(); + }*/ + ); + } + protected void createAddChildMethod(ObjectModelClass output, + String attrName, + String attrType, + String constantName, + boolean usePCS) { + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("add", attrName), + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, attrType, attrName); + + String methodName = getJavaBeanMethodName("get", attrName); + StringBuilder buffer = new StringBuilder("" + /*{ + <%=methodName%>().add(<%=attrName%>); + }*/ + ); + if (usePCS) { + buffer.append("" + /*{ firePropertyChange(<%=constantName%>, null, <%=attrName%>); + }*/ + ); + } + setOperationBody(operation, buffer.toString()); + } + + protected void createAddAllChildrenMethod(ObjectModelClass output, + String attrName, + String attrType, + String constantName, + boolean usePCS) { + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("addAll", attrName), + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, "java.util.Collection<" + attrType + ">", attrName); + + String methodName = getJavaBeanMethodName("get", attrName); + StringBuilder buffer = new StringBuilder("" + /*{ + <%=methodName%>().addAll(<%=attrName%>); + }*/ + ); + if (usePCS) { + buffer.append("" + /*{ firePropertyChange(<%=constantName%>, null, <%=attrName%>); + }*/ + ); + } + setOperationBody(operation, buffer.toString()); + } + + protected void createRemoveChildMethod(ObjectModelClass output, + String attrName, + String attrType, + String constantName, + boolean usePCS) { + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("remove", attrName), + "boolean", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, attrType, attrName); + String methodName = getJavaBeanMethodName("get", attrName); + StringBuilder buffer = new StringBuilder(); + buffer.append("" + /*{ + boolean removed = <%=methodName%>().remove(<%=attrName%>);}*/ + ); + + if (usePCS) { + buffer.append("" + /*{ + if (removed) { + firePropertyChange(<%=constantName%>, <%=attrName%>, null); + }}*/ + ); + } + buffer.append("" + /*{ + return removed; + }*/ + ); + setOperationBody(operation, buffer.toString()); + } + + protected void createRemoveAllChildrenMethod(ObjectModelClass output, + String attrName, + String attrType, + String constantName, + boolean usePCS) { + + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("removeAll", attrName), + "boolean", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, "java.util.Collection<" + attrType + ">", attrName); + StringBuilder buffer = new StringBuilder(); + String methodName = getJavaBeanMethodName("get", attrName); + buffer.append("" + /*{ + boolean removed = <%=methodName%>().removeAll(<%=attrName%>);}*/ + ); + + if (usePCS) { + buffer.append("" + /*{ + if (removed) { + firePropertyChange(<%=constantName%>, <%=attrName%>, null); + }}*/ + ); + } + buffer.append("" + /*{ + return removed; + }*/ + ); + setOperationBody(operation, buffer.toString()); + } + + protected void createContainsChildMethod(ObjectModelClass output, + String attrName, + String attrType, + String constantName, + boolean usePCS) { + + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("contains", attrName), + "boolean", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, attrType, attrName); + StringBuilder buffer = new StringBuilder(); + String methodName = getJavaBeanMethodName("get", attrName); + buffer.append("" + /*{ + boolean contains = <%=methodName%>().contains(<%=attrName%>); + return contains; + }*/ + ); + setOperationBody(operation, buffer.toString()); + } + + protected void createContainsAllChildrenMethod(ObjectModelClass output, + String attrName, + String attrType, + String constantName, + boolean usePCS) { + + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("containsAll", attrName), + "boolean", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, "java.util.Collection<" + attrType + ">", attrName); + StringBuilder buffer = new StringBuilder(); + String methodName = getJavaBeanMethodName("get", attrName); + buffer.append("" + /*{ + boolean contains = <%=methodName%>().containsAll(<%=attrName%>); + return contains; + }*/ + ); + setOperationBody(operation, buffer.toString()); + } + + protected void createSetMethod(ObjectModelClass output, + String attrName, + String attrType, + String simpleType, + String constantName, + boolean usePCS) { + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("set", attrName), + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, attrType, attrName); + + if (usePCS) { + String methodName = getJavaBeanMethodName("get", attrName); + setOperationBody(operation, "" + /*{ + <%=simpleType%> oldValue = <%=methodName%>(); + this.<%=attrName%> = <%=attrName%>; + firePropertyChange(<%=constantName%>, oldValue, <%=attrName%>); + }*/ + ); + } else { + setOperationBody(operation, "" + /*{ + this.<%=attrName%> = <%=attrName%>; + }*/ + ); + } + } + + protected void addSerializable(ObjectModelClass input, + ObjectModelClass output, + boolean interfaceFound) { + if (!interfaceFound) { + addInterface(output, Serializable.class); + } + + // Generate the serialVersionUID + long serialVersionUID = JavaGeneratorUtil.generateSerialVersionUID(input); + + addConstant(output, + JavaGeneratorUtil.SERIAL_VERSION_UID, + "long", + serialVersionUID + "L", + ObjectModelJavaModifier.PRIVATE + ); + } + + /** + * Add all interfaces defines in input class and returns if + * {@link Serializable} interface was found. + * + * @param input the input model class to process + * @param output the output generated class + * @return {@code true} if {@link Serializable} was found from input, + * {@code false} otherwise + */ + protected boolean addInterfaces(ObjectModelClass input, + ObjectModelClass output) { + boolean foundSerializable = false; + for (ObjectModelInterface parentInterface : input.getInterfaces()) { + String fqn = parentInterface.getQualifiedName(); + addInterface(output, fqn); + if (Serializable.class.getName().equals(fqn)) { + foundSerializable = true; + } + } + return foundSerializable; + } + + protected void createPropertyChangeSupport(ObjectModelClass output) { + + addAttribute(output, + "pcs", + PropertyChangeSupport.class, + "new PropertyChangeSupport(this)", + ObjectModelJavaModifier.PROTECTED, + ObjectModelJavaModifier.FINAL + ); + + // Add PropertyListener + + ObjectModelOperation operation; + + operation = addOperation(output, + "addPropertyChangeListener", + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, PropertyChangeListener.class, "listener"); + setOperationBody(operation, "" + /*{ + pcs.addPropertyChangeListener(listener); + }*/ + ); + + operation = addOperation(output, + "addPropertyChangeListener", + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, String.class, "propertyName"); + addParameter(operation, PropertyChangeListener.class, "listener"); + setOperationBody(operation, "" + /*{ + pcs.addPropertyChangeListener(propertyName, listener); + }*/ + ); + + operation = addOperation(output, + "removePropertyChangeListener", + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, PropertyChangeListener.class, "listener"); + setOperationBody(operation, "" + /*{ + pcs.removePropertyChangeListener(listener); + }*/ + ); + + operation = addOperation(output, + "removePropertyChangeListener", + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, String.class, "propertyName"); + addParameter(operation, PropertyChangeListener.class, "listener"); + setOperationBody(operation, "" + /*{ + pcs.removePropertyChangeListener(propertyName, listener); + }*/ + ); + + operation = addOperation(output, + "firePropertyChange", + "void", + ObjectModelJavaModifier.PROTECTED + ); + addParameter(operation, String.class, "propertyName"); + addParameter(operation, Object.class, "oldValue"); + addParameter(operation, Object.class, "newValue"); + setOperationBody(operation, "" + /*{ + pcs.firePropertyChange(propertyName, oldValue, newValue); + }*/ + ); + + operation = addOperation(output, + "firePropertyChange", + "void", + ObjectModelJavaModifier.PROTECTED + ); + addParameter(operation, String.class, "propertyName"); + addParameter(operation, Object.class, "newValue"); + setOperationBody(operation, "" + /*{ + firePropertyChange(propertyName, null, newValue); + }*/ + ); + } + + protected void createGetChildMethod(ObjectModelClass output) { + ObjectModelOperation getChild = addOperation( + output, + "getChild", "<T> T", + ObjectModelJavaModifier.PROTECTED + ); + addImport(output, List.class); + + addParameter(getChild, "java.util.Collection<T>", "childs"); + addParameter(getChild, "int", "index"); + setOperationBody(getChild, "" +/*{ + T result = null; + if (childs != null) { + if (childs instanceof List) { + if (index < childs.size()) { + result = ((List<T>) childs).get(index); + } + } else { + int i = 0; + for (T o : childs) { + if (index == i) { + result = o; + break; + } + i++; + } + } + } + return result; +}*/ + ); + } +} Property changes on: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/AbstractJavaBeanTransformer.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java =================================================================== --- trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java 2012-11-25 09:48:44 UTC (rev 1215) +++ trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java 2012-12-10 00:29:12 UTC (rev 1216) @@ -30,13 +30,9 @@ import org.apache.commons.logging.LogFactory; import org.nuiton.eugene.models.object.ObjectModelAttribute; import org.nuiton.eugene.models.object.ObjectModelClass; -import org.nuiton.eugene.models.object.ObjectModelInterface; import org.nuiton.eugene.models.object.ObjectModelJavaModifier; import org.nuiton.eugene.models.object.ObjectModelOperation; -import java.beans.PropertyChangeListener; -import java.beans.PropertyChangeSupport; -import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; @@ -60,12 +56,10 @@ * @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.eugene.java.JavaBeanTransformer" * @since 2.0.2 */ -public class JavaBeanTransformer extends ObjectModelTransformerToJava { +public class JavaBeanTransformer extends AbstractJavaBeanTransformer { private static final Log log = LogFactory.getLog(JavaBeanTransformer.class); - public static final String DEFAULT_CONSTANT_PREFIX = "PROPERTY_"; - @Override public void transformFromClass(ObjectModelClass input) { @@ -75,17 +69,86 @@ return; } - if (canGenerateImpl(input)) { + ObjectModelClass output = null; + if (canGenerateAbstract(input)) { + + // nothing more to do + output = generateAbstract(input); + } + + if (canGenerateImpl(input, output)) { + generateImpl(input); } - - if (!canGenerateAbstract(input)) { + } - // nothing more to do - return; + protected boolean canGenerateAbstract(ObjectModelClass input) { + boolean b = !isInClassPath(input); + return b; + } + + protected boolean canGenerateImpl(ObjectModelClass input, + ObjectModelClass abstractOutput) { + String fqn = input.getQualifiedName() + "Impl"; + + if (isInClassPath(fqn)) { + + // already in class-path + return false; } + Collection<ObjectModelOperation> operations = input.getOperations(); + if (!operations.isEmpty()) { + + // this input have some specific operations (so Impl can not be generated) + return false; + } + + Collection<ObjectModelOperation> allOtherOperations = + input.getAllOtherOperations(true); + + if (!allOtherOperations.isEmpty()) { + + Collection<ObjectModelOperation> allExistingOperations = + new ArrayList<ObjectModelOperation>(input.getAllSuperclassOperations(true)); + + if (abstractOutput!=null) { + allExistingOperations.addAll(abstractOutput.getOperations()); + } + + for (ObjectModelOperation operation : allOtherOperations) { + if (!allExistingOperations.contains(operation)) { + + // found one method not on super classe, so can't generate Impl + return false; + } + } + } + return true; + +// boolean hasOperations = !allOtherOperations.isEmpty() || +// !operations.isEmpty(); +// +// // generate only if no user operations found +// return !hasOperations; + } + + + + + protected void createAbstractOperations(ObjectModelClass ouput, + Iterable<ObjectModelOperation> operations) { + JavaGeneratorUtil.cloneOperations( + this, + operations, + ouput, + true, + ObjectModelJavaModifier.ABSTRACT + ); + } + + protected ObjectModelClass generateAbstract(ObjectModelClass input) { // test if a super class has bean stereotype boolean superClassIsBean = false; Collection<ObjectModelClass> superclasses = input.getSuperclasses(); @@ -193,545 +256,29 @@ } } + return output; } - protected boolean canGenerateAbstract(ObjectModelClass input) { - boolean b = !isInClassPath(input); - return b; - } + protected ObjectModelClass generateImpl(ObjectModelClass input) { - protected boolean canGenerateImpl(ObjectModelClass input) { - String fqn = input.getQualifiedName() + "Impl"; - - if (isInClassPath(fqn)) { - - // already in class-path - return false; - } - - boolean hasOperations = !input.getAllOtherOperations(true).isEmpty() || - !input.getOperations().isEmpty(); - - // generate only if no user operations found - return !hasOperations; - } - - protected void createPropertyConstant(ObjectModelClass output, - ObjectModelAttribute attr, - String prefix, - Set<String> constantNames) { - - String attrName = getAttributeName(attr); - - String constantName = prefix + builder.getConstantName(attrName); - - if (constantNames.contains(constantName)) { - - // already generated - return; - } - - addConstant(output, - constantName, - String.class, - "\"" + attrName + "\"", - ObjectModelJavaModifier.PUBLIC + ObjectModelClass resultClassImpl = createClass( + input.getName() + "Impl", + input.getPackageName() ); - } - protected String getAttributeName(ObjectModelAttribute attr) { - String attrName = attr.getName(); - if (attr.hasAssociationClass()) { - String assocAttrName = JavaGeneratorUtil.getAssocAttrName(attr); - attrName = JavaGeneratorUtil.toLowerCaseFirstLetter(assocAttrName); - } - return attrName; - } + // set the abstract resulClass as the resultClassImpl super class + setSuperClass(resultClassImpl, input.getQualifiedName()); - protected String getAttributeType(ObjectModelAttribute attr) { - String attrType = attr.getType(); - if (attr.hasAssociationClass()) { - attrType = attr.getAssociationClass().getName(); - } - return attrType; - } - - protected boolean containsMutiple(List<ObjectModelAttribute> attributes) { - - boolean result = false; - - for (ObjectModelAttribute attr : attributes) { - - if (JavaGeneratorUtil.isNMultiplicity(attr)) { - result = true; - - break; - } - - } - return result; - } - - protected void createProperty(ObjectModelClass output, - ObjectModelAttribute attr, - boolean usePCS, - boolean generateBooleanGetMethods) { - - String attrName = getAttributeName(attr); - String attrType = getAttributeType(attr); - - boolean multiple = JavaGeneratorUtil.isNMultiplicity(attr); - - String constantName = getConstantName(attrName); - String simpleType = JavaGeneratorUtil.getSimpleName(attrType); - - if (multiple) { - - createGetChildMethod(output, - attrName, - attrType, - simpleType - ); - - createIsEmptyMethod(output, - attrName - ); - - createSizeMethod(output, - attrName - ); - - createAddChildMethod(output, - attrName, - attrType, - constantName, - usePCS - ); - - createAddAllChildrenMethod(output, - attrName, - attrType, - constantName, - usePCS - ); - - createRemoveChildMethod(output, - attrName, - attrType, - constantName, - usePCS - ); - - createRemoveAllChildrenMethod(output, - attrName, - attrType, - constantName, - usePCS - ); - - createContainsChildMethod(output, - attrName, - attrType, - constantName, - usePCS - ); - - createContainsAllChildrenMethod(output, - attrName, - attrType, - constantName, - usePCS - ); - - // Change type for Multiple attribute - if (attr.isOrdered()) { - attrType = List.class.getName() + "<" + attrType + ">"; - } else { - attrType = Collection.class.getName() + "<" + attrType + ">"; - } - - simpleType = JavaGeneratorUtil.getSimpleName(attrType); - } - - boolean booleanProperty = JavaGeneratorUtil.isBooleanPrimitive(attr); - - if (booleanProperty && !multiple) { - - // creates a isXXX method - createGetMethod(output, - attrName, - attrType, - JavaGeneratorUtil.OPERATION_GETTER_BOOLEAN_PREFIX - ); - } - - if (multiple || !booleanProperty || generateBooleanGetMethods) { - - createGetMethod(output, - attrName, - attrType, - JavaGeneratorUtil.OPERATION_GETTER_DEFAULT_PREFIX - ); - - } - createSetMethod(output, - attrName, - attrType, - simpleType, - constantName, - usePCS - ); - - // Add attribute to the class - addAttribute(output, - attrName, - attrType, - "", - ObjectModelJavaModifier.PROTECTED - ); - - } - - protected void createAbstractOperations(ObjectModelClass ouput, - Iterable<ObjectModelOperation> operations) { - JavaGeneratorUtil.cloneOperations( - this, - operations, - ouput, - true, - ObjectModelJavaModifier.ABSTRACT - ); - } - - protected List<ObjectModelAttribute> getProperties(ObjectModelClass input) { - List<ObjectModelAttribute> attributes = - (List<ObjectModelAttribute>) input.getAttributes(); - - List<ObjectModelAttribute> attrs = - new ArrayList<ObjectModelAttribute>(); - for (ObjectModelAttribute attr : attributes) { - if (attr.isNavigable()) { - - // only keep navigable attributes - attrs.add(attr); - } - } - return attrs; - } - - protected void createGetMethod(ObjectModelClass output, - String attrName, - String attrType, - String methodPrefix) { - - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName(methodPrefix , attrName), - attrType, - ObjectModelJavaModifier.PUBLIC - ); - setOperationBody(operation, "" - /*{ - return <%=attrName%>; - }*/ - ); - } - - protected void createGetChildMethod(ObjectModelClass output, - String attrName, - String attrType, - String simpleType) { - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("get", attrName), - attrType, - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, "int", "index"); - setOperationBody(operation, "" - /*{ - <%=simpleType%> o = getChild(<%=attrName%>, index); - return o; - }*/ - ); - } - - protected void createIsEmptyMethod(ObjectModelClass output, - String attrName) { - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("is", attrName)+"Empty", - boolean.class, - ObjectModelJavaModifier.PUBLIC - ); - setOperationBody(operation, "" - /*{ - return <%=attrName%> == null || <%=attrName%>.isEmpty(); - }*/ - ); - } - - protected void createSizeMethod(ObjectModelClass output, - String attrName) { - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("size", attrName), - int.class, - ObjectModelJavaModifier.PUBLIC - ); - setOperationBody(operation, "" - /*{ - return <%=attrName%> == null ? 0 : <%=attrName%>.size(); - }*/ - ); - } - protected void createAddChildMethod(ObjectModelClass output, - String attrName, - String attrType, - String constantName, - boolean usePCS) { - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("add", attrName), - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, attrType, attrName); - - String methodName = getJavaBeanMethodName("get", attrName); - StringBuilder buffer = new StringBuilder("" - /*{ - <%=methodName%>().add(<%=attrName%>); - }*/ - ); - if (usePCS) { - buffer.append("" - /*{ firePropertyChange(<%=constantName%>, null, <%=attrName%>); - }*/ - ); - } - setOperationBody(operation, buffer.toString()); - } - - protected void createAddAllChildrenMethod(ObjectModelClass output, - String attrName, - String attrType, - String constantName, - boolean usePCS) { - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("addAll", attrName), - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, "java.util.Collection<" + attrType + ">", attrName); - - String methodName = getJavaBeanMethodName("get", attrName); - StringBuilder buffer = new StringBuilder("" - /*{ - <%=methodName%>().addAll(<%=attrName%>); - }*/ - ); - if (usePCS) { - buffer.append("" - /*{ firePropertyChange(<%=constantName%>, null, <%=attrName%>); - }*/ - ); - } - setOperationBody(operation, buffer.toString()); - } - - protected void createRemoveChildMethod(ObjectModelClass output, - String attrName, - String attrType, - String constantName, - boolean usePCS) { - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("remove", attrName), - "boolean", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, attrType, attrName); - String methodName = getJavaBeanMethodName("get", attrName); - StringBuilder buffer = new StringBuilder(); - buffer.append("" - /*{ - boolean removed = <%=methodName%>().remove(<%=attrName%>);}*/ - ); - - if (usePCS) { - buffer.append("" - /*{ - if (removed) { - firePropertyChange(<%=constantName%>, <%=attrName%>, null); - }}*/ - ); - } - buffer.append("" - /*{ - return removed; - }*/ - ); - setOperationBody(operation, buffer.toString()); - } - - protected void createRemoveAllChildrenMethod(ObjectModelClass output, - String attrName, - String attrType, - String constantName, - boolean usePCS) { - - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("removeAll", attrName), - "boolean", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, "java.util.Collection<" + attrType + ">", attrName); - StringBuilder buffer = new StringBuilder(); - String methodName = getJavaBeanMethodName("get", attrName); - buffer.append("" - /*{ - boolean removed = <%=methodName%>().removeAll(<%=attrName%>);}*/ - ); - - if (usePCS) { - buffer.append("" - /*{ - if (removed) { - firePropertyChange(<%=constantName%>, <%=attrName%>, null); - }}*/ - ); - } - buffer.append("" - /*{ - return removed; - }*/ - ); - setOperationBody(operation, buffer.toString()); - } - - protected void createContainsChildMethod(ObjectModelClass output, - String attrName, - String attrType, - String constantName, - boolean usePCS) { - - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("contains", attrName), - "boolean", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, attrType, attrName); - StringBuilder buffer = new StringBuilder(); - String methodName = getJavaBeanMethodName("get", attrName); - buffer.append("" - /*{ - boolean contains = <%=methodName%>().contains(<%=attrName%>); - return contains; - }*/ - ); - setOperationBody(operation, buffer.toString()); - } - - protected void createContainsAllChildrenMethod(ObjectModelClass output, - String attrName, - String attrType, - String constantName, - boolean usePCS) { - - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("containsAll", attrName), - "boolean", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, "java.util.Collection<" + attrType + ">", attrName); - StringBuilder buffer = new StringBuilder(); - String methodName = getJavaBeanMethodName("get", attrName); - buffer.append("" - /*{ - boolean contains = <%=methodName%>().containsAll(<%=attrName%>); - return contains; - }*/ - ); - setOperationBody(operation, buffer.toString()); - } - - protected void createSetMethod(ObjectModelClass output, - String attrName, - String attrType, - String simpleType, - String constantName, - boolean usePCS) { - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("set", attrName), - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, attrType, attrName); - - if (usePCS) { - String methodName = getJavaBeanMethodName("get", attrName); - setOperationBody(operation, "" - /*{ - <%=simpleType%> oldValue = <%=methodName%>(); - this.<%=attrName%> = <%=attrName%>; - firePropertyChange(<%=constantName%>, oldValue, <%=attrName%>); - }*/ - ); - } else { - setOperationBody(operation, "" - /*{ - this.<%=attrName%> = <%=attrName%>; - }*/ - ); - } - } - - protected void addSerializable(ObjectModelClass input, - ObjectModelClass output, - boolean interfaceFound) { - if (!interfaceFound) { - addInterface(output, Serializable.class); - } - - // Generate the serialVersionUID - long serialVersionUID = JavaGeneratorUtil.generateSerialVersionUID(input); - - addConstant(output, + // add a fix serialVersionUID, since the class has no field nor method + addConstant(resultClassImpl, JavaGeneratorUtil.SERIAL_VERSION_UID, "long", - serialVersionUID + "L", + "1L", ObjectModelJavaModifier.PRIVATE ); + return resultClassImpl; } - /** - * Add all interfaces defines in input class and returns if - * {@link Serializable} interface was found. - * - * @param input the input model class to process - * @param output the output generated class - * @return {@code true} if {@link Serializable} was found from input, - * {@code false} otherwise - */ - protected boolean addInterfaces(ObjectModelClass input, - ObjectModelClass output) { - boolean foundSerializable = false; - for (ObjectModelInterface parentInterface : input.getInterfaces()) { - String fqn = parentInterface.getQualifiedName(); - addInterface(output, fqn); - if (Serializable.class.getName().equals(fqn)) { - foundSerializable = true; - } - } - return foundSerializable; - } - protected void addSuperClass(ObjectModelClass input, ObjectModelClass output) { // Set superclass @@ -744,151 +291,4 @@ setSuperClass(output, qualifiedName); } } - - protected ObjectModelClass generateImpl(ObjectModelClass input) { - - ObjectModelClass resultClassImpl = createClass( - input.getName() + "Impl", - input.getPackageName() - ); - - // set the abstract resulClass as the resultClassImpl super class - setSuperClass(resultClassImpl, input.getQualifiedName()); - - // add a fix serialVersionUID, since the class has no field nor method - addConstant(resultClassImpl, - JavaGeneratorUtil.SERIAL_VERSION_UID, - "long", - "1L", - ObjectModelJavaModifier.PRIVATE - ); - return resultClassImpl; - } - - protected void createPropertyChangeSupport(ObjectModelClass output) { - - addAttribute(output, - "pcs", - PropertyChangeSupport.class, - "new PropertyChangeSupport(this)", - ObjectModelJavaModifier.PROTECTED, - ObjectModelJavaModifier.FINAL - ); - - // Add PropertyListener - - ObjectModelOperation operation; - - operation = addOperation(output, - "addPropertyChangeListener", - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, PropertyChangeListener.class, "listener"); - setOperationBody(operation, "" - /*{ - pcs.addPropertyChangeListener(listener); - }*/ - ); - - operation = addOperation(output, - "addPropertyChangeListener", - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, String.class, "propertyName"); - addParameter(operation, PropertyChangeListener.class, "listener"); - setOperationBody(operation, "" - /*{ - pcs.addPropertyChangeListener(propertyName, listener); - }*/ - ); - - operation = addOperation(output, - "removePropertyChangeListener", - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, PropertyChangeListener.class, "listener"); - setOperationBody(operation, "" - /*{ - pcs.removePropertyChangeListener(listener); - }*/ - ); - - operation = addOperation(output, - "removePropertyChangeListener", - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, String.class, "propertyName"); - addParameter(operation, PropertyChangeListener.class, "listener"); - setOperationBody(operation, "" - /*{ - pcs.removePropertyChangeListener(propertyName, listener); - }*/ - ); - - operation = addOperation(output, - "firePropertyChange", - "void", - ObjectModelJavaModifier.PROTECTED - ); - addParameter(operation, String.class, "propertyName"); - addParameter(operation, Object.class, "oldValue"); - addParameter(operation, Object.class, "newValue"); - setOperationBody(operation, "" - /*{ - pcs.firePropertyChange(propertyName, oldValue, newValue); - }*/ - ); - - operation = addOperation(output, - "firePropertyChange", - "void", - ObjectModelJavaModifier.PROTECTED - ); - addParameter(operation, String.class, "propertyName"); - addParameter(operation, Object.class, "newValue"); - setOperationBody(operation, "" - /*{ - firePropertyChange(propertyName, null, newValue); - }*/ - ); - } - - protected void createGetChildMethod(ObjectModelClass output) { - ObjectModelOperation getChild = addOperation( - output, - "getChild", "<T> T", - ObjectModelJavaModifier.PROTECTED - ); - addImport(output, List.class); - - addParameter(getChild, "java.util.Collection<T>", "childs"); - addParameter(getChild, "int", "index"); - setOperationBody(getChild, "" -/*{ - T result = null; - if (childs != null) { - if (childs instanceof List) { - if (index < childs.size()) { - result = ((List<T>) childs).get(index); - } - } else { - int i = 0; - for (T o : childs) { - if (index == i) { - result = o; - break; - } - i++; - } - } - } - return result; -}*/ - ); - } - } \ No newline at end of file Added: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanTransformer.java =================================================================== --- trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanTransformer.java (rev 0) +++ trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanTransformer.java 2012-12-10 00:29:12 UTC (rev 1216) @@ -0,0 +1,214 @@ +package org.nuiton.eugene.java; + +/* + * #%L + * EUGene :: Java templates + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 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% + */ + + +/*{generator option: parentheses = false}*/ +/*{generator option: writeString = +}*/ + +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.eugene.GeneratorException; +import org.nuiton.eugene.models.object.ObjectModelAttribute; +import org.nuiton.eugene.models.object.ObjectModelClass; + +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +/** + * SimpleJavaBeanTransformer generates simple bean with pcs support + * (and nothing else) according to the JavaBeans 1.1 norm with no Impl generation mecanism. + * <p/> + * So if there is so operation described on model, you should use the + * {@link JavaBeanTransformer} instead. + * + * @author tchemit <chemit@codelutin.com> + * @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.eugene.java.SimpleJavaBeanTransformer" + * @since 2.6 + */ +public class SimpleJavaBeanTransformer extends AbstractJavaBeanTransformer { + + /** Logger. */ + private static final Log log = + LogFactory.getLog(SimpleJavaBeanTransformer.class); + + @Override + public void transformFromClass(ObjectModelClass input) { + + if (!JavaTemplatesGeneratorUtil.hasBeanStereotype(input)) { + + // not a bean + return; + } + + + if (canGenerate(input)) { + + ObjectModelClass output = generateBean(input); + } + + } + + protected boolean canGenerate(ObjectModelClass input) { + boolean canGenerate = !isInClassPath(input); + + if (canGenerate) { + + // check there is no operation on input + if (!input.getOperations().isEmpty()) { + + throw new GeneratorException( + "Can't generate a simple bean as class " + + input.getQualifiedName() + + " contains so operations." + + "\nUse instead the JavaBeanTransformer."); + } + } + return canGenerate; + } + + protected ObjectModelClass generateBean(ObjectModelClass input) { + + // test if a super class has bean stereotype + boolean superClassIsBean = false; + Collection<ObjectModelClass> superclasses = input.getSuperclasses(); + if (CollectionUtils.isNotEmpty(superclasses)) { + for (ObjectModelClass superclass : superclasses) { + if (JavaTemplatesGeneratorUtil.hasBeanStereotype(superclass)) { + superClassIsBean = true; + break; + } + } + } + + String superClass = null; + + if (!superClassIsBean) { + + // try to find a super class by tag-value + superClass = + JavaTemplatesGeneratorUtil.getBeanSuperClassTagValue( + model, input); + if (superClass != null) { + + // will act as if super class is a bean + superClassIsBean = true; + } + } + + ObjectModelClass output = + createClass(input.getName(), input.getPackageName()); + + if (superClass != null) { + setSuperClass(output, superClass); + } + if (log.isDebugEnabled()) { + log.debug("will generate " + output.getQualifiedName()); + } + + String i18nPrefix = JavaGeneratorUtil.getI18nPrefixTagValue(input, model); + if (!StringUtils.isEmpty(i18nPrefix)) { + generateI18nBlock(input, output, i18nPrefix); + } + + String noPCSTagValue = JavaTemplatesGeneratorUtil.getNoPCSTagValue(model, input); + boolean usePCS = StringUtils.isEmpty(noPCSTagValue) || + !"true".equals(noPCSTagValue.trim()); + + String noGenerateBooleanGetMethods = + JavaGeneratorUtil.getDoNotGenerateBooleanGetMethods(model, input); + boolean generateBooleanGetMethods = + StringUtils.isEmpty(noGenerateBooleanGetMethods) || + !"true".equals(noGenerateBooleanGetMethods.trim()); + + String prefix = getConstantPrefix(input, DEFAULT_CONSTANT_PREFIX); + + setConstantPrefix(prefix); + + addSuperClass(input, output); + + boolean serializableFound = addInterfaces(input, output); + + if (superClassIsBean) { + serializableFound = true; + } + + addSerializable(input, output, serializableFound); + + Set<String> constantNames = addConstantsFromDependency(input, output); + + // Get available properties + List<ObjectModelAttribute> properties = getProperties(input); + + // Add properties constant + for (ObjectModelAttribute attr : properties) { + + createPropertyConstant(output, attr, prefix, constantNames); + } + + // Add properties field + javabean methods + for (ObjectModelAttribute attr : properties) { + + createProperty(output, attr, usePCS, generateBooleanGetMethods); + } + + if (!superClassIsBean) { + + if (usePCS) { + + // Add property change support + createPropertyChangeSupport(output); + } + } + + boolean hasAMultipleProperty = containsMutiple(properties); + + // Add helper operations + if (hasAMultipleProperty) { + + if (!superClassIsBean) { + + // add getChild methods + createGetChildMethod(output); + } + } + return output; + } + + protected void addSuperClass(ObjectModelClass input, + ObjectModelClass output) { + // Set superclass + Iterator<ObjectModelClass> j = input.getSuperclasses().iterator(); + if (j.hasNext()) { + ObjectModelClass p = j.next(); + String qualifiedName = p.getQualifiedName(); + setSuperClass(output, qualifiedName); + } + } +} Property changes on: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanTransformer.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native