cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-7267] Support for the case where BeanParam field names do not match the setter names
Date Mon, 06 Mar 2017 14:44:37 GMT
Repository: cxf
Updated Branches:
  refs/heads/master b1c6125f5 -> ad18f5aef


[CXF-7267] Support for the case where BeanParam field names do not match the setter names


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

Branch: refs/heads/master
Commit: ad18f5aef28eb82d613b3f4226b2ddae19b6415f
Parents: b1c6125
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Mon Mar 6 14:44:21 2017 +0000
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Mon Mar 6 14:44:21 2017 +0000

----------------------------------------------------------------------
 .../cxf/jaxrs/client/ClientProxyImpl.java       | 48 +++++++++++++++-----
 .../org/apache/cxf/systest/jaxrs/BookStore.java |  6 +--
 2 files changed, 39 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/ad18f5ae/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
----------------------------------------------------------------------
diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
index 04a112d..7362b71 100644
--- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
+++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
@@ -523,18 +523,19 @@ public class ClientProxyImpl extends AbstractClient implements
     private Map<String, BeanPair> getValuesFromBeanParam(Object bean,
                                                          Class<? extends Annotation>
annClass,
                                                          Map<String, BeanPair> values)
{
+        boolean completeFieldIntrospectionNeeded = false;
         for (Method m : bean.getClass().getMethods()) {
             if (m.getName().startsWith("set")) {
                 try {
                     String propertyName = m.getName().substring(3);
-                    Annotation annotation = m.getAnnotation(annClass);
+                    Annotation methodAnnotation = m.getAnnotation(annClass);
                     boolean beanParam = m.getAnnotation(BeanParam.class) != null;
-                    if (annotation != null || beanParam) {
+                    if (methodAnnotation != null || beanParam) {
                         Method getter = bean.getClass().getMethod("get" + propertyName, new
Class[]{});
                         Object value = getter.invoke(bean, new Object[]{});
                         if (value != null) {
-                            if (annotation != null) {
-                                String annotationValue = AnnotationUtils.getAnnotationValue(annotation);
+                            if (methodAnnotation != null) {
+                                String annotationValue = AnnotationUtils.getAnnotationValue(methodAnnotation);
                                 values.put(annotationValue, new BeanPair(value, m.getParameterAnnotations()[0]));
                             } else {
                                 getValuesFromBeanParam(value, annClass, values);
@@ -544,16 +545,11 @@ public class ClientProxyImpl extends AbstractClient implements
                         String fieldName = StringUtils.uncapitalize(propertyName);
                         Field f = InjectionUtils.getDeclaredField(bean.getClass(), fieldName);
                         if (f == null) {
+                            completeFieldIntrospectionNeeded = true;
                             continue;
                         }
-                        annotation = f.getAnnotation(annClass);
-                        if (annotation != null) {
-                            Object value = ReflectionUtil.accessDeclaredField(f, bean, Object.class);
-                            if (value != null) {
-                                String annotationValue = AnnotationUtils.getAnnotationValue(annotation);
-                                values.put(annotationValue, new BeanPair(value, f.getAnnotations()));
-                            }
-                        } else if (f.getAnnotation(BeanParam.class) != null) {
+                        boolean jaxrsParamAnnAvailable = getValuesFromBeanParamField(bean,
f, annClass, values);
+                        if (!jaxrsParamAnnAvailable && f.getAnnotation(BeanParam.class)
!= null) {
                             Object value = ReflectionUtil.accessDeclaredField(f, bean, Object.class);
                             if (value != null) {
                                 getValuesFromBeanParam(value, annClass, values);
@@ -564,10 +560,38 @@ public class ClientProxyImpl extends AbstractClient implements
                     // ignore
                 }
             }
+            if (completeFieldIntrospectionNeeded) {
+                for (Field f : bean.getClass().getDeclaredFields()) {
+                    boolean jaxrsParamAnnAvailable = getValuesFromBeanParamField(bean, f,
annClass, values);
+                    if (!jaxrsParamAnnAvailable && f.getAnnotation(BeanParam.class)
!= null) {
+                        Object value = ReflectionUtil.accessDeclaredField(f, bean, Object.class);
+                        if (value != null) {
+                            getValuesFromBeanParam(value, annClass, values);
+                        }
+                    }
+                }
+            }
         }
         return values;
     }
 
+    private boolean getValuesFromBeanParamField(Object bean,
+                                                Field f,
+                                                Class<? extends Annotation> annClass,
+                                                Map<String, BeanPair> values) {
+        boolean jaxrsParamAnnAvailable = false;
+        Annotation fieldAnnotation = f.getAnnotation(annClass);
+        if (fieldAnnotation != null) {
+            jaxrsParamAnnAvailable = true;
+            Object value = ReflectionUtil.accessDeclaredField(f, bean, Object.class);
+            if (value != null) {
+                String annotationValue = AnnotationUtils.getAnnotationValue(fieldAnnotation);
+                values.put(annotationValue, new BeanPair(value, f.getAnnotations()));
+            }
+        }
+        return jaxrsParamAnnAvailable;
+    }
+    
     private void handleMatrixes(Method m,
                                 Object[] params,
                                 MultivaluedMap<ParameterType, Parameter> map,

http://git-wip-us.apache.org/repos/asf/cxf/blob/ad18f5ae/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
index 2424d40..dde2889 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
@@ -1873,7 +1873,7 @@ public class BookStore {
     public static class BookBean2 {
         private long id;
         @QueryParam("id_2")
-        private long id2;
+        private long mId2;
         private long id3;
         public long getId() {
             return id;
@@ -1885,11 +1885,11 @@ public class BookStore {
         }
 
         public long getId2() {
-            return id2;
+            return mId2;
         }
 
         public void setId2(long id2) {
-            this.id2 = id2;
+            this.mId2 = id2;
         }
 
         @Context


Mime
View raw message