This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository eugene. See http://git.nuiton.org/eugene.git commit dd8b0765c2d755cff6d42a1d839cf77da4ce279e Author: Tony CHEMIT <chemit@codelutin.com> Date: Fri Aug 14 15:36:37 2015 +0200 Pouvoir générer des méthodes utiles (predicat, function) sur des beans (refs #3745) --- .../nuiton/eugene/java/JavaTemplatesTagValues.java | 65 ++++++++ .../SimpleJavaBeanWithNoInterfaceTransformer.java | 176 ++++++++++++++++++++- 2 files changed, 239 insertions(+), 2 deletions(-) diff --git a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaTemplatesTagValues.java b/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaTemplatesTagValues.java index 77ad643..cf8b1e9 100644 --- a/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaTemplatesTagValues.java +++ b/eugene-java-templates/src/main/java/org/nuiton/eugene/java/JavaTemplatesTagValues.java @@ -516,6 +516,36 @@ public class JavaTemplatesTagValues extends TagValueDefinitionProvider { public static final String TAG_SIMPLE_BEAN_WITH_NO_INTERFACE_DEFAULTS_CLASS_NAME_SUFFIX = "simpleBeanWithNoInterfaceDefaultsClassNameSuffix"; /** + * 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 #isSimpleBeanWithNoInterfaceGeneratePredicates(ObjectModelClassifier, ObjectModelPackage, ObjectModel) + * @since 3.0 + */ + @TagValueDefinition( + target = {ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class}, + defaultValue = "true", + documentation = "To generate or not guava predicates on propertyes on beans.\n" + + "(only effective with simple bean with no interface generator)") + public static final String TAG_SIMPLE_BEAN_WITH_NO_INTERFACE_GENERATE_PREDICATES = "simpleBeanWithNoInterfaceGeneratePredicates"; + + /** + * 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 #isSimpleBeanWithNoInterfaceGenerateFunctions(ObjectModelClassifier, ObjectModelPackage, ObjectModel) + * @since 3.0 + */ + @TagValueDefinition( + target = {ObjectModel.class, ObjectModelPackage.class, ObjectModelClassifier.class}, + defaultValue = "true", + documentation = "To generate or not guava predicates on propertyes on beans.\n" + + "(only effective with simple bean with no interface generator)") + public static final String TAG_SIMPLE_BEAN_WITH_NO_INTERFACE_GENERATE_FUNCTIONS= "simpleBeanWithNoInterfaceGenerateFunctions"; + + /** * Obtain the value of the {@link #TAG_GENERATE_PROPERTY_CHANGE_SUPPORT} 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. @@ -880,4 +910,39 @@ public class JavaTemplatesTagValues extends TagValueDefinitionProvider { return value; } + /** + * Obtain the value of the {@link #TAG_SIMPLE_BEAN_WITH_NO_INTERFACE_GENERATE_PREDICATES} 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 #TAG_SIMPLE_BEAN_WITH_NO_INTERFACE_GENERATE_PREDICATES + * @since 3.0 + */ + public boolean isSimpleBeanWithNoInterfaceGeneratePredicates(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { + boolean value = findBooleanTagValue(TAG_SIMPLE_BEAN_WITH_NO_INTERFACE_GENERATE_PREDICATES, classifier, aPackage, model); + return value; + } +/** + * Obtain the value of the {@link #TAG_SIMPLE_BEAN_WITH_NO_INTERFACE_GENERATE_FUNCTIONS} 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 #TAG_SIMPLE_BEAN_WITH_NO_INTERFACE_GENERATE_FUNCTIONS + * @since 3.0 + */ + public boolean isSimpleBeanWithNoInterfaceGenerateFunctions(ObjectModelClassifier classifier, ObjectModelPackage aPackage, ObjectModel model) { + boolean value = findBooleanTagValue(TAG_SIMPLE_BEAN_WITH_NO_INTERFACE_GENERATE_FUNCTIONS, classifier, aPackage, model); + return value; + } + } 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 94a8b3f..b904c55 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 @@ -25,6 +25,11 @@ package org.nuiton.eugene.java; /*{generator option: parentheses = false}*/ /*{generator option: writeString = +}*/ +import com.google.common.base.Function; +import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Iterables; +import com.google.common.collect.Maps; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; @@ -40,6 +45,7 @@ import org.nuiton.util.beans.BinderFactory; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Objects; /** * Generates a java bean and a utility class around it. This transformer acts like {@link SimpleJavaBeanTransformer} @@ -270,6 +276,9 @@ public class SimpleJavaBeanWithNoInterfaceTransformer extends AbstractJavaBeanTr if (!isInClassPath(packageName, abstractClassName)) { + boolean generatePredicates = getJavaTemplatesTagValues().isSimpleBeanWithNoInterfaceGeneratePredicates(aClass, aPackage, model); + boolean generateFunctions = getJavaTemplatesTagValues().isSimpleBeanWithNoInterfaceGenerateFunctions(aClass, aPackage, model); + // generate abstract defaults class // try to find a super class by tag-value @@ -324,7 +333,7 @@ public class SimpleJavaBeanWithNoInterfaceTransformer extends AbstractJavaBeanTr setOperationBody(operation, "" /*{ Class<BeanType> sourceType = typeOf<%=typeName%>(); - Binder<BeanType,BeanType> binder = BinderFactory.newBinder(sourceType); + Binder<BeanType, BeanType> binder = BinderFactory.newBinder(sourceType); BeanType result = new<%=typeName%>(source, binder); return result; }*/ @@ -359,7 +368,7 @@ public class SimpleJavaBeanWithNoInterfaceTransformer extends AbstractJavaBeanTr setOperationBody(operation, "" /*{ Class<BeanType> sourceType = typeOf<%=typeName%>(); - Binder<BeanType,BeanType> binder = BinderFactory.newBinder(sourceType); + Binder<BeanType, BeanType> binder = BinderFactory.newBinder(sourceType); binder.copy(source, target); }*/ ); @@ -379,7 +388,170 @@ public class SimpleJavaBeanWithNoInterfaceTransformer extends AbstractJavaBeanTr binder.copy(source, target); }*/ ); + + if (generatePredicates) { + generatePredicates(aClass, output, typeName); + } + + if (generateFunctions) { + generateFunctions(aClass, output, typeName); + } + } + } + + protected void generatePredicates(ObjectModelClass input, ObjectModelClass output, String typeName) { + + List<ObjectModelAttribute> properties = getProperties(input); + + if (!properties.isEmpty()) { + addImport(output, Predicate.class); + addImport(output, Objects.class); + addImport(output, Iterable.class); + addImport(output, Iterables.class); + } + + for (ObjectModelAttribute attribute : properties) { + + boolean multiple = JavaGeneratorUtil.isNMultiplicity(attribute); + + if (multiple) { + continue; + } + + String attrName = getAttributeName(attribute); + String attrType = getAttributeType(attribute); + + String simpleType = JavaGeneratorUtil.getSimpleName(attrType); + + String capitalizeAttrName = JavaGeneratorUtil.capitalizeJavaBeanPropertyName(attrName); + String newPreficateMethodName= "new" + capitalizeAttrName + "Predicate"; + ObjectModelOperation operation = addOperation( + output, + newPreficateMethodName, + "<BeanType extends " + typeName + "> Predicate<BeanType>", + ObjectModelJavaModifier.STATIC, + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, simpleType, attrName); + + String getterName = getGetterName(attrName, attribute); + 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 + "> Iterable<BeanType>", + ObjectModelJavaModifier.STATIC, + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, "Iterable<BeanType>", "source"); + addParameter(operation, simpleType, attrName); + setOperationBody(operation, "" + /*{ + return Iterables.filter(source, <%=newPreficateMethodName%>(<%=attrName%>)); + }*/ + ); + } + + } + + protected void generateFunctions(ObjectModelClass input, ObjectModelClass output, String typeName) { + + List<ObjectModelAttribute> properties = getProperties(input); + if (!properties.isEmpty()) { + addImport(output, Function.class); + addImport(output, Objects.class); + addImport(output, ImmutableMap.class); + addImport(output, Iterable.class); + addImport(output, Maps.class); + } + + for (ObjectModelAttribute attribute : properties) { + + boolean multiple = JavaGeneratorUtil.isNMultiplicity(attribute); + + if (multiple) { + continue; + } + + String attrName = getAttributeName(attribute); + String attrType = getAttributeType(attribute); + + String simpleType = JavaGeneratorUtil.getSimpleName(attrType); + String capitalizeAttrName = JavaGeneratorUtil.capitalizeJavaBeanPropertyName(attrName); + String newFunctionMethodName= "new" + capitalizeAttrName + "Function"; + String newFunctionTypeName= "Function<BeanType, " + simpleType + ">"; + + ObjectModelOperation operation = addOperation( + output, + newFunctionMethodName, + "<BeanType extends " + typeName + "> "+newFunctionTypeName, + ObjectModelJavaModifier.STATIC, + ObjectModelJavaModifier.PUBLIC + ); + + String getterName = getGetterName(attrName, attribute); + setOperationBody(operation, "" + /*{ + return new <%=newFunctionTypeName%>() { + + @Override + public <%=simpleType%> apply(BeanType input) { + return input.<%=getterName%>(); + } + }; + + }*/ + ); + + String getFunctionFieldName = JavaGeneratorUtil.convertVariableNameToConstantName("get" + capitalizeAttrName); + addAttribute( + output, + getFunctionFieldName, + "Function<" + typeName + ", " + simpleType + ">", + newFunctionMethodName + "()", + ObjectModelJavaModifier.FINAL, + ObjectModelJavaModifier.STATIC, + ObjectModelJavaModifier.PROTECTED + ); + + operation = addOperation( + output, + "uniqueIndexBy" + capitalizeAttrName, + "<BeanType extends " + typeName + "> ImmutableMap<" + simpleType + ", BeanType>", + ObjectModelJavaModifier.STATIC, + ObjectModelJavaModifier.PUBLIC + ); + addParameter(operation, "Iterable<BeanType>", "source"); + setOperationBody(operation, "" + /*{ + return Maps.uniqueIndex(source, <%=getFunctionFieldName%>); + }*/ + ); + } + + } + + private String getGetterName(String attrName, ObjectModelAttribute attribute) { + boolean booleanProperty = JavaGeneratorUtil.isBooleanPrimitive(attribute); + String methodPrefix = JavaGeneratorUtil.OPERATION_GETTER_DEFAULT_PREFIX; + if (booleanProperty) { + methodPrefix = JavaGeneratorUtil.OPERATION_GETTER_BOOLEAN_PREFIX; } + String getterName = getJavaBeanMethodName(methodPrefix, attrName); + return getterName; } protected Collection<ObjectModelOperation> getPublicOperations(ObjectModelClass clazz) { -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.