You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2022/03/04 08:34:20 UTC

[camel] branch CAMEL-17741/fix-default-name-retrieval created (now 6332c6a)

This is an automated email from the ASF dual-hosted git repository.

nfilotto pushed a change to branch CAMEL-17741/fix-default-name-retrieval
in repository https://gitbox.apache.org/repos/asf/camel.git.


      at 6332c6a  CAMEL-17741: Improper way to get the default name of a field annotated with XmlElement

This branch includes the following new commits:

     new 6332c6a  CAMEL-17741: Improper way to get the default name of a field annotated with XmlElement

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[camel] 01/01: CAMEL-17741: Improper way to get the default name of a field annotated with XmlElement

Posted by nf...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

nfilotto pushed a commit to branch CAMEL-17741/fix-default-name-retrieval
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 6332c6a2aee429d6caea3150a0c9abd2b3921709
Author: Nicolas Filotto <nf...@talend.com>
AuthorDate: Fri Mar 4 09:32:39 2022 +0100

    CAMEL-17741: Improper way to get the default name of a field annotated with XmlElement
---
 .../maven/dsl/yaml/GenerateYamlSupportMojo.java    | 68 +++++++++++++---------
 1 file changed, 39 insertions(+), 29 deletions(-)

diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl-maven-plugin/src/main/java/org/apache/camel/maven/dsl/yaml/GenerateYamlSupportMojo.java b/dsl/camel-yaml-dsl/camel-yaml-dsl-maven-plugin/src/main/java/org/apache/camel/maven/dsl/yaml/GenerateYamlSupportMojo.java
index 46b48dc..9928bbb 100644
--- a/dsl/camel-yaml-dsl/camel-yaml-dsl-maven-plugin/src/main/java/org/apache/camel/maven/dsl/yaml/GenerateYamlSupportMojo.java
+++ b/dsl/camel-yaml-dsl/camel-yaml-dsl-maven-plugin/src/main/java/org/apache/camel/maven/dsl/yaml/GenerateYamlSupportMojo.java
@@ -35,7 +35,6 @@ import com.squareup.javapoet.AnnotationSpec;
 import com.squareup.javapoet.ClassName;
 import org.apache.camel.maven.dsl.yaml.support.IndexerSupport;
 import org.apache.camel.util.AntPathMatcher;
-import org.apache.camel.util.ObjectHelper;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugins.annotations.Parameter;
@@ -168,6 +167,10 @@ public abstract class GenerateYamlSupportMojo extends AbstractMojo {
                     long.class.getName(),
                     float.class.getName(),
                     double.class.getName()));
+    /**
+     * The default value the String attributes of all the JAXB annotations.
+     */
+    private static final String XML_ANNOTATION_DEFAULT_VALUE = "##default";
 
     protected IndexView view;
 
@@ -231,13 +234,11 @@ public abstract class GenerateYamlSupportMojo extends AbstractMojo {
                 name);
     }
 
-    protected static Optional<AnnotationValue> annotationValue(MethodInfo target, DotName annotationName, String name) {
+    private static Optional<AnnotationInstance> annotation(FieldInfo target, DotName annotationName) {
         if (target == null) {
             return Optional.empty();
         }
-        return annotationValue(
-                target.annotation(annotationName),
-                name);
+        return Optional.ofNullable(target.annotation(annotationName));
     }
 
     /**
@@ -571,26 +572,23 @@ public abstract class GenerateYamlSupportMojo extends AbstractMojo {
         return Optional.empty();
     }
 
+    /**
+     * @see #fieldName(ClassInfo, FieldInfo)
+     */
     protected String fieldName(FieldInfo field) {
-        ClassInfo ct = view.getClassByName(field.type().name());
+        return fieldName(view.getClassByName(field.type().name()), field);
+    }
 
-        return firstPresent(
-                annotationValue(field, DSL_PROPERTY_ANNOTATION, "name")
-                        .map(AnnotationValue::asString)
-                        .filter(value -> ObjectHelper.isNotEmpty(value)),
-                annotationValue(field, XML_VALUE_ANNOTATION_CLASS, "name")
-                        .map(AnnotationValue::asString)
-                        .filter(value -> !"##default".equals(value)),
-                annotationValue(field, XML_ATTRIBUTE_ANNOTATION_CLASS, "name")
-                        .map(AnnotationValue::asString)
-                        .filter(value -> !"##default".equals(value)),
-                annotationValue(field, XML_ELEMENT_ANNOTATION_CLASS, "name")
-                        .map(AnnotationValue::asString)
-                        .filter(value -> !"##default".equals(value)),
-                annotationValue(ct, XML_ROOT_ELEMENT_ANNOTATION_CLASS, "name")
-                        .map(AnnotationValue::asString)
-                        .filter(value -> !"##default".equals(value)))
-                                .orElseGet(field::name);
+    /**
+     * @return the name from the given annotation or from the annotation {@code @XmlRootElement} of the provided class.
+     */
+    private Optional<String> getNameFromAnnotationOrRef(AnnotationInstance annotation, ClassInfo refClass, String emptyValue) {
+        return annotationValue(annotation, "name")
+            .map(AnnotationValue::asString)
+            .filter(value -> !emptyValue.equals(value))
+            .or(() -> annotationValue(refClass, XML_ROOT_ELEMENT_ANNOTATION_CLASS, "name")
+                .map(AnnotationValue::asString)
+                .filter(v -> !XML_ANNOTATION_DEFAULT_VALUE.equals(v)));
     }
 
     protected boolean isRequired(FieldInfo fi) {
@@ -685,20 +683,32 @@ public abstract class GenerateYamlSupportMojo extends AbstractMojo {
                 .sorted(Comparator.comparing(ClassInfo::name));
     }
 
+    /**
+     * As stated in the JAXB specification:
+     * <ul>
+     *     <li>In case of {@code @XmlAttribute} and {@code @XmlElement}, the name is retrieved from the annotation if it
+     *     has been set, otherwise the field name is used</li>
+     *     <li>In case of {@code @XmlElementRef} and {@code @DslProperty} (the latter is specific to Camel), the name is
+     *     retrieved from the annotation if it has been set, otherwise it retrieved from the annotation
+     *     {@code @XmlRootElement} on the type being referenced.
+     *     </li>
+     * </ul>
+     */
     protected String fieldName(ClassInfo ci, FieldInfo fi) {
         return firstPresent(
+            annotation(fi, DSL_PROPERTY_ANNOTATION)
+                .flatMap(annotation -> getNameFromAnnotationOrRef(annotation, ci, "")),
             annotationValue(fi, XML_VALUE_ANNOTATION_CLASS, "name")
                 .map(AnnotationValue::asString)
-                .filter(value -> !"##default".equals(value)),
+                .filter(value -> !XML_ANNOTATION_DEFAULT_VALUE.equals(value)),
             annotationValue(fi, XML_ATTRIBUTE_ANNOTATION_CLASS, "name")
                 .map(AnnotationValue::asString)
-                .filter(value -> !"##default".equals(value)),
+                .filter(value -> !XML_ANNOTATION_DEFAULT_VALUE.equals(value)),
             annotationValue(fi, XML_ELEMENT_ANNOTATION_CLASS, "name")
                 .map(AnnotationValue::asString)
-                .filter(value -> !"##default".equals(value)),
-            annotationValue(ci, XML_ROOT_ELEMENT_ANNOTATION_CLASS, "name")
-                .map(AnnotationValue::asString)
-                .filter(value -> !"##default".equals(value))
+                .filter(value -> !XML_ANNOTATION_DEFAULT_VALUE.equals(value)),
+            annotation(fi, XML_ELEMENT_REF_ANNOTATION_CLASS)
+                .flatMap(annotation -> getNameFromAnnotationOrRef(annotation, ci, XML_ANNOTATION_DEFAULT_VALUE))
         ).orElseGet(fi::name);
     }
 }