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:48:12 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.0.x-fixes 903caf5e9 -> faa2fd56d


[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/faa2fd56
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/faa2fd56
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/faa2fd56

Branch: refs/heads/3.0.x-fixes
Commit: faa2fd56dcc246284e38b46bf0c48c682ed4aad9
Parents: 903caf5
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:47:59 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/faa2fd56/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 989dcea..995b295 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
@@ -522,18 +522,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);
@@ -543,16 +544,11 @@ public class ClientProxyImpl extends AbstractClient implements
                         String fieldName = StringUtils.uncapitalize(propertyName);
                         Field f = 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);
@@ -563,9 +559,37 @@ 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 static Field getDeclaredField(Class<?> cls, String fieldName) {
         if (cls == null || cls == Object.class) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/faa2fd56/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 cdb979d..37fb3b2 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
@@ -1865,7 +1865,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;
@@ -1877,11 +1877,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