You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by il...@apache.org on 2022/03/22 11:36:24 UTC

[cxf] branch 3.4.x-fixes updated: [CXF-8680] Encapsulating getter method resolution in ClientProxyImpl (#926)

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

ilgrosso pushed a commit to branch 3.4.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/3.4.x-fixes by this push:
     new b822ae5  [CXF-8680] Encapsulating getter method resolution in ClientProxyImpl (#926)
b822ae5 is described below

commit b822ae56951fec5bb16de8baf5fb536015df7ef3
Author: Francesco Chicchiriccò <il...@users.noreply.github.com>
AuthorDate: Tue Mar 22 12:32:42 2022 +0100

    [CXF-8680] Encapsulating getter method resolution in ClientProxyImpl (#926)
---
 .../apache/cxf/jaxrs/client/ClientProxyImpl.java   | 63 ++++++++++++++++------
 1 file changed, 46 insertions(+), 17 deletions(-)

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 8937909..76bcbc0 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
@@ -42,8 +42,11 @@ import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
 import java.util.ResourceBundle;
 import java.util.function.Predicate;
+import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.stream.Collectors;
 
@@ -407,6 +410,23 @@ public class ClientProxyImpl extends AbstractClient implements
         return index;
     }
 
+    protected static Optional<Method> getBeanGetter(
+            final Class<?> clazz, final String property, final Class<?>... parameterTypes) {
+
+        try {
+            return Optional.of(clazz.getMethod("get" + StringUtils.capitalize(property), parameterTypes));
+        } catch (Throwable t1) {
+            try {
+                return Optional.of(clazz.getMethod("is" + StringUtils.capitalize(property), parameterTypes));
+            } catch (Throwable t2) {
+                LOG.log(Level.SEVERE,
+                        "While attempting to find getter method from {0}#{1}",
+                        new Object[] {clazz.getName(), property});
+                return Optional.empty();
+            }
+        }
+    }
+
     protected void checkResponse(Method m, Response r, Message inMessage) throws Throwable {
         Throwable t = null;
         int status = r.getStatus();
@@ -564,13 +584,13 @@ public class ClientProxyImpl extends AbstractClient implements
                 BeanPair pair = beanParamValues.get(varName);
                 list.add(convertParamValue(pair.getValue(), pair.getAnns()));
             } else if (requestBody != null) {
-                try {
-                    Method getter = requestBody.getClass().
-                            getMethod("get" + StringUtils.capitalize(varName), new Class<?>[]{});
-                    list.add(getter.invoke(requestBody, new Object[]{}));
-                } catch (Exception ex) {
-                    // continue
-                }
+                getBeanGetter(requestBody.getClass(), varName, new Class<?>[] {}).ifPresent(getter -> {
+                    try {
+                        getter.invoke(requestBody, new Object[] {});
+                    } catch (Exception ex) {
+                        // continue
+                    }
+                });
             }
         });
 
@@ -640,16 +660,24 @@ public class ClientProxyImpl extends AbstractClient implements
                     Annotation methodAnnotation = m.getAnnotation(annClass);
                     boolean beanParam = m.getAnnotation(BeanParam.class) != null;
                     if (methodAnnotation != null || beanParam) {
-                        Method getter = bean.getClass().getMethod("get" + propertyName, new Class<?>[]{});
-                        Object value = getter.invoke(bean, new Object[]{});
-                        if (value != null) {
-                            if (methodAnnotation != null) {
-                                String annotationValue = AnnotationUtils.getAnnotationValue(methodAnnotation);
-                                values.put(annotationValue, new BeanPair(value, m.getParameterAnnotations()[0]));
-                            } else {
-                                getValuesFromBeanParam(value, annClass, values);
-                            }
-                        }
+                        getBeanGetter(bean.getClass(), propertyName, new Class<?>[] {}).
+                                map(getter -> {
+                                    try {
+                                        return getter.invoke(bean, new Object[] {});
+                                    } catch (Exception ex) {
+                                        // ignore
+                                        return null;
+                                    }
+                                }).
+                                filter(Objects::nonNull).
+                                ifPresent(value -> {
+                                    if (methodAnnotation != null) {
+                                        String annValue = AnnotationUtils.getAnnotationValue(methodAnnotation);
+                                        values.put(annValue, new BeanPair(value, m.getParameterAnnotations()[0]));
+                                    } else {
+                                        getValuesFromBeanParam(value, annClass, values);
+                                    }
+                                });
                     } else {
                         String fieldName = StringUtils.uncapitalize(propertyName);
                         Field f = InjectionUtils.getDeclaredField(bean.getClass(), fieldName);
@@ -1032,6 +1060,7 @@ public class ClientProxyImpl extends AbstractClient implements
         return method.getReturnType();
     }
 
+    @Override
     public Object getInvocationHandler() {
         return this;
     }