Skip to content

Commit

Permalink
CXF-8701: move getPropertyDescriptorsAvoidSunBug to the only place wh…
Browse files Browse the repository at this point in the history
…ere it's used, reduce footprint of `java.desktop` dependency

Signed-off-by: Raymond Augé <[email protected]>
  • Loading branch information
rotty3000 committed May 7, 2022
1 parent 97819b5 commit 14ff3fd
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 70 deletions.
65 changes: 0 additions & 65 deletions core/src/main/java/org/apache/cxf/common/util/ReflectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,18 @@

package org.apache.cxf.common.util;

import java.beans.BeanInfo;
import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.List;

import org.apache.cxf.common.classloader.ClassLoaderUtils;

public final class ReflectionUtil {

private static Method springBeanUtilsDescriptorFetcher;
private static boolean springChecked;

private ReflectionUtil() {
// intentionally empty
}
Expand Down Expand Up @@ -195,61 +185,6 @@ public T run() {
});
}

/**
* create own array of property descriptors to:
* <pre>
* - prevent memory leaks by Introspector's cache
* - get correct type for generic properties from superclass
* that are limited to a specific type in beanClass
* see http://bugs.sun.com/view_bug.do?bug_id=6528714
* we cannot use BeanUtils.getPropertyDescriptors because of issue SPR-6063
* </pre>
* @param refClass calling class for class loading.
* @param beanInfo Bean in question
* @param beanClass class for bean in question
* @param propertyDescriptors raw descriptors
*/
public static PropertyDescriptor[] getPropertyDescriptorsAvoidSunBug(Class<?> refClass,
BeanInfo beanInfo,
Class<?> beanClass,
PropertyDescriptor[] propertyDescriptors) {
if (!springChecked) {
try {
springChecked = true;
Class<?> cls = ClassLoaderUtils
.loadClass("org.springframework.beans.BeanUtils", refClass);
springBeanUtilsDescriptorFetcher
= cls.getMethod("getPropertyDescriptor", Class.class, String.class);
} catch (Exception e) {
//ignore - just assume it's an unsupported/unknown annotation
}
}

if (springBeanUtilsDescriptorFetcher != null) {
if (propertyDescriptors != null) {
List<PropertyDescriptor> descriptors = new ArrayList<>(propertyDescriptors.length);
for (int i = 0; i < propertyDescriptors.length; i++) {
PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
try {
propertyDescriptor = (PropertyDescriptor)springBeanUtilsDescriptorFetcher.invoke(null,
beanClass,
propertyDescriptor.getName());
if (propertyDescriptor != null) {
descriptors.add(propertyDescriptor);
}
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getCause());
}
}
return descriptors.toArray(new PropertyDescriptor[0]);
}
return null;
}
return beanInfo.getPropertyDescriptors();
}

/**
* Look for a specified annotation on a method. If there, return it. If not, search it's containing class.
* Assume that the annotation is marked @Inherited.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
Expand All @@ -38,9 +40,12 @@
import org.apache.cxf.aegis.type.AegisType;
import org.apache.cxf.aegis.type.TypeCreator;
import org.apache.cxf.aegis.type.TypeMapping;
import org.apache.cxf.common.util.ReflectionUtil;
import org.apache.cxf.common.classloader.ClassLoaderUtils;

public class BeanTypeInfo {
private static Method springBeanUtilsDescriptorFetcher;
private static boolean springChecked;

private Map<QName, QName> mappedName2typeName = new HashMap<>();
private Map<QName, String> mappedName2pdName = new HashMap<>();
private Map<QName, AegisType> mappedName2type = new HashMap<>();
Expand Down Expand Up @@ -106,6 +111,61 @@ public void initialize() {
}
}

/**
* create own array of property descriptors to:
* <pre>
* - prevent memory leaks by Introspector's cache
* - get correct type for generic properties from superclass
* that are limited to a specific type in beanClass
* see http://bugs.sun.com/view_bug.do?bug_id=6528714
* we cannot use BeanUtils.getPropertyDescriptors because of issue SPR-6063
* </pre>
* @param refClass calling class for class loading.
* @param beanInfo Bean in question
* @param beanClass class for bean in question
* @param propertyDescriptors raw descriptors
*/
public static PropertyDescriptor[] getPropertyDescriptorsAvoidSunBug(Class<?> refClass,
BeanInfo beanInfo,
Class<?> beanClass,
PropertyDescriptor[] propertyDescriptors) {
if (!springChecked) {
try {
springChecked = true;
Class<?> cls = ClassLoaderUtils
.loadClass("org.springframework.beans.BeanUtils", refClass);
springBeanUtilsDescriptorFetcher
= cls.getMethod("getPropertyDescriptor", Class.class, String.class);
} catch (Exception e) {
//ignore - just assume it's an unsupported/unknown annotation
}
}

if (springBeanUtilsDescriptorFetcher != null) {
if (propertyDescriptors != null) {
List<PropertyDescriptor> descriptors = new ArrayList<>(propertyDescriptors.length);
for (int i = 0; i < propertyDescriptors.length; i++) {
PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
try {
propertyDescriptor = (PropertyDescriptor)springBeanUtilsDescriptorFetcher.invoke(null,
beanClass,
propertyDescriptor.getName());
if (propertyDescriptor != null) {
descriptors.add(propertyDescriptor);
}
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getCause());
}
}
return descriptors.toArray(new PropertyDescriptor[0]);
}
return null;
}
return beanInfo.getPropertyDescriptors();
}

private synchronized void initializeSync() {
if (!initialized) {
for (int i = 0; i < descriptors.length; i++) {
Expand Down Expand Up @@ -285,10 +345,10 @@ private void initializeProperties() {
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
if (propertyDescriptors != null) {
// see comments on this function.
descriptors = ReflectionUtil.getPropertyDescriptorsAvoidSunBug(getClass(),
beanInfo,
beanClass,
propertyDescriptors);
descriptors = getPropertyDescriptorsAvoidSunBug(getClass(),
beanInfo,
beanClass,
propertyDescriptors);
}
}

Expand Down

0 comments on commit 14ff3fd

Please sign in to comment.