Skip to content

Commit bb9e79d

Browse files
committed
Polishing
1 parent 6d9d415 commit bb9e79d

File tree

6 files changed

+32
-50
lines changed

6 files changed

+32
-50
lines changed

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws
227227
* @since 5.0
228228
* @see <a href="https://kotlinlang.org/docs/reference/classes.html#constructors">Kotlin docs</a>
229229
*/
230-
@SuppressWarnings("unchecked")
231230
@Nullable
232231
public static <T> Constructor<T> findPrimaryConstructor(Class<T> clazz) {
233232
Assert.notNull(clazz, "Class must not be null");
@@ -442,8 +441,7 @@ else if (startParen == -1) {
442441
* @throws BeansException if PropertyDescriptor look fails
443442
*/
444443
public static PropertyDescriptor[] getPropertyDescriptors(Class<?> clazz) throws BeansException {
445-
CachedIntrospectionResults cr = CachedIntrospectionResults.forClass(clazz);
446-
return cr.getPropertyDescriptors();
444+
return CachedIntrospectionResults.forClass(clazz).getPropertyDescriptors();
447445
}
448446

449447
/**
@@ -454,11 +452,8 @@ public static PropertyDescriptor[] getPropertyDescriptors(Class<?> clazz) throws
454452
* @throws BeansException if PropertyDescriptor lookup fails
455453
*/
456454
@Nullable
457-
public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String propertyName)
458-
throws BeansException {
459-
460-
CachedIntrospectionResults cr = CachedIntrospectionResults.forClass(clazz);
461-
return cr.getPropertyDescriptor(propertyName);
455+
public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String propertyName) throws BeansException {
456+
return CachedIntrospectionResults.forClass(clazz).getPropertyDescriptor(propertyName);
462457
}
463458

464459
/**

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ public final class CachedIntrospectionResults {
9292
*/
9393
public static final String IGNORE_BEANINFO_PROPERTY_NAME = "spring.beaninfo.ignore";
9494

95+
private static final PropertyDescriptor[] EMPTY_PROPERTY_DESCRIPTOR_ARRAY = {};
96+
9597

9698
private static final boolean shouldIntrospectorIgnoreBeaninfoClasses =
9799
SpringProperties.getFlag(IGNORE_BEANINFO_PROPERTY_NAME);
@@ -253,7 +255,7 @@ private static BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionExce
253255
private final BeanInfo beanInfo;
254256

255257
/** PropertyDescriptor objects keyed by property name String. */
256-
private final Map<String, PropertyDescriptor> propertyDescriptorCache;
258+
private final Map<String, PropertyDescriptor> propertyDescriptors;
257259

258260
/** TypeDescriptor objects keyed by PropertyDescriptor. */
259261
private final ConcurrentMap<PropertyDescriptor, TypeDescriptor> typeDescriptorCache;
@@ -274,7 +276,7 @@ private CachedIntrospectionResults(Class<?> beanClass) throws BeansException {
274276
if (logger.isTraceEnabled()) {
275277
logger.trace("Caching PropertyDescriptors for class [" + beanClass.getName() + "]");
276278
}
277-
this.propertyDescriptorCache = new LinkedHashMap<>();
279+
this.propertyDescriptors = new LinkedHashMap<>();
278280

279281
// This call is slow so we do it once.
280282
PropertyDescriptor[] pds = this.beanInfo.getPropertyDescriptors();
@@ -291,7 +293,7 @@ private CachedIntrospectionResults(Class<?> beanClass) throws BeansException {
291293
"; editor [" + pd.getPropertyEditorClass().getName() + "]" : ""));
292294
}
293295
pd = buildGenericTypeAwarePropertyDescriptor(beanClass, pd);
294-
this.propertyDescriptorCache.put(pd.getName(), pd);
296+
this.propertyDescriptors.put(pd.getName(), pd);
295297
}
296298

297299
// Explicitly check implemented interfaces for setter/getter methods as well,
@@ -313,13 +315,13 @@ private void introspectInterfaces(Class<?> beanClass, Class<?> currClass) throws
313315
for (Class<?> ifc : currClass.getInterfaces()) {
314316
if (!ClassUtils.isJavaLanguageInterface(ifc)) {
315317
for (PropertyDescriptor pd : getBeanInfo(ifc).getPropertyDescriptors()) {
316-
PropertyDescriptor existingPd = this.propertyDescriptorCache.get(pd.getName());
318+
PropertyDescriptor existingPd = this.propertyDescriptors.get(pd.getName());
317319
if (existingPd == null ||
318320
(existingPd.getReadMethod() == null && pd.getReadMethod() != null)) {
319321
// GenericTypeAwarePropertyDescriptor leniently resolves a set* write method
320322
// against a declared read method, so we prefer read method descriptors here.
321323
pd = buildGenericTypeAwarePropertyDescriptor(beanClass, pd);
322-
this.propertyDescriptorCache.put(pd.getName(), pd);
324+
this.propertyDescriptors.put(pd.getName(), pd);
323325
}
324326
}
325327
introspectInterfaces(ifc, ifc);
@@ -338,27 +340,19 @@ Class<?> getBeanClass() {
338340

339341
@Nullable
340342
PropertyDescriptor getPropertyDescriptor(String name) {
341-
PropertyDescriptor pd = this.propertyDescriptorCache.get(name);
343+
PropertyDescriptor pd = this.propertyDescriptors.get(name);
342344
if (pd == null && StringUtils.hasLength(name)) {
343345
// Same lenient fallback checking as in Property...
344-
pd = this.propertyDescriptorCache.get(StringUtils.uncapitalize(name));
346+
pd = this.propertyDescriptors.get(StringUtils.uncapitalize(name));
345347
if (pd == null) {
346-
pd = this.propertyDescriptorCache.get(StringUtils.capitalize(name));
348+
pd = this.propertyDescriptors.get(StringUtils.capitalize(name));
347349
}
348350
}
349-
return (pd == null || pd instanceof GenericTypeAwarePropertyDescriptor ? pd :
350-
buildGenericTypeAwarePropertyDescriptor(getBeanClass(), pd));
351+
return pd;
351352
}
352353

353354
PropertyDescriptor[] getPropertyDescriptors() {
354-
PropertyDescriptor[] pds = new PropertyDescriptor[this.propertyDescriptorCache.size()];
355-
int i = 0;
356-
for (PropertyDescriptor pd : this.propertyDescriptorCache.values()) {
357-
pds[i] = (pd instanceof GenericTypeAwarePropertyDescriptor ? pd :
358-
buildGenericTypeAwarePropertyDescriptor(getBeanClass(), pd));
359-
i++;
360-
}
361-
return pds;
355+
return this.propertyDescriptors.values().toArray(EMPTY_PROPERTY_DESCRIPTOR_ARRAY);
362356
}
363357

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

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -60,12 +60,13 @@ final class GenericTypeAwarePropertyDescriptor extends PropertyDescriptor {
6060
@Nullable
6161
private Class<?> propertyType;
6262

63+
@Nullable
6364
private final Class<?> propertyEditorClass;
6465

6566

6667
public GenericTypeAwarePropertyDescriptor(Class<?> beanClass, String propertyName,
67-
@Nullable Method readMethod, @Nullable Method writeMethod, Class<?> propertyEditorClass)
68-
throws IntrospectionException {
68+
@Nullable Method readMethod, @Nullable Method writeMethod,
69+
@Nullable Class<?> propertyEditorClass) throws IntrospectionException {
6970

7071
super(propertyName, null, null);
7172
this.beanClass = beanClass;
@@ -156,6 +157,7 @@ public Class<?> getPropertyType() {
156157
}
157158

158159
@Override
160+
@Nullable
159161
public Class<?> getPropertyEditorClass() {
160162
return this.propertyEditorClass;
161163
}

spring-jdbc/src/main/java/org/springframework/jdbc/core/BeanPropertyRowMapper.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -114,8 +114,6 @@ public BeanPropertyRowMapper() {
114114
/**
115115
* Create a new {@code BeanPropertyRowMapper}, accepting unpopulated
116116
* properties in the target bean.
117-
* <p>Consider using the {@link #newInstance} factory method instead,
118-
* which allows for specifying the mapped type once only.
119117
* @param mappedClass the class that each row should be mapped to
120118
*/
121119
public BeanPropertyRowMapper(Class<T> mappedClass) {
@@ -222,8 +220,8 @@ protected void initialize(Class<T> mappedClass) {
222220
this.mappedClass = mappedClass;
223221
this.mappedFields = new HashMap<>();
224222
this.mappedProperties = new HashSet<>();
225-
PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass);
226-
for (PropertyDescriptor pd : pds) {
223+
224+
for (PropertyDescriptor pd : BeanUtils.getPropertyDescriptors(mappedClass)) {
227225
if (pd.getWriteMethod() != null) {
228226
this.mappedFields.put(lowerCaseName(pd.getName()), pd);
229227
String underscoredName = underscoreName(pd.getName());
@@ -247,6 +245,7 @@ protected String underscoreName(String name) {
247245
if (!StringUtils.hasLength(name)) {
248246
return "";
249247
}
248+
250249
StringBuilder result = new StringBuilder();
251250
result.append(lowerCaseName(name.substring(0, 1)));
252251
for (int i = 1; i < name.length(); i++) {
@@ -337,8 +336,7 @@ public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
337336

338337
if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) {
339338
throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields " +
340-
"necessary to populate object of class [" + this.mappedClass.getName() + "]: " +
341-
this.mappedProperties);
339+
"necessary to populate object of " + this.mappedClass + ": " + this.mappedProperties);
342340
}
343341

344342
return mappedObject;
@@ -380,8 +378,7 @@ protected Object getColumnValue(ResultSet rs, int index, PropertyDescriptor pd)
380378

381379

382380
/**
383-
* Static factory method to create a new {@code BeanPropertyRowMapper}
384-
* (with the mapped class specified only once).
381+
* Static factory method to create a new {@code BeanPropertyRowMapper}.
385382
* @param mappedClass the class that each row should be mapped to
386383
* @see #newInstance(Class, ConversionService)
387384
*/
@@ -390,8 +387,7 @@ public static <T> BeanPropertyRowMapper<T> newInstance(Class<T> mappedClass) {
390387
}
391388

392389
/**
393-
* Static factory method to create a new {@code BeanPropertyRowMapper}
394-
* (with the required type specified only once).
390+
* Static factory method to create a new {@code BeanPropertyRowMapper}.
395391
* @param mappedClass the class that each row should be mapped to
396392
* @param conversionService the {@link ConversionService} for binding
397393
* JDBC values to bean properties, or {@code null} for none

spring-jdbc/src/main/java/org/springframework/jdbc/core/SingleColumnRowMapper.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -62,8 +62,6 @@ public SingleColumnRowMapper() {
6262

6363
/**
6464
* Create a new {@code SingleColumnRowMapper}.
65-
* <p>Consider using the {@link #newInstance} factory method instead,
66-
* which allows for specifying the required type once only.
6765
* @param requiredType the type that each result object is expected to match
6866
*/
6967
public SingleColumnRowMapper(Class<T> requiredType) {
@@ -216,8 +214,7 @@ else if (this.conversionService != null && this.conversionService.canConvert(val
216214

217215

218216
/**
219-
* Static factory method to create a new {@code SingleColumnRowMapper}
220-
* (with the required type specified only once).
217+
* Static factory method to create a new {@code SingleColumnRowMapper}.
221218
* @param requiredType the type that each result object is expected to match
222219
* @since 4.1
223220
* @see #newInstance(Class, ConversionService)
@@ -227,8 +224,7 @@ public static <T> SingleColumnRowMapper<T> newInstance(Class<T> requiredType) {
227224
}
228225

229226
/**
230-
* Static factory method to create a new {@code SingleColumnRowMapper}
231-
* (with the required type specified only once).
227+
* Static factory method to create a new {@code SingleColumnRowMapper}.
232228
* @param requiredType the type that each result object is expected to match
233229
* @param conversionService the {@link ConversionService} for converting a
234230
* fetched value, or {@code null} for none

spring-jdbc/src/test/java/org/springframework/jdbc/core/BeanPropertyRowMapperTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,9 +37,8 @@
3737
*/
3838
public class BeanPropertyRowMapperTests extends AbstractRowMapperTests {
3939

40-
4140
@Test
42-
@SuppressWarnings({ "unchecked", "rawtypes" })
41+
@SuppressWarnings({"unchecked", "rawtypes"})
4342
public void testOverridingDifferentClassDefinedForMapping() {
4443
BeanPropertyRowMapper mapper = new BeanPropertyRowMapper(Person.class);
4544
assertThatExceptionOfType(InvalidDataAccessApiUsageException.class).isThrownBy(() ->

0 commit comments

Comments
 (0)