r1235 - in trunk: . eugene eugene-java-templates eugene-java-templates/src/main/java/org/nuiton/eugene/java eugene-jpa-templates eugene-maven-plugin
Author: tchemit Date: 2013-03-30 00:36:59 +0100 (Sat, 30 Mar 2013) New Revision: 1235 Url: http://nuiton.org/projects/eugene/repository/revisions/1235 Log: - move to version 2.6.2-SNAPSHOT - remove from build eugene-jpa-templates (don't want to expose it in this version) fixes #2636: SimpleBean generator does not add abstract modifier if required fixes #2637: Can suffix simple bean generated classes names fixes #2638: Can generate beanFactory from simple bean fixes #2639: Can generate interface from simple bean fixes #2640: Can prefix simple bean generated classes names fixes #2641: Can prefix simple bean generated interfaces names fixes #2642: Can suffix simple bean generated interfaces names fixes #2643: Can choose a default interface to place on generated interface of simple beans (when simple bean has not super bean) fixes #2644: Can choose a default class to place on generated simple beans (when simple bean has not super bean) Modified: trunk/eugene-java-templates/pom.xml trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/AbstractJavaBeanTransformer.java trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaTemplatesGeneratorUtil.java trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaTemplatesTagValues.java trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanTransformer.java trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/package-info.java trunk/eugene-jpa-templates/pom.xml trunk/eugene-maven-plugin/pom.xml trunk/eugene/pom.xml trunk/pom.xml Modified: trunk/eugene/pom.xml =================================================================== --- trunk/eugene/pom.xml 2013-03-28 11:13:11 UTC (rev 1234) +++ trunk/eugene/pom.xml 2013-03-29 23:36:59 UTC (rev 1235) @@ -32,7 +32,7 @@ <parent> <groupId>org.nuiton</groupId> <artifactId>eugene</artifactId> - <version>2.7-SNAPSHOT</version> + <version>2.6.2-SNAPSHOT</version> </parent> <groupId>org.nuiton.eugene</groupId> Modified: trunk/eugene-java-templates/pom.xml =================================================================== --- trunk/eugene-java-templates/pom.xml 2013-03-28 11:13:11 UTC (rev 1234) +++ trunk/eugene-java-templates/pom.xml 2013-03-29 23:36:59 UTC (rev 1235) @@ -29,7 +29,7 @@ <parent> <groupId>org.nuiton</groupId> <artifactId>eugene</artifactId> - <version>2.7-SNAPSHOT</version> + <version>2.6.2-SNAPSHOT</version> </parent> <groupId>org.nuiton.eugene</groupId> Modified: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/AbstractJavaBeanTransformer.java =================================================================== --- trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/AbstractJavaBeanTransformer.java 2013-03-28 11:13:11 UTC (rev 1234) +++ trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/AbstractJavaBeanTransformer.java 2013-03-29 23:36:59 UTC (rev 1235) @@ -28,8 +28,10 @@ /*{generator option: parentheses = false}*/ /*{generator option: writeString = +}*/ +import org.apache.commons.lang3.StringUtils; 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; @@ -39,6 +41,7 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; +import java.util.HashSet; import java.util.List; import java.util.Set; @@ -54,7 +57,7 @@ public static final String DEFAULT_CONSTANT_PREFIX = "PROPERTY_"; - protected void createPropertyConstant(ObjectModelClass output, + protected void createPropertyConstant(ObjectModelClassifier output, ObjectModelAttribute attr, String prefix, Set<String> constantNames) { @@ -538,21 +541,27 @@ * Add all interfaces defines in input class and returns if * {@link Serializable} interface was found. * - * @param input the input model class to process + * @param input the input model class to process * @param output the output generated class * @return {@code true} if {@link Serializable} was found from input, * {@code false} otherwise */ protected boolean addInterfaces(ObjectModelClass input, - ObjectModelClass output) { + ObjectModelClassifier output, + String extraInterfaceName) { boolean foundSerializable = false; + Set<String> added = new HashSet<String>(); for (ObjectModelInterface parentInterface : input.getInterfaces()) { String fqn = parentInterface.getQualifiedName(); + added.add(fqn); addInterface(output, fqn); - if (Serializable.class.getName().equals(fqn)) { + if (Serializable.class.getName().equals(fqn)) { foundSerializable = true; } } + if (extraInterfaceName != null && !added.contains(extraInterfaceName)) { + addInterface(output, extraInterfaceName); + } return foundSerializable; } @@ -682,4 +691,46 @@ }*/ ); } + + protected void generateI18nBlockAndConstants(ObjectModelClass input, + ObjectModelClassifier output) { + + String i18nPrefix = JavaGeneratorUtil.getI18nPrefixTagValue(input, model); + if (!StringUtils.isEmpty(i18nPrefix)) { + generateI18nBlock(input, output, i18nPrefix); + } + + String prefix = getConstantPrefix(input, DEFAULT_CONSTANT_PREFIX); + + 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); + } + } } Modified: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java =================================================================== --- trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java 2013-03-28 11:13:11 UTC (rev 1234) +++ trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaBeanTransformer.java 2013-03-29 23:36:59 UTC (rev 1235) @@ -37,7 +37,6 @@ import java.util.Collection; import java.util.Iterator; import java.util.List; -import java.util.Set; /*{generator option: parentheses = false}*/ /*{generator option: writeString = +}*/ @@ -45,7 +44,7 @@ /** * JavaBeanTransformer generates simple bean with pcs support * (and nothing else) according to the JavaBeans 1.1 norm. - * + * <p/> * Since version 2.2.1, it is possible to * <ul> * <li>generate a simple POJO (says with no PCS support) by using the tag value {@link JavaTemplatesTagValues#TAG_NO_PCS}.</li> @@ -113,7 +112,7 @@ Collection<ObjectModelOperation> allExistingOperations = new ArrayList<ObjectModelOperation>(input.getAllSuperclassOperations(true)); - if (abstractOutput!=null) { + if (abstractOutput != null) { allExistingOperations.addAll(abstractOutput.getOperations()); } @@ -126,20 +125,12 @@ } } return true; - -// boolean hasOperations = !allOtherOperations.isEmpty() || -// !operations.isEmpty(); -// -// // generate only if no user operations found -// return !hasOperations; } - - protected void createAbstractOperations(ObjectModelClass ouput, Iterable<ObjectModelOperation> operations) { - JavaGeneratorUtil.cloneOperations( + JavaTemplatesGeneratorUtil.cloneOperations( this, operations, ouput, @@ -152,7 +143,7 @@ // test if a super class has bean stereotype boolean superClassIsBean = false; Collection<ObjectModelClass> superclasses = input.getSuperclasses(); - if(CollectionUtils.isNotEmpty(superclasses)) { + if (CollectionUtils.isNotEmpty(superclasses)) { for (ObjectModelClass superclass : superclasses) { if (JavaTemplatesGeneratorUtil.hasBeanStereotype(superclass)) { superClassIsBean = true; @@ -186,46 +177,31 @@ log.debug("will generate " + output.getQualifiedName()); } - String i18nPrefix = JavaGeneratorUtil.getI18nPrefixTagValue(input, model); - if (!StringUtils.isEmpty(i18nPrefix)) { - generateI18nBlock(input, output, i18nPrefix); + addSuperClass(input, output); + + boolean serializableFound = addInterfaces(input, output, null); + + if (superClassIsBean) { + serializableFound = true; } + addSerializable(input, output, serializableFound); + + generateI18nBlockAndConstants(input, output); + String noPCSTagValue = JavaTemplatesGeneratorUtil.getNoPCSTagValue(model, input); boolean usePCS = StringUtils.isEmpty(noPCSTagValue) || !"true".equals(noPCSTagValue.trim()); String noGenerateBooleanGetMethods = - JavaGeneratorUtil.getDoNotGenerateBooleanGetMethods(model, input); + JavaTemplatesGeneratorUtil.getDoNotGenerateBooleanGetMethods(model, input); boolean generateBooleanGetMethods = StringUtils.isEmpty(noGenerateBooleanGetMethods) || !"true".equals(noGenerateBooleanGetMethods.trim()); - String prefix = getConstantPrefix(input, DEFAULT_CONSTANT_PREFIX); - - setConstantPrefix(prefix); - - addSuperClass(input, output); - - boolean serializableFound = addInterfaces(input, output); - - if (superClassIsBean) { - serializableFound = true; - } - - addSerializable(input, output, serializableFound); - - Set<String> constantNames = addConstantsFromDependency(input, output); - // Get available properties List<ObjectModelAttribute> properties = getProperties(input); - // Add properties constant - for (ObjectModelAttribute attr : properties) { - - createPropertyConstant(output, attr, prefix, constantNames); - } - // Add properties field + javabean methods for (ObjectModelAttribute attr : properties) { @@ -236,26 +212,9 @@ createAbstractOperations(output, input.getOperations()); if (!superClassIsBean) { - - if (usePCS) { - - // Add property change support - createPropertyChangeSupport(output); - } + addDefaultMethodForNoneBeanSuperClass(output, usePCS, properties); } - boolean hasAMultipleProperty = containsMutiple(properties); - - // Add helper operations - if (hasAMultipleProperty) { - - if (!superClassIsBean) { - - // add getChild methods - createGetChildMethod(output); - } - - } return output; } @@ -271,7 +230,7 @@ // add a fix serialVersionUID, since the class has no field nor method addConstant(resultClassImpl, - JavaGeneratorUtil.SERIAL_VERSION_UID, + JavaTemplatesGeneratorUtil.SERIAL_VERSION_UID, "long", "1L", ObjectModelJavaModifier.PRIVATE Modified: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaTemplatesGeneratorUtil.java =================================================================== --- trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaTemplatesGeneratorUtil.java 2013-03-28 11:13:11 UTC (rev 1234) +++ trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaTemplatesGeneratorUtil.java 2013-03-29 23:36:59 UTC (rev 1235) @@ -80,4 +80,137 @@ String value = findTagValue(JavaTemplatesTagValues.TAG_BEAN_SUPER_CLASS, classifier, model); return value; } + + /** + * Obtain the value of the {@link JavaTemplatesTagValues#TAG_SIMPLE_BEAN_SUPER_CLASS} + * tag value on the given model or classifier. + * <p/> + * It will first look on the model, and then in the given classifier. + * + * @param model model to seek + * @param classifier classifier to seek + * @return the none empty value of the found tag value or {@code null} if not found nor empty. + * @see JavaTemplatesTagValues#TAG_SIMPLE_BEAN_SUPER_CLASS + * @since 2.6.2 + */ + public static String getSimpleBeanSuperClassTagValue(ObjectModel model, ObjectModelClassifier classifier) { + String value = findTagValue(JavaTemplatesTagValues.TAG_SIMPLE_BEAN_SUPER_CLASS, classifier, model); + return value; + } + + /** + * Obtain the value of the {@link JavaTemplatesTagValues#TAG_SIMPLE_BEAN_GENERATE_INTERFACE} + * tag value on the given model or classifier. + * <p/> + * It will first look on the model, and then in the given classifier. + * + * @param model model to seek + * @param classifier classifier to seek + * @return the none empty value of the found tag value or {@code null} if not found nor empty. + * @see JavaTemplatesTagValues#TAG_SIMPLE_BEAN_GENERATE_INTERFACE + * @since 2.3 + */ + public static String getSimpleBeanGenerateInterfaceTagValue(ObjectModel model, ObjectModelClassifier classifier) { + String value = findTagValue(JavaTemplatesTagValues.TAG_SIMPLE_BEAN_GENERATE_INTERFACE, classifier, model); + return value; + } + + /** + * Obtain the value of the {@link JavaTemplatesTagValues#TAG_SIMPLE_BEAN_GENERATE_FACTORY} + * 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 JavaTemplatesTagValues#TAG_SIMPLE_BEAN_GENERATE_FACTORY + * @since 2.3 + */ + public static String getSimpleBeanGenerateFactoryTagValue(ObjectModel model) { + String value = findTagValue(JavaTemplatesTagValues.TAG_SIMPLE_BEAN_GENERATE_FACTORY, null, model); + return value; + } + + /** + * Obtain the value of the {@link JavaTemplatesTagValues#TAG_SIMPLE_BEAN_INTERFACE_SUPER_CLASS} + * tag value on the given model or classifier. + * <p/> + * It will first look on the model, and then in the given classifier. + * + * @param model model to seek + * @param classifier classifier to seek + * @return the none empty value of the found tag value or {@code null} if not found nor empty. + * @see JavaTemplatesTagValues#TAG_SIMPLE_BEAN_INTERFACE_SUPER_CLASS + * @since 2.6.2 + */ + public static String getSimpleBeanInterfaceSuperClassTagValue(ObjectModel model, ObjectModelClassifier classifier) { + String value = findTagValue(JavaTemplatesTagValues.TAG_SIMPLE_BEAN_INTERFACE_SUPER_CLASS, classifier, model); + return value; + } + + /** + * Obtain the value of the {@link JavaTemplatesTagValues#TAG_SIMPLE_BEAN_CLASS_NAME_PREFIX} + * tag value on the given model or classifier. + * <p/> + * It will first look on the model, and then in the given classifier. + * + * @param model model to seek + * @param classifier classifier to seek + * @return the none empty value of the found tag value or {@code null} if not found nor empty. + * @see JavaTemplatesTagValues#TAG_SIMPLE_BEAN_CLASS_NAME_PREFIX + * @since 2.6.2 + */ + public static String getSimpleBeanClassNamePrefixTagValue(ObjectModel model, ObjectModelClassifier classifier) { + String value = findTagValue(JavaTemplatesTagValues.TAG_SIMPLE_BEAN_CLASS_NAME_PREFIX, classifier, model); + return value; + } + + /** + * Obtain the value of the {@link JavaTemplatesTagValues#TAG_SIMPLE_BEAN_CLASS_NAME_SUFFIX} + * tag value on the given model or classifier. + * <p/> + * It will first look on the model, and then in the given classifier. + * + * @param model model to seek + * @param classifier classifier to seek + * @return the none empty value of the found tag value or {@code null} if not found nor empty. + * @see JavaTemplatesTagValues#TAG_SIMPLE_BEAN_CLASS_NAME_SUFFIX + * @since 2.6.2 + */ + public static String getSimpleBeanClassNameSuffixTagValue(ObjectModel model, ObjectModelClassifier classifier) { + String value = findTagValue(JavaTemplatesTagValues.TAG_SIMPLE_BEAN_CLASS_NAME_SUFFIX, classifier, model); + return value; + } + + /** + * Obtain the value of the {@link JavaTemplatesTagValues#TAG_SIMPLE_BEAN_INTERFACE_NAME_PREFIX} + * tag value on the given model or classifier. + * <p/> + * It will first look on the model, and then in the given classifier. + * + * @param model model to seek + * @param classifier classifier to seek + * @return the none empty value of the found tag value or {@code null} if not found nor empty. + * @see JavaTemplatesTagValues#TAG_SIMPLE_BEAN_INTERFACE_NAME_PREFIX + * @since 2.6.2 + */ + public static String getSimpleBeanInterfaceNamePrefixTagValue(ObjectModel model, ObjectModelClassifier classifier) { + String value = findTagValue(JavaTemplatesTagValues.TAG_SIMPLE_BEAN_INTERFACE_NAME_PREFIX, classifier, model); + return value; + } + + /** + * Obtain the value of the {@link JavaTemplatesTagValues#TAG_SIMPLE_BEAN_INTERFACE_NAME_SUFFIX} + * tag value on the given model or classifier. + * <p/> + * It will first look on the model, and then in the given classifier. + * + * @param model model to seek + * @param classifier classifier to seek + * @return the none empty value of the found tag value or {@code null} if not found nor empty. + * @see JavaTemplatesTagValues#TAG_SIMPLE_BEAN_INTERFACE_NAME_SUFFIX + * @since 2.6.2 + */ + public static String getSimpleBeanInterfaceNameSuffixTagValue(ObjectModel model, ObjectModelClassifier classifier) { + String value = findTagValue(JavaTemplatesTagValues.TAG_SIMPLE_BEAN_INTERFACE_NAME_SUFFIX, classifier, model); + return value; + } } Modified: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaTemplatesTagValues.java =================================================================== --- trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaTemplatesTagValues.java 2013-03-28 11:13:11 UTC (rev 1234) +++ trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaTemplatesTagValues.java 2013-03-29 23:36:59 UTC (rev 1235) @@ -111,6 +111,175 @@ @ModelPropertiesUtil.TagValueDefinition( target = {ObjectModel.class, ObjectModelClassifier.class}, documentation = "To specify a super-class to used on generated bean " + - "for a class or any class of a model") + "for a class or any class of a model.\n" + + "(only effective with bean generator)") String TAG_BEAN_SUPER_CLASS = "beanSuperClass"; + + /** + * Tag value to use a super class for generated bean. + * <p/> + * If the bean needs Property change support (says you do not add the {@link #TAG_NO_PCS} on classifier or model, + * then your class must provide evrything for it. + * <p/> + * 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)} + * <p/> + * 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> + * <p/> + * You can globaly use it on the complete model or to a specific classifier. + * + * @see JavaTemplatesGeneratorUtil#getSimpleBeanSuperClassTagValue(ObjectModel, ObjectModelClassifier) + * @since 2.5.6 + */ + @ModelPropertiesUtil.TagValueDefinition( + target = {ObjectModel.class, ObjectModelClassifier.class}, + documentation = "To specify a super-class to used on generated simple bean " + + "for a class or any class of a model.\n" + + "(only effective with simple bean generator)") + String TAG_SIMPLE_BEAN_SUPER_CLASS = "simpleBeanSuperClass"; + + /** + * To add a prefix on the name of each generated bean class. + * <p/> + * You can globaly use it on the complete model or to a specific classifier. + * + * @see JavaTemplatesGeneratorUtil#getSimpleBeanClassNamePrefixTagValue(ObjectModel, ObjectModelClassifier) + * @since 2.6.2 + */ + @ModelPropertiesUtil.TagValueDefinition( + target = {ObjectModel.class, ObjectModelClassifier.class}, + documentation = "To add a prefix on class name of generated bean " + + "for a class or any class of a model.\n" + + "(only effective with simple bean generator)") + String TAG_SIMPLE_BEAN_CLASS_NAME_PREFIX = "simpleBeanClassNamePrefix"; + + /** + * To add a prefix on the name of each generated bean class. + * <p/> + * You can globaly use it on the complete model or to a specific classifier. + * + * @see JavaTemplatesGeneratorUtil#getSimpleBeanClassNameSuffixTagValue(ObjectModel, ObjectModelClassifier) + * @since 2.6.2 + */ + @ModelPropertiesUtil.TagValueDefinition( + target = {ObjectModel.class, ObjectModelClassifier.class}, + documentation = "To add a suffix on class name of generated bean " + + "for a class or any class of a model.\n" + + "(only effective with simple bean generator)") + String TAG_SIMPLE_BEAN_CLASS_NAME_SUFFIX = "simpleBeanClassNameSuffix"; + + /** + * To generate an interface of each simple bean. + * <p/> + * You can globaly use it on the complete model or to a specific classifier. + * + * @see JavaTemplatesGeneratorUtil#getSimpleBeanGenerateInterfaceTagValue(ObjectModel, ObjectModelClassifier) + * @since 2.6.2 + */ + @ModelPropertiesUtil.TagValueDefinition( + target = {ObjectModel.class, ObjectModelClassifier.class}, + documentation = "To generate an interface for each bean " + + "for a class or any class of a model.\n" + + "(only effective with simple bean generator)") + String TAG_SIMPLE_BEAN_GENERATE_INTERFACE = "simpleBeanGenerateInterface"; + + /** + * Tag value to use a super interface for generated interfaces on simple bean. + * <p/> + * You can globaly use it on the complete model or to a specific classifier. + * + * @see JavaTemplatesGeneratorUtil#getSimpleBeanInterfaceSuperClassTagValue(ObjectModel, ObjectModelClassifier) + * @since 2.6.2 + */ + @ModelPropertiesUtil.TagValueDefinition( + target = {ObjectModel.class, ObjectModelClassifier.class}, + documentation = "To specify a super-class to used on generated interfaces " + + "for a class or any class of a model.\n" + + "(only effective with simple bean generator)") + String TAG_SIMPLE_BEAN_INTERFACE_SUPER_CLASS = "simpleBeanInterfaceSuperClass"; + + /** + * To add a prefix on the name of each generated interface of a simple bean. + * <p/> + * You can globaly use it on the complete model or to a specific classifier. + * + * @see JavaTemplatesGeneratorUtil#getSimpleBeanInterfaceNamePrefixTagValue(ObjectModel, ObjectModelClassifier) + * @since 2.6.2 + */ + @ModelPropertiesUtil.TagValueDefinition( + target = {ObjectModel.class, ObjectModelClassifier.class}, + documentation = "To add a prefix on interface name of generated bean " + + "for a class or any class of a model.\n" + + "(only effective with simple bean generator)") + String TAG_SIMPLE_BEAN_INTERFACE_NAME_PREFIX = "simpleBeanInterfaceNamePrefix"; + + /** + * To add a prefix on the name of each generated interface of a simple bean. + * <p/> + * You can globaly use it on the complete model or to a specific classifier. + * + * @see JavaTemplatesGeneratorUtil#getSimpleBeanInterfaceNameSuffixTagValue(ObjectModel, ObjectModelClassifier) + * @since 2.6.2 + */ + @ModelPropertiesUtil.TagValueDefinition( + target = {ObjectModel.class, ObjectModelClassifier.class}, + documentation = "To add a suffix on interface name of generated bean " + + "for a class or any class of a model.\n" + + "(only effective with simple bean generator)") + String TAG_SIMPLE_BEAN_INTERFACE_NAME_SUFFIX = "simpleBeanInterfaceNameSuffix"; + + /** + * To generate an factory of generated simple beans. + * <p/> + * You must use it on the complete model. + * + * @see JavaTemplatesGeneratorUtil#getSimpleBeanGenerateFactoryTagValue(ObjectModel) + * @since 2.6.2 + */ + @ModelPropertiesUtil.TagValueDefinition( + target = {ObjectModel.class}, + documentation = "To generate a factory of simple bean.\n" + + "(only effective with simple bean generator)") + String TAG_SIMPLE_BEAN_GENERATE_FACTORY = "simpleBeanGenerateFactory"; } Modified: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanTransformer.java =================================================================== --- trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanTransformer.java 2013-03-28 11:13:11 UTC (rev 1234) +++ trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanTransformer.java 2013-03-29 23:36:59 UTC (rev 1235) @@ -24,7 +24,6 @@ * #L% */ - /*{generator option: parentheses = false}*/ /*{generator option: writeString = +}*/ @@ -33,13 +32,17 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.eugene.GeneratorException; +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 java.io.Serializable; +import java.util.ArrayList; import java.util.Collection; -import java.util.Iterator; import java.util.List; -import java.util.Set; /** * SimpleJavaBeanTransformer generates simple bean with pcs support @@ -59,6 +62,17 @@ LogFactory.getLog(SimpleJavaBeanTransformer.class); @Override + public void transformFromModel(ObjectModel model) { + + String className = model.getName() + "BeanFactory"; + + if (canGenerateFactory(model, className)) { + + generateBeanFactory(model, className); + } + } + + @Override public void transformFromClass(ObjectModelClass input) { if (!JavaTemplatesGeneratorUtil.hasBeanStereotype(input)) { @@ -67,25 +81,102 @@ return; } + String interfaceName = getBeanInterfaceName(input); + String className = getBeanClassName(input); - if (canGenerate(input)) { + boolean generateInterface = canGenerateInterface(input, interfaceName); - ObjectModelClass output = generateBean(input); + boolean generateClass = canGenerateBean(input, className); + + if (generateClass || generateInterface) { + + ObjectModelClass outputClass = generateBeanClass(input, + className, + interfaceName); + if (generateInterface) { + + generateBeanInterface(input, interfaceName, outputClass); + + if (generateClass) { + + // add override annotation on all public operations + + for (ObjectModelOperation operation : getPublicOperations(outputClass)) { + addAnnotation(outputClass, + operation, + Override.class.getSimpleName()); + } + + } + } + + if (!generateClass) { + + // remove bean class from builder (just used to build the interface) + builder.getModel().getClasses().remove(outputClass); + } } + } - protected boolean canGenerate(ObjectModelClass input) { - boolean canGenerate = !isInClassPath(input); + protected boolean canGenerateFactory(ObjectModel model, String className) { + String generateTagValue = + JavaTemplatesGeneratorUtil.getSimpleBeanGenerateFactoryTagValue(model); + boolean generateFactory = generateTagValue != null && + Boolean.valueOf(generateTagValue); + + String defaultPackage = + getConfiguration().getProperty(PROP_DEFAULT_PACKAGE); + + String fqn = defaultPackage + "." + className; + boolean canGenerate = generateFactory && + !isInClassPath(fqn); + + return canGenerate; + } + + protected boolean canGenerateInterface(ObjectModelClass input, + String className) { + + String generateTagValue = + JavaTemplatesGeneratorUtil.getSimpleBeanGenerateInterfaceTagValue(model, input); + + boolean generateInterface = generateTagValue != null && + Boolean.valueOf(generateTagValue); + + String fqn = input.getPackageName() + "." + className; + + boolean canGenerate = generateInterface && !isInClassPath(fqn); + if (canGenerate) { // check there is no operation on input if (!input.getOperations().isEmpty()) { throw new GeneratorException( + "Can't generate a simple bean interface as class " + + fqn + + " contains so operations." + + "\nUse instead the JavaBeanTransformer."); + } + } + return canGenerate; + } + + protected boolean canGenerateBean(ObjectModelClass input, String className) { + String fqn = input.getPackageName() + "." + className; + boolean canGenerate = !isInClassPath(fqn); + + if (canGenerate) { + + // check there is no operation on input + if (!input.getOperations().isEmpty()) { + + throw new GeneratorException( "Can't generate a simple bean as class " + - input.getQualifiedName() + + fqn + " contains so operations." + "\nUse instead the JavaBeanTransformer."); } @@ -93,27 +184,129 @@ return canGenerate; } - protected ObjectModelClass generateBean(ObjectModelClass input) { + protected void generateBeanFactory(ObjectModel model, String className) { + String defaultPackage = + getConfiguration().getProperty(PROP_DEFAULT_PACKAGE); + ObjectModelClass output = createClass(className, defaultPackage); + + for (ObjectModelClass aClass : model.getClasses()) { + if (!aClass.isAbstract() && + JavaTemplatesGeneratorUtil.hasBeanStereotype(aClass)) { + String packageName = aClass.getPackageName(); + String typeName = getBeanInterfaceName(aClass); + String typeBeanName = getBeanClassName(aClass); + addImport(output, packageName + "." + typeName); + addImport(output, packageName + "." + typeBeanName); + ObjectModelOperation operation = addOperation( + output, + "typeOf" + typeName, + "Class<? extends " + typeName + ">", + ObjectModelJavaModifier.STATIC, + ObjectModelJavaModifier.PUBLIC + ); + setOperationBody(operation, "" + /*{ + return <%=typeBeanName%>.class; + }*/ + ); + operation = addOperation( + output, + "new" + typeName, + typeName, + ObjectModelJavaModifier.STATIC, + ObjectModelJavaModifier.PUBLIC + ); + setOperationBody(operation, "" + /*{ + return new <%=typeBeanName%>(); + }*/ + ); + } + } + } + + protected ObjectModelInterface generateBeanInterface(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 - boolean superClassIsBean = false; + Collection<ObjectModelClass> superclasses = input.getSuperclasses(); if (CollectionUtils.isNotEmpty(superclasses)) { for (ObjectModelClass superclass : superclasses) { if (JavaTemplatesGeneratorUtil.hasBeanStereotype(superclass)) { superClassIsBean = true; + superClass = superclass.getPackageName() + "." + + getBeanInterfaceName(superclass); break; } + superClass = superclass.getQualifiedName(); } } + if (superClass == null) { + + // try to find a super class by tag-value + superClass = + JavaTemplatesGeneratorUtil.getSimpleBeanInterfaceSuperClassTagValue( + model, input); + } + + boolean serializableFound = addInterfaces(input, output, superClass); + + if (serializableFound || superClassIsBean) { + addInterface(output, Serializable.class); + } + + generateI18nBlockAndConstants(input, output); + + for (ObjectModelOperation operation : getPublicOperations(outputClass)) { + cloneOperation(operation, output, true); + } + + return output; + } + + protected ObjectModelClass generateBeanClass(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 (JavaTemplatesGeneratorUtil.hasBeanStereotype(superclass)) { + superClassIsBean = true; + superClass = getBeanClassName(superclass); + break; + } else { + superClass = superclass.getQualifiedName(); + } + } + } + if (!superClassIsBean) { // try to find a super class by tag-value superClass = - JavaTemplatesGeneratorUtil.getBeanSuperClassTagValue( + JavaTemplatesGeneratorUtil.getSimpleBeanSuperClassTagValue( model, input); if (superClass != null) { @@ -122,9 +315,14 @@ } } - ObjectModelClass output = - createClass(input.getName(), input.getPackageName()); + ObjectModelClass output; + if (input.isAbstract()) { + output = createAbstractClass(className, input.getPackageName()); + } else { + output = createClass(className, input.getPackageName()); + } + if (superClass != null) { setSuperClass(output, superClass); } @@ -132,83 +330,99 @@ log.debug("will generate " + output.getQualifiedName()); } - String i18nPrefix = JavaGeneratorUtil.getI18nPrefixTagValue(input, model); - if (!StringUtils.isEmpty(i18nPrefix)) { - generateI18nBlock(input, output, i18nPrefix); - } + boolean serializableFound; - String noPCSTagValue = JavaTemplatesGeneratorUtil.getNoPCSTagValue(model, input); - boolean usePCS = StringUtils.isEmpty(noPCSTagValue) || - !"true".equals(noPCSTagValue.trim()); + if (generateInterface) { - String noGenerateBooleanGetMethods = - JavaGeneratorUtil.getDoNotGenerateBooleanGetMethods(model, input); - boolean generateBooleanGetMethods = - StringUtils.isEmpty(noGenerateBooleanGetMethods) || - !"true".equals(noGenerateBooleanGetMethods.trim()); + addInterface(output, interfaceName); - String prefix = getConstantPrefix(input, DEFAULT_CONSTANT_PREFIX); + serializableFound = true; + } else { - setConstantPrefix(prefix); + serializableFound = addInterfaces(input, output, null); - addSuperClass(input, output); - - boolean serializableFound = addInterfaces(input, output); - - if (superClassIsBean) { - serializableFound = true; + generateI18nBlockAndConstants(input, output); } - addSerializable(input, output, serializableFound); + addSerializable(input, output, serializableFound || superClassIsBean); - Set<String> constantNames = addConstantsFromDependency(input, output); - // Get available properties List<ObjectModelAttribute> properties = getProperties(input); - // Add properties constant - for (ObjectModelAttribute attr : properties) { + String noPCSTagValue = JavaTemplatesGeneratorUtil.getNoPCSTagValue(model, input); + boolean usePCS = StringUtils.isEmpty(noPCSTagValue) || + !"true".equals(noPCSTagValue.trim()); - createPropertyConstant(output, attr, prefix, constantNames); - } + String noGenerateBooleanGetMethods = + JavaGeneratorUtil.getDoNotGenerateBooleanGetMethods(model, input); + boolean generateBooleanGetMethods = + StringUtils.isEmpty(noGenerateBooleanGetMethods) || + !"true".equals(noGenerateBooleanGetMethods.trim()); // Add properties field + javabean methods for (ObjectModelAttribute attr : properties) { - createProperty(output, attr, usePCS, generateBooleanGetMethods); + createProperty(output, + attr, + usePCS, + generateBooleanGetMethods); } if (!superClassIsBean) { + addDefaultMethodForNoneBeanSuperClass(output, usePCS, properties); + } + return output; + } - if (usePCS) { - // Add property change support - createPropertyChangeSupport(output); + protected Collection<ObjectModelOperation> getPublicOperations(ObjectModelClass clazz) { + + Collection<ObjectModelOperation> result = new ArrayList<ObjectModelOperation>(); + for (ObjectModelOperation operation : clazz.getOperations()) { + + ObjectModelJavaModifier visibility = + ObjectModelJavaModifier.fromVisibility( + operation.getVisibility()); + if (ObjectModelJavaModifier.PUBLIC == visibility) { + result.add(operation); } } + return result; + } - boolean hasAMultipleProperty = containsMutiple(properties); + protected String getBeanInterfaceName(ObjectModelClass input) { + String interfaceNamePrefix = JavaTemplatesGeneratorUtil.getSimpleBeanInterfaceNamePrefixTagValue(model, input); + String interfaceNameSuffix = JavaTemplatesGeneratorUtil.getSimpleBeanInterfaceNameSuffixTagValue(model, input); - // Add helper operations - if (hasAMultipleProperty) { + return generateName( + interfaceNamePrefix, + input.getName(), + interfaceNameSuffix + ); + } - if (!superClassIsBean) { + protected String getBeanClassName(ObjectModelClass input) { + String classNamePrefix = JavaTemplatesGeneratorUtil.getSimpleBeanClassNamePrefixTagValue(model, input); + String classNameSuffix = JavaTemplatesGeneratorUtil.getSimpleBeanClassNameSuffixTagValue(model, input); - // add getChild methods - createGetChildMethod(output); - } - } - return output; + return generateName( + classNamePrefix, + input.getName(), + classNameSuffix + ); } - protected void addSuperClass(ObjectModelClass input, - ObjectModelClass output) { - // Set superclass - Iterator<ObjectModelClass> j = input.getSuperclasses().iterator(); - if (j.hasNext()) { - ObjectModelClass p = j.next(); - String qualifiedName = p.getQualifiedName(); - setSuperClass(output, qualifiedName); + 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(); } } Modified: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/package-info.java =================================================================== --- trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/package-info.java 2013-03-28 11:13:11 UTC (rev 1234) +++ trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/package-info.java 2013-03-29 23:36:59 UTC (rev 1235) @@ -1,9 +1,10 @@ /** * Eugene java package : Java generators. * <pre> - * - Generator : {@link JavaBeanTransformer } to generate a bean. + * - Generator : {@link JavaBeanTransformer } to generate a bean (with abstract and impl class). * - Generator : {@link JavaEnumerationTransformer } to generate a enumeration. * - Generator : {@link JavaInterfaceTransformer } to generate a interface. + * - Generator : {@link SimpleJavaBeanTransformer} to generate a bean (one class and interface if required). * </pre> */ package org.nuiton.eugene.java; Modified: trunk/eugene-jpa-templates/pom.xml =================================================================== --- trunk/eugene-jpa-templates/pom.xml 2013-03-28 11:13:11 UTC (rev 1234) +++ trunk/eugene-jpa-templates/pom.xml 2013-03-29 23:36:59 UTC (rev 1235) @@ -29,7 +29,7 @@ <parent> <groupId>org.nuiton</groupId> <artifactId>eugene</artifactId> - <version>2.7-SNAPSHOT</version> + <version>2.6.2-SNAPSHOT</version> </parent> <groupId>org.nuiton.eugene</groupId> Modified: trunk/eugene-maven-plugin/pom.xml =================================================================== --- trunk/eugene-maven-plugin/pom.xml 2013-03-28 11:13:11 UTC (rev 1234) +++ trunk/eugene-maven-plugin/pom.xml 2013-03-29 23:36:59 UTC (rev 1235) @@ -33,7 +33,7 @@ <parent> <groupId>org.nuiton</groupId> <artifactId>eugene</artifactId> - <version>2.7-SNAPSHOT</version> + <version>2.6.2-SNAPSHOT</version> </parent> <groupId>org.nuiton.eugene</groupId> Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2013-03-28 11:13:11 UTC (rev 1234) +++ trunk/pom.xml 2013-03-29 23:36:59 UTC (rev 1235) @@ -36,12 +36,12 @@ </parent> <artifactId>eugene</artifactId> - <version>2.7-SNAPSHOT</version> + <version>2.6.2-SNAPSHOT</version> <modules> <module>eugene</module> <module>eugene-java-templates</module> - <module>eugene-jpa-templates</module> + <!--module>eugene-jpa-templates</module--> <module>eugene-maven-plugin</module> </modules>
participants (1)
-
tchemit@users.nuiton.org