cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject [3/4] cxf git commit: [CXF-7186][CXF-7188] Aegis DataBinding ignores certain JAXB annotations Patch from SanjinTulac applied (only to 3.2 branch due to behavior change) This closes #216
Date Thu, 30 Mar 2017 15:38:38 GMT
[CXF-7186][CXF-7188] Aegis DataBinding ignores certain JAXB annotations
Patch from SanjinTulac applied (only to 3.2 branch due to behavior change)
This closes #216


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/9263c72e
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/9263c72e
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/9263c72e

Branch: refs/heads/master
Commit: 9263c72e29ce5169af9f9393bc1e391f0e8d2f64
Parents: b578737
Author: Daniel Kulp <dkulp@apache.org>
Authored: Thu Mar 30 09:56:15 2017 -0400
Committer: Daniel Kulp <dkulp@apache.org>
Committed: Thu Mar 30 10:27:13 2017 -0400

----------------------------------------------------------------------
 .../cxf/aegis/type/java5/AnnotatedTypeInfo.java | 44 +++++++++++++++++++-
 .../cxf/aegis/type/java5/AnnotationReader.java  | 19 ++++++++-
 .../apache/cxf/aegis/type/java5/EnumType.java   | 37 +++++++++++++---
 .../cxf/aegis/type/java5/EnumTypeTest.java      | 33 +++++++++++++++
 .../apache/cxf/aegis/type/java5/JaxbBean1.java  | 10 +++++
 .../cxf/aegis/type/java5/JaxbTestEnum.java      |  7 +++-
 .../cxf/aegis/type/java5/JaxbTypeTest.java      | 28 ++++++++++++-
 7 files changed, 165 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/9263c72e/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/java5/AnnotatedTypeInfo.java
----------------------------------------------------------------------
diff --git a/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/java5/AnnotatedTypeInfo.java
b/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/java5/AnnotatedTypeInfo.java
index 0a4532f..a978bee 100644
--- a/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/java5/AnnotatedTypeInfo.java
+++ b/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/java5/AnnotatedTypeInfo.java
@@ -19,6 +19,7 @@
 package org.apache.cxf.aegis.type.java5;
 
 import java.beans.PropertyDescriptor;
+import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 
 import javax.xml.namespace.QName;
@@ -93,14 +94,46 @@ public class AnnotatedTypeInfo extends BeanTypeInfo {
     }
 
     protected QName createQName(PropertyDescriptor desc) {
+        String name = getName(desc);
+        String namespace = getNamespace(desc);
+        return new QName(namespace, name);
+    }
+
+    /**
+     * XML Name of a field is derived from the following sources in this order of priorities:
+     * <ul>
+     *   <li> getter method annotation
+     *   <li> field annotation
+     *   <li> field name
+     * </ul>
+     */
+    private String getName(PropertyDescriptor desc) {
         String name = annotationReader.getName(desc.getReadMethod());
         if (name == null) {
+            name = annotationReader.getName(getField(desc));
+        }
+        if (name == null) {
             name = desc.getName();
         }
+        return name;
+    }
 
-        // namespace: method, class, package, generated
+    /**
+     * XML Namespace of a field is derived from the following sources in this order of priorities:
+     * <ol>
+     *  <li> getter method annotation
+     *  <li> field annotation
+     *  <li> class annotation
+     *  <li> package annotation
+     *  <li> fully qualified package name
+     * </ol>
+     */
+    private String getNamespace(PropertyDescriptor desc) {
         String namespace = annotationReader.getNamespace(desc.getReadMethod());
         if (namespace == null) {
+            namespace = annotationReader.getNamespace(getField(desc));
+        }
+        if (namespace == null) {
             namespace = annotationReader.getNamespace(getTypeClass());
         }
         if (namespace == null) {
@@ -109,8 +142,15 @@ public class AnnotatedTypeInfo extends BeanTypeInfo {
         if (namespace == null) {
             namespace = NamespaceHelper.makeNamespaceFromClassName(getTypeClass().getName(),
"http");
         }
+        return namespace;
+    }
 
-        return new QName(namespace, name);
+    private Field getField(PropertyDescriptor desc) {
+        try {
+            return getTypeClass().getDeclaredField(desc.getName());
+        } catch (NoSuchFieldException e) {
+            return null;
+        }
     }
 
     public boolean isNillable(QName name) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/9263c72e/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/java5/AnnotationReader.java
----------------------------------------------------------------------
diff --git a/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/java5/AnnotationReader.java
b/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/java5/AnnotationReader.java
index d4fcb5a..33f1b4f 100644
--- a/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/java5/AnnotationReader.java
+++ b/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/java5/AnnotationReader.java
@@ -20,8 +20,11 @@ package org.apache.cxf.aegis.type.java5;
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 
+import javax.xml.bind.annotation.XmlEnumValue;
+
 import org.apache.cxf.aegis.type.AegisType;
 
 public class AnnotationReader {
@@ -271,7 +274,7 @@ public class AnnotationReader {
             Class<? extends Annotation>... annotations) {
 
         for (Class<?> annotation : annotations) {
-            if (annotation != null) {
+            if (annotation != null && element != null) {
                 try {
                     Annotation ann = element.getAnnotation(annotation.asSubclass(Annotation.class));
                     if (ann != null) {
@@ -369,5 +372,19 @@ public class AnnotationReader {
         return false;
     }
 
+    public static String getEnumValue(Enum<?> enumConstant) {
+        @SuppressWarnings("rawtypes")
+        Class<? extends Enum> enumClass = enumConstant.getClass();
+        try {
+            Field constantField = enumClass.getDeclaredField(enumConstant.name());
+            XmlEnumValue constantValueAnnotation = constantField.getAnnotation(XmlEnumValue.class);
+            if (constantValueAnnotation == null) {
+                return null;
+            }
+            return constantValueAnnotation.value();
+        } catch (NoSuchFieldException e) {
+            return null;
+        }
+    }
 
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/9263c72e/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/java5/EnumType.java
----------------------------------------------------------------------
diff --git a/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/java5/EnumType.java
b/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/java5/EnumType.java
index 82b8cc8..91a4eb2 100644
--- a/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/java5/EnumType.java
+++ b/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/java5/EnumType.java
@@ -34,19 +34,16 @@ import org.apache.ws.commons.schema.XmlSchemaSimpleTypeRestriction;
 import org.apache.ws.commons.schema.constants.Constants;
 
 public class EnumType extends AegisType {
-    @SuppressWarnings("unchecked")
     @Override
     public Object readObject(MessageReader reader, Context context) {
         String value = reader.getValue();
-        @SuppressWarnings("rawtypes")
-        Class<? extends Enum> cls = (Class<? extends Enum>)getTypeClass();
-        return Enum.valueOf(cls, value.trim());
+        return matchValue(value);
     }
 
     @Override
     public void writeObject(Object object, MessageWriter writer, Context context) {
         // match the reader.
-        writer.writeValue(((Enum<?>)object).name());
+        writer.writeValue(getValue((Enum<?>)object));
     }
 
     @Override
@@ -77,11 +74,39 @@ public class EnumType extends AegisType {
         List<XmlSchemaFacet> facets = restriction.getFacets();
         for (Object constant : constants) {
             XmlSchemaEnumerationFacet f = new XmlSchemaEnumerationFacet();
-            f.setValue(((Enum<?>)constant).name());
+            f.setValue(getValue(constant));
             facets.add(f);
         }
     }
 
+    @SuppressWarnings("unchecked")
+    private Enum<?> matchValue(String value) {
+        if (value == null) {
+            return null;
+        }
+        @SuppressWarnings("rawtypes")
+        Class<? extends Enum> enumClass = (Class<? extends Enum>)getTypeClass();
+        for (Enum<?> enumConstant : enumClass.getEnumConstants()) {
+            if (value.equals(AnnotationReader.getEnumValue(enumConstant))) {
+                return enumConstant;
+            }
+        }
+        return Enum.valueOf(enumClass, value.trim());
+    }
+
+
+    private Object getValue(Object constant) {
+        if (!(constant instanceof Enum<?>)) {
+            return null;
+        }
+        Enum<?> enumConstant = (Enum<?>)constant;
+        String annotatedValue = AnnotationReader.getEnumValue(enumConstant);
+        if (annotatedValue != null) {
+            return annotatedValue;
+        }
+        return enumConstant.name();
+    }
+
     @Override
     public boolean isComplex() {
         return true;

http://git-wip-us.apache.org/repos/asf/cxf/blob/9263c72e/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/EnumTypeTest.java
----------------------------------------------------------------------
diff --git a/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/EnumTypeTest.java
b/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/EnumTypeTest.java
index 96dd683..83a4b87 100644
--- a/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/EnumTypeTest.java
+++ b/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/EnumTypeTest.java
@@ -157,6 +157,39 @@ public class EnumTypeTest extends AbstractAegisTest {
         EnumBean bean = (EnumBean)value;
         assertNull(bean.getCurrency());
     }
+    
+    /**
+     * {@link https://issues.apache.org/jira/browse/CXF-7188}
+     */
+    @Test
+    public void testTypeWithJaxbAnnotations() throws Exception {
+        AegisType type = tm.getTypeCreator().createType(JaxbTestEnum.class);
+
+        Element element = writeObjectToElement(type, JaxbTestEnum.VALUE1, getContext());
+
+        assertEquals("Value1", element.getTextContent());
+        XMLStreamReader xreader = StaxUtils.createXMLStreamReader(element);
+        ElementReader reader = new ElementReader(xreader);
+        Object value = type.readObject(reader, getContext());
+        assertEquals(JaxbTestEnum.VALUE1, value);
+    }
+    
+    /**
+     * {@link https://issues.apache.org/jira/browse/CXF-7188}
+    */
+    @Test
+    public void testWsdlFromJaxbAnnotations() throws Exception {
+        AegisType type = tm.getTypeCreator().createType(JaxbTestEnum.class);
+        XmlSchema schema = newXmlSchema("urn:test");
+        type.writeSchema(schema);
+
+        XmlSchemaSerializer ser = new XmlSchemaSerializer();
+        Document doc = ser.serializeSchema(schema, false)[0];
+        addNamespace("xsd", Constants.URI_2001_SCHEMA_XSD);
+        assertValid("//xsd:simpleType[@name='bar']/xsd:restriction[@base='xsd:string']",
doc);
+        assertValid("//xsd:restriction[@base='xsd:string']/xsd:enumeration[@value='Value1']",
doc);
+        assertValid("//xsd:restriction[@base='xsd:string']/xsd:enumeration[@value='VALUE2']",
doc);
+    }
 
     public static class EnumBean {
         private Currency currency;

http://git-wip-us.apache.org/repos/asf/cxf/blob/9263c72e/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/JaxbBean1.java
----------------------------------------------------------------------
diff --git a/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/JaxbBean1.java
b/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/JaxbBean1.java
index d3312bb..64e8d8e 100644
--- a/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/JaxbBean1.java
+++ b/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/JaxbBean1.java
@@ -25,6 +25,8 @@ public class JaxbBean1 {
     private String elementProperty;
     private String attributeProperty;
     private String bogusProperty;
+    @XmlElement(name = "Annotated")
+    private String annotatedProperty;
 
     @XmlAttribute
     public String getAttributeProperty() {
@@ -51,4 +53,12 @@ public class JaxbBean1 {
     public void setElementProperty(String elementProperty) {
         this.elementProperty = elementProperty;
     }
+
+    public String getAnnotatedProperty() {
+        return annotatedProperty;
+    }
+
+    public void setAnnotatedProperty(String annotatedProperty) {
+        this.annotatedProperty = annotatedProperty;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/9263c72e/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/JaxbTestEnum.java
----------------------------------------------------------------------
diff --git a/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/JaxbTestEnum.java
b/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/JaxbTestEnum.java
index aacb8ab..aad0f91 100644
--- a/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/JaxbTestEnum.java
+++ b/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/JaxbTestEnum.java
@@ -18,9 +18,12 @@
  */
 package org.apache.cxf.aegis.type.java5;
 
+import javax.xml.bind.annotation.XmlEnumValue;
 import javax.xml.bind.annotation.XmlType;
 
-@XmlType(namespace = "urn:xfire:foo")
+@XmlType(namespace = "urn:xfire:foo", name = "bar")
 public enum JaxbTestEnum {
-    VALUE1, VALUE2
+    @XmlEnumValue("Value1")
+    VALUE1,
+    VALUE2
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/9263c72e/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/JaxbTypeTest.java
----------------------------------------------------------------------
diff --git a/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/JaxbTypeTest.java
b/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/JaxbTypeTest.java
index 33f6911..d7a1258 100644
--- a/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/JaxbTypeTest.java
+++ b/rt/databinding/aegis/src/test/java/org/apache/cxf/aegis/type/java5/JaxbTypeTest.java
@@ -67,7 +67,7 @@ public class JaxbTypeTest extends AbstractAegisTest {
 
         Iterator<QName> elements = info.getElements().iterator();
         assertTrue(elements.hasNext());
-        QName element = elements.next();
+        QName element = elements.next(); //1st element of 3 expected
         assertTrue(elements.hasNext());
 
         AegisType custom = info.getType(element);
@@ -76,10 +76,28 @@ public class JaxbTypeTest extends AbstractAegisTest {
             assertTrue(custom instanceof StringType);
         } else if ("elementProperty".equals(element.getLocalPart())) {
             assertTrue(custom instanceof CustomStringType);
+        } else if ("Annotated".equals(element.getLocalPart())) {
+            assertTrue(custom instanceof StringType);
         } else {
             fail("Unexpected element name: " + element.getLocalPart());
         }
-        element = elements.next();
+
+        assertTrue(elements.hasNext());
+        element = elements.next(); //2nd element of 3 expected
+        assertTrue(elements.hasNext());
+
+        custom = info.getType(element);
+
+        if ("bogusProperty".equals(element.getLocalPart())) {
+            assertTrue(custom instanceof StringType);
+        } else if ("elementProperty".equals(element.getLocalPart())) {
+            assertTrue(custom instanceof CustomStringType);
+        } else if ("Annotated".equals(element.getLocalPart())) {
+            assertTrue(custom instanceof StringType);
+        } else {
+            fail("Unexpected element name: " + element.getLocalPart());
+        }
+        element = elements.next();  //3rd element of 3 expected
         assertFalse(elements.hasNext());
 
         custom = info.getType(element);
@@ -88,6 +106,8 @@ public class JaxbTypeTest extends AbstractAegisTest {
             assertTrue(custom instanceof StringType);
         } else if ("elementProperty".equals(element.getLocalPart())) {
             assertTrue(custom instanceof CustomStringType);
+        } else if ("Annotated".equals(element.getLocalPart())) {
+            assertTrue(custom instanceof StringType);
         } else {
             fail("Unexpected element name: " + element.getLocalPart());
         }
@@ -165,6 +185,10 @@ public class JaxbTypeTest extends AbstractAegisTest {
                     "//xsd:complexType[@name='JaxbBean1']/xsd:sequence/xsd:element"
                     + "[@name='bogusProperty']",
                     wsdl);
+        assertValid(
+                    "//xsd:complexType[@name='JaxbBean1']/xsd:sequence/xsd:element"
+                    + "[@name='Annotated']",
+                    wsdl);
 
         assertValid(
                     "//xsd:complexType[@name='JaxbBean2']/xsd:sequence/xsd:element"


Mime
View raw message