From 94f3c96bc14a8a31af529cd3b91c385350c9123a Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 28 Aug 2020 22:47:28 +0200 Subject: [PATCH] Polishing --- .../org/springframework/beans/BeanUtils.java | 13 ++++--------- .../GenericTypeAwarePropertyDescriptor.java | 8 +++++--- .../jdbc/core/BeanPropertyRowMapper.java | 16 +++++++--------- .../jdbc/core/SingleColumnRowMapper.java | 11 ++++------- .../jdbc/core/BeanPropertyRowMapperTests.java | 4 ++-- 5 files changed, 22 insertions(+), 30 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java b/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java index 925aafbef4f..434827701e4 100644 --- a/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java +++ b/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -192,7 +192,6 @@ public abstract class BeanUtils { * @since 5.0 * @see Kotlin docs */ - @SuppressWarnings("unchecked") @Nullable public static Constructor findPrimaryConstructor(Class clazz) { Assert.notNull(clazz, "Class must not be null"); @@ -407,8 +406,7 @@ public abstract class BeanUtils { * @throws BeansException if PropertyDescriptor look fails */ public static PropertyDescriptor[] getPropertyDescriptors(Class clazz) throws BeansException { - CachedIntrospectionResults cr = CachedIntrospectionResults.forClass(clazz); - return cr.getPropertyDescriptors(); + return CachedIntrospectionResults.forClass(clazz).getPropertyDescriptors(); } /** @@ -419,11 +417,8 @@ public abstract class BeanUtils { * @throws BeansException if PropertyDescriptor lookup fails */ @Nullable - public static PropertyDescriptor getPropertyDescriptor(Class clazz, String propertyName) - throws BeansException { - - CachedIntrospectionResults cr = CachedIntrospectionResults.forClass(clazz); - return cr.getPropertyDescriptor(propertyName); + public static PropertyDescriptor getPropertyDescriptor(Class clazz, String propertyName) throws BeansException { + return CachedIntrospectionResults.forClass(clazz).getPropertyDescriptor(propertyName); } /** diff --git a/spring-beans/src/main/java/org/springframework/beans/GenericTypeAwarePropertyDescriptor.java b/spring-beans/src/main/java/org/springframework/beans/GenericTypeAwarePropertyDescriptor.java index c58cd665613..399fb1e3335 100644 --- a/spring-beans/src/main/java/org/springframework/beans/GenericTypeAwarePropertyDescriptor.java +++ b/spring-beans/src/main/java/org/springframework/beans/GenericTypeAwarePropertyDescriptor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -60,12 +60,13 @@ final class GenericTypeAwarePropertyDescriptor extends PropertyDescriptor { @Nullable private Class propertyType; + @Nullable private final Class propertyEditorClass; public GenericTypeAwarePropertyDescriptor(Class beanClass, String propertyName, - @Nullable Method readMethod, @Nullable Method writeMethod, Class propertyEditorClass) - throws IntrospectionException { + @Nullable Method readMethod, @Nullable Method writeMethod, + @Nullable Class propertyEditorClass) throws IntrospectionException { super(propertyName, null, null); this.beanClass = beanClass; @@ -157,6 +158,7 @@ final class GenericTypeAwarePropertyDescriptor extends PropertyDescriptor { } @Override + @Nullable public Class getPropertyEditorClass() { return this.propertyEditorClass; } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/BeanPropertyRowMapper.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/BeanPropertyRowMapper.java index 24e9c92a8ad..fb7d4c9775e 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/BeanPropertyRowMapper.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/BeanPropertyRowMapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -73,6 +73,7 @@ import org.springframework.util.StringUtils; * @author Thomas Risberg * @author Juergen Hoeller * @since 2.5 + * @param the result type */ public class BeanPropertyRowMapper implements RowMapper { @@ -113,8 +114,6 @@ public class BeanPropertyRowMapper implements RowMapper { /** * Create a new {@code BeanPropertyRowMapper}, accepting unpopulated * properties in the target bean. - *

Consider using the {@link #newInstance} factory method instead, - * which allows for specifying the mapped type once only. * @param mappedClass the class that each row should be mapped to */ public BeanPropertyRowMapper(Class mappedClass) { @@ -221,8 +220,8 @@ public class BeanPropertyRowMapper implements RowMapper { this.mappedClass = mappedClass; this.mappedFields = new HashMap<>(); this.mappedProperties = new HashSet<>(); - PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass); - for (PropertyDescriptor pd : pds) { + + for (PropertyDescriptor pd : BeanUtils.getPropertyDescriptors(mappedClass)) { if (pd.getWriteMethod() != null) { this.mappedFields.put(lowerCaseName(pd.getName()), pd); String underscoredName = underscoreName(pd.getName()); @@ -246,6 +245,7 @@ public class BeanPropertyRowMapper implements RowMapper { if (!StringUtils.hasLength(name)) { return ""; } + StringBuilder result = new StringBuilder(); result.append(lowerCaseName(name.substring(0, 1))); for (int i = 1; i < name.length(); i++) { @@ -336,8 +336,7 @@ public class BeanPropertyRowMapper implements RowMapper { if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) { throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields " + - "necessary to populate object of class [" + this.mappedClass.getName() + "]: " + - this.mappedProperties); + "necessary to populate object of " + this.mappedClass + ": " + this.mappedProperties); } return mappedObject; @@ -379,8 +378,7 @@ public class BeanPropertyRowMapper implements RowMapper { /** - * Static factory method to create a new {@code BeanPropertyRowMapper} - * (with the mapped class specified only once). + * Static factory method to create a new {@code BeanPropertyRowMapper}. * @param mappedClass the class that each row should be mapped to */ public static BeanPropertyRowMapper newInstance(Class mappedClass) { diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/SingleColumnRowMapper.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/SingleColumnRowMapper.java index cd26f3da2e2..3b762906d80 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/SingleColumnRowMapper.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/SingleColumnRowMapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,6 +41,7 @@ import org.springframework.util.NumberUtils; * @author Juergen Hoeller * @author Kazuki Shimizu * @since 1.2 + * @param the result type * @see JdbcTemplate#queryForList(String, Class) * @see JdbcTemplate#queryForObject(String, Class) */ @@ -61,8 +62,6 @@ public class SingleColumnRowMapper implements RowMapper { /** * Create a new {@code SingleColumnRowMapper}. - *

Consider using the {@link #newInstance} factory method instead, - * which allows for specifying the required type once only. * @param requiredType the type that each result object is expected to match */ public SingleColumnRowMapper(Class requiredType) { @@ -215,8 +214,7 @@ public class SingleColumnRowMapper implements RowMapper { /** - * Static factory method to create a new {@code SingleColumnRowMapper} - * (with the required type specified only once). + * Static factory method to create a new {@code SingleColumnRowMapper}. * @param requiredType the type that each result object is expected to match * @since 4.1 * @see #newInstance(Class, ConversionService) @@ -226,8 +224,7 @@ public class SingleColumnRowMapper implements RowMapper { } /** - * Static factory method to create a new {@code SingleColumnRowMapper} - * (with the required type specified only once). + * Static factory method to create a new {@code SingleColumnRowMapper}. * @param requiredType the type that each result object is expected to match * @param conversionService the {@link ConversionService} for converting a * fetched value, or {@code null} for none diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/core/BeanPropertyRowMapperTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/core/BeanPropertyRowMapperTests.java index 1d8df5fe7fc..8763d3bd0a0 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/core/BeanPropertyRowMapperTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/core/BeanPropertyRowMapperTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,7 +43,7 @@ public class BeanPropertyRowMapperTests extends AbstractRowMapperTests { @Test - @SuppressWarnings({ "unchecked", "rawtypes" }) + @SuppressWarnings({"unchecked", "rawtypes"}) public void testOverridingDifferentClassDefinedForMapping() { BeanPropertyRowMapper mapper = new BeanPropertyRowMapper(Person.class); thrown.expect(InvalidDataAccessApiUsageException.class);