You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2024/06/07 22:22:23 UTC

(cxf-xjc-utils) branch 3.x-fixes updated: CXFXJC-47: XJC DefaultValue plugin uses JAXBElement that does not have a default constructor (#129)

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

reta pushed a commit to branch 3.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf-xjc-utils.git


The following commit(s) were added to refs/heads/3.x-fixes by this push:
     new 148e041  CXFXJC-47: XJC DefaultValue plugin uses JAXBElement that does not have a default constructor (#129)
148e041 is described below

commit 148e04199134248e6439796044e9fd21b31dc58f
Author: Marcel Hödl <ma...@hotmail.de>
AuthorDate: Sat Jun 8 00:15:55 2024 +0200

    CXFXJC-47: XJC DefaultValue plugin uses JAXBElement that does not have a default constructor (#129)
    
    * Added new Parameter to exclude specific classes from default value generation
    
    * CXFXJC-47: XJC DefaultValue plugin uses JAXBElement that does not have a default constructor
    
    ---------
    
    Co-authored-by: Marcel Hödl <ma...@ofdka.bwl.de>
    Co-authored-by: Andriy Redko <dr...@gmail.com>
---
 .../src/test/resources/schemas/configuration/foo.xsd  | 18 +++++++++++++++++-
 .../org/apache/cxf/xjc/dv/DefaultValuePlugin.java     | 19 +++++++++++++++----
 2 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/dv-test/src/test/resources/schemas/configuration/foo.xsd b/dv-test/src/test/resources/schemas/configuration/foo.xsd
index bc0e0ec..dfa9b2b 100644
--- a/dv-test/src/test/resources/schemas/configuration/foo.xsd
+++ b/dv-test/src/test/resources/schemas/configuration/foo.xsd
@@ -147,5 +147,21 @@
       <xs:sequence>
         <xs:element minOccurs="0" name="redemptionNumber" type="xs:string" />
       </xs:sequence>
-    </xs:complexType>    
+    </xs:complexType>
+
+    <xs:element name="vehicle" type="tns:vehicle"/>
+    <xs:complexType name="vehicle" abstract="true"/>
+
+    <xs:complexType name="car">
+        <xs:complexContent>
+            <xs:extension base="tns:vehicle"/>
+        </xs:complexContent>
+    </xs:complexType>
+    <xs:element name="car" type="tns:car" substitutionGroup="tns:vehicle"/>
+
+    <xs:complexType name="trip">
+        <xs:sequence>
+            <xs:element ref="tns:vehicle"/>
+        </xs:sequence>
+    </xs:complexType>
 </xs:schema>
diff --git a/dv/src/main/java/org/apache/cxf/xjc/dv/DefaultValuePlugin.java b/dv/src/main/java/org/apache/cxf/xjc/dv/DefaultValuePlugin.java
index 1dd2382..0b67943 100644
--- a/dv/src/main/java/org/apache/cxf/xjc/dv/DefaultValuePlugin.java
+++ b/dv/src/main/java/org/apache/cxf/xjc/dv/DefaultValuePlugin.java
@@ -21,7 +21,10 @@ package org.apache.cxf.xjc.dv;
 
 import java.io.IOException;
 import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -70,8 +73,17 @@ import com.sun.xml.xsom.XmlString;
  * with their default value.
  */
 public class DefaultValuePlugin {
-    
+
     private static final Logger LOG = Logger.getLogger(DefaultValuePlugin.class.getName()); //NOPMD
+
+    // Known JAXB / JAXWS classes that do not have default constructors.
+    private static final Set<String> KNOWN_NO_DV_CLASSES = new HashSet<>(
+            Arrays.asList(
+                "javax.xml.ws.wsaddressing.W3CEndpointReference",
+                "javax.xml.bind.JAXBElement"
+            )
+        );
+
     private boolean complexTypes;
     private boolean active;
     
@@ -143,7 +155,7 @@ public class DefaultValuePlugin {
     private boolean isElementRequired(XSParticle particle) {
         return particle != null && getMinOccurs(particle) != 0 && getMaxOccurs(particle) == 1;
     }
-    
+
     private int getMinOccurs(XSParticle particle) {
         try {
             Number o = (Number)particle.getClass().getMethod("getMinOccurs").invoke(particle);
@@ -203,8 +215,7 @@ public class DefaultValuePlugin {
                     String varName = f.getPropertyInfo().getName(false);
                     JFieldVar var = co.implClass.fields().get(varName);
                     final JType rawType = f.getRawType();
-                    if (var != null
-                            && !"javax.xml.ws.wsaddressing.W3CEndpointReference".equals(f.getRawType().fullName())) {
+                    if (var != null && !KNOWN_NO_DV_CLASSES.contains(rawType.erasure().fullName())) {
                         if (rawType instanceof JClass) {
                             final JClass jclazz = (JClass) rawType;
                             if (!jclazz.isAbstract() && !jclazz.isInterface()) {