Skip to content

Commit b796b52

Browse files
committed
Extract SimpleBeanInfo adaptation into internal SimpleBeanInfoFactory
See gh-29320
1 parent bba313c commit b796b52

File tree

3 files changed

+73
-14
lines changed

3 files changed

+73
-14
lines changed

spring-beans/src/main/java/org/springframework/beans/CachedIntrospectionResults.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@
1919
import java.beans.BeanInfo;
2020
import java.beans.IntrospectionException;
2121
import java.beans.PropertyDescriptor;
22-
import java.beans.SimpleBeanInfo;
2322
import java.lang.reflect.Method;
2423
import java.lang.reflect.Modifier;
2524
import java.net.URL;
2625
import java.security.ProtectionDomain;
27-
import java.util.Collection;
2826
import java.util.Collections;
2927
import java.util.HashSet;
3028
import java.util.LinkedHashMap;
@@ -79,12 +77,11 @@
7977
*/
8078
public final class CachedIntrospectionResults {
8179

82-
private static final PropertyDescriptor[] EMPTY_PROPERTY_DESCRIPTOR_ARRAY = {};
83-
84-
8580
private static final List<BeanInfoFactory> beanInfoFactories = SpringFactoriesLoader.loadFactories(
8681
BeanInfoFactory.class, CachedIntrospectionResults.class.getClassLoader());
8782

83+
private static final SimpleBeanInfoFactory simpleBeanInfoFactory = new SimpleBeanInfoFactory();
84+
8885
private static final Log logger = LogFactory.getLog(CachedIntrospectionResults.class);
8986

9087
/**
@@ -228,14 +225,7 @@ private static BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionExce
228225
return beanInfo;
229226
}
230227
}
231-
232-
Collection<PropertyDescriptor> pds = PropertyDescriptorUtils.determineBasicProperties(beanClass);
233-
return new SimpleBeanInfo() {
234-
@Override
235-
public PropertyDescriptor[] getPropertyDescriptors() {
236-
return pds.toArray(EMPTY_PROPERTY_DESCRIPTOR_ARRAY);
237-
}
238-
};
228+
return simpleBeanInfoFactory.getBeanInfo(beanClass);
239229
}
240230

241231

@@ -405,7 +395,7 @@ PropertyDescriptor getPropertyDescriptor(String name) {
405395
}
406396

407397
PropertyDescriptor[] getPropertyDescriptors() {
408-
return this.propertyDescriptors.values().toArray(EMPTY_PROPERTY_DESCRIPTOR_ARRAY);
398+
return this.propertyDescriptors.values().toArray(PropertyDescriptorUtils.EMPTY_PROPERTY_DESCRIPTOR_ARRAY);
409399
}
410400

411401
private PropertyDescriptor buildGenericTypeAwarePropertyDescriptor(Class<?> beanClass, PropertyDescriptor pd) {

spring-beans/src/main/java/org/springframework/beans/PropertyDescriptorUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
*/
3737
abstract class PropertyDescriptorUtils {
3838

39+
public static final PropertyDescriptor[] EMPTY_PROPERTY_DESCRIPTOR_ARRAY = {};
40+
41+
3942
/**
4043
* Simple introspection algorithm for basic set/get/is accessor methods,
4144
* building corresponding JavaBeans property descriptors for them.
@@ -46,6 +49,7 @@ abstract class PropertyDescriptorUtils {
4649
* @return a collection of property descriptors
4750
* @throws IntrospectionException from introspecting the given bean class
4851
* @since 6.0
52+
* @see SimpleBeanInfoFactory
4953
* @see java.beans.Introspector#getBeanInfo(Class)
5054
*/
5155
public static Collection<PropertyDescriptor> determineBasicProperties(Class<?> beanClass) throws IntrospectionException {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.beans;
18+
19+
import java.beans.BeanInfo;
20+
import java.beans.IntrospectionException;
21+
import java.beans.PropertyDescriptor;
22+
import java.beans.SimpleBeanInfo;
23+
import java.util.Collection;
24+
25+
import org.springframework.core.Ordered;
26+
import org.springframework.lang.NonNull;
27+
28+
/**
29+
* {@link BeanInfoFactory} implementation that bypasses the standard {@link java.beans.Introspector}
30+
* for faster introspection, reduced to basic property determination (as commonly needed in Spring).
31+
*
32+
* <p>Used by default in 6.0 through direct invocation from {@link CachedIntrospectionResults}.
33+
* Potentially configured via a {@code META-INF/spring.factories} file with the following content,
34+
* overriding other custom {@code org.springframework.beans.BeanInfoFactory} declarations:
35+
* {@code org.springframework.beans.BeanInfoFactory=org.springframework.beans.SimpleBeanInfoFactory}
36+
*
37+
* <p>Ordered at {@code Ordered.LOWEST_PRECEDENCE - 1} to override {@link ExtendedBeanInfoFactory}
38+
* (registered by default in 5.3) if necessary while still allowing other user-defined
39+
* {@link BeanInfoFactory} types to take precedence.
40+
*
41+
* @author Juergen Hoeller
42+
* @since 6.0
43+
* @see ExtendedBeanInfoFactory
44+
* @see CachedIntrospectionResults
45+
*/
46+
class SimpleBeanInfoFactory implements BeanInfoFactory, Ordered {
47+
48+
@Override
49+
@NonNull
50+
public BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException {
51+
Collection<PropertyDescriptor> pds = PropertyDescriptorUtils.determineBasicProperties(beanClass);
52+
return new SimpleBeanInfo() {
53+
@Override
54+
public PropertyDescriptor[] getPropertyDescriptors() {
55+
return pds.toArray(PropertyDescriptorUtils.EMPTY_PROPERTY_DESCRIPTOR_ARRAY);
56+
}
57+
};
58+
}
59+
60+
@Override
61+
public int getOrder() {
62+
return Ordered.LOWEST_PRECEDENCE - 1;
63+
}
64+
65+
}

0 commit comments

Comments
 (0)