From d3cee45f30c95d1c17acc101434f586400ca9bae Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 13 Feb 2018 13:15:29 +0100 Subject: [PATCH] Polishing --- .../interceptor/CacheResolverAdapter.java | 31 +++++++++++++++---- .../CacheResolverAdapterTests.java | 4 +-- .../cache/interceptor/CacheResolver.java | 3 +- .../HeadersMethodArgumentResolver.java | 13 ++++---- ...andlerMethodArgumentResolverComposite.java | 6 ++-- .../oxm/jaxb/Jaxb2Marshaller.java | 22 ++++++------- 6 files changed, 47 insertions(+), 32 deletions(-) diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResolverAdapter.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResolverAdapter.java index 5081f448d04..f1efb621efa 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResolverAdapter.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResolverAdapter.java @@ -1,5 +1,20 @@ -package org.springframework.cache.jcache.interceptor; +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cache.jcache.interceptor; import java.util.Collection; import java.util.Collections; @@ -23,17 +38,19 @@ class CacheResolverAdapter implements CacheResolver { private final javax.cache.annotation.CacheResolver target; + /** * Create a new instance with the JSR-107 cache resolver to invoke. */ public CacheResolverAdapter(javax.cache.annotation.CacheResolver target) { - Assert.notNull(target, "JSR-107 cache resolver must be set."); + Assert.notNull(target, "JSR-107 CacheResolver is required"); this.target = target; } + /** - * Return the underlying {@link javax.cache.annotation.CacheResolver} that this - * instance is using. + * Return the underlying {@link javax.cache.annotation.CacheResolver} + * that this instance is using. */ protected javax.cache.annotation.CacheResolver getTarget() { return target; @@ -45,8 +62,10 @@ class CacheResolverAdapter implements CacheResolver { throw new IllegalStateException("Unexpected context " + context); } CacheInvocationContext cacheInvocationContext = (CacheInvocationContext) context; - javax.cache.Cache cache = target.resolveCache(cacheInvocationContext); - Assert.notNull(cache, () -> "Cannot resolve cache for '" + context + "' using '" + target + "'"); + javax.cache.Cache cache = this.target.resolveCache(cacheInvocationContext); + if (cache == null) { + throw new IllegalStateException("Could not resolve cache for " + context + " using " + this.target); + } return Collections.singleton(new JCacheCache(cache)); } diff --git a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheResolverAdapterTests.java b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheResolverAdapterTests.java index 56fc64be855..f97225b4377 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheResolverAdapterTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheResolverAdapterTests.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. @@ -58,7 +58,7 @@ public class CacheResolverAdapterTests extends AbstractJCacheTests { DefaultCacheInvocationContext dummyContext = createDummyContext(); CacheResolverAdapter adapter = new CacheResolverAdapter(getCacheResolver(dummyContext, null)); - thrown.expect(IllegalArgumentException.class); + thrown.expect(IllegalStateException.class); adapter.resolveCaches(dummyContext); } diff --git a/spring-context/src/main/java/org/springframework/cache/interceptor/CacheResolver.java b/spring-context/src/main/java/org/springframework/cache/interceptor/CacheResolver.java index 8b5b4be8339..dbdeac74adb 100644 --- a/spring-context/src/main/java/org/springframework/cache/interceptor/CacheResolver.java +++ b/spring-context/src/main/java/org/springframework/cache/interceptor/CacheResolver.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. @@ -35,6 +35,7 @@ public interface CacheResolver { * Return the cache(s) to use for the specified invocation. * @param context the context of the particular invocation * @return the cache(s) to use (never {@code null}) + * @throws IllegalStateException if cache resolution failed */ Collection resolveCaches(CacheOperationInvocationContext context); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/HeadersMethodArgumentResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/HeadersMethodArgumentResolver.java index 9b7e5febe53..ba4132dcc38 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/HeadersMethodArgumentResolver.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/HeadersMethodArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 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. @@ -26,7 +26,6 @@ import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.handler.annotation.Headers; import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver; import org.springframework.messaging.support.MessageHeaderAccessor; -import org.springframework.util.Assert; import org.springframework.util.ReflectionUtils; /** @@ -46,16 +45,13 @@ public class HeadersMethodArgumentResolver implements HandlerMethodArgumentResol public boolean supportsParameter(MethodParameter parameter) { Class paramType = parameter.getParameterType(); return ((parameter.hasParameterAnnotation(Headers.class) && Map.class.isAssignableFrom(paramType)) || - MessageHeaders.class == paramType || - MessageHeaderAccessor.class.isAssignableFrom(paramType)); + MessageHeaders.class == paramType || MessageHeaderAccessor.class.isAssignableFrom(paramType)); } @Override @Nullable public Object resolveArgument(MethodParameter parameter, Message message) throws Exception { - Class paramType = parameter.getParameterType(); - if (Map.class.isAssignableFrom(paramType)) { return message.getHeaders(); } @@ -70,7 +66,10 @@ public class HeadersMethodArgumentResolver implements HandlerMethodArgumentResol } else { Method method = ReflectionUtils.findMethod(paramType, "wrap", Message.class); - Assert.notNull(method, () -> "Cannot create accessor of type " + paramType + " for message " + message); + if (method == null) { + throw new IllegalStateException( + "Cannot create accessor of type " + paramType + " for message " + message); + } return ReflectionUtils.invokeMethod(method, null, message); } } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/HandlerMethodArgumentResolverComposite.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/HandlerMethodArgumentResolverComposite.java index d3566d0ccfe..f4f7622a06d 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/HandlerMethodArgumentResolverComposite.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/HandlerMethodArgumentResolverComposite.java @@ -25,7 +25,6 @@ import java.util.concurrent.ConcurrentHashMap; import org.springframework.core.MethodParameter; import org.springframework.lang.Nullable; import org.springframework.messaging.Message; -import org.springframework.util.Assert; /** * Resolves method parameters by delegating to a list of registered @@ -110,9 +109,10 @@ public class HandlerMethodArgumentResolverComposite implements HandlerMethodArgu @Override @Nullable public Object resolveArgument(MethodParameter parameter, Message message) throws Exception { - HandlerMethodArgumentResolver resolver = getArgumentResolver(parameter); - Assert.notNull(resolver, () -> "Unknown parameter type [" + parameter.getParameterType().getName() + "]"); + if (resolver == null) { + throw new IllegalStateException("Unknown parameter type [" + parameter.getParameterType().getName() + "]"); + } return resolver.resolveArgument(parameter, message); } diff --git a/spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java b/spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java index 3c565e4016a..30475ec6032 100644 --- a/spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java +++ b/spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.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. @@ -579,10 +579,10 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); for (int i = 0; i < resources.length; i++) { - Resource currentResource = resources[i]; - Assert.notNull(currentResource, "Resource is null"); - Assert.isTrue(currentResource.exists(), () -> "Resource " + currentResource + " does not exist"); - InputSource inputSource = SaxResourceUtils.createInputSource(currentResource); + Resource resource = resources[i]; + Assert.notNull(resource, "Resource is null"); + Assert.isTrue(resource.exists(), () -> "Resource " + resource + " does not exist"); + InputSource inputSource = SaxResourceUtils.createInputSource(resource); schemaSources[i] = new SAXSource(xmlReader, inputSource); } SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage); @@ -1060,12 +1060,12 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi } @Override - public InputStream getInputStream() throws IOException { + public InputStream getInputStream() { return new ByteArrayInputStream(this.data, this.offset, this.length); } @Override - public OutputStream getOutputStream() throws IOException { + public OutputStream getOutputStream() { throw new UnsupportedOperationException(); } @@ -1081,11 +1081,7 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi } - private static final EntityResolver NO_OP_ENTITY_RESOLVER = new EntityResolver() { - @Override - public InputSource resolveEntity(String publicId, String systemId) { - return new InputSource(new StringReader("")); - } - }; + private static final EntityResolver NO_OP_ENTITY_RESOLVER = + (publicId, systemId) -> new InputSource(new StringReader("")); }