branch develop updated (df817ad -> 3ea5e82)
This is an automated email from the git hooks/post-receive script. New change to branch develop in repository eugene. See https://gitlab.nuiton.org/nuiton/eugene.git from df817ad Introduce a ng bean transformer (Fixes #4071) new 3ea5e82 Depreciates (and make them no more usable) old bean transformers (Fixes #4072) The 1 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit 3ea5e82b118a582adc6a37d416c050979a5f9c95 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Oct 18 12:49:25 2016 +0200 Depreciates (and make them no more usable) old bean transformers (Fixes #4072) Summary of changes: .../eugene/java/AbstractJavaBeanTransformer.java | 906 --------------------- .../org/nuiton/eugene/java/BeanTransformer.java | 846 ++++++++++++++++++- .../eugene/java/BeanTransformerTagValues.java | 81 +- .../nuiton/eugene/java/EugeneJavaTagValues.java | 117 --- .../nuiton/eugene/java/JavaBeanTransformer.java | 204 +---- .../eugene/java/JavaBeanTransformerTagValues.java | 181 ---- .../eugene/java/SimpleJavaBeanTransformer.java | 520 +----------- .../java/SimpleJavaBeanTransformerTagValues.java | 461 ----------- .../SimpleJavaBeanWithNoInterfaceTransformer.java | 716 +--------------- ...avaBeanWithNoInterfaceTransformerTagValues.java | 421 ---------- ...ion.tagvalue.provider.TagValueMetadatasProvider | 5 +- .../i18n/eugene-java-templates_en_GB.properties | 4 +- .../i18n/eugene-java-templates_fr_FR.properties | 4 +- .../eugene/java/BeanTransformerTagValuesTest.java | 74 ++ .../eugene/java/EugeneJavaTagValuesTest.java | 11 +- eugene-maven-plugin/src/it/generate/all/pom.xml | 4 +- 16 files changed, 980 insertions(+), 3575 deletions(-) delete mode 100644 eugene-java-templates/src/main/java/org/nuiton/eugene/java/AbstractJavaBeanTransformer.java delete mode 100644 eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaBeanTransformerTagValues.java delete mode 100644 eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanTransformerTagValues.java delete mode 100644 eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanWithNoInterfaceTransformerTagValues.java create mode 100644 eugene-java-templates/src/test/java/org/nuiton/eugene/java/BeanTransformerTagValuesTest.java -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository eugene. See https://gitlab.nuiton.org/nuiton/eugene.git commit 3ea5e82b118a582adc6a37d416c050979a5f9c95 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Oct 18 12:49:25 2016 +0200 Depreciates (and make them no more usable) old bean transformers (Fixes #4072) --- .../eugene/java/AbstractJavaBeanTransformer.java | 906 --------------------- .../org/nuiton/eugene/java/BeanTransformer.java | 846 ++++++++++++++++++- .../eugene/java/BeanTransformerTagValues.java | 81 +- .../nuiton/eugene/java/EugeneJavaTagValues.java | 117 --- .../nuiton/eugene/java/JavaBeanTransformer.java | 204 +---- .../eugene/java/JavaBeanTransformerTagValues.java | 181 ---- .../eugene/java/SimpleJavaBeanTransformer.java | 520 +----------- .../java/SimpleJavaBeanTransformerTagValues.java | 461 ----------- .../SimpleJavaBeanWithNoInterfaceTransformer.java | 716 +--------------- ...avaBeanWithNoInterfaceTransformerTagValues.java | 421 ---------- ...ion.tagvalue.provider.TagValueMetadatasProvider | 5 +- .../i18n/eugene-java-templates_en_GB.properties | 4 +- .../i18n/eugene-java-templates_fr_FR.properties | 4 +- .../eugene/java/BeanTransformerTagValuesTest.java | 74 ++ .../eugene/java/EugeneJavaTagValuesTest.java | 11 +- eugene-maven-plugin/src/it/generate/all/pom.xml | 4 +- 16 files changed, 980 insertions(+), 3575 deletions(-) diff --git a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/AbstractJavaBeanTransformer.java b/eugene-java-templates/src/main/java/org/nuiton/eugene/java/AbstractJavaBeanTransformer.java deleted file mode 100644 index cfb4c18..0000000 --- a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/AbstractJavaBeanTransformer.java +++ /dev/null @@ -1,906 +0,0 @@ -package org.nuiton.eugene.java; - -/* - * #%L - * EUGene :: Java templates - * %% - * 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.lang3.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.eugene.GeneratorUtil; -import org.nuiton.eugene.models.extension.tagvalue.MissingStereoTypeException; -import org.nuiton.eugene.models.object.ObjectModelAttribute; -import org.nuiton.eugene.models.object.ObjectModelClass; -import org.nuiton.eugene.models.object.ObjectModelClassifier; -import org.nuiton.eugene.models.object.ObjectModelInterface; -import org.nuiton.eugene.models.object.ObjectModelJavaModifier; -import org.nuiton.eugene.models.object.ObjectModelOperation; -import org.nuiton.eugene.models.object.ObjectModelPackage; - -import java.beans.PropertyChangeListener; -import java.beans.PropertyChangeSupport; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * Common class form javabean like templates. - * - * @author Tony Chemit - chemit@codelutin.com - * @see SimpleJavaBeanTransformer - * @see JavaBeanTransformer - * @since 2.6 - */ -public abstract class AbstractJavaBeanTransformer extends ObjectModelTransformerToJava { - - /** Logger. */ - private static final Log log = LogFactory.getLog(AbstractJavaBeanTransformer.class); - - protected final EugeneJavaTagValues javaTemplatesTagValues; - - public AbstractJavaBeanTransformer() { - javaTemplatesTagValues = new EugeneJavaTagValues(); - } - - protected boolean notFoundInClassPath(ObjectModelClass input, String className) { - String fqn = input.getPackageName() + "." + className; - boolean inClassPath = getResourcesHelper().isJavaFileInClassPath(fqn); - return !inClassPath; - } - - protected void createPropertyConstant(ObjectModelClassifier output, - ObjectModelAttribute attr, - String prefix, - Set<String> constantNames) { - - String attrName = getAttributeName(attr); - - String constantName = prefix + builder.getConstantName(attrName); - - if (!constantNames.contains(constantName)) { - - 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 String getAttributeTypeWithGeneric(ObjectModelAttribute attr) { - String attrType = getAttributeType(attr); - String generic = eugeneTagValues.getAttributeGenericTagValue(attr); - if (generic != null) { - attrType += "<" + getAttributeType(generic) + ">"; - } - return attrType; - } - - protected String getAttributeType(String attrType) { - 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, - boolean generateNotEmptyCollections) { - - String attrName = getAttributeName(attr); - String attrType = getAttributeTypeWithGeneric(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 - attrType = JavaGeneratorUtil.getAttributeInterfaceType(attr, getAttributeTypeWithGeneric(attr), true); - simpleType = JavaGeneratorUtil.getSimpleName(attrType); - } - - boolean booleanProperty = JavaGeneratorUtil.isBooleanPrimitive(attr); - - if (multiple) { - - String collectionImplementationType = JavaGeneratorUtil.getAttributeImplementationType(attr, getAttributeTypeWithGeneric(attr), true); - - // creates a getXXX (multiple) method - createGetMethod(output, - attrName, - attrType, - JavaGeneratorUtil.OPERATION_GETTER_DEFAULT_PREFIX, - generateNotEmptyCollections, - collectionImplementationType - ); - - } else { - - if (booleanProperty) { - - // creates a isXXX method - createGetMethod(output, - attrName, - attrType, - JavaGeneratorUtil.OPERATION_GETTER_BOOLEAN_PREFIX - ); - } - - if (!booleanProperty || generateBooleanGetMethods) { - - // creates a getXXX method - 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<>(); - 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, - boolean generateLayzCode, - String collectionImplementationType) { - - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName(methodPrefix, attrName), - attrType, - ObjectModelJavaModifier.PUBLIC - ); - if (generateLayzCode) { - addImport(output, collectionImplementationType); - String implementationSimpleType = JavaGeneratorUtil.getSimpleName(collectionImplementationType); - setOperationBody(operation, "" -/*{ - if (<%=attrName%> == null) { - <%=attrName%> = new <%=implementationSimpleType%>(); - } - return <%=attrName%>; -}*/ - ); - } else { - setOperationBody(operation, "" -/*{ - return <%=attrName%>; -}*/ - ); - } - - } - - 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) { - boolean booleanProperty = GeneratorUtil.isBooleanPrimitive(simpleType); - ObjectModelOperation operation = addOperation( - output, - getJavaBeanMethodName("set", attrName), - "void", - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, attrType, attrName); - - if (usePCS) { - String methodPrefix = JavaGeneratorUtil.OPERATION_GETTER_DEFAULT_PREFIX; - if (booleanProperty) { - methodPrefix = JavaGeneratorUtil.OPERATION_GETTER_BOOLEAN_PREFIX; - } - String methodName = getJavaBeanMethodName(methodPrefix, 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, - ObjectModelClassifier output, - String extraInterfaceName) { - boolean foundSerializable = false; - Set<String> added = new HashSet<>(); - for (ObjectModelInterface parentInterface : input.getInterfaces()) { - String fqn = parentInterface.getQualifiedName(); - added.add(fqn); - addInterface(output, fqn); - if (Serializable.class.getName().equals(fqn)) { - foundSerializable = true; - } - } - if (extraInterfaceName != null && !added.contains(extraInterfaceName)) { - addInterface(output, extraInterfaceName); - } - return foundSerializable; - } - - protected void createPropertyChangeSupport(ObjectModelClass output) { - - addAttribute(output, - "pcs", - PropertyChangeSupport.class, - "new PropertyChangeSupport(this)", - ObjectModelJavaModifier.PROTECTED, - ObjectModelJavaModifier.FINAL, - ObjectModelJavaModifier.TRANSIENT - ); - - // 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; -}*/ - ); - } - - protected void generateI18nBlockAndConstants(ObjectModelPackage aPackage, - ObjectModelClass input, - ObjectModelClassifier output) { - - String i18nPrefix = eugeneTagValues.getI18nPrefixTagValue(input, - aPackage, - model); - if (!StringUtils.isEmpty(i18nPrefix)) { - generateI18nBlock(input, output, i18nPrefix); - } - - String prefix = getConstantPrefix(input); - - setConstantPrefix(prefix); - - Set<String> constantNames = addConstantsFromDependency(input, output); - - // Add properties constant - for (ObjectModelAttribute attr : getProperties(input)) { - - createPropertyConstant(output, attr, prefix, constantNames); - } - } - - protected void addDefaultMethodForNoneBeanSuperClass(ObjectModelClass output, - boolean usePCS, - List<ObjectModelAttribute> properties) { - - - if (usePCS) { - - // Add property change support - createPropertyChangeSupport(output); - } - - boolean hasAMultipleProperty = containsMutiple(properties); - - // Add helper operations - if (hasAMultipleProperty) { - - // add getChild methods - createGetChildMethod(output); - } - } - - protected String wrapPrimitiveType(String attrType) { - if (JavaGeneratorUtil.isPrimitiveType(attrType)) { - attrType = JavaGeneratorUtil.getPrimitiveWrapType(attrType); - } - return attrType; - } - - protected String getGetterName(ObjectModelAttribute attribute, String attrName) { - boolean booleanProperty = JavaGeneratorUtil.isBooleanPrimitive(attribute); - String methodPrefix = JavaGeneratorUtil.OPERATION_GETTER_DEFAULT_PREFIX; - if (booleanProperty) { - methodPrefix = JavaGeneratorUtil.OPERATION_GETTER_BOOLEAN_PREFIX; - } - return getJavaBeanMethodName(methodPrefix, attrName); - } - - protected String generateName(String prefix, String name, String suffix) { - StringBuilder sb = new StringBuilder(); - if (StringUtils.isNotEmpty(prefix)) { - sb.append(prefix); - } - sb.append(name); - if (StringUtils.isNotEmpty(suffix)) { - sb.append(suffix); - } - return sb.toString(); - } - - protected boolean canGenerateAbstractClass(ObjectModelPackage aPackage, ObjectModelClass aclass, String abstractClassName) { - - boolean notInClassPath = notFoundInClassPath(aclass, abstractClassName); - - if (!notInClassPath) { - - boolean canOverride = javaTemplatesTagValues.isOverrideAbstractClasses(aclass, aPackage, model); - String fqn = aclass.getPackageName() + "." + abstractClassName; - if (!canOverride) { - String message = "Can not override abstract class " + fqn + ", already found in classpath"; - message += "\n\nTo remove this check, add the stereotype *overrideAbstractClasses* on model, package or class."; - MissingStereoTypeException exception = new MissingStereoTypeException("overrideAbstractClasses", message, aPackage, aclass); - if (log.isErrorEnabled()) { - log.error(exception.toString()); - } - throw exception; - } - if (log.isWarnEnabled()) { - log.warn("Will not generate abstract class: " + fqn); - } - } - - return notInClassPath; - - } - - protected boolean canGenerateClassWithMethods(ObjectModelPackage aPackage, ObjectModelClass aclass, String className) { - - boolean noMethods = aclass.getOperations().isEmpty(); - - if (!noMethods) { - - boolean canOverride = javaTemplatesTagValues.isAcceptBeanWithMethods(aclass, aPackage, model); - String fqn = aclass.getPackageName() + "." + className; - if (!canOverride) { - String message = "Concrete class " + fqn + " contains some methods, the current generator does not accept it."; - message += "\n\nSuch methods are not made to be designed, you should directly write them in the concrete class."; - message += "\n\nTo remove this check, add the stereotype *acceptBeanWithMethods* on model, package or class."; - MissingStereoTypeException exception = new MissingStereoTypeException("acceptBeanWithMethods", message, aPackage, aclass); - if (log.isErrorEnabled()) { - log.error(exception.toString()); - } - throw exception; - - } - if (log.isWarnEnabled()) { - log.warn("Will generate class (but designed methods won't be generated): " + fqn); - } - } - - return true; - - } - - protected boolean isUseJava8() { - return javaTemplatesTagValues.isUseJava8(model); - } - -} diff --git a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/BeanTransformer.java b/eugene-java-templates/src/main/java/org/nuiton/eugene/java/BeanTransformer.java index 8d169a7..940b08e 100644 --- a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/BeanTransformer.java +++ b/eugene-java-templates/src/main/java/org/nuiton/eugene/java/BeanTransformer.java @@ -33,9 +33,11 @@ import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; 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.ObjectModelClass; +import org.nuiton.eugene.models.object.ObjectModelClassifier; import org.nuiton.eugene.models.object.ObjectModelInterface; import org.nuiton.eugene.models.object.ObjectModelJavaModifier; import org.nuiton.eugene.models.object.ObjectModelOperation; @@ -43,10 +45,15 @@ import org.nuiton.eugene.models.object.ObjectModelPackage; import org.nuiton.util.beans.Binder; import org.nuiton.util.beans.BinderFactory; +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; +import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; /** * Generates a bean and a helper class around it. @@ -65,21 +72,24 @@ import java.util.List; * @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.eugene.java.BeanTransformer" * @since 3.0 */ -public class BeanTransformer extends AbstractJavaBeanTransformer { +public class BeanTransformer extends ObjectModelTransformerToJava { /** Logger. */ private static final Log log = LogFactory.getLog(BeanTransformer.class); ImmutableMap<ObjectModelClass, String> classesNameTranslation; - ImmutableMap<ObjectModelClass, String> helpersNameTranslation; + ImmutableMap<ObjectModelClass, String> helpersNameTranslation; ImmutableSet<ObjectModelClass> classes; + ImmutableSet<ObjectModelClass> helpers; protected boolean useJava8; + protected final EugeneJavaTagValues javaTemplatesTagValues; protected final BeanTransformerTagValues beanTagValues; public BeanTransformer() { + javaTemplatesTagValues = new EugeneJavaTagValues(); beanTagValues = new BeanTransformerTagValues(); } @@ -87,7 +97,7 @@ public class BeanTransformer extends AbstractJavaBeanTransformer { public void transformFromModel(ObjectModel model) { super.transformFromModel(model); - useJava8 = isUseJava8(); + useJava8 = javaTemplatesTagValues.isUseJava8(model); ImmutableMap.Builder<ObjectModelClass, String> classesNameTranslationBuilder = new ImmutableMap.Builder<>(); ImmutableMap.Builder<ObjectModelClass, String> helpersNameTranslationBuilder = new ImmutableMap.Builder<>(); ImmutableSet.Builder<ObjectModelClass> classesBuilder = new ImmutableSet.Builder<>(); @@ -204,13 +214,12 @@ public class BeanTransformer extends AbstractJavaBeanTransformer { String className = classesNameTranslation.get(input); String generatedClassName = "Generated" + className; - boolean generateClass = canGenerateClassWithMethods(aPackage, input, className) - && notFoundInClassPath(input, className); + boolean generateClass = notFoundInClassPath(input, className); if (generateClass) { generateClass(input, className, generatedClassName); } - boolean generateGeneratedClass = canGenerateAbstractClass(aPackage, input, generatedClassName); + boolean generateGeneratedClass = canGenerateAbstractClass(input, generatedClassName); if (generateGeneratedClass) { generateGeneratedClass(aPackage, input, generatedClassName); } @@ -226,7 +235,7 @@ public class BeanTransformer extends AbstractJavaBeanTransformer { generateHelper(input, generatedHelperClassName, helperClassName); } - if (canGenerateAbstractClass(aPackage, input, generatedHelperClassName)) { + if (canGenerateAbstractClass(input, generatedHelperClassName)) { generateGeneratedHelper(aPackage, input, className, generatedHelperClassName); } @@ -313,9 +322,9 @@ public class BeanTransformer extends AbstractJavaBeanTransformer { // Get available properties List<ObjectModelAttribute> properties = getProperties(input); - boolean usePCS = !javaTemplatesTagValues.isSkipGeneratePropertyChangeSupport(input, aPackage, model); + boolean usePCS = beanTagValues.isGeneratePropertyChangeSupport(input, aPackage, model); boolean generateBooleanGetMethods = eugeneTagValues.isGenerateBooleanGetMethods(input, aPackage, model); - boolean generateNotEmptyCollections = !javaTemplatesTagValues.isSkipGenerateNotEmptyCollections(input, aPackage, model); + boolean generateNotEmptyCollections = beanTagValues.isGenerateNotEmptyCollections(input, aPackage, model); // Add properties field + javabean methods for (ObjectModelAttribute attr : properties) { @@ -351,7 +360,7 @@ public class BeanTransformer extends AbstractJavaBeanTransformer { String abstractClassName) { ObjectModelClass output = createAbstractClass(abstractClassName, aPackage.getName()); - String superClassName = getAbstractDefaultsSuperClassName(aPackage, aClass); + String superClassName = getGeneratedHelperSuperClassName(aPackage, aClass); if (StringUtils.isNotBlank(superClassName)) { setSuperClass(output, superClassName); @@ -508,16 +517,15 @@ public class BeanTransformer extends AbstractJavaBeanTransformer { ObjectModelOperation operation; - - operation = addOperation( - output, - newPreficateMethodName, - "<BeanType extends " + typeName + "> Predicate<BeanType>", - ObjectModelJavaModifier.STATIC, - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, simpleType, attrName); - String getterName = getGetterName(attribute, attrName); + operation = addOperation( + output, + newPreficateMethodName, + "<BeanType extends " + typeName + "> Predicate<BeanType>", + ObjectModelJavaModifier.STATIC, + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, simpleType, attrName); + String getterName = getGetterName(attribute, attrName); if (useJava8) { setOperationBody(operation, "" @@ -568,7 +576,7 @@ public class BeanTransformer extends AbstractJavaBeanTransformer { } if (atLeastOnePropertyFound) { - if(useJava8) { + if (useJava8) { addImport(output, Collection.class); addImport(output, List.class); @@ -619,7 +627,7 @@ public class BeanTransformer extends AbstractJavaBeanTransformer { useJava8 ? typeName + "::" + getterName : newFunctionMethodName + "()", ObjectModelJavaModifier.FINAL, ObjectModelJavaModifier.STATIC, - useJava8 ? ObjectModelJavaModifier.PUBLIC: ObjectModelJavaModifier.PROTECTED + useJava8 ? ObjectModelJavaModifier.PUBLIC : ObjectModelJavaModifier.PROTECTED ); if (!useJava8) { @@ -701,7 +709,7 @@ public class BeanTransformer extends AbstractJavaBeanTransformer { } - protected String getAbstractDefaultsSuperClassName(ObjectModelPackage aPackage, ObjectModelClass aClass) { + protected String getGeneratedHelperSuperClassName(ObjectModelPackage aPackage, ObjectModelClass aClass) { String superClassName = null; // test if a super class has bean stereotype @@ -721,15 +729,21 @@ public class BeanTransformer extends AbstractJavaBeanTransformer { if (!superClassIsBean) { // try to find a super class by tag-value - superClassName = beanTagValues.getDefaultsSuperClassTagValue(aClass, aPackage, model); + superClassName = beanTagValues.getHelperSuperClassTagValue(aClass, aPackage, model); } return superClassName; } - @Override protected String getAttributeType(ObjectModelAttribute attr) { - String attrType = super.getAttributeType(attr); + String attrType = attr.getType(); + if (attr.hasAssociationClass()) { + attrType = attr.getAssociationClass().getName(); + } + return getAttributeType(attrType); + } + + protected String getAttributeType(String attrType) { if (!JavaGeneratorUtil.isPrimitiveType(attrType)) { boolean hasClass = model.hasClass(attrType); if (hasClass) { @@ -743,18 +757,782 @@ public class BeanTransformer extends AbstractJavaBeanTransformer { return attrType; } - protected String getAttributeType(String attrType) { - if (!JavaGeneratorUtil.isPrimitiveType(attrType)) { - boolean hasClass = model.hasClass(attrType); - if (hasClass) { - ObjectModelClass attributeClass = model.getClass(attrType); - String attributeType = classesNameTranslation.get(attributeClass); - if (attributeType != null) { - attrType = attributeClass.getPackageName() + "." + attributeType; + protected boolean notFoundInClassPath(ObjectModelClass input, String className) { + String fqn = input.getPackageName() + "." + className; + boolean inClassPath = getResourcesHelper().isJavaFileInClassPath(fqn); + return !inClassPath; + } + + protected void createProperty(ObjectModelClass output, + ObjectModelAttribute attr, + boolean usePCS, + boolean generateBooleanGetMethods, + boolean generateNotEmptyCollections) { + + String attrName = getAttributeName(attr); + String attrType = getAttributeTypeWithGeneric(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 + ); + + // Change type for Multiple attribute + attrType = JavaGeneratorUtil.getAttributeInterfaceType(attr, getAttributeTypeWithGeneric(attr), true); + simpleType = JavaGeneratorUtil.getSimpleName(attrType); + } + + boolean booleanProperty = JavaGeneratorUtil.isBooleanPrimitive(attr); + + if (multiple) { + + String collectionImplementationType = JavaGeneratorUtil.getAttributeImplementationType(attr, getAttributeTypeWithGeneric(attr), true); + + // creates a getXXX (multiple) method + createGetMethod(output, + attrName, + attrType, + JavaGeneratorUtil.OPERATION_GETTER_DEFAULT_PREFIX, + generateNotEmptyCollections, + collectionImplementationType + ); + + } else { + + if (booleanProperty) { + + // creates a isXXX method + createGetMethod(output, + attrName, + attrType, + JavaGeneratorUtil.OPERATION_GETTER_BOOLEAN_PREFIX + ); + } + + if (!booleanProperty || generateBooleanGetMethods) { + + // creates a getXXX method + 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<>(); + 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, + boolean generateLayzCode, + String collectionImplementationType) { + + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName(methodPrefix, attrName), + attrType, + ObjectModelJavaModifier.PUBLIC + ); + if (generateLayzCode) { + addImport(output, collectionImplementationType); + String implementationSimpleType = JavaGeneratorUtil.getSimpleName(collectionImplementationType); + setOperationBody(operation, "" +/*{ + if (<%=attrName%> == null) { + <%=attrName%> = new <%=implementationSimpleType%>(); + } + return <%=attrName%>; +}*/ + ); + } else { + setOperationBody(operation, "" +/*{ + return <%=attrName%>; +}*/ + ); + } + + } + + 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) { + + 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) { + boolean booleanProperty = GeneratorUtil.isBooleanPrimitive(simpleType); + ObjectModelOperation operation = addOperation( + output, + getJavaBeanMethodName("set", attrName), + "void", + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, attrType, attrName); + + if (usePCS) { + String methodPrefix = JavaGeneratorUtil.OPERATION_GETTER_DEFAULT_PREFIX; + if (booleanProperty) { + methodPrefix = JavaGeneratorUtil.OPERATION_GETTER_BOOLEAN_PREFIX; + } + String methodName = getJavaBeanMethodName(methodPrefix, 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, + ObjectModelClassifier output, + String extraInterfaceName) { + boolean foundSerializable = false; + Set<String> added = new HashSet<>(); + for (ObjectModelInterface parentInterface : input.getInterfaces()) { + String fqn = parentInterface.getQualifiedName(); + added.add(fqn); + addInterface(output, fqn); + if (Serializable.class.getName().equals(fqn)) { + foundSerializable = true; + } + } + if (extraInterfaceName != null && !added.contains(extraInterfaceName)) { + addInterface(output, extraInterfaceName); + } + return foundSerializable; + } + + protected void createPropertyChangeSupport(ObjectModelClass output) { + + addAttribute(output, + "pcs", + PropertyChangeSupport.class, + "new PropertyChangeSupport(this)", + ObjectModelJavaModifier.PROTECTED, + ObjectModelJavaModifier.FINAL, + ObjectModelJavaModifier.TRANSIENT + ); + + // 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; +}*/ + ); + } + + protected void generateI18nBlockAndConstants(ObjectModelPackage aPackage, + ObjectModelClass input, + ObjectModelClassifier output) { + + String i18nPrefix = eugeneTagValues.getI18nPrefixTagValue(input, + aPackage, + model); + if (!StringUtils.isEmpty(i18nPrefix)) { + generateI18nBlock(input, output, i18nPrefix); + } + + String prefix = getConstantPrefix(input); + + setConstantPrefix(prefix); + + Set<String> constantNames = addConstantsFromDependency(input, output); + + // Add properties constant + for (ObjectModelAttribute attr : getProperties(input)) { + + createPropertyConstant(output, attr, prefix, constantNames); + } + } + + protected void addDefaultMethodForNoneBeanSuperClass(ObjectModelClass output, + boolean usePCS, + List<ObjectModelAttribute> properties) { + + + if (usePCS) { + + // Add property change support + createPropertyChangeSupport(output); + } + + boolean hasAMultipleProperty = containsMultiple(properties); + + // Add helper operations + if (hasAMultipleProperty) { + + // add getChild methods + createGetChildMethod(output); + } + } + + protected String wrapPrimitiveType(String attrType) { + if (JavaGeneratorUtil.isPrimitiveType(attrType)) { + attrType = JavaGeneratorUtil.getPrimitiveWrapType(attrType); + } return attrType; } + protected String getGetterName(ObjectModelAttribute attribute, String attrName) { + boolean booleanProperty = JavaGeneratorUtil.isBooleanPrimitive(attribute); + String methodPrefix = JavaGeneratorUtil.OPERATION_GETTER_DEFAULT_PREFIX; + if (booleanProperty) { + methodPrefix = JavaGeneratorUtil.OPERATION_GETTER_BOOLEAN_PREFIX; + } + return getJavaBeanMethodName(methodPrefix, attrName); + } + + protected String generateName(String prefix, String name, String suffix) { + StringBuilder sb = new StringBuilder(); + if (StringUtils.isNotEmpty(prefix)) { + sb.append(prefix); + } + sb.append(name); + if (StringUtils.isNotEmpty(suffix)) { + sb.append(suffix); + } + return sb.toString(); + } + + protected boolean canGenerateAbstractClass(ObjectModelClass aClass, String abstractClassName) { + + boolean inClassPath = !notFoundInClassPath(aClass, abstractClassName); + + if (inClassPath) { + throw new IllegalStateException(String.format("Can't generate %s, already found in class-path, this is a generated class, you should not ovveride it.\n\nPlease remove it from class path and use the %s class instead.", aClass.getPackageName() + "." + abstractClassName, aClass)); + } + + return true; + + } + + protected void createPropertyConstant(ObjectModelClassifier output, + ObjectModelAttribute attr, + String prefix, + Set<String> constantNames) { + + String attrName = getAttributeName(attr); + + String constantName = prefix + builder.getConstantName(attrName); + + if (!constantNames.contains(constantName)) { + + 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 getAttributeTypeWithGeneric(ObjectModelAttribute attr) { + String attrType = getAttributeType(attr); + String generic = eugeneTagValues.getAttributeGenericTagValue(attr); + if (generic != null) { + attrType += "<" + getAttributeType(generic) + ">"; + } + return attrType; + } + + protected boolean containsMultiple(List<ObjectModelAttribute> attributes) { + + boolean result = false; + + for (ObjectModelAttribute attr : attributes) { + + if (JavaGeneratorUtil.isNMultiplicity(attr)) { + result = true; + + break; + } + + } + return result; + } + + } diff --git a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/BeanTransformerTagValues.java b/eugene-java-templates/src/main/java/org/nuiton/eugene/java/BeanTransformerTagValues.java index ace441e..203b737 100644 --- a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/BeanTransformerTagValues.java +++ b/eugene-java-templates/src/main/java/org/nuiton/eugene/java/BeanTransformerTagValues.java @@ -67,7 +67,7 @@ public class BeanTransformerTagValues extends DefaultTagValueMetadatasProvider { /** * To generate or not guava predicates on each property of the bean. * - * You can globaly use it on the complete model or to a specific classifier. + * You can globally use it on the complete model or to a specific classifier. * * @see #isGenerateHelperPredicates(ObjectModelClassifier, ObjectModelPackage, ObjectModel) * @since 3.0 @@ -77,7 +77,7 @@ public class BeanTransformerTagValues extends DefaultTagValueMetadatasProvider { /** * To generate or not guava functions on each property of the bean. * - * You can globaly use it on the complete model or to a specific classifier. + * You can globally use it on the complete model or to a specific classifier. * * @see #isGenerateHelperFunctions(ObjectModelClassifier, ObjectModelPackage, ObjectModel) * @since 3.0 @@ -87,7 +87,7 @@ public class BeanTransformerTagValues extends DefaultTagValueMetadatasProvider { /** * To generate or not constructors methods on Default classes. * - * You can globaly use it on the complete model, package or on a specific classifier. + * You can globally use it on the complete model, package or on a specific classifier. * * @see #isGenerateHelperConstructors(ObjectModelClassifier, ObjectModelPackage, ObjectModel)} * @since 3.0 @@ -95,10 +95,30 @@ public class BeanTransformerTagValues extends DefaultTagValueMetadatasProvider { generateHelperConstructors(n("eugene.bean.tagvalue.generateHelperConstructors"), boolean.class, "true", ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), /** + * Tag value to generate property change support on generated beans. + * + * You can globally use it on the complete model, on packages, or to a specific classifier. + * + * @see #isGeneratePropertyChangeSupport(ObjectModelClassifier, ObjectModelPackage, ObjectModel) + * @since 3.0 + */ + generatePropertyChangeSupport(n("eugene.bean.tagvalue.generatePropertyChangeSupport"), boolean.class, "true", ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), + + /** + * Tag value to generate lazy instantiation of any collection to avoid NPEs. + * + * You can globally use it on the complete model or a package, or to a specific classifier. + * + * @see #isGenerateNotEmptyCollections(ObjectModelClassifier, ObjectModelPackage, ObjectModel) + * @since 3.0 + */ + generateNotEmptyCollections(n("eugene.bean.tagvalue.generateNotEmptyCollections"), boolean.class, "true", ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), + + /** * Tag value to use a super class for generated bean. * - * If the bean needs Property change support (says you use the tag-value {@link EugeneJavaTagValues.Store#generatePropertyChangeSupport}, - * then your class must provide evrything for it. + * If the bean needs Property change support (says you use the tag-value {@link Store#generatePropertyChangeSupport}, + * then your class must provide everything for it. * * More over, if you use some collections in your bean you must also define * two method named {@code getChild(Collection list, int index)} and @@ -146,7 +166,7 @@ public class BeanTransformerTagValues extends DefaultTagValueMetadatasProvider { * } * </pre> * - * You can globaly use it on the complete model or to a specific classifier. + * You can globally use it on the complete model or to a specific classifier. * * @see #getSuperClassTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) * @since 3.0 @@ -156,9 +176,9 @@ public class BeanTransformerTagValues extends DefaultTagValueMetadatasProvider { /** * Tag value to use a super super-class for generated defaults class of a simple bean. * - * You can globaly use it on the complete model or to a specific classifier. + * You can globally use it on the complete model or to a specific classifier. * - * @see #getDefaultsSuperClassTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) + * @see #getHelperSuperClassTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) * @since 3.0 */ helperSuperClass(n("eugene.bean.tagvalue.helperSuperClass"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), @@ -166,7 +186,7 @@ public class BeanTransformerTagValues extends DefaultTagValueMetadatasProvider { /** * To add a prefix on the name of each generated bean class. * - * You can globaly use it on the complete model or to a specific classifier. + * You can globally use it on the complete model or to a specific classifier. * * @see #getClassNamePrefixTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) * @since 3.0 @@ -176,7 +196,7 @@ public class BeanTransformerTagValues extends DefaultTagValueMetadatasProvider { /** * To add a prefix on the name of each generated bean class. * - * You can globaly use it on the complete model or to a specific classifier. + * You can globally use it on the complete model or to a specific classifier. * * @see #getClassNameSuffixTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) * @since 3.0 @@ -186,7 +206,7 @@ public class BeanTransformerTagValues extends DefaultTagValueMetadatasProvider { /** * To add a prefix on the name of each generated bean class. * - * You can globaly use it on the complete model or to a specific classifier. + * You can globally use it on the complete model or to a specific classifier. * * @see #getHelperClassNamePrefixTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) * @since 3.0 @@ -196,7 +216,7 @@ public class BeanTransformerTagValues extends DefaultTagValueMetadatasProvider { /** * To add a suffix on the name of each generated bean class. * - * You can globaly use it on the complete model or to a specific classifier. + * You can globally use it on the complete model or to a specific classifier. * * @see #getHelperClassNameSuffixTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) * @since 3.0 @@ -282,7 +302,7 @@ public class BeanTransformerTagValues extends DefaultTagValueMetadatasProvider { * @see Store#helperSuperClass * @since 3.0 */ - public String getDefaultsSuperClassTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { + public String getHelperSuperClassTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { return TagValueUtil.findTagValue(Store.helperSuperClass, classifier, aPackage, model); } @@ -418,4 +438,39 @@ public class BeanTransformerTagValues extends DefaultTagValueMetadatasProvider { return TagValueUtil.findBooleanTagValue(Store.generateHelperConstructors, classifier, aPackage, model); } + /** + * Obtain the value of the {@link Store#generatePropertyChangeSupport} tag value on the given model, package or classifier. + * + * It will first look on the model, then and package and then in the given classifier. + * + * If no value found, then will use the default value of the tag value. + * + * @param classifier classifier to seek + * @param aPackage package to seek + * @param model model to seek + * @return the none empty value of the found tag value or {@code null} if not found nor empty. + * @see Store#generatePropertyChangeSupport + * @since 3.0 + */ + public boolean isGeneratePropertyChangeSupport(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { + return TagValueUtil.findBooleanTagValue(Store.generatePropertyChangeSupport, classifier, aPackage, model); + } + + /** + * Obtain the value of the {@link Store#generateNotEmptyCollections} tag value on the given model, package or classifier. + * + * It will first look on the model, then and package and then in the given classifier. + * + * If no value found, then will use the default value of the tag value. + * + * @param classifier classifier to seek + * @param aPackage package to seek + * @param model model to seek + * @return the none empty value of the found tag value or {@code null} if not found nor empty. + * @see Store#generateNotEmptyCollections + * @since 3.0 + */ + public boolean isGenerateNotEmptyCollections(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { + return TagValueUtil.findBooleanTagValue(Store.generateNotEmptyCollections, classifier, aPackage, model); + } } diff --git a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/EugeneJavaTagValues.java b/eugene-java-templates/src/main/java/org/nuiton/eugene/java/EugeneJavaTagValues.java index e782605..4f6906f 100644 --- a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/EugeneJavaTagValues.java +++ b/eugene-java-templates/src/main/java/org/nuiton/eugene/java/EugeneJavaTagValues.java @@ -61,51 +61,6 @@ public class EugeneJavaTagValues extends DefaultTagValueMetadatasProvider { bean(n("eugene.java.stereotype.bean"), boolean.class, null, ObjectModelClassifier.class, ObjectModelPackage.class), /** - * Tag value to authorize user to generate some bean with methods, some generators won't generate them : Lots code). - * - * By default, user should never add methods in bean classes, simply write them in your java code!. - * - * You can globally use it on the complete model, on packages, or to a specific classifier. - * - * @see #isAcceptBeanWithMethods(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 3.0 - */ - acceptBeanWithMethods(n("eugene.java.tagvalue.acceptBeanWithMethods"), boolean.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - - /** - * Tag value to authorize user to override abstract classes. - * - * By default, user should never override abstract classes but works on implementation ones. - * - * You can globally use it on the complete model, on packages, or to a specific classifier. - * - * @see #isOverrideAbstractClasses(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 3.0 - */ - overrideAbstractClasses(n("eugene.java.tagvalue.overrideAbstractClasses"), boolean.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - /** - * Tag value to generate property change support on generated beans. - * - * You can globally use it on the complete model, on packages, or to a specific classifier. - * - * @see #isSkipGeneratePropertyChangeSupport(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 3.0 - */ - generatePropertyChangeSupport(n("eugene.java.tagvalue.generatePropertyChangeSupport"), boolean.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - /** - * Tag value to generate lazy instantiation of any collection to avoid NPEs. - * - * You can globally use it on the complete model or a package, or to a specific classifier. - * - * @see #isSkipGenerateNotEmptyCollections(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 3.0 - */ - generateNotEmptyCollections(n("eugene.java.tagvalue.generateNotEmptyCollections"), boolean.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - /** * To use java 8 new syntax and api in generation. * * You can globally use it on the complete model. @@ -169,78 +124,6 @@ public class EugeneJavaTagValues extends DefaultTagValueMetadatasProvider { } /** - * Obtain the value of the {@link Store#acceptBeanWithMethods} tag value on the given model, package or classifier. - * - * It will first look on the model, then and package and then in the given classifier. - * - * If no value found, then will use the default value of the tag value. - * - * @param classifier classifier to seek - * @param aPackage package to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#acceptBeanWithMethods - * @since 3.0 - */ - public boolean isAcceptBeanWithMethods(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findBooleanTagValue(Store.acceptBeanWithMethods, classifier, aPackage, model); - } - - /** - * Obtain the value of the {@link Store#overrideAbstractClasses} tag value on the given model, package or classifier. - * - * It will first look on the model, then and package and then in the given classifier. - * - * If no value found, then will use the default value of the tag value. - * - * @param classifier classifier to seek - * @param aPackage package to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#overrideAbstractClasses - * @since 3.0 - */ - public boolean isOverrideAbstractClasses(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findBooleanTagValue(Store.overrideAbstractClasses, classifier, aPackage, model); - } - - /** - * Obtain the value of the {@link Store#generatePropertyChangeSupport} tag value on the given model, package or classifier. - * - * It will first look on the model, then and package and then in the given classifier. - * - * If no value found, then will use the default value of the tag value. - * - * @param classifier classifier to seek - * @param aPackage package to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#generatePropertyChangeSupport - * @since 2.12 - */ - public boolean isSkipGeneratePropertyChangeSupport(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findBooleanTagValue(Store.generatePropertyChangeSupport, classifier, aPackage, model); - } - - /** - * Obtain the value of the {@link Store#generateNotEmptyCollections} tag value on the given model, package or classifier. - * - * It will first look on the model, then and package and then in the given classifier. - * - * If no value found, then will use the default value of the tag value. - * - * @param classifier classifier to seek - * @param aPackage package to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#generateNotEmptyCollections - * @since 2.12 - */ - public boolean isSkipGenerateNotEmptyCollections(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findBooleanTagValue(Store.generateNotEmptyCollections, classifier, aPackage, model); - } - - /** * Obtain the value of the {@link Store#java8} tag value on the given model. * * @param model model to seek diff --git a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java b/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java index c47e3b7..854aab1 100644 --- a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java +++ b/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java @@ -21,218 +21,24 @@ */ package org.nuiton.eugene.java; -import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.logging.Log; -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.ObjectModelJavaModifier; -import org.nuiton.eugene.models.object.ObjectModelOperation; -import org.nuiton.eugene.models.object.ObjectModelPackage; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; - -/*{generator option: parentheses = false}*/ -/*{generator option: writeString = +}*/ /** * JavaBeanTransformer generates simple bean with pcs support * (and nothing else) according to the JavaBeans 1.1 norm. * + * <b>This transformer is deprecated and you should use now {@link BeanTransformer} instead.</b> + * * @author Tony Chemit - chemit@codelutin.com * @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.eugene.java.JavaBeanTransformer" * @since 2.0.2 + * @deprecated since 3.0, no more usable, replaced by {@link BeanTransformer}. */ -public class JavaBeanTransformer extends AbstractJavaBeanTransformer { - - private static final Log log = LogFactory.getLog(JavaBeanTransformer.class); - - protected final JavaBeanTransformerTagValues javaBeanTransformerTagValues; - - public JavaBeanTransformer() { - javaBeanTransformerTagValues = new JavaBeanTransformerTagValues(); - } +public class JavaBeanTransformer extends ObjectModelTransformerToJava { @Override public void transformFromClass(ObjectModelClass input) { - - ObjectModelPackage aPackage = getPackage(input); - - if (!javaTemplatesTagValues.isBean(input, aPackage)) { - - // not a bean - return; - } - - ObjectModelClass output = null; - - if (canGenerateAbstractClass(aPackage, input, input.getName())) { - - // nothing more to do - output = generateAbstract(aPackage, input); - } - - if (canGenerateImpl(input, output)) { - - generateImpl(input); - } - } - - protected boolean canGenerateImpl(ObjectModelClass input, ObjectModelClass abstractOutput) { - String fqn = input.getQualifiedName() + "Impl"; - - if (getResourcesHelper().isJavaFileInClassPath(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<>(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; - } - - - protected void createAbstractOperations(ObjectModelClass ouput, Iterable<ObjectModelOperation> operations) { - JavaGeneratorUtil.cloneOperations( - this, - operations, - ouput, - true, - ObjectModelJavaModifier.ABSTRACT - ); + throw new IllegalStateException("You can't use any longer this transformer, please use now " + BeanTransformer.class.getName() + "."); } - protected ObjectModelClass generateAbstract(ObjectModelPackage aPackage, 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 (javaTemplatesTagValues.isBean(superclass, aPackage)) { - superClassIsBean = true; - break; - } - } - } - - String superClass = null; - - if (!superClassIsBean) { - - // try to find a super class by tag-value - superClass = javaBeanTransformerTagValues.getBeanSuperClassTagValue(input, aPackage, model); - if (superClass != null) { - - // will act as if super class is a bean - superClassIsBean = true; - } - } - - ObjectModelClass output = - createAbstractClass(input.getName(), input.getPackageName()); - - if (superClass != null) { - setSuperClass(output, superClass); - } - if (log.isDebugEnabled()) { - log.debug("will generate " + output.getQualifiedName()); - } - - addSuperClass(input, output); - - boolean serializableFound = addInterfaces(input, output, null); - - if (superClassIsBean) { - serializableFound = true; - } - - addSerializable(input, output, serializableFound); - - generateI18nBlockAndConstants(aPackage, input, output); - - boolean usePCS = !javaTemplatesTagValues.isSkipGeneratePropertyChangeSupport(input, aPackage, model); - - boolean generateBooleanGetMethods = eugeneTagValues.isGenerateBooleanGetMethods(input, aPackage, model); - boolean generateNotEmptyCollections = !javaTemplatesTagValues.isSkipGenerateNotEmptyCollections(input, aPackage, model); - - // Get available properties - List<ObjectModelAttribute> properties = getProperties(input); - - // Add properties field + javabean methods - for (ObjectModelAttribute attr : properties) { - - createProperty(output, attr, usePCS, generateBooleanGetMethods, generateNotEmptyCollections); - } - - // Add operations - createAbstractOperations(output, input.getOperations()); - - if (!superClassIsBean) { - addDefaultMethodForNoneBeanSuperClass(output, usePCS, properties); - } - - return output; - } - - 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 addSuperClass(ObjectModelClass input, ObjectModelClass output) { - // Set superclass - Iterator<ObjectModelClass> j = input.getSuperclasses().iterator(); - if (j.hasNext()) { - ObjectModelClass p = j.next(); - // We want to set the inheritance to the implementation class of the father - // Ex for model : A -> B (a inherits B) we want : A -> BImpl -> B - String qualifiedName = p.getQualifiedName() + "Impl"; - setSuperClass(output, qualifiedName); - } - } } diff --git a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaBeanTransformerTagValues.java b/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaBeanTransformerTagValues.java deleted file mode 100644 index f2a20e8..0000000 --- a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaBeanTransformerTagValues.java +++ /dev/null @@ -1,181 +0,0 @@ -package org.nuiton.eugene.java; - -/* - * #%L - * EUGene :: Java templates - * %% - * 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% - */ - -import com.google.common.collect.ImmutableSet; -import org.nuiton.eugene.models.extension.tagvalue.TagValueMetadata; -import org.nuiton.eugene.models.extension.tagvalue.TagValueUtil; -import org.nuiton.eugene.models.extension.tagvalue.matcher.EqualsTagValueNameMatcher; -import org.nuiton.eugene.models.extension.tagvalue.provider.DefaultTagValueMetadatasProvider; -import org.nuiton.eugene.models.object.ObjectModel; -import org.nuiton.eugene.models.object.ObjectModelClassifier; -import org.nuiton.eugene.models.object.ObjectModelPackage; - -import java.util.Set; - -import static org.nuiton.i18n.I18n.n; -import static org.nuiton.i18n.I18n.t; - -/** - * Defines all tag values managed by Java templates. - * - * @author Tony Chemit - chemit@codelutin.com - * @plexus.component role="org.nuiton.eugene.models.extension.tagvalue.provider.TagValueMetadatasProvider" role-hint="eugene-java-templates" - * @since 2.5.6 - */ -public class JavaBeanTransformerTagValues extends DefaultTagValueMetadatasProvider { - - @Override - public String getDescription() { - return t("eugene.javaBeanTransformer.tagvalues"); - } - - public enum Store implements TagValueMetadata { - - /** - * Tag value to use a super class for generated bean. - * - * If the bean needs Property change support (says you use the {@link EugeneJavaTagValues.Store#generatePropertyChangeSupport}), - * then your class must provide everything for it. - * - * More over, if you use some collections in your bean you must also define - * two method named {@code getChild(Collection list, int index)} and - * {@code getChild(List list, int index)} - * - * See new code to know minimum stuff to add in your class for this purpose. - * <pre> - * public abstract class AbstractBean implements Serializable { - * - * private static final long serialVersionUID = 1L; - * - * protected final PropertyChangeSupport pcs = new PropertyChangeSupport(this); - * - * public void addPropertyChangeListener(PropertyChangeListener listener) { - * pcs.addPropertyChangeListener(listener); - * } - * - * public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { - * pcs.addPropertyChangeListener(propertyName, listener); - * } - * - * public void removePropertyChangeListener(PropertyChangeListener listener) { - * pcs.removePropertyChangeListener(listener); - * } - * - * public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { - * pcs.removePropertyChangeListener(propertyName, listener); - * } - * - * protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { - * pcs.firePropertyChange(propertyName, oldValue, newValue); - * } - * - * protected void firePropertyChange(String propertyName, Object newValue) { - * firePropertyChange(propertyName, null, newValue); - * } - * - * protected <T> T getChild(Collection<T> list, int index) { - * return CollectionUtil.getOrNull(list, index); - * } - * - * protected <T> T getChild(List<T> list, int index) { - * return CollectionUtil.getOrNull(list, index); - * } - * } - * </pre> - * - * You can globaly use it on the complete model or to a specific classifier. - * - * @see #getBeanSuperClassTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 2.5.6 - */ - beanSuperClass(n("eugene.javaBeanTransformer.tagvalue.beanSuperClass"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class); - private final Set<Class<?>> targets; - private final Class<?> type; - private final String i18nDescriptionKey; - private final String defaultValue; - - Store(String i18nDescriptionKey, Class<?> type, String defaultValue, Class<?>... targets) { - this.targets = ImmutableSet.copyOf(targets); - this.type = type; - this.i18nDescriptionKey = i18nDescriptionKey; - this.defaultValue = defaultValue; - } - - @Override - public String getName() { - return name(); - } - - @Override - public Set<Class<?>> getTargets() { - return targets; - } - - @Override - public Class<?> getType() { - return type; - } - - @Override - public Class<EqualsTagValueNameMatcher> getMatcherClass() { - return EqualsTagValueNameMatcher.class; - } - - @Override - public String getDescription() { - return t(i18nDescriptionKey); - } - - @Override - public String getDefaultValue() { - return defaultValue; - } - - @Override - public boolean isDeprecated() { - return false; - } - - } - - public JavaBeanTransformerTagValues() { - super((TagValueMetadata[]) Store.values()); - } - - /** - * Obtain the value of the {@link Store#beanSuperClass} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#beanSuperClass - * @since 2.3 - */ - public String getBeanSuperClassTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findTagValue(Store.beanSuperClass, classifier, aPackage, model); - } - -} diff --git a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanTransformer.java b/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanTransformer.java index 7bc676e..d09c73f 100644 --- a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanTransformer.java +++ b/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanTransformer.java @@ -22,540 +22,32 @@ package org.nuiton.eugene.java; * #L% */ -/*{generator option: parentheses = false}*/ -/*{generator option: writeString = +}*/ - -import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.nuiton.eugene.models.object.ObjectModel; -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 org.nuiton.eugene.models.object.ObjectModelPackage; -import org.nuiton.util.beans.Binder; -import org.nuiton.util.beans.BinderFactory; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; /** * SimpleJavaBeanTransformer generates simple bean with pcs support * (and nothing else) according to the JavaBeans 1.1 norm with no Impl generation mecanism. * - * So if there is so operation described on model, you should use the - * {@link JavaBeanTransformer} instead. + * <b>This transformer is deprecated and you should use now {@link BeanTransformer} instead.</b> * * @author Tony Chemit - chemit@codelutin.com * @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.eugene.java.SimpleJavaBeanTransformer" * @since 2.6 + * @deprecated since 3.0, no more usable, replaced by {@link BeanTransformer}. */ -public class SimpleJavaBeanTransformer extends AbstractJavaBeanTransformer { - - /** Logger. */ - private static final Log log = LogFactory.getLog(SimpleJavaBeanTransformer.class); - - protected final SimpleJavaBeanTransformerTagValues simpleJavaBeanTransformerTagValues; - - public SimpleJavaBeanTransformer() { - simpleJavaBeanTransformerTagValues = new SimpleJavaBeanTransformerTagValues(); - } +@Deprecated +public class SimpleJavaBeanTransformer extends ObjectModelTransformerToJava { @Override public void transformFromModel(ObjectModel model) { - - String className = model.getName() + "BeanFactory"; - - if (canGenerateFactory(model, className)) { - - generateBeanFactory(model, className); - } + throw new IllegalStateException("You can't use any longer this transformer, please use now " + BeanTransformer.class.getName() + "."); } @Override public void transformFromClass(ObjectModelClass input) { - - ObjectModelPackage aPackage = getPackage(input); - - if (!javaTemplatesTagValues.isBean(input, aPackage)) { - - // not a bean - return; - } - - String prefix = getConstantPrefix(input); - setConstantPrefix(prefix); - - String interfaceName = getBeanInterfaceName(aPackage, input); - String className = getBeanClassName(aPackage, input); - String abstractClassName = "Abstract" + className; - - boolean generateAbstractClass = canGenerateAbstractClass(aPackage, input, abstractClassName); - ObjectModelClass outputAbstractClass = null; - if (generateAbstractClass) { - - outputAbstractClass = generateAbstractBeanClass(aPackage, - input, - abstractClassName, - interfaceName); - } - - boolean generateInterface = canGenerateInterface(aPackage, input, interfaceName); - if (generateInterface) { - - ObjectModelClass outputAbstractClass1 = outputAbstractClass; - - if (outputAbstractClass == null) { - - outputAbstractClass1 = input; - - } - - generateBeanInterface(aPackage, input, interfaceName, outputAbstractClass1); - - if (generateAbstractClass) { - - // add override annotation on all public operations - for (ObjectModelOperation operation : getPublicOperations(outputAbstractClass)) { - addAnnotation(outputAbstractClass, - operation, - Override.class); - } - } - - } - - boolean generateClass = notFoundInClassPath(input, className) - && canGenerateClassWithMethods(aPackage, input, className); - - if (generateClass) { - - generateBeanClass(input, className, abstractClassName); - - } - - String defaultClassName = getBeanDefaultsClassName(aPackage, input); - String abstractDefaultClassName = "Abstract" + defaultClassName; - boolean generateDefaults = canGenerateBeanDefaults(aPackage, input, defaultClassName); - if (generateDefaults) { - - generateBeanDefaults(input, abstractDefaultClassName, defaultClassName); - } - - boolean generateAbstractDefaults = !simpleJavaBeanTransformerTagValues.isSimpleBeanSkipGenerateDefaults(input, aPackage, model) - && canGenerateAbstractClass(aPackage, input, abstractDefaultClassName); - if (generateAbstractDefaults) { - - generateBeanAbstractDefaults(aPackage, input, abstractDefaultClassName); - } - } - - protected boolean canGenerateFactory(ObjectModel model, String className) { - - boolean generateFactory = !simpleJavaBeanTransformerTagValues.isSimpleBeanSkipGenerateFactory(model); - - String defaultPackage = getDefaultPackageName(); - - String fqn = defaultPackage + "." + className; - - return generateFactory && !getResourcesHelper().isJavaFileInClassPath(fqn); - } - - protected boolean canGenerateInterface(ObjectModelPackage aPackage, - ObjectModelClass input, - String className) { - - boolean generateInterface = !simpleJavaBeanTransformerTagValues.isSimpleBeanSkipGenerateInterface(input, aPackage, model); - - return generateInterface && notFoundInClassPath(input, className); - - } - - protected boolean canGenerateBeanDefaults(ObjectModelPackage aPackage, ObjectModelClass input, String className) { - - boolean withInput = input != null; - - boolean canGenerate = notFoundInClassPath(input, className) - && !simpleJavaBeanTransformerTagValues.isSimpleBeanSkipGenerateDefaults(input, aPackage, model); - - if (canGenerate) { - - if (withInput) { - - // class in not abstract - // class is a bean - - canGenerate = !input.isAbstract() && javaTemplatesTagValues.isBean(input, aPackage); - - } - } - - return canGenerate; - } - - protected void generateBeanFactory(ObjectModel model, String className) { - String defaultPackage = getDefaultPackageName(); - - ObjectModelClass output = createClass(className, defaultPackage); - - for (ObjectModelClass aClass : model.getClasses()) { - - String packageName = aClass.getPackageName(); - - ObjectModelPackage aPackage = getPackage(packageName); - - if (!aClass.isAbstract() && javaTemplatesTagValues.isBean(aClass, aPackage)) { - - String typeName = getBeanInterfaceName(aPackage, aClass); - String typeBeanName = getBeanClassName(aPackage, aClass); - addImport(output, packageName + "." + typeName); - addImport(output, packageName + "." + typeBeanName); - ObjectModelOperation operation = addOperation( - output, - "typeOf" + typeName, - "<BeanType extends " + typeName + "> Class<BeanType>", - ObjectModelJavaModifier.STATIC, - ObjectModelJavaModifier.PUBLIC - ); - setOperationBody(operation, "" - /*{ - return (Class<BeanType>) <%=typeBeanName%>.class; - }*/ - ); - operation = addOperation( - output, - "new" + typeName, - typeName, - ObjectModelJavaModifier.STATIC, - ObjectModelJavaModifier.PUBLIC - ); - setOperationBody(operation, "" - /*{ - return new <%=typeBeanName%>(); - }*/ - ); - } - } - } - - protected ObjectModelInterface generateBeanInterface(ObjectModelPackage aPackage, - ObjectModelClass input, - String className, - ObjectModelClass outputClass) { - - ObjectModelInterface output = - createInterface(className, input.getPackageName()); - - if (log.isDebugEnabled()) { - log.debug("will generate " + output.getQualifiedName()); - } - - boolean superClassIsBean = false; - - String superClass = null; - - // test if a super class has bean stereotype - - Collection<ObjectModelClass> superclasses = input.getSuperclasses(); - if (CollectionUtils.isNotEmpty(superclasses)) { - for (ObjectModelClass superclass : superclasses) { - if (javaTemplatesTagValues.isBean(superclass, aPackage)) { - superClassIsBean = true; - superClass = superclass.getPackageName() + "." + - getBeanInterfaceName(aPackage, superclass); - break; - } - superClass = superclass.getQualifiedName(); - } - } - - if (superClass == null) { - - // try to find a super class by tag-value - superClass = - simpleJavaBeanTransformerTagValues.getSimpleBeanInterfaceSuperClassTagValue( - input, aPackage, model); - } - - boolean serializableFound = addInterfaces(input, output, superClass); - - if (serializableFound || superClassIsBean) { - addInterface(output, Serializable.class); - } - - generateI18nBlockAndConstants(aPackage, input, output); - - for (ObjectModelOperation operation : getPublicOperations(outputClass)) { - cloneOperation(operation, output, true); - } - - for (ObjectModelOperation operation : getPublicOperations(input)) { - cloneOperation(operation, output, true); - } - - return output; - } - - protected ObjectModelClass generateBeanClass(ObjectModelClass input, - String className, - String abstractClassName) { - - ObjectModelClass output; - - if (input.isAbstract()) { - output = createAbstractClass(className, input.getPackageName()); - } else { - output = createClass(className, input.getPackageName()); - } - - setSuperClass(output, abstractClassName); - - if (log.isDebugEnabled()) { - log.debug("will generate " + output.getQualifiedName()); - } - - addSerializable(input, output, true); - - return output; - } - - protected ObjectModelClass generateAbstractBeanClass(ObjectModelPackage aPackage, - ObjectModelClass input, - String className, - String interfaceName) { - - boolean generateInterface = interfaceName != null; - - String superClass = null; - - // 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 (javaTemplatesTagValues.isBean(superclass, aPackage)) { - superClassIsBean = true; - superClass = superclass.getPackageName() + "." + getBeanClassName(aPackage, superclass); - break; - } else { - superClass = superclass.getQualifiedName(); - } - } - } - - if (!superClassIsBean) { - - // try to find a super class by tag-value - superClass = simpleJavaBeanTransformerTagValues.getSimpleBeanSuperClassTagValue(input, aPackage, model); - if (superClass != null) { - - // will act as if super class is a bean - superClassIsBean = true; - } - } - - ObjectModelClass output; - - output = createAbstractClass(className, input.getPackageName()); - - if (superClass != null) { - setSuperClass(output, superClass); - } - if (log.isDebugEnabled()) { - log.debug("will generate " + output.getQualifiedName()); - } - - boolean serializableFound; - - if (generateInterface) { - - addInterface(output, interfaceName); - - serializableFound = true; - } else { - - serializableFound = addInterfaces(input, output, null); - - generateI18nBlockAndConstants(aPackage, input, output); - } - - addSerializable(input, output, serializableFound || superClassIsBean); - - // Get available properties - List<ObjectModelAttribute> properties = getProperties(input); - - boolean usePCS = !javaTemplatesTagValues.isSkipGeneratePropertyChangeSupport(input, aPackage, model); - - boolean generateBooleanGetMethods = eugeneTagValues.isGenerateBooleanGetMethods(input, aPackage, model); - boolean generateNotEmptyCollections = !javaTemplatesTagValues.isSkipGenerateNotEmptyCollections(input, aPackage, model); - - // Add properties field + javabean methods - for (ObjectModelAttribute attr : properties) { - - createProperty(output, - attr, - usePCS, - generateBooleanGetMethods, - generateNotEmptyCollections); - } - - if (!superClassIsBean) { - addDefaultMethodForNoneBeanSuperClass(output, usePCS, properties); - } - return output; - } - - protected void generateBeanAbstractDefaults(ObjectModelPackage aPackage, - ObjectModelClass aClass, - String abstractoutclassName) { - - - String packageName = aClass.getPackageName(); - String typeName = getBeanInterfaceName(aPackage, aClass); - String typeBeanName = getBeanClassName(aPackage, aClass); - - // try to find a super class by tag-value - String superClassName = - simpleJavaBeanTransformerTagValues.getSimpleBeanDefaultsSuperClassTagValue( - aClass, aPackage, model); - - ObjectModelClass output = createAbstractClass(abstractoutclassName, packageName); - if (StringUtils.isNotBlank(superClassName)) { - setSuperClass(output, superClassName); - } - - if (log.isDebugEnabled()) { - log.debug("will generate " + output.getQualifiedName()); - } - - ObjectModelOperation operation = addOperation( - output, - "typeOf" + typeName, - "<BeanType extends " + typeName + "> Class<BeanType>", - ObjectModelJavaModifier.STATIC, - ObjectModelJavaModifier.PUBLIC - ); - setOperationBody(operation, "" - /*{ - return (Class<BeanType>) <%=typeBeanName%>.class; - }*/ - ); - operation = addOperation( - output, - "new" + typeName, - typeName, - ObjectModelJavaModifier.STATIC, - ObjectModelJavaModifier.PUBLIC - ); - setOperationBody(operation, "" - /*{ - return new <%=typeBeanName%>(); - }*/ - ); - - addImport(output, Binder.class); - addImport(output, BinderFactory.class); - operation = addOperation( - output, - "new" + typeName, - "<BeanType extends " + typeName + "> BeanType", - ObjectModelJavaModifier.STATIC, - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, "BeanType", "source"); - setOperationBody(operation, "" - /*{ - Class<BeanType> sourceType = typeOf<%=typeName%>(); - Binder<BeanType,BeanType> binder = BinderFactory.newBinder(sourceType); - BeanType result = new<%=typeName%>(source, binder); - return result; - }*/ - ); - - operation = addOperation( - output, - "new" + typeName, - "<BeanType extends " + typeName + "> BeanType", - ObjectModelJavaModifier.STATIC, - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, "BeanType", "source"); - addParameter(operation, "Binder<BeanType, BeanType>", "binder"); - setOperationBody(operation, "" - /*{ - BeanType result = (BeanType) new<%=typeName%>(); - binder.copy(source, result); - return result; - }*/ - ); - - } - - protected void generateBeanDefaults(ObjectModelClass aClass, - String abstractoutclassName, - String defaultClassName) { - - String packageName = aClass.getPackageName(); - - ObjectModelClass output = createClass(defaultClassName, packageName); - setSuperClass(output, packageName + "." + abstractoutclassName); - if (log.isDebugEnabled()) { - log.debug("will generate " + output.getQualifiedName()); - } - - } - - protected Collection<ObjectModelOperation> getPublicOperations(ObjectModelClass clazz) { - - Collection<ObjectModelOperation> result = new ArrayList<>(); - for (ObjectModelOperation operation : clazz.getOperations()) { - - ObjectModelJavaModifier visibility = - ObjectModelJavaModifier.fromVisibility( - operation.getVisibility()); - if (ObjectModelJavaModifier.PUBLIC == visibility) { - result.add(operation); - } - } - return result; - } - - protected String getBeanInterfaceName(ObjectModelPackage aPackage, ObjectModelClass input) { - String interfaceNamePrefix = simpleJavaBeanTransformerTagValues.getSimpleBeanInterfaceNamePrefixTagValue(input, aPackage, model); - String interfaceNameSuffix = simpleJavaBeanTransformerTagValues.getSimpleBeanInterfaceNameSuffixTagValue(input, aPackage, model); - - return generateName( - interfaceNamePrefix, - input.getName(), - interfaceNameSuffix - ); - } - - protected String getBeanClassName(ObjectModelPackage aPackage, ObjectModelClass input) { - String classNamePrefix = simpleJavaBeanTransformerTagValues.getSimpleBeanClassNamePrefixTagValue(input, aPackage, model); - String classNameSuffix = simpleJavaBeanTransformerTagValues.getSimpleBeanClassNameSuffixTagValue(input, aPackage, model); - - return generateName( - classNamePrefix, - input.getName(), - classNameSuffix - ); - } - - protected String getBeanDefaultsClassName(ObjectModelPackage aPackage, ObjectModelClass input) { - String classNamePrefix = simpleJavaBeanTransformerTagValues.getSimpleBeanDefaultsClassNamePrefixTagValue(input, aPackage, model); - String classNameSuffix = simpleJavaBeanTransformerTagValues.getSimpleBeanDefaultsClassNameSuffixTagValue(input, aPackage, model); - return generateName( - classNamePrefix, - input.getName(), - classNameSuffix - ); + throw new IllegalStateException("You can't use any longer this transformer, please use now " + BeanTransformer.class.getName() + "."); } } diff --git a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanTransformerTagValues.java b/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanTransformerTagValues.java deleted file mode 100644 index ca5e907..0000000 --- a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanTransformerTagValues.java +++ /dev/null @@ -1,461 +0,0 @@ -package org.nuiton.eugene.java; - -/* - * #%L - * EUGene :: Java templates - * %% - * 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% - */ - -import com.google.common.collect.ImmutableSet; -import org.apache.commons.lang3.StringUtils; -import org.nuiton.eugene.models.extension.tagvalue.TagValueMetadata; -import org.nuiton.eugene.models.extension.tagvalue.TagValueUtil; -import org.nuiton.eugene.models.extension.tagvalue.matcher.EqualsTagValueNameMatcher; -import org.nuiton.eugene.models.extension.tagvalue.provider.DefaultTagValueMetadatasProvider; -import org.nuiton.eugene.models.object.ObjectModel; -import org.nuiton.eugene.models.object.ObjectModelClassifier; -import org.nuiton.eugene.models.object.ObjectModelPackage; - -import java.util.Set; - -import static org.nuiton.i18n.I18n.n; -import static org.nuiton.i18n.I18n.t; - -/** - * Defines all tag values managed by Java templates. - * - * @author Tony Chemit - chemit@codelutin.com - * @plexus.component role="org.nuiton.eugene.models.extension.tagvalue.provider.TagValueMetadatasProvider" role-hint="simpleJavaBean" - * @since 2.5.6 - */ -public class SimpleJavaBeanTransformerTagValues extends DefaultTagValueMetadatasProvider { - @Override - public String getDescription() { - return t("eugene.simpleBeanTransformer.tagvalues"); - } - - public enum Store implements TagValueMetadata { - - /** - * To generate an interface of each simple bean. - * - * You can globaly use it on the complete model or to a specific classifier. - * - * @see #isSimpleBeanSkipGenerateInterface(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 2.6.2 - */ - simpleBeanGenerateInterface(n("eugene.simpleBeanTransformer.tagvalue.simpleBeanGenerateInterface"), boolean.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - /** - * To generate a factory of generated simple beans. - * - * You must use it on the complete model. - * - * @see #isSimpleBeanSkipGenerateFactory(ObjectModel) - * @since 2.6.2 - */ - simpleBeanGenerateFactory(n("eugene.simpleBeanTransformer.tagvalue.simpleBeanGenerateFactory"), boolean.class, null, ObjectModel.class), - - /** - * To generate a factory of generated simple beans. - * - * You must use it on the complete model. - * - * @see #isSimpleBeanSkipGenerateDefaults(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 2.7.2 - */ - simpleBeanGenerateDefaults(n("eugene.simpleBeanTransformer.tagvalue.simpleBeanGenerateDefaults"), boolean.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - /** - * Tag value to use a super class for generated bean. - * - * If the bean needs Property change support (says you use the tag-value {@link EugeneJavaTagValues.Store#generatePropertyChangeSupport}, - * then your class must provide evrything for it. - * - * More over, if you use some collections in your bean you must also define - * two method named {@code getChild(Collection list, int index)} and - * {@code getChild(List list, int index)} - * - * See new code to know minimum stuff to add in your class for this purpose. - * <pre> - * public abstract class AbstractBean implements Serializable { - * - * private static final long serialVersionUID = 1L; - * - * protected final PropertyChangeSupport pcs = new PropertyChangeSupport(this); - * - * public void addPropertyChangeListener(PropertyChangeListener listener) { - * pcs.addPropertyChangeListener(listener); - * } - * - * public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { - * pcs.addPropertyChangeListener(propertyName, listener); - * } - * - * public void removePropertyChangeListener(PropertyChangeListener listener) { - * pcs.removePropertyChangeListener(listener); - * } - * - * public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { - * pcs.removePropertyChangeListener(propertyName, listener); - * } - * - * protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { - * pcs.firePropertyChange(propertyName, oldValue, newValue); - * } - * - * protected void firePropertyChange(String propertyName, Object newValue) { - * firePropertyChange(propertyName, null, newValue); - * } - * - * protected <T> T getChild(Collection<T> list, int index) { - * return CollectionUtil.getOrNull(list, index); - * } - * - * protected <T> T getChild(List<T> list, int index) { - * return CollectionUtil.getOrNull(list, index); - * } - * } - * </pre> - * - * You can globaly use it on the complete model or to a specific classifier. - * - * @see #getSimpleBeanSuperClassTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 2.5.6 - */ - simpleBeanSuperClass(n("eugene.simpleBeanTransformer.tagvalue.simpleBeanSuperClass"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - /** - * To add a prefix on the name of each generated bean class. - * - * You can globaly use it on the complete model or to a specific classifier. - * - * @see #getSimpleBeanClassNamePrefixTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 2.6.2 - */ - simpleBeanClassNamePrefix(n("eugene.simpleBeanTransformer.tagvalue.simpleBeanClassNamePrefix"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - /** - * To add a prefix on the name of each generated bean class. - * - * You can globaly use it on the complete model or to a specific classifier. - * - * @see #getSimpleBeanClassNameSuffixTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 2.6.2 - */ - simpleBeanClassNameSuffix(n("eugene.simpleBeanTransformer.tagvalue.simpleBeanClassNameSuffix"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - /** - * Tag value to use a super interface for generated interfaces on simple bean. - * - * You can globaly use it on the complete model or to a specific classifier. - * - * @see #getSimpleBeanInterfaceSuperClassTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 2.6.2 - */ - simpleBeanInterfaceSuperClass(n("eugene.simpleBeanTransformer.tagvalue.simpleBeanInterfaceSuperClass"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - /** - * To add a prefix on the name of each generated interface of a simple bean. - * - * You can globaly use it on the complete model or to a specific classifier. - * - * @see #getSimpleBeanInterfaceNamePrefixTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 2.6.2 - */ - simpleBeanInterfaceNamePrefix(n("eugene.simpleBeanTransformer.tagvalue.simpleBeanInterfaceNamePrefix"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - /** - * To add a prefix on the name of each generated interface of a simple bean. - * - * You can globaly use it on the complete model or to a specific classifier. - * - * @see #getSimpleBeanInterfaceNameSuffixTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 2.6.2 - */ - simpleBeanInterfaceNameSuffix(n("eugene.simpleBeanTransformer.tagvalue.simpleBeanInterfaceNameSuffix"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - /** - * Tag value to use a super super-class for generated defaults class of a - * simple bean. - * - * You can globaly use it on the complete model or to a specific classifier. - * - * @see #getSimpleBeanDefaultsSuperClassTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 2.7.2 - */ - simpleBeanDefaultsSuperClass(n("eugene.simpleBeanTransformer.tagvalue.simpleBeanDefaultsSuperClass"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - /** - * To add a prefix on the name of each generated bean class. - * - * You can globaly use it on the complete model or to a specific classifier. - * - * @see #getSimpleBeanDefaultsClassNamePrefixTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 2.7.2 - */ - simpleBeanDefaultsClassNamePrefix(n("eugene.simpleBeanTransformer.tagvalue.simpleBeanDefaultsClassNamePrefix"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - /** - * To add a suffix on the name of each generated bean class. - * - * You can globaly use it on the complete model or to a specific classifier. - * - * @see #getSimpleBeanDefaultsClassNameSuffixTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 2.6.2 - */ - simpleBeanDefaultsClassNameSuffix(n("eugene.simpleBeanTransformer.tagvalue.simpleBeanDefaultsClassNameSuffix"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class),; - - private final Set<Class<?>> targets; - private final Class<?> type; - private final String i18nDescriptionKey; - private final String defaultValue; - - Store(String i18nDescriptionKey, Class<?> type, String defaultValue, Class<?>... targets) { - this.targets = ImmutableSet.copyOf(targets); - this.type = type; - this.i18nDescriptionKey = i18nDescriptionKey; - this.defaultValue = defaultValue; - } - - @Override - public String getName() { - return name(); - } - - @Override - public Set<Class<?>> getTargets() { - return targets; - } - - @Override - public Class<?> getType() { - return type; - } - - @Override - public Class<EqualsTagValueNameMatcher> getMatcherClass() { - return EqualsTagValueNameMatcher.class; - } - - @Override - public String getDescription() { - return t(i18nDescriptionKey); - } - - @Override - public String getDefaultValue() { - return defaultValue; - } - - @Override - public boolean isDeprecated() { - return false; - } - - } - - public SimpleJavaBeanTransformerTagValues() { - super((TagValueMetadata[]) Store.values()); - } - - /** - * Obtain the value of the {@link Store#simpleBeanSuperClass} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanSuperClass - * @since 2.6.2 - */ - public String getSimpleBeanSuperClassTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findTagValue(Store.simpleBeanSuperClass, classifier, aPackage, model); - } - - /** - * Obtain the value of the {@link Store#simpleBeanInterfaceSuperClass} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanInterfaceSuperClass - * @since 2.6.2 - */ - public String getSimpleBeanInterfaceSuperClassTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findTagValue(Store.simpleBeanInterfaceSuperClass, classifier, aPackage, model); - } - - /** - * Obtain the value of the {@link Store#simpleBeanClassNamePrefix} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanClassNamePrefix - * @since 2.6.2 - */ - public String getSimpleBeanClassNamePrefixTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findTagValue(Store.simpleBeanClassNamePrefix, classifier, aPackage, model); - } - - /** - * Obtain the value of the {@link Store#simpleBeanClassNameSuffix} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanClassNameSuffix - * @since 2.6.2 - */ - public String getSimpleBeanClassNameSuffixTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findTagValue(Store.simpleBeanClassNameSuffix, classifier, aPackage, model); - } - - /** - * Obtain the value of the {@link Store#simpleBeanInterfaceNamePrefix} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanInterfaceNamePrefix - * @since 2.6.2 - */ - public String getSimpleBeanInterfaceNamePrefixTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findTagValue(Store.simpleBeanInterfaceNamePrefix, classifier, aPackage, model); - } - - /** - * Obtain the value of the {@link Store#simpleBeanInterfaceNameSuffix} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanInterfaceNameSuffix - * @since 2.6.2 - */ - public String getSimpleBeanInterfaceNameSuffixTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findTagValue(Store.simpleBeanInterfaceNameSuffix, classifier, aPackage, model); - } - - /** - * Obtain the value of the {@link Store#simpleBeanDefaultsSuperClass} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanDefaultsSuperClass - * @since 2.7.2 - */ - public String getSimpleBeanDefaultsSuperClassTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findTagValue(Store.simpleBeanDefaultsSuperClass, classifier, aPackage, model); - } - - /** - * Obtain the value of the {@link Store#simpleBeanDefaultsClassNamePrefix} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanDefaultsClassNamePrefix - * @since 2.7.2 - */ - public String getSimpleBeanDefaultsClassNamePrefixTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findTagValue(Store.simpleBeanDefaultsClassNamePrefix, classifier, aPackage, model); - } - - /** - * Obtain the value of the {@link Store#simpleBeanDefaultsClassNameSuffix} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * <strong>If not filled, then use default {@code s} value.</strong> - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanDefaultsClassNameSuffix - * @since 2.6.2 - */ - public String getSimpleBeanDefaultsClassNameSuffixTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - String value = TagValueUtil.findTagValue(Store.simpleBeanDefaultsClassNameSuffix, classifier, aPackage, model); - if (StringUtils.isBlank(value)) { - value = "s"; - } - return value; - } - - /** - * Obtain the value of the {@link Store#simpleBeanGenerateInterface} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanGenerateInterface - * @since 2.3 - */ - public boolean isSimpleBeanSkipGenerateInterface(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findBooleanTagValue(Store.simpleBeanGenerateInterface, classifier, aPackage, model); - } - - /** - * Obtain the value of the {@link Store#simpleBeanGenerateFactory} tag value on the given model or classifier. - * - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanGenerateFactory - * @since 2.7.2 - */ - public boolean isSimpleBeanSkipGenerateFactory(ObjectModel model) { - return TagValueUtil.findBooleanTagValue(Store.simpleBeanGenerateFactory, model); - } - - /** - * Obtain the value of the {@link Store#simpleBeanGenerateDefaults} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanGenerateDefaults - * @since 2.7.2 - */ - public boolean isSimpleBeanSkipGenerateDefaults(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findBooleanTagValue(Store.simpleBeanGenerateDefaults, classifier, aPackage, model); - } - -} diff --git a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanWithNoInterfaceTransformer.java b/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanWithNoInterfaceTransformer.java index a143d3c..b8f48eb 100644 --- a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanWithNoInterfaceTransformer.java +++ b/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanWithNoInterfaceTransformer.java @@ -22,31 +22,8 @@ package org.nuiton.eugene.java; * #L% */ -/*{generator option: parentheses = false}*/ -/*{generator option: writeString = +}*/ - -import com.google.common.base.Function; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Maps; -import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.nuiton.eugene.models.object.ObjectModel; -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 org.nuiton.eugene.models.object.ObjectModelPackage; -import org.nuiton.util.beans.Binder; -import org.nuiton.util.beans.BinderFactory; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; /** * Generates a java bean and a utility class around it. This templates acts like {@link SimpleJavaBeanTransformer} @@ -60,702 +37,23 @@ import java.util.List; * Boats (extends AbstractBoats) * </pre> * + * <b>This transformer is deprecated and you should use now {@link BeanTransformer} instead.</b> + * * @author Tony Chemit - chemit@codelutin.com * @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.eugene.java.SimpleJavaBeanWithNoInterfaceTransformer" - * @since 3.0 + * @deprecated since 3.0, no more usable, replaced by {@link BeanTransformer}. */ -public class SimpleJavaBeanWithNoInterfaceTransformer extends AbstractJavaBeanTransformer { - - /** Logger. */ - private static final Log log = LogFactory.getLog(SimpleJavaBeanWithNoInterfaceTransformer.class); - - ImmutableMap<ObjectModelClass, String> beanNameTranslation; - - ImmutableMap<ObjectModelClass, String> beanDefaultsNameTranslation; - - ImmutableSet<ObjectModelClass> beanClasses; - - ImmutableSet<ObjectModelClass> beanDefaultClasses; - protected boolean useJava8; - - protected final SimpleJavaBeanWithNoInterfaceTransformerTagValues simpleJavaBeanWithNoInterfaceTransformerTagValues; - - public SimpleJavaBeanWithNoInterfaceTransformer() { - simpleJavaBeanWithNoInterfaceTransformerTagValues = new SimpleJavaBeanWithNoInterfaceTransformerTagValues(); - } +@Deprecated +public class SimpleJavaBeanWithNoInterfaceTransformer extends ObjectModelTransformerToJava { @Override public void transformFromModel(ObjectModel model) { - super.transformFromModel(model); - - useJava8 = isUseJava8(); - ImmutableMap.Builder<ObjectModelClass, String> beanNameTranslationBuilder = new ImmutableMap.Builder<>(); - ImmutableMap.Builder<ObjectModelClass, String> beanDefaultsNameTranslationBuilder = new ImmutableMap.Builder<>(); - ImmutableSet.Builder<ObjectModelClass> beanClassesBuilder = new ImmutableSet.Builder<>(); - ImmutableSet.Builder<ObjectModelClass> beanDefaultClassesBuilder = new ImmutableSet.Builder<>(); - - for (ObjectModelClass aClass : model.getClasses()) { - - ObjectModelPackage aPackage = model.getPackage(aClass.getPackageName()); - if (javaTemplatesTagValues.isBean(aClass, aPackage)) { - - beanClassesBuilder.add(aClass); - - String classNamePrefix = simpleJavaBeanWithNoInterfaceTransformerTagValues.getSimpleBeanWithNoInterfaceClassNamePrefixTagValue(aClass, aPackage, model); - String classNameSuffix = simpleJavaBeanWithNoInterfaceTransformerTagValues.getSimpleBeanWithNoInterfaceClassNameSuffixTagValue(aClass, aPackage, model); - - String generateName = generateName(classNamePrefix, aClass.getName(), classNameSuffix); - beanNameTranslationBuilder.put(aClass, generateName); - - boolean canGenerateDefaults = !simpleJavaBeanWithNoInterfaceTransformerTagValues.isSimpleBeanWithNoInterfaceSkipGenerateDefaults(aClass, aPackage, model); - - if (canGenerateDefaults) { - - beanDefaultClassesBuilder.add(aClass); - - String classDefaultsNamePrefix = simpleJavaBeanWithNoInterfaceTransformerTagValues.getSimpleBeanWithNoInterfaceDefaultsClassNamePrefixTagValue(aClass, aPackage, model); - String classDefaultsNameSuffix = simpleJavaBeanWithNoInterfaceTransformerTagValues.getSimpleBeanWithNoInterfaceDefaultsClassNameSuffixTagValue(aClass, aPackage, model); - - String generateDefaultsName = generateName(classDefaultsNamePrefix, aClass.getName(), classDefaultsNameSuffix); - beanDefaultsNameTranslationBuilder.put(aClass, generateDefaultsName); - - } - - } - } - - beanClasses = beanClassesBuilder.build(); - beanDefaultClasses = beanDefaultClassesBuilder.build(); - beanNameTranslation = beanNameTranslationBuilder.build(); - beanDefaultsNameTranslation = beanDefaultsNameTranslationBuilder.build(); - - ImmutableMap<String, ObjectModelClass> beanClassesByFqn = Maps.uniqueIndex(beanClasses, new Function<ObjectModelClass, String>() { - - @Override - public String apply(ObjectModelClass input) { - return input.getQualifiedName(); - } - }); - ArrayList<String> beanClassesFqn = new ArrayList<>(beanClassesByFqn.keySet()); - Collections.sort(beanClassesFqn); - - String defaultPackageName = getDefaultPackageName(); - - String modelBeanInitializeClassName = model.getName() + "ModelInitializer"; - boolean generateModelInitializer = !getResourcesHelper().isJavaFileInClassPath(defaultPackageName + "." + modelBeanInitializeClassName); - if (generateModelInitializer) { - - ObjectModelInterface anInterface = createInterface(modelBeanInitializeClassName, defaultPackageName); - - addOperation(anInterface, "start", "void"); - addOperation(anInterface, "end", "void"); - - for (String fqn : beanClassesFqn) { - ObjectModelClass beanClass = beanClassesByFqn.get(fqn); - String beanName = beanNameTranslation.get(beanClass); - addImport(anInterface, beanName); - addOperation(anInterface, "init" + beanName, "void"); - - } - } - - String modelInitializerRunnerClassName = model.getName() + "ModelInitializerRunner"; - boolean generateInitializerRunnerClassName = !getResourcesHelper().isJavaFileInClassPath(defaultPackageName + "." + modelInitializerRunnerClassName); - if (generateInitializerRunnerClassName) { - - ObjectModelClass aClass = createClass(modelInitializerRunnerClassName, defaultPackageName); - - StringBuilder bodyBuilder = new StringBuilder(); - bodyBuilder.append("" -/*{ - initializer.start();}*/ - ); - for (String fqn : beanClassesFqn) { - ObjectModelClass beanClass = beanClassesByFqn.get(fqn); - String beanName = beanNameTranslation.get(beanClass); - addImport(aClass, beanName); - bodyBuilder.append("" -/*{ - initializer.init<%=beanName%>();}*/ - ); - - } - - bodyBuilder.append("" -/*{ - initializer.end();}*/ - ); - ObjectModelOperation operation = addOperation(aClass, "init", "void", ObjectModelJavaModifier.STATIC); - addParameter(operation, modelBeanInitializeClassName, "initializer"); - setOperationBody(operation, bodyBuilder.toString()); - } - + throw new IllegalStateException("You can't use any longer this transformer, please use now " + BeanTransformer.class.getName() + "."); } @Override public void transformFromClass(ObjectModelClass input) { - - ObjectModelPackage aPackage = getPackage(input); - - if (beanClasses.contains(input)) { - - String prefix = getConstantPrefix(input); - setConstantPrefix(prefix); - - String className = beanNameTranslation.get(input); - String abstractClassName = "Abstract" + className; - - boolean generateClass = canGenerateClassWithMethods(aPackage, input, className) - && notFoundInClassPath(input, className); - if (generateClass) { - generateBeanClass(input, className, abstractClassName); - } - - boolean generateAbstractClass = canGenerateAbstractClass(aPackage, input, abstractClassName); - if (generateAbstractClass) { - generateAbstractBeanClass(aPackage, input, abstractClassName); - } - - boolean generateDefaults = beanDefaultClasses.contains(input); - if (generateDefaults) { - - String defaultClassName = beanDefaultsNameTranslation.get(input); - String abstractDefaultClassName = "Abstract" + defaultClassName; - - if (notFoundInClassPath(input, defaultClassName)) { - - generateBeanDefaults(input, abstractDefaultClassName, defaultClassName); - } - - if (canGenerateAbstractClass(aPackage, input, abstractDefaultClassName)) { - - generateAbstractBeanDefaults(aPackage, input, className, abstractDefaultClassName); - } - - } - - } - - } - - protected ObjectModelClass generateBeanClass(ObjectModelClass input, - String className, - String abstractClassName) { - - ObjectModelClass output; - - if (input.isAbstract()) { - output = createAbstractClass(className, input.getPackageName()); - } else { - output = createClass(className, input.getPackageName()); - } - - setSuperClass(output, abstractClassName); - - if (log.isDebugEnabled()) { - log.debug("will generate " + output.getQualifiedName()); - } - - addSerializable(input, output, true); - - return output; - } - - protected ObjectModelClass generateAbstractBeanClass(ObjectModelPackage aPackage, - ObjectModelClass input, - String className) { - - String superClass = null; - - // test if a super class has bean stereotype - boolean superClassIsBean = false; - Collection<ObjectModelClass> superclasses = input.getSuperclasses(); - if (CollectionUtils.isNotEmpty(superclasses)) { - for (ObjectModelClass superclass : superclasses) { - superClassIsBean = beanClasses.contains(superclass); - if (superClassIsBean) { - superClass = superclass.getPackageName() + "." + beanNameTranslation.get(superclass); - break; - } - superClass = superclass.getQualifiedName(); - } - } - - if (!superClassIsBean) { - - // try to find a super class by tag-value - superClass = simpleJavaBeanWithNoInterfaceTransformerTagValues.getSimpleBeanWithNoInterfaceSuperClassTagValue(input, aPackage, model); - if (superClass != null) { - - // will act as if super class is a bean - superClassIsBean = true; - } - } - - ObjectModelClass output; - - output = createAbstractClass(className, input.getPackageName()); - - if (superClass != null) { - setSuperClass(output, superClass); - } - if (log.isDebugEnabled()) { - log.debug("will generate " + output.getQualifiedName()); - } - - boolean serializableFound; - - serializableFound = addInterfaces(input, output, null); - - generateI18nBlockAndConstants(aPackage, input, output); - - addSerializable(input, output, serializableFound || superClassIsBean); - - // Get available properties - List<ObjectModelAttribute> properties = getProperties(input); - - boolean usePCS = !javaTemplatesTagValues.isSkipGeneratePropertyChangeSupport(input, aPackage, model); - boolean generateBooleanGetMethods = eugeneTagValues.isGenerateBooleanGetMethods(input, aPackage, model); - boolean generateNotEmptyCollections = !javaTemplatesTagValues.isSkipGenerateNotEmptyCollections(input, aPackage, model); - - // Add properties field + javabean methods - for (ObjectModelAttribute attr : properties) { - - createProperty(output, - attr, - usePCS, - generateBooleanGetMethods, - generateNotEmptyCollections); - } - - if (!superClassIsBean) { - addDefaultMethodForNoneBeanSuperClass(output, usePCS, properties); - } - return output; - } - - protected void generateBeanDefaults(ObjectModelClass aClass, String abstractClassName, String defaultClassName) { - - String packageName = aClass.getPackageName(); - - ObjectModelClass output = createClass(defaultClassName, packageName); - setSuperClass(output, packageName + "." + abstractClassName); - if (log.isDebugEnabled()) { - log.debug("will generate " + output.getQualifiedName()); - } - - } - - protected void generateAbstractBeanDefaults(ObjectModelPackage aPackage, - ObjectModelClass aClass, - String typeName, - String abstractClassName) { - - ObjectModelClass output = createAbstractClass(abstractClassName, aPackage.getName()); - String superClassName = getAbstractDefaultsSuperClassName(aPackage, aClass); - - if (StringUtils.isNotBlank(superClassName)) { - setSuperClass(output, superClassName); - } - - if (log.isDebugEnabled()) { - log.debug("will generate " + output.getQualifiedName()); - } - - addImport(output, Binder.class); - addImport(output, BinderFactory.class); - - ObjectModelOperation operation = addOperation( - output, - "typeOf" + typeName, - "<BeanType extends " + typeName + "> Class<BeanType>", - ObjectModelJavaModifier.STATIC, - ObjectModelJavaModifier.PUBLIC - ); - setOperationBody(operation, "" - /*{ - return (Class<BeanType>) <%=typeName%>.class; - }*/ - ); - - boolean generateContructors = !simpleJavaBeanWithNoInterfaceTransformerTagValues.isSimpleBeanWithNoInterfaceSkipGenerateDefaultConstructors(aClass, aPackage, model) && !aClass.isAbstract(); - if (generateContructors) { - generateAbstractBeanDefaultsConstructors(output, typeName); - } - - generateAbstractBeanDefaultsCopyMethods(output, typeName); - - boolean generatePredicates = !simpleJavaBeanWithNoInterfaceTransformerTagValues.isSimpleBeanWithNoInterfaceSkipGeneratePredicates(aClass, aPackage, model); - if (generatePredicates) { - generateAbstractBeanDefaultsPredicates(aClass, output, typeName); - } - - boolean generateFunctions = !simpleJavaBeanWithNoInterfaceTransformerTagValues.isSimpleBeanWithNoInterfaceSkipGenerateFunctions(aClass, aPackage, model); - if (generateFunctions) { - generateAbstractBeanDefaultsFunctions(aClass, output, typeName); - } - - } - - protected void generateAbstractBeanDefaultsConstructors(ObjectModelClass output, String typeName) { - - ObjectModelOperation operation = addOperation( - output, - "new" + typeName, - typeName, - ObjectModelJavaModifier.STATIC, - ObjectModelJavaModifier.PUBLIC - ); - setOperationBody(operation, "" - /*{ - return new <%=typeName%>(); - }*/ - ); - - operation = addOperation( - output, - "new" + typeName, - "<BeanType extends " + typeName + "> BeanType", - ObjectModelJavaModifier.STATIC, - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, "BeanType", "source"); - setOperationBody(operation, "" - /*{ - Class<BeanType> sourceType = typeOf<%=typeName%>(); - Binder<BeanType, BeanType> binder = BinderFactory.newBinder(sourceType); - BeanType result = new<%=typeName%>(source, binder); - return result; - }*/ - ); - - operation = addOperation( - output, - "new" + typeName, - "<BeanType extends " + typeName + "> BeanType", - ObjectModelJavaModifier.STATIC, - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, "BeanType", "source"); - addParameter(operation, "Binder<BeanType, BeanType>", "binder"); - setOperationBody(operation, "" - /*{ - BeanType result = (BeanType) new<%=typeName%>(); - binder.copy(source, result); - return result; - }*/ - ); - - } - - protected void generateAbstractBeanDefaultsCopyMethods(ObjectModelClass output, String typeName) { - - ObjectModelOperation operation = addOperation( - output, - "copy" + typeName, - "<BeanType extends " + typeName + "> void", - ObjectModelJavaModifier.STATIC, - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, "BeanType", "source"); - addParameter(operation, "BeanType", "target"); - setOperationBody(operation, "" - /*{ - Class<BeanType> sourceType = typeOf<%=typeName%>(); - Binder<BeanType, BeanType> binder = BinderFactory.newBinder(sourceType); - binder.copy(source, target); - }*/ - ); - - operation = addOperation( - output, - "copy" + typeName, - "<BeanType extends " + typeName + "> void", - ObjectModelJavaModifier.STATIC, - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, "BeanType", "source"); - addParameter(operation, "BeanType", "target"); - addParameter(operation, "Binder<BeanType, BeanType>", "binder"); - setOperationBody(operation, "" - /*{ - binder.copy(source, target); - }*/ - ); - - } - - protected void generateAbstractBeanDefaultsPredicates(ObjectModelClass input, ObjectModelClass output, String typeName) { - - boolean atLeastOnePropertyFound = false; - for (ObjectModelAttribute attribute : getProperties(input)) { - - boolean multiple = JavaGeneratorUtil.isNMultiplicity(attribute); - - if (multiple) { - continue; - } - - atLeastOnePropertyFound = true; - String attrName = getAttributeName(attribute); - String attrType = getAttributeTypeWithGeneric(attribute); - addImport(output, attrType); - - String simpleType = JavaGeneratorUtil.getSimpleName(attrType); - - String capitalizeAttrName = JavaGeneratorUtil.capitalizeJavaBeanPropertyName(attrName); - String newPreficateMethodName = "new" + capitalizeAttrName + "Predicate"; - - ObjectModelOperation operation; - - - - operation = addOperation( - output, - newPreficateMethodName, - "<BeanType extends " + typeName + "> Predicate<BeanType>", - ObjectModelJavaModifier.STATIC, - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, simpleType, attrName); - String getterName = getGetterName(attribute, attrName); - - if (useJava8) { - setOperationBody(operation, "" - /*{ - return o -> Objects.equals(<%=attrName%>, o.<%=getterName%>()); - - }*/ - ); - } else { - setOperationBody(operation, "" - /*{ - final <%=simpleType%> $tmp = <%=attrName%>; - return new Predicate<BeanType>() { - - @Override - public boolean apply(BeanType input) { - return Objects.equals($tmp, input.<%=getterName%>()); - } - }; - - }*/ - ); - } - - operation = addOperation( - output, - "filterBy" + capitalizeAttrName, - "<BeanType extends " + typeName + "> List<BeanType>", - ObjectModelJavaModifier.STATIC, - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, "Collection<BeanType>", "source"); - addParameter(operation, simpleType, attrName); - - if (useJava8) { - setOperationBody(operation, "" - /*{ - return source.stream().filter(<%=newPreficateMethodName%>(<%=attrName%>)).collect(Collectors.toList()); - }*/ - ); - } else { - setOperationBody(operation, "" - /*{ - return Iterables.filter(source, <%=newPreficateMethodName%>(<%=attrName%>)); - }*/ - ); - } - } - - if (atLeastOnePropertyFound) { - if(useJava8) { - - addImport(output, Collection.class); - addImport(output, List.class); - addImport(output, "java.util.Objects"); - addImport(output, "java.util.function.Predicate"); - addImport(output, "java.util.stream.Collectors"); - } else { - addImport(output, com.google.common.base.Objects.class); - addImport(output, com.google.common.base.Predicate.class); - addImport(output, com.google.common.collect.Iterables.class); - } - addImport(output, Iterable.class); - } - - } - - protected void generateAbstractBeanDefaultsFunctions(ObjectModelClass input, ObjectModelClass output, String typeName) { - - boolean atLeastOnePropertyFound = false; - for (ObjectModelAttribute attribute : getProperties(input)) { - - boolean multiple = JavaGeneratorUtil.isNMultiplicity(attribute); - - if (multiple) { - continue; - } - - atLeastOnePropertyFound = true; - - String attrName = getAttributeName(attribute); - String attrType = getAttributeTypeWithGeneric(attribute); - addImport(output, attrType); - - String simpleType = JavaGeneratorUtil.getSimpleName(attrType); - simpleType = wrapPrimitiveType(simpleType); - String capitalizeAttrName = JavaGeneratorUtil.capitalizeJavaBeanPropertyName(attrName); - String getterName = getGetterName(attribute, attrName); - - String newFunctionMethodName = "new" + capitalizeAttrName + "Function"; - String getFunctionMethodName = "get" + capitalizeAttrName + "Function"; - String functionTypeName = "Function<BeanType, " + simpleType + ">"; - - String functionFieldName = JavaGeneratorUtil.convertVariableNameToConstantName(capitalizeAttrName + "Function"); - addAttribute( - output, - functionFieldName, - "Function<" + typeName + ", " + simpleType + ">", - useJava8 ? typeName + "::" + getterName : newFunctionMethodName + "()", - ObjectModelJavaModifier.FINAL, - ObjectModelJavaModifier.STATIC, - useJava8 ? ObjectModelJavaModifier.PUBLIC: ObjectModelJavaModifier.PROTECTED - ); - - if (!useJava8) { - ObjectModelOperation operation = addOperation( - output, - getFunctionMethodName, - "<BeanType extends " + typeName + "> " + functionTypeName, - ObjectModelJavaModifier.STATIC, - ObjectModelJavaModifier.PUBLIC - ); - - - setOperationBody(operation, "" - /*{ - return (<%=functionTypeName%>) <%=functionFieldName%>; - - }*/ - ); - - operation = addOperation( - output, - newFunctionMethodName, - "<BeanType extends " + typeName + "> " + functionTypeName, - ObjectModelJavaModifier.STATIC, - ObjectModelJavaModifier.PUBLIC - ); - - setOperationBody(operation, "" -/*{ - return new <%=functionTypeName%>() { - - @Override - public <%=simpleType%> apply(BeanType input) { - return input.<%=getterName%>(); - } - }; - -}*/ - ); - } - - ObjectModelOperation operation = addOperation( - output, - "uniqueIndexBy" + capitalizeAttrName, - "<BeanType extends " + typeName + "> ImmutableMap<" + simpleType + ", BeanType>", - ObjectModelJavaModifier.STATIC, - ObjectModelJavaModifier.PUBLIC - ); - addParameter(operation, "Iterable<BeanType>", "source"); - if (useJava8) { - setOperationBody(operation, "" - /*{ - return Maps.uniqueIndex(source, <%=functionFieldName%>::apply); - }*/ - ); - } else { - setOperationBody(operation, "" - /*{ - return Maps.uniqueIndex(source, <%=functionFieldName%>); - }*/ - ); - } - } - - if (atLeastOnePropertyFound) { - if (useJava8) { - addImport(output, "java.util.function.Function"); - addImport(output, "java.util.Objects"); - } else { - addImport(output, com.google.common.base.Function.class); - addImport(output, com.google.common.collect.Iterables.class); - addImport(output, com.google.common.base.Objects.class); - } - - addImport(output, ImmutableMap.class); - addImport(output, Iterable.class); - addImport(output, Maps.class); - } - - } - - protected String getAbstractDefaultsSuperClassName(ObjectModelPackage aPackage, ObjectModelClass aClass) { - String superClassName = null; - - // test if a super class has bean stereotype - boolean superClassIsBean = false; - Collection<ObjectModelClass> superclasses = aClass.getSuperclasses(); - if (CollectionUtils.isNotEmpty(superclasses)) { - for (ObjectModelClass superclass : superclasses) { - superClassIsBean = beanDefaultClasses.contains(superclass); - if (superClassIsBean) { - superClassName = superclass.getPackageName() + "." + beanDefaultsNameTranslation.get(superclass); - break; - } - superClassName = superclass.getQualifiedName(); - } - } - - if (!superClassIsBean) { - - // try to find a super class by tag-value - superClassName = simpleJavaBeanWithNoInterfaceTransformerTagValues.getSimpleBeanWithNoInterfaceDefaultsSuperClassTagValue(aClass, aPackage, model); - - } - return superClassName; - } - - @Override - protected String getAttributeType(ObjectModelAttribute attr) { - String attrType = super.getAttributeType(attr); - if (!JavaGeneratorUtil.isPrimitiveType(attrType)) { - boolean hasClass = model.hasClass(attrType); - if (hasClass) { - ObjectModelClass attributeClass = model.getClass(attrType); - String attributeType = beanNameTranslation.get(attributeClass); - if (attributeType != null) { - attrType = attributeClass.getPackageName() + "." + attributeType; - } - } - } - return attrType; - } - - protected String getAttributeType(String attrType) { - if (!JavaGeneratorUtil.isPrimitiveType(attrType)) { - boolean hasClass = model.hasClass(attrType); - if (hasClass) { - ObjectModelClass attributeClass = model.getClass(attrType); - String attributeType = beanNameTranslation.get(attributeClass); - if (attributeType != null) { - attrType = attributeClass.getPackageName() + "." + attributeType; - } - } - } - return attrType; + throw new IllegalStateException("You can't use any longer this transformer, please use now " + BeanTransformer.class.getName() + "."); } } diff --git a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanWithNoInterfaceTransformerTagValues.java b/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanWithNoInterfaceTransformerTagValues.java deleted file mode 100644 index 7887a24..0000000 --- a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanWithNoInterfaceTransformerTagValues.java +++ /dev/null @@ -1,421 +0,0 @@ -package org.nuiton.eugene.java; - -/* - * #%L - * EUGene :: Java templates - * %% - * 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% - */ - -import com.google.common.collect.ImmutableSet; -import org.apache.commons.lang3.StringUtils; -import org.nuiton.eugene.models.extension.tagvalue.TagValueMetadata; -import org.nuiton.eugene.models.extension.tagvalue.TagValueUtil; -import org.nuiton.eugene.models.extension.tagvalue.matcher.EqualsTagValueNameMatcher; -import org.nuiton.eugene.models.extension.tagvalue.provider.DefaultTagValueMetadatasProvider; -import org.nuiton.eugene.models.object.ObjectModel; -import org.nuiton.eugene.models.object.ObjectModelClassifier; -import org.nuiton.eugene.models.object.ObjectModelPackage; - -import java.util.Set; - -import static org.nuiton.i18n.I18n.n; -import static org.nuiton.i18n.I18n.t; - -/** - * Defines all tag values managed by Java templates. - * - * @author Tony Chemit - chemit@codelutin.com - * @plexus.component role="org.nuiton.eugene.models.extension.tagvalue.provider.TagValueMetadatasProvider" role-hint="simpleJavaBeanWithNoInterface" - * @since 2.5.6 - */ -public class SimpleJavaBeanWithNoInterfaceTransformerTagValues extends DefaultTagValueMetadatasProvider { - - @Override - public String getDescription() { - return t("eugene.simpleBeanWithNoInterfaceTransformer.tagvalues"); - } - - public enum Store implements TagValueMetadata { - - /** - * To generate a factory of generated simple beans. - * - * You must use it on the complete model. - * - * @see #isSimpleBeanWithNoInterfaceSkipGenerateDefaults(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 3.0 - */ - simpleBeanWithNoInterfaceGenerateDefaults(n("eugene.simpleBeanWithNoInterfaceTransformer.tagvalue.simpleBeanWithNoInterfaceGenerateDefaults"), boolean.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - - /** - * To generate or not guava predicates on each property of the bean. - * - * You can globaly use it on the complete model or to a specific classifier. - * - * @see #isSimpleBeanWithNoInterfaceSkipGeneratePredicates(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 3.0 - */ - simpleBeanWithNoInterfaceGeneratePredicates(n("eugene.simpleBeanWithNoInterfaceTransformer.tagvalue.simpleBeanWithNoInterfaceGeneratePredicates"), boolean.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - /** - * To generate or not guava functions on each property of the bean. - * - * You can globaly use it on the complete model or to a specific classifier. - * - * @see #isSimpleBeanWithNoInterfaceSkipGenerateFunctions(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 3.0 - */ - simpleBeanWithNoInterfaceGenerateFunctions(n("eugene.simpleBeanWithNoInterfaceTransformer.tagvalue.simpleBeanWithNoInterfaceGenerateFunctions"), boolean.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - /** - * To generate or not constructors methods on Default classes. - * - * You can globaly use it on the complete model, package or on a specific classifier. - * - * @see #isSimpleBeanWithNoInterfaceSkipGenerateDefaultConstructors(ObjectModelClassifier, ObjectModelPackage, ObjectModel)} - * @since 3.0 - */ - simpleBeanWithNoInterfaceSkipGenerateConstructors(n("eugene.simpleBeanWithNoInterfaceTransformer.tagvalue.simpleBeanWithNoInterfaceGenerateConstructors"), boolean.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - /** - * Tag value to use a super class for generated bean. - * - * If the bean needs Property change support (says you use the tag-value {@link EugeneJavaTagValues.Store#generatePropertyChangeSupport}, - * then your class must provide evrything for it. - * - * More over, if you use some collections in your bean you must also define - * two method named {@code getChild(Collection list, int index)} and - * {@code getChild(List list, int index)} - * - * See new code to know minimum stuff to add in your class for this purpose. - * <pre> - * public abstract class AbstractBean implements Serializable { - * - * private static final long serialVersionUID = 1L; - * - * protected final PropertyChangeSupport pcs = new PropertyChangeSupport(this); - * - * public void addPropertyChangeListener(PropertyChangeListener listener) { - * pcs.addPropertyChangeListener(listener); - * } - * - * public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { - * pcs.addPropertyChangeListener(propertyName, listener); - * } - * - * public void removePropertyChangeListener(PropertyChangeListener listener) { - * pcs.removePropertyChangeListener(listener); - * } - * - * public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { - * pcs.removePropertyChangeListener(propertyName, listener); - * } - * - * protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { - * pcs.firePropertyChange(propertyName, oldValue, newValue); - * } - * - * protected void firePropertyChange(String propertyName, Object newValue) { - * firePropertyChange(propertyName, null, newValue); - * } - * - * protected <T> T getChild(Collection<T> list, int index) { - * return CollectionUtil.getOrNull(list, index); - * } - * - * protected <T> T getChild(List<T> list, int index) { - * return CollectionUtil.getOrNull(list, index); - * } - * } - * </pre> - * - * You can globaly use it on the complete model or to a specific classifier. - * - * @see #getSimpleBeanWithNoInterfaceSuperClassTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 3.0 - */ - simpleBeanWithNoInterfaceSuperClass(n("eugene.simpleBeanWithNoInterfaceTransformer.tagvalue.simpleBeanWithNoInterfaceSuperClass"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - /** - * Tag value to use a super super-class for generated defaults class of a simple bean. - * - * You can globaly use it on the complete model or to a specific classifier. - * - * @see #getSimpleBeanWithNoInterfaceDefaultsSuperClassTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 3.0 - */ - simpleBeanWithNoInterfaceDefaultsSuperClass(n("eugene.simpleBeanWithNoInterfaceTransformer.tagvalue.simpleBeanWithNoInterfaceDefaultsSuperClass"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - /** - * To add a prefix on the name of each generated bean class. - * - * You can globaly use it on the complete model or to a specific classifier. - * - * @see #getSimpleBeanWithNoInterfaceClassNamePrefixTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 3.0 - */ - simpleBeanWithNoInterfaceClassNamePrefix(n("eugene.simpleBeanWithNoInterfaceTransformer.tagvalue.simpleBeanWithNoInterfaceClassNamePrefix"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - /** - * To add a prefix on the name of each generated bean class. - * - * You can globaly use it on the complete model or to a specific classifier. - * - * @see #getSimpleBeanWithNoInterfaceClassNameSuffixTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 3.0 - */ - simpleBeanWithNoInterfaceClassNameSuffix(n("eugene.simpleBeanWithNoInterfaceTransformer.tagvalue.simpleBeanWithNoInterfaceClassNameSuffix"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - /** - * To add a prefix on the name of each generated bean class. - * - * You can globaly use it on the complete model or to a specific classifier. - * - * @see #getSimpleBeanWithNoInterfaceDefaultsClassNamePrefixTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 3.0 - */ - simpleBeanWithNoInterfaceDefaultsClassNamePrefix(n("eugene.simpleBeanWithNoInterfaceTransformer.tagvalue.simpleBeanWithNoInterfaceDefaultsClassNamePrefix"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class), - - /** - * To add a suffix on the name of each generated bean class. - * - * You can globaly use it on the complete model or to a specific classifier. - * - * @see #getSimpleBeanWithNoInterfaceDefaultsClassNameSuffixTagValue(ObjectModelClassifier, ObjectModelPackage, ObjectModel) - * @since 3.0 - */ - simpleBeanWithNoInterfaceDefaultsClassNameSuffix(n("eugene.simpleBeanWithNoInterfaceTransformer.tagvalue.simpleBeanWithNoInterfaceDefaultsClassNameSuffix"), String.class, null, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class); - - private final Set<Class<?>> targets; - private final Class<?> type; - private final String i18nDescriptionKey; - private final String defaultValue; - - Store(String i18nDescriptionKey, Class<?> type, String defaultValue, Class<?>... targets) { - this.targets = ImmutableSet.copyOf(targets); - this.type = type; - this.i18nDescriptionKey = i18nDescriptionKey; - this.defaultValue = defaultValue; - } - - @Override - public String getName() { - return name(); - } - - @Override - public Set<Class<?>> getTargets() { - return targets; - } - - @Override - public Class<?> getType() { - return type; - } - - @Override - public Class<EqualsTagValueNameMatcher> getMatcherClass() { - return EqualsTagValueNameMatcher.class; - } - - @Override - public String getDescription() { - return t(i18nDescriptionKey); - } - - @Override - public String getDefaultValue() { - return defaultValue; - } - - @Override - public boolean isDeprecated() { - return false; - } - - } - - public SimpleJavaBeanWithNoInterfaceTransformerTagValues() { - super((TagValueMetadata[]) Store.values()); - } - - /** - * Obtain the value of the {@link Store#simpleBeanWithNoInterfaceSuperClass} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanWithNoInterfaceSuperClass - * @since 3.0 - */ - public String getSimpleBeanWithNoInterfaceSuperClassTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findTagValue(Store.simpleBeanWithNoInterfaceSuperClass, classifier, aPackage, model); - } - - /** - * Obtain the value of the {@link Store#simpleBeanWithNoInterfaceDefaultsSuperClass} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanWithNoInterfaceDefaultsSuperClass - * @since 3.0 - */ - public String getSimpleBeanWithNoInterfaceDefaultsSuperClassTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findTagValue(Store.simpleBeanWithNoInterfaceDefaultsSuperClass, classifier, aPackage, model); - } - - /** - * Obtain the value of the {@link Store#simpleBeanWithNoInterfaceClassNamePrefix} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanWithNoInterfaceClassNamePrefix - * @since 3.0 - */ - public String getSimpleBeanWithNoInterfaceClassNamePrefixTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findTagValue(Store.simpleBeanWithNoInterfaceClassNamePrefix, classifier, aPackage, model); - } - - /** - * Obtain the value of the {@link Store#simpleBeanWithNoInterfaceClassNameSuffix} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanWithNoInterfaceClassNameSuffix - * @since 3.0 - */ - public String getSimpleBeanWithNoInterfaceClassNameSuffixTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findTagValue(Store.simpleBeanWithNoInterfaceClassNameSuffix, classifier, aPackage, model); - } - - /** - * Obtain the value of the {@link Store#simpleBeanWithNoInterfaceDefaultsClassNamePrefix} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanWithNoInterfaceDefaultsClassNamePrefix - * @since 3.0 - */ - public String getSimpleBeanWithNoInterfaceDefaultsClassNamePrefixTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findTagValue(Store.simpleBeanWithNoInterfaceDefaultsClassNamePrefix, classifier, aPackage, model); - } - - /** - * Obtain the value of the {@link Store#simpleBeanWithNoInterfaceDefaultsClassNameSuffix} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * <strong>If not filled, then use default {@code s} value.</strong> - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanWithNoInterfaceDefaultsClassNameSuffix - * @since 3.0 - */ - public String getSimpleBeanWithNoInterfaceDefaultsClassNameSuffixTagValue(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - String value = TagValueUtil.findTagValue(Store.simpleBeanWithNoInterfaceDefaultsClassNameSuffix, classifier, aPackage, model); - if (StringUtils.isBlank(value)) { - value = "s"; - } - return value; - } - - /** - * Obtain the value of the {@link Store#simpleBeanWithNoInterfaceGenerateDefaults} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanWithNoInterfaceGenerateDefaults - * @since 3.0 - */ - public boolean isSimpleBeanWithNoInterfaceSkipGenerateDefaults(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findBooleanTagValue(Store.simpleBeanWithNoInterfaceGenerateDefaults, classifier, aPackage, model); - } - - /** - * Obtain the value of the {@link Store#simpleBeanWithNoInterfaceGeneratePredicates} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * <strong>If not filled, then use default {@code s} value.</strong> - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanWithNoInterfaceGeneratePredicates - * @since 3.0 - */ - public boolean isSimpleBeanWithNoInterfaceSkipGeneratePredicates(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findBooleanTagValue(Store.simpleBeanWithNoInterfaceGeneratePredicates, classifier, aPackage, model); - } - - /** - * Obtain the value of the {@link Store#simpleBeanWithNoInterfaceGenerateFunctions} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * <strong>If not filled, then use default {@code s} value.</strong> - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanWithNoInterfaceGenerateFunctions - * @since 3.0 - */ - public boolean isSimpleBeanWithNoInterfaceSkipGenerateFunctions(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findBooleanTagValue(Store.simpleBeanWithNoInterfaceGenerateFunctions, classifier, aPackage, model); - } - - /** - * Obtain the value of the {@link Store#simpleBeanWithNoInterfaceSkipGenerateConstructors} tag value on the given model or classifier. - * - * It will first look on the model, and then in the given classifier. - * - * <strong>If not filled, then use default {@code s} value.</strong> - * - * @param classifier classifier to seek - * @param model model to seek - * @return the none empty value of the found tag value or {@code null} if not found nor empty. - * @see Store#simpleBeanWithNoInterfaceSkipGenerateConstructors - * @since 3.0 - */ - public boolean isSimpleBeanWithNoInterfaceSkipGenerateDefaultConstructors(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { - return TagValueUtil.findBooleanTagValue(Store.simpleBeanWithNoInterfaceSkipGenerateConstructors, classifier, aPackage, model); - } - -} diff --git a/eugene-java-templates/src/main/resources/META-INF/services/org.nuiton.eugene.models.extension.tagvalue.provider.TagValueMetadatasProvider b/eugene-java-templates/src/main/resources/META-INF/services/org.nuiton.eugene.models.extension.tagvalue.provider.TagValueMetadatasProvider index ee3ae73..2b39b6b 100644 --- a/eugene-java-templates/src/main/resources/META-INF/services/org.nuiton.eugene.models.extension.tagvalue.provider.TagValueMetadatasProvider +++ b/eugene-java-templates/src/main/resources/META-INF/services/org.nuiton.eugene.models.extension.tagvalue.provider.TagValueMetadatasProvider @@ -1,5 +1,2 @@ org.nuiton.eugene.java.EugeneJavaTagValues -org.nuiton.eugene.java.BeanTransformerTagValues -org.nuiton.eugene.java.JavaBeanTransformerTagValues -org.nuiton.eugene.java.SimpleJavaBeanTransformerTagValues -org.nuiton.eugene.java.SimpleJavaBeanWithNoInterfaceTransformerTagValues +org.nuiton.eugene.java.BeanTransformerTagValues \ No newline at end of file diff --git a/eugene-java-templates/src/main/resources/i18n/eugene-java-templates_en_GB.properties b/eugene-java-templates/src/main/resources/i18n/eugene-java-templates_en_GB.properties index 4ea8cdb..f236ba2 100644 --- a/eugene-java-templates/src/main/resources/i18n/eugene-java-templates_en_GB.properties +++ b/eugene-java-templates/src/main/resources/i18n/eugene-java-templates_en_GB.properties @@ -4,6 +4,8 @@ eugene.bean.tagvalue.generateHelper=Generate helper ? eugene.bean.tagvalue.generateHelperConstructors=Generate helper constructor methods ? eugene.bean.tagvalue.generateHelperFunctions=Generate helper function methods ? eugene.bean.tagvalue.generateHelperPredicates=Generate helper predicate methods ? +eugene.bean.tagvalue.generateNotEmptyCollections=Generate none empty collections? +eugene.bean.tagvalue.generatePropertyChangeSupport=Generate PropertychangeSupport? eugene.bean.tagvalue.helperClassNamePrefix=Helper name's prefix eugene.bean.tagvalue.helperClassNameSuffix=Helper name's suffix eugene.bean.tagvalue.helperSuperClass=Helper super-class @@ -23,8 +25,6 @@ eugene.java.stereotype.simpleBeanWithNoInterfaceGeneratePredicates=Generate pred eugene.java.stereotype.simpleBeanWithNoInterfaceSkipGenerateConstructors=No generate constructors' bean? eugene.java.stereotypes=EUGeNe Java stereotypes eugene.java.tagvalue.acceptBeanWithMethods=Accept methods on bean? -eugene.java.tagvalue.generateNotEmptyCollections=Generate none empty collections? -eugene.java.tagvalue.generatePropertyChangeSupport=Generate PropertychangeSupport? eugene.java.tagvalue.java8=Java 8 support eugene.java.tagvalue.overrideAbstractClasses=Overrides abstract classes? eugene.java.tagvalues=EUGeNe Java tag values diff --git a/eugene-java-templates/src/main/resources/i18n/eugene-java-templates_fr_FR.properties b/eugene-java-templates/src/main/resources/i18n/eugene-java-templates_fr_FR.properties index 86d46c1..ba3bc99 100644 --- a/eugene-java-templates/src/main/resources/i18n/eugene-java-templates_fr_FR.properties +++ b/eugene-java-templates/src/main/resources/i18n/eugene-java-templates_fr_FR.properties @@ -4,6 +4,8 @@ eugene.bean.tagvalue.generateHelper=Générer les helper ? eugene.bean.tagvalue.generateHelperConstructors=Générer les méthodes de constructeurs sur le helper ? eugene.bean.tagvalue.generateHelperFunctions=Générer les fonctions sur le helper ? eugene.bean.tagvalue.generateHelperPredicates=Générer les prédicats sur le helper ? +eugene.bean.tagvalue.generateNotEmptyCollections=Générer des collections non vides ? +eugene.bean.tagvalue.generatePropertyChangeSupport=Générer le support PropertyChangeSupport ? eugene.bean.tagvalue.helperClassNamePrefix=Préfixe du nom de la classe du helper eugene.bean.tagvalue.helperClassNameSuffix=Suffixe du nom de la classe du helper eugene.bean.tagvalue.helperSuperClass=Super-classe des helpers @@ -23,8 +25,6 @@ eugene.java.stereotype.simpleBeanWithNoInterfaceGeneratePredicates=Générer les eugene.java.stereotype.simpleBeanWithNoInterfaceSkipGenerateConstructors=Ne pas générer les constructeurs sur bean ? eugene.java.stereotypes=EUGeNe Java stéréotypes eugene.java.tagvalue.acceptBeanWithMethods=Accepter les beans avec des méthodes ? -eugene.java.tagvalue.generateNotEmptyCollections=Générer des collections non vides ? -eugene.java.tagvalue.generatePropertyChangeSupport=Générer le support PropertyChangeSupport ? eugene.java.tagvalue.java8=Utiliser java 8 ? eugene.java.tagvalue.overrideAbstractClasses=Surcharger les classes abstraites de bean ? eugene.java.tagvalues=EUGeNe Java tag values diff --git a/eugene-java-templates/src/test/java/org/nuiton/eugene/java/BeanTransformerTagValuesTest.java b/eugene-java-templates/src/test/java/org/nuiton/eugene/java/BeanTransformerTagValuesTest.java new file mode 100644 index 0000000..38e78d5 --- /dev/null +++ b/eugene-java-templates/src/test/java/org/nuiton/eugene/java/BeanTransformerTagValuesTest.java @@ -0,0 +1,74 @@ +package org.nuiton.eugene.java; + +/* + * #%L + * EUGene :: Java templates + * %% + * Copyright (C) 2012 - 2014 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% + */ + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.nuiton.eugene.models.extension.tagvalue.MismatchTagValueTargetException; +import org.nuiton.eugene.models.extension.tagvalue.TagValueNotFoundException; +import org.nuiton.eugene.models.extension.tagvalue.provider.TagValueMetadatasProvider; +import org.nuiton.eugene.models.object.ObjectModel; +import org.nuiton.eugene.models.object.ObjectModelAttribute; +import org.nuiton.eugene.models.object.ObjectModelClass; +import org.nuiton.eugene.models.object.ObjectModelClassifier; +import org.nuiton.eugene.models.object.ObjectModelEnumeration; +import org.nuiton.eugene.models.object.ObjectModelOperation; +import org.nuiton.eugene.models.object.ObjectModelPackage; + +public class BeanTransformerTagValuesTest { + + protected TagValueMetadatasProvider provider; + + @Before + public void setUp() throws Exception { + provider = new BeanTransformerTagValues(); + } + + @Test + public void validate() throws Exception { + + validate(BeanTransformerTagValues.Store.generateNotEmptyCollections.getName(), true, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class, ObjectModelClass.class, ObjectModelEnumeration.class); + validate(BeanTransformerTagValues.Store.generatePropertyChangeSupport.getName(), true, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class, ObjectModelClass.class, ObjectModelEnumeration.class); + validate(BeanTransformerTagValues.Store.generateNotEmptyCollections.getName(), false, ObjectModelOperation.class, ObjectModelAttribute.class); + validate(BeanTransformerTagValues.Store.generatePropertyChangeSupport.getName(), false, ObjectModelOperation.class, ObjectModelAttribute.class); + + long l = System.nanoTime(); + + validate(BeanTransformerTagValues.Store.generateNotEmptyCollections.getName() + l, false, ObjectModelPackage.class, ObjectModelOperation.class, ObjectModelAttribute.class); + validate(BeanTransformerTagValues.Store.generatePropertyChangeSupport.getName() + l, false, ObjectModelPackage.class, ObjectModelOperation.class, ObjectModelAttribute.class); + } + + + protected void validate(String name, boolean expected, Class<?>... types) { + for (Class<?> type : types) { + try { + provider.validate(name, type); + Assert.assertTrue(expected); + } catch (TagValueNotFoundException | MismatchTagValueTargetException e) { + Assert.assertFalse(expected); + } + } + } + +} diff --git a/eugene-java-templates/src/test/java/org/nuiton/eugene/java/EugeneJavaTagValuesTest.java b/eugene-java-templates/src/test/java/org/nuiton/eugene/java/EugeneJavaTagValuesTest.java index 66b7507..b5eee75 100644 --- a/eugene-java-templates/src/test/java/org/nuiton/eugene/java/EugeneJavaTagValuesTest.java +++ b/eugene-java-templates/src/test/java/org/nuiton/eugene/java/EugeneJavaTagValuesTest.java @@ -50,10 +50,6 @@ public class EugeneJavaTagValuesTest { public void validate() throws Exception { validate(EugeneJavaTagValues.Store.bean.getName(), true, ObjectModelPackage.class, ObjectModelClassifier.class, ObjectModelClass.class, ObjectModelEnumeration.class, ObjectModelInterface.class); - validate(EugeneJavaTagValues.Store.generateNotEmptyCollections.getName(), true, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class, ObjectModelClass.class, ObjectModelEnumeration.class); - validate(EugeneJavaTagValues.Store.generatePropertyChangeSupport.getName(), true, ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class, ObjectModelClass.class, ObjectModelEnumeration.class); - validate(EugeneJavaTagValues.Store.generateNotEmptyCollections.getName(), false, ObjectModelOperation.class, ObjectModelAttribute.class); - validate(EugeneJavaTagValues.Store.generatePropertyChangeSupport.getName(), false, ObjectModelOperation.class, ObjectModelAttribute.class); validate(EugeneJavaTagValues.Store.bean.getName(), false, ObjectModel.class, ObjectModelOperation.class); @@ -63,9 +59,6 @@ public class EugeneJavaTagValuesTest { long l = System.nanoTime(); validate(EugeneJavaTagValues.Store.bean.getName() + l, false, ObjectModelPackage.class, ObjectModel.class, ObjectModelOperation.class); - validate(EugeneJavaTagValues.Store.generateNotEmptyCollections.getName() + l, false, ObjectModelPackage.class, ObjectModelOperation.class, ObjectModelAttribute.class); - validate(EugeneJavaTagValues.Store.generatePropertyChangeSupport.getName() + l, false, ObjectModelPackage.class, ObjectModelOperation.class, ObjectModelAttribute.class); - validate(EugeneJavaTagValues.Store.java8.getName() + l, false, ObjectModel.class, ObjectModelOperation.class, ObjectModelAttribute.class); } @@ -75,9 +68,7 @@ public class EugeneJavaTagValuesTest { try { provider.validate(name, type); Assert.assertTrue(expected); - } catch (TagValueNotFoundException e) { - Assert.assertFalse(expected); - } catch (MismatchTagValueTargetException e) { + } catch (TagValueNotFoundException | MismatchTagValueTargetException e) { Assert.assertFalse(expected); } } diff --git a/eugene-maven-plugin/src/it/generate/all/pom.xml b/eugene-maven-plugin/src/it/generate/all/pom.xml index 06a95d7..71d767e 100644 --- a/eugene-maven-plugin/src/it/generate/all/pom.xml +++ b/eugene-maven-plugin/src/it/generate/all/pom.xml @@ -66,7 +66,7 @@ <verbose>true</verbose> <resolver>org.nuiton.util.FasterCachedResourceResolver</resolver> <fullPackagePath>org.nuiton</fullPackagePath> - <templates>org.nuiton.eugene.java.JavaBeanTransformer</templates> + <templates>org.nuiton.eugene.java.BeanTransformer</templates> </configuration> <executions> <execution> @@ -75,7 +75,7 @@ <configuration> <inputs>zargo:src/main/zargo:*.zargo</inputs> <outputDirectory>target/generated-sources-zargo2Java</outputDirectory> - <templates>org.nuiton.eugene.java.JavaBeanTransformer,org.nuiton.eugene.java.JavaInterfaceTransformer</templates> + <templates>org.nuiton.eugene.java.BeanTransformer,org.nuiton.eugene.java.JavaInterfaceTransformer</templates> </configuration> <goals> <goal>generate</goal> -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
participants (1)
-
nuiton.org scm