r1286 - in trunk: eugene/src/main/java/org/nuiton/eugene/java eugene/src/main/java/org/nuiton/eugene/java/extension eugene-java-templates/src/main/java/org/nuiton/eugene/java
Author: tchemit Date: 2013-07-11 15:53:23 +0200 (Thu, 11 Jul 2013) New Revision: 1286 Url: http://nuiton.org/projects/eugene/repository/revisions/1286 Log: fixes #2738: Improve api to add annotations Added: trunk/eugene/src/main/java/org/nuiton/eugene/java/extension/ObjectModelAnnotation.java trunk/eugene/src/main/java/org/nuiton/eugene/java/extension/ObjectModelAnnotationParameter.java Modified: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanTransformer.java trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaBuilder.java trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaGenerator.java trunk/eugene/src/main/java/org/nuiton/eugene/java/ObjectModelTransformerToJava.java trunk/eugene/src/main/java/org/nuiton/eugene/java/extension/AnnotationsManager.java trunk/eugene/src/main/java/org/nuiton/eugene/java/extension/AnnotationsManagerExtension.java Modified: trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaBuilder.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaBuilder.java 2013-07-11 13:50:16 UTC (rev 1285) +++ trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaBuilder.java 2013-07-11 13:53:23 UTC (rev 1286) @@ -25,9 +25,12 @@ package org.nuiton.eugene.java; +import com.google.common.base.Preconditions; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.eugene.GeneratorUtil; +import org.nuiton.eugene.java.extension.ObjectModelAnnotation; +import org.nuiton.eugene.java.extension.ObjectModelAnnotationParameter; import org.nuiton.eugene.java.extension.AnnotationsManager; import org.nuiton.eugene.java.extension.AnnotationsManagerExtension; import org.nuiton.eugene.java.extension.CodesManager; @@ -48,6 +51,7 @@ import org.nuiton.eugene.models.object.xml.ObjectModelClassImpl; import org.nuiton.eugene.models.object.xml.ObjectModelOperationImpl; +import javax.lang.model.SourceVersion; import java.util.Arrays; import java.util.HashSet; import java.util.Set; @@ -295,25 +299,37 @@ * * @param classifier where the annotation will be added. * @param element element on which add annotation - * @param annotation annotation to add + * @param annotationType type of annotation to add + * @return the instanciated annotation */ - public void addAnnotation(ObjectModelClassifier classifier, + public ObjectModelAnnotation addAnnotation(ObjectModelClassifier classifier, ObjectModelElement element, - String annotation) { - if (annotation == null) { - return; - } + String annotationType) { + Preconditions.checkNotNull("Can't add a null annotation"); + Preconditions.checkArgument(SourceVersion.isName(annotationType),"Annotation type must be a valid type , but was: "+annotationType); +// if (annotation == null) { +// return; +// } AnnotationsManager manager = annotationsManagerExtension.getManager(classifier); - annotation = annotation.trim(); +// annotation = annotation.trim(); + ObjectModelAnnotation annotation = new ObjectModelAnnotation(annotationType); manager.addAnnotation(element, annotation); if (log.isDebugEnabled()) { log.debug("Add annotation for <" + classifier.getQualifiedName() + ":" + element.getName() + "> : " + annotation); } + return annotation; } + public ObjectModelAnnotation addAnnotationParameter(ObjectModelAnnotation annotation, + String parameterName, + Object parameterValue) { + annotation.addParameter(new ObjectModelAnnotationParameter(parameterName, parameterValue)); + return annotation; + } + /** * Set the superclass of an other class. Only one superclass can be set * to the class. Modified: trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaGenerator.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaGenerator.java 2013-07-11 13:50:16 UTC (rev 1285) +++ trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaGenerator.java 2013-07-11 13:53:23 UTC (rev 1286) @@ -25,6 +25,8 @@ package org.nuiton.eugene.java; +import com.google.common.base.Joiner; +import com.google.common.collect.Lists; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; @@ -32,6 +34,8 @@ import org.nuiton.eugene.GeneratorUtil; import org.nuiton.eugene.java.extension.AnnotationsManagerExtension; import org.nuiton.eugene.java.extension.ImportsManagerExtension; +import org.nuiton.eugene.java.extension.ObjectModelAnnotation; +import org.nuiton.eugene.java.extension.ObjectModelAnnotationParameter; import org.nuiton.eugene.models.object.ObjectModelAttribute; import org.nuiton.eugene.models.object.ObjectModelClass; import org.nuiton.eugene.models.object.ObjectModelClassifier; @@ -222,17 +226,44 @@ AnnotationsManagerExtension managers = getModel().getExtension( AnnotationsManagerExtension.OBJECTMODEL_EXTENSION, AnnotationsManagerExtension.class); - String[] annotations = managers.getAnnotations(clazz,element); - for (String annotation : annotations) { - if (!annotation.trim().startsWith("@")) { - // add @ prefix - annotation = "@" + annotation.trim(); + List<ObjectModelAnnotation> annotations = + managers.getAnnotations(clazz, element); + for (ObjectModelAnnotation annotation : annotations) { +// if (!annotation.trim().startsWith("@")) { +// // add @ prefix +// annotation = "@" + annotation.trim(); +// } + StringBuilder annotationBuilder= new StringBuilder("@"+annotation.getType()); + List<ObjectModelAnnotationParameter> annotationParameters = annotation.getParameters(); + if (CollectionUtils.isNotEmpty(annotationParameters)) { + annotationBuilder.append('('); + List<String> params = Lists.newArrayList(); + for (ObjectModelAnnotationParameter annotationParameter : annotationParameters) { + String paramStr = annotationParameter.getName()+" = "; + Object value = annotationParameter.getValue(); + if (value instanceof String) { + paramStr+="\""+value+"\""; + } else if (value instanceof Enum) { + Enum anEnum = (Enum) value; + + paramStr += anEnum.getClass().getSimpleName() + "." + value; + } + else { + paramStr+= value.toString(); + } + params.add(paramStr); + } + Joiner.on(", ").appendTo(annotationBuilder, params); + + annotationBuilder.append(')'); } + String annotationStr =annotationBuilder.toString(); + if (element instanceof ObjectModelOperation || element instanceof ObjectModelAttribute) { /*{<%=prefix%>}*/ - annotation = " "+annotation; + annotationStr = " "+annotationStr; } -/*{<%=annotation%>}*/ +/*{<%=annotationStr%>}*/ if (element instanceof ObjectModelClassifier || element instanceof ObjectModelOperation || element instanceof ObjectModelAttribute) { /*{ }*/ Modified: trunk/eugene/src/main/java/org/nuiton/eugene/java/ObjectModelTransformerToJava.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/java/ObjectModelTransformerToJava.java 2013-07-11 13:50:16 UTC (rev 1285) +++ trunk/eugene/src/main/java/org/nuiton/eugene/java/ObjectModelTransformerToJava.java 2013-07-11 13:53:23 UTC (rev 1286) @@ -32,6 +32,7 @@ import org.nuiton.eugene.EugeneTagValues; import org.nuiton.eugene.GeneratorUtil; import org.nuiton.eugene.Template; +import org.nuiton.eugene.java.extension.ObjectModelAnnotation; import org.nuiton.eugene.java.extension.ImportsManager; import org.nuiton.eugene.models.object.ObjectModel; import org.nuiton.eugene.models.object.ObjectModelAttribute; @@ -122,18 +123,16 @@ * Add the {@link Generated} annotation to the given {@link ObjectModelClassifier} * * @param element the element to be generated on which the annotation will be added + * @since 2.7 */ // TODO AThimel 06/07/13 Maybe be skipped by configuration ? protected void addGeneratedAnnotation(ObjectModelClassifier element) { - addImport(element, Generated.class); - // TODO AThimel 08/07/13 Fix syntax as soon as it is possible to use annotations with parameters : http://www.nuiton.org/issues/2738 - String annotationName = Generated.class.getSimpleName(); String generatorName = getClass().getName(); Date now = new Date(); - String annotationText = String.format("%s(value = \"%s\", date = \"%s\")", annotationName, generatorName, now); - - addAnnotation(element, element, annotationText); + ObjectModelAnnotation annotation = addAnnotation(element, element, Generated.class); + addAnnotationParameter(element, annotation, "value", generatorName); + addAnnotationParameter(element, annotation, "date", now.toString()); } protected ObjectModelClass createClass(String name, String packageName) { @@ -424,13 +423,64 @@ builder.addComment(element, comment); } - public void addAnnotation(ObjectModelClassifier classifier, - ObjectModelElement element, - String annotation) { - builder.addAnnotation(classifier, element, annotation); + /** + * Add an annotation of the given {@code element} of the given {@code classifier}. + * + * @param classifier holder of element to treat + * @param element where to add the annotation + * @param annotationType type of annotation to create + * @return the instanciated annotation + * @since 2.6.4 + */ + public ObjectModelAnnotation addAnnotation(ObjectModelClassifier classifier, + ObjectModelElement element, + String annotationType) { + return builder.addAnnotation(classifier, element, annotationType); } /** + * Add an annotation of the given {@code element} of the given {@code classifier}. + * + * @param classifier holder of element to treat + * @param element where to add the annotation + * @param annotationType type of annotation to create + * @return the instanciated annotation + * @since 2.6.4 + */ + public ObjectModelAnnotation addAnnotation(ObjectModelClassifier classifier, + ObjectModelElement element, + Class<?> annotationType) { + addImport(classifier, annotationType); + return builder.addAnnotation(classifier, + element, + annotationType.getSimpleName()); + } + + /** + * Add a parameter to an annotation. + * + * + * @param classifier + * @param annotation where to add the parameter + * @param annotationName parameter name + * @param annotationValue paramter value + * @return the annotation + * @since 2.6.4 + */ + public ObjectModelAnnotation addAnnotationParameter(ObjectModelClassifier classifier, + ObjectModelAnnotation annotation, + String annotationName, + Object annotationValue) { + if (annotationValue instanceof Enum) { + Enum value = (Enum) annotationValue; + addImport(classifier, value.getClass()); + } + return builder.addAnnotationParameter(annotation, + annotationName, + annotationValue); + } + + /** * add an operation to the classifier with the form of a simple block * of code. * <p/> Modified: trunk/eugene/src/main/java/org/nuiton/eugene/java/extension/AnnotationsManager.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/java/extension/AnnotationsManager.java 2013-07-11 13:50:16 UTC (rev 1285) +++ trunk/eugene/src/main/java/org/nuiton/eugene/java/extension/AnnotationsManager.java 2013-07-11 13:53:23 UTC (rev 1286) @@ -28,6 +28,7 @@ import org.nuiton.eugene.models.object.ObjectModelElement; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -43,9 +44,9 @@ */ public class AnnotationsManager { - private static final String[] EMPTY_STRING_ARRAY = new String[]{}; +// private static final String[] EMPTY_STRING_ARRAY = new String[]{}; - protected Map<ObjectModelElement, List<String>> annotations; + protected Map<ObjectModelElement, List<ObjectModelAnnotation>> annotations; /** * Add the {@code annotation} for the given {@code element} of @@ -54,11 +55,12 @@ * @param element the element where to register the annotation * @param annotation the annotation to register */ - public void addAnnotation(ObjectModelElement element, String annotation) { - Map<ObjectModelElement, List<String>> map = getAnnotations(); - List<String> list = map.get(element); + public void addAnnotation(ObjectModelElement element, + ObjectModelAnnotation annotation) { + Map<ObjectModelElement, List<ObjectModelAnnotation>> map = getAnnotations(); + List<ObjectModelAnnotation> list = map.get(element); if (list == null) { - list = new ArrayList<String>(); + list = new ArrayList<ObjectModelAnnotation>(); map.put(element, list); } list.add(annotation); @@ -71,16 +73,15 @@ * @param element the element where to search for annotations * @return the annotations for the element (empty arry if none found */ - public String[] getAnnotations(ObjectModelElement element) { - Map<ObjectModelElement, List<String>> map = getAnnotations(); - List<String> list = map.get(element); - return list == null ? EMPTY_STRING_ARRAY : - list.toArray(new String[list.size()]); + public List<ObjectModelAnnotation> getAnnotations(ObjectModelElement element) { + Map<ObjectModelElement, List<ObjectModelAnnotation>> map = getAnnotations(); + List<ObjectModelAnnotation> list = map.get(element); + return list == null ? Collections.<ObjectModelAnnotation>emptyList() : list; } - protected Map<ObjectModelElement, List<String>> getAnnotations() { + protected Map<ObjectModelElement, List<ObjectModelAnnotation>> getAnnotations() { if (annotations == null) { - annotations = new HashMap<ObjectModelElement, List<String>>(); + annotations = new HashMap<ObjectModelElement, List<ObjectModelAnnotation>>(); } return annotations; } Modified: trunk/eugene/src/main/java/org/nuiton/eugene/java/extension/AnnotationsManagerExtension.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/java/extension/AnnotationsManagerExtension.java 2013-07-11 13:50:16 UTC (rev 1285) +++ trunk/eugene/src/main/java/org/nuiton/eugene/java/extension/AnnotationsManagerExtension.java 2013-07-11 13:53:23 UTC (rev 1286) @@ -30,7 +30,9 @@ import org.nuiton.eugene.models.object.ObjectModelClassifier; import org.nuiton.eugene.models.object.ObjectModelElement; +import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -55,28 +57,28 @@ */ protected Map<String, AnnotationsManager> managers; - private static final String[] EMPTY_STRING_ARRAY = new String[]{}; +// private static final String[] EMPTY_STRING_ARRAY = new String[]{}; /** * Get the registred annotations for the given {@code element} in the * given {@code classifier}. - * + * <p/> * <b>Note:</b> The method always returns a {@code none null} value, but * an empty array when no annotation when no annotation found for the - * element. - * + * element. + * * @param classifier the classifier where is the element - * @param element the element on which searching annotations - * @return the array of annotation registred or an empty array if none. + * @param element the element on which searching annotations + * @return the list of annotations registred or an empty list if none. */ - public String[] getAnnotations(ObjectModelClassifier classifier, - ObjectModelElement element) { + public List<ObjectModelAnnotation> getAnnotations(ObjectModelClassifier classifier, + ObjectModelElement element) { AnnotationsManager annotationsManager = getManager(classifier); - String[] result = null; + List<ObjectModelAnnotation> result = null; if (annotationsManager != null) { result = annotationsManager.getAnnotations(element); } - return result == null ? EMPTY_STRING_ARRAY : result; + return result == null ? Collections.<ObjectModelAnnotation>emptyList() : result; } /** Added: trunk/eugene/src/main/java/org/nuiton/eugene/java/extension/ObjectModelAnnotation.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/java/extension/ObjectModelAnnotation.java (rev 0) +++ trunk/eugene/src/main/java/org/nuiton/eugene/java/extension/ObjectModelAnnotation.java 2013-07-11 13:53:23 UTC (rev 1286) @@ -0,0 +1,76 @@ +package org.nuiton.eugene.java.extension; + +/* + * #%L + * EUGene :: EUGene + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2013 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 java.util.ArrayList; +import java.util.List; + +/** + * Define a annotation. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.6.4 + */ +public class ObjectModelAnnotation { + + protected final String type; + + protected List<ObjectModelAnnotationParameter> parameters; + + public ObjectModelAnnotation(String type) { + this.type = type; + } + + public String getType() { + return type; + } + + + public List<ObjectModelAnnotationParameter> getParameters() { + return parameters; + } + + public void addParameter(ObjectModelAnnotationParameter parameter) { + if (parameters == null) { + parameters = new ArrayList<ObjectModelAnnotationParameter>(); + } + parameters.add(parameter); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof ObjectModelAnnotation)) return false; + + ObjectModelAnnotation that = (ObjectModelAnnotation) o; + + return type.equals(that.type); + } + + @Override + public int hashCode() { + return type.hashCode(); + } +} Property changes on: trunk/eugene/src/main/java/org/nuiton/eugene/java/extension/ObjectModelAnnotation.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/eugene/src/main/java/org/nuiton/eugene/java/extension/ObjectModelAnnotationParameter.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/java/extension/ObjectModelAnnotationParameter.java (rev 0) +++ trunk/eugene/src/main/java/org/nuiton/eugene/java/extension/ObjectModelAnnotationParameter.java 2013-07-11 13:53:23 UTC (rev 1286) @@ -0,0 +1,52 @@ +package org.nuiton.eugene.java.extension; + +/* + * #%L + * EUGene :: EUGene + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2013 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% + */ + +/** + * Defines a annotation parameter. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.6.4 + */ +public class ObjectModelAnnotationParameter { + + protected final String name; + + protected final Object value; + + public ObjectModelAnnotationParameter(String name, Object value) { + this.name = name; + this.value = value; + } + + public String getName() { + return name; + } + + public Object getValue() { + return value; + } + +} Property changes on: trunk/eugene/src/main/java/org/nuiton/eugene/java/extension/ObjectModelAnnotationParameter.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanTransformer.java =================================================================== --- trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanTransformer.java 2013-07-11 13:50:16 UTC (rev 1285) +++ trunk/eugene-java-templates/src/main/java/org/nuiton/eugene/java/SimpleJavaBeanTransformer.java 2013-07-11 13:53:23 UTC (rev 1286) @@ -104,7 +104,7 @@ for (ObjectModelOperation operation : getPublicOperations(outputClass)) { addAnnotation(outputClass, operation, - Override.class.getSimpleName()); + Override.class); } }
participants (1)
-
tchemit@users.nuiton.org