From 2ae224984264e486a4b081d0e2b0593f71be927b Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 24 Jul 2018 15:17:33 +0200 Subject: [PATCH] Polishing --- .../AbstractAutowireCapableBeanFactory.java | 6 ++-- ...njectAnnotationBeanPostProcessorTests.java | 26 ++++++++--------- .../interceptor/AbstractCacheResolver.java | 29 ++++++++++++------- .../OkHttp3ClientHttpRequestFactory.java | 8 +++-- 4 files changed, 37 insertions(+), 32 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java index cc58344041..aeb468078a 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java @@ -31,9 +31,7 @@ import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; -import java.util.LinkedList; import java.util.List; -import java.util.Map; import java.util.Set; import java.util.TreeSet; import java.util.concurrent.ConcurrentHashMap; @@ -146,7 +144,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac private final Set> ignoredDependencyInterfaces = new HashSet>(); /** Cache of unfinished FactoryBean instances: FactoryBean name --> BeanWrapper */ - private final Map factoryBeanInstanceCache = + private final ConcurrentMap factoryBeanInstanceCache = new ConcurrentHashMap(16); /** Cache of filtered PropertyDescriptors: bean Class -> PropertyDescriptor array */ @@ -1422,7 +1420,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac */ protected PropertyDescriptor[] filterPropertyDescriptorsForDependencyCheck(BeanWrapper bw) { List pds = - new LinkedList(Arrays.asList(bw.getPropertyDescriptors())); + new ArrayList(Arrays.asList(bw.getPropertyDescriptors())); for (Iterator it = pds.iterator(); it.hasNext();) { PropertyDescriptor pd = it.next(); if (isExcludedFromDependencyCheck(pd)) { diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java index 63390cb8ca..347a9b57c5 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 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. @@ -45,7 +45,7 @@ import static org.junit.Assert.*; /** * Unit tests for {@link org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor} - * processing the JSR-303 {@link javax.inject.Inject} annotation. + * processing the JSR-330 {@link javax.inject.Inject} annotation. * * @author Juergen Hoeller * @since 3.0 @@ -548,11 +548,9 @@ public class InjectAnnotationBeanPostProcessorTests { } /** - * Verifies that a dependency on a {@link org.springframework.beans.factory.FactoryBean} can be autowired via - * {@link org.springframework.beans.factory.annotation.Autowired @Inject}, specifically addressing the JIRA issue - * raised in SPR-4040. + * Verifies that a dependency on a {@link org.springframework.beans.factory.FactoryBean} + * can be autowired via {@link org.springframework.beans.factory.annotation.Autowired @Inject}, + * specifically addressing SPR-4040. */ @Test public void testBeanAutowiredWithFactoryBean() { @@ -1259,7 +1257,7 @@ public class InjectAnnotationBeanPostProcessorTests { public static class StringFactoryBean implements FactoryBean { @Override - public String getObject() throws Exception { + public String getObject() { return ""; } @@ -1291,8 +1289,8 @@ public class InjectAnnotationBeanPostProcessorTests { private Optional testBean; @Inject - public void setTestBean(Optional testBeanFactory) { - this.testBean = testBeanFactory; + public void setTestBean(Optional testBean) { + this.testBean = testBean; } public Optional getTestBean() { @@ -1317,8 +1315,8 @@ public class InjectAnnotationBeanPostProcessorTests { private Optional> testBean; @Inject - public void setTestBean(Optional> testBeanFactory) { - this.testBean = testBeanFactory; + public void setTestBean(Optional> testBean) { + this.testBean = testBean; } public Optional> getTestBean() { @@ -1343,8 +1341,8 @@ public class InjectAnnotationBeanPostProcessorTests { private Provider> testBean; @Inject - public void setTestBean(Provider> testBeanFactory) { - this.testBean = testBeanFactory; + public void setTestBean(Provider> testBean) { + this.testBean = testBean; } public Optional getTestBean() { diff --git a/spring-context/src/main/java/org/springframework/cache/interceptor/AbstractCacheResolver.java b/spring-context/src/main/java/org/springframework/cache/interceptor/AbstractCacheResolver.java index 9ee27e000e..5f8cc15913 100644 --- a/spring-context/src/main/java/org/springframework/cache/interceptor/AbstractCacheResolver.java +++ b/spring-context/src/main/java/org/springframework/cache/interceptor/AbstractCacheResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -31,6 +31,7 @@ import org.springframework.util.Assert; * invocation context. * * @author Stephane Nicoll + * @author Juergen Hoeller * @since 4.1 */ public abstract class AbstractCacheResolver implements CacheResolver, InitializingBean { @@ -38,9 +39,17 @@ public abstract class AbstractCacheResolver implements CacheResolver, Initializi private CacheManager cacheManager; + /** + * Construct a new {@code AbstractCacheResolver}. + * @see #setCacheManager + */ protected AbstractCacheResolver() { } + /** + * Construct a new {@code AbstractCacheResolver} for the given {@link CacheManager}. + * @param cacheManager the CacheManager to use + */ protected AbstractCacheResolver(CacheManager cacheManager) { this.cacheManager = cacheManager; } @@ -72,18 +81,16 @@ public abstract class AbstractCacheResolver implements CacheResolver, Initializi if (cacheNames == null) { return Collections.emptyList(); } - else { - Collection result = new ArrayList(); - for (String cacheName : cacheNames) { - Cache cache = getCacheManager().getCache(cacheName); - if (cache == null) { - throw new IllegalArgumentException("Cannot find cache named '" + - cacheName + "' for " + context.getOperation()); - } - result.add(cache); + Collection result = new ArrayList(cacheNames.size()); + for (String cacheName : cacheNames) { + Cache cache = getCacheManager().getCache(cacheName); + if (cache == null) { + throw new IllegalArgumentException("Cannot find cache named '" + + cacheName + "' for " + context.getOperation()); } - return result; + result.add(cache); } + return result; } /** diff --git a/spring-web/src/main/java/org/springframework/http/client/OkHttp3ClientHttpRequestFactory.java b/spring-web/src/main/java/org/springframework/http/client/OkHttp3ClientHttpRequestFactory.java index b45d36e329..5e4f4f7f3f 100644 --- a/spring-web/src/main/java/org/springframework/http/client/OkHttp3ClientHttpRequestFactory.java +++ b/spring-web/src/main/java/org/springframework/http/client/OkHttp3ClientHttpRequestFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 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. @@ -23,6 +23,7 @@ import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; +import okhttp3.Cache; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; @@ -118,8 +119,9 @@ public class OkHttp3ClientHttpRequestFactory public void destroy() throws IOException { if (this.defaultClient) { // Clean up the client if we created it in the constructor - if (this.client.cache() != null) { - this.client.cache().close(); + Cache cache = this.client.cache(); + if (cache != null) { + cache.close(); } this.client.dispatcher().executorService().shutdown(); }