Skip to content

Commit 14ff3fd

Browse files
committed
CXF-8701: move getPropertyDescriptorsAvoidSunBug to the only place where it's used, reduce footprint of java.desktop dependency
Signed-off-by: Raymond Augé <rotty3000@apache.org>
1 parent 97819b5 commit 14ff3fd

File tree

2 files changed

+65
-70
lines changed

2 files changed

+65
-70
lines changed

core/src/main/java/org/apache/cxf/common/util/ReflectionUtil.java

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,18 @@
1919

2020
package org.apache.cxf.common.util;
2121

22-
import java.beans.BeanInfo;
23-
import java.beans.PropertyDescriptor;
2422
import java.lang.annotation.Annotation;
2523
import java.lang.reflect.AccessibleObject;
2624
import java.lang.reflect.Constructor;
2725
import java.lang.reflect.Field;
28-
import java.lang.reflect.InvocationTargetException;
2926
import java.lang.reflect.Method;
3027
import java.security.AccessController;
3128
import java.security.PrivilegedAction;
3229
import java.security.PrivilegedActionException;
3330
import java.security.PrivilegedExceptionAction;
34-
import java.util.ArrayList;
35-
import java.util.List;
36-
37-
import org.apache.cxf.common.classloader.ClassLoaderUtils;
3831

3932
public final class ReflectionUtil {
4033

41-
private static Method springBeanUtilsDescriptorFetcher;
42-
private static boolean springChecked;
43-
4434
private ReflectionUtil() {
4535
// intentionally empty
4636
}
@@ -195,61 +185,6 @@ public T run() {
195185
});
196186
}
197187

198-
/**
199-
* create own array of property descriptors to:
200-
* <pre>
201-
* - prevent memory leaks by Introspector's cache
202-
* - get correct type for generic properties from superclass
203-
* that are limited to a specific type in beanClass
204-
* see http://bugs.sun.com/view_bug.do?bug_id=6528714
205-
* we cannot use BeanUtils.getPropertyDescriptors because of issue SPR-6063
206-
* </pre>
207-
* @param refClass calling class for class loading.
208-
* @param beanInfo Bean in question
209-
* @param beanClass class for bean in question
210-
* @param propertyDescriptors raw descriptors
211-
*/
212-
public static PropertyDescriptor[] getPropertyDescriptorsAvoidSunBug(Class<?> refClass,
213-
BeanInfo beanInfo,
214-
Class<?> beanClass,
215-
PropertyDescriptor[] propertyDescriptors) {
216-
if (!springChecked) {
217-
try {
218-
springChecked = true;
219-
Class<?> cls = ClassLoaderUtils
220-
.loadClass("org.springframework.beans.BeanUtils", refClass);
221-
springBeanUtilsDescriptorFetcher
222-
= cls.getMethod("getPropertyDescriptor", Class.class, String.class);
223-
} catch (Exception e) {
224-
//ignore - just assume it's an unsupported/unknown annotation
225-
}
226-
}
227-
228-
if (springBeanUtilsDescriptorFetcher != null) {
229-
if (propertyDescriptors != null) {
230-
List<PropertyDescriptor> descriptors = new ArrayList<>(propertyDescriptors.length);
231-
for (int i = 0; i < propertyDescriptors.length; i++) {
232-
PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
233-
try {
234-
propertyDescriptor = (PropertyDescriptor)springBeanUtilsDescriptorFetcher.invoke(null,
235-
beanClass,
236-
propertyDescriptor.getName());
237-
if (propertyDescriptor != null) {
238-
descriptors.add(propertyDescriptor);
239-
}
240-
} catch (IllegalArgumentException | IllegalAccessException e) {
241-
throw new RuntimeException(e);
242-
} catch (InvocationTargetException e) {
243-
throw new RuntimeException(e.getCause());
244-
}
245-
}
246-
return descriptors.toArray(new PropertyDescriptor[0]);
247-
}
248-
return null;
249-
}
250-
return beanInfo.getPropertyDescriptors();
251-
}
252-
253188
/**
254189
* Look for a specified annotation on a method. If there, return it. If not, search it's containing class.
255190
* Assume that the annotation is marked @Inherited.

rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/BeanTypeInfo.java

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.beans.IntrospectionException;
2323
import java.beans.Introspector;
2424
import java.beans.PropertyDescriptor;
25+
import java.lang.reflect.InvocationTargetException;
26+
import java.lang.reflect.Method;
2527
import java.util.ArrayList;
2628
import java.util.Arrays;
2729
import java.util.Comparator;
@@ -38,9 +40,12 @@
3840
import org.apache.cxf.aegis.type.AegisType;
3941
import org.apache.cxf.aegis.type.TypeCreator;
4042
import org.apache.cxf.aegis.type.TypeMapping;
41-
import org.apache.cxf.common.util.ReflectionUtil;
43+
import org.apache.cxf.common.classloader.ClassLoaderUtils;
4244

4345
public class BeanTypeInfo {
46+
private static Method springBeanUtilsDescriptorFetcher;
47+
private static boolean springChecked;
48+
4449
private Map<QName, QName> mappedName2typeName = new HashMap<>();
4550
private Map<QName, String> mappedName2pdName = new HashMap<>();
4651
private Map<QName, AegisType> mappedName2type = new HashMap<>();
@@ -106,6 +111,61 @@ public void initialize() {
106111
}
107112
}
108113

114+
/**
115+
* create own array of property descriptors to:
116+
* <pre>
117+
* - prevent memory leaks by Introspector's cache
118+
* - get correct type for generic properties from superclass
119+
* that are limited to a specific type in beanClass
120+
* see http://bugs.sun.com/view_bug.do?bug_id=6528714
121+
* we cannot use BeanUtils.getPropertyDescriptors because of issue SPR-6063
122+
* </pre>
123+
* @param refClass calling class for class loading.
124+
* @param beanInfo Bean in question
125+
* @param beanClass class for bean in question
126+
* @param propertyDescriptors raw descriptors
127+
*/
128+
public static PropertyDescriptor[] getPropertyDescriptorsAvoidSunBug(Class<?> refClass,
129+
BeanInfo beanInfo,
130+
Class<?> beanClass,
131+
PropertyDescriptor[] propertyDescriptors) {
132+
if (!springChecked) {
133+
try {
134+
springChecked = true;
135+
Class<?> cls = ClassLoaderUtils
136+
.loadClass("org.springframework.beans.BeanUtils", refClass);
137+
springBeanUtilsDescriptorFetcher
138+
= cls.getMethod("getPropertyDescriptor", Class.class, String.class);
139+
} catch (Exception e) {
140+
//ignore - just assume it's an unsupported/unknown annotation
141+
}
142+
}
143+
144+
if (springBeanUtilsDescriptorFetcher != null) {
145+
if (propertyDescriptors != null) {
146+
List<PropertyDescriptor> descriptors = new ArrayList<>(propertyDescriptors.length);
147+
for (int i = 0; i < propertyDescriptors.length; i++) {
148+
PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
149+
try {
150+
propertyDescriptor = (PropertyDescriptor)springBeanUtilsDescriptorFetcher.invoke(null,
151+
beanClass,
152+
propertyDescriptor.getName());
153+
if (propertyDescriptor != null) {
154+
descriptors.add(propertyDescriptor);
155+
}
156+
} catch (IllegalArgumentException | IllegalAccessException e) {
157+
throw new RuntimeException(e);
158+
} catch (InvocationTargetException e) {
159+
throw new RuntimeException(e.getCause());
160+
}
161+
}
162+
return descriptors.toArray(new PropertyDescriptor[0]);
163+
}
164+
return null;
165+
}
166+
return beanInfo.getPropertyDescriptors();
167+
}
168+
109169
private synchronized void initializeSync() {
110170
if (!initialized) {
111171
for (int i = 0; i < descriptors.length; i++) {
@@ -285,10 +345,10 @@ private void initializeProperties() {
285345
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
286346
if (propertyDescriptors != null) {
287347
// see comments on this function.
288-
descriptors = ReflectionUtil.getPropertyDescriptorsAvoidSunBug(getClass(),
289-
beanInfo,
290-
beanClass,
291-
propertyDescriptors);
348+
descriptors = getPropertyDescriptorsAvoidSunBug(getClass(),
349+
beanInfo,
350+
beanClass,
351+
propertyDescriptors);
292352
}
293353
}
294354

0 commit comments

Comments
 (0)