Skip to content
Permalink
Browse files
[CXF-8680] Encapsulating getter method resolution in ClientProxyImpl (#…
  • Loading branch information
ilgrosso committed Mar 22, 2022
1 parent e2cb683 commit 27df629dcd18af625c28086655a0f5dec847fc46
Showing 1 changed file with 46 additions and 17 deletions.
@@ -42,8 +42,11 @@
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 @@ protected static int getBodyIndex(MultivaluedMap<ParameterType, Parameter> map,
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 @@ protected List<Object> getPathParamValues(Method m,
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 @@ protected Map<String, BeanPair> getValuesFromBeanParam(Object bean,
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 @@ protected Class<?> getReturnType(Method method, Message outMessage) {
return method.getReturnType();
}

@Override
public Object getInvocationHandler() {
return this;
}

0 comments on commit 27df629

Please sign in to comment.