Browse Source

Polishing

pull/1723/head
Juergen Hoeller 8 years ago
parent
commit
0030ff8711
  1. 9
      spring-beans/src/main/java/org/springframework/beans/factory/xml/ParserContext.java
  2. 31
      spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResolverAdapter.java
  3. 4
      spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheResolverAdapterTests.java
  4. 3
      spring-context/src/main/java/org/springframework/cache/interceptor/CacheResolver.java
  5. 3
      spring-context/src/main/java/org/springframework/validation/AbstractErrors.java
  6. 13
      spring-core/src/main/java/org/springframework/core/ParameterizedTypeReference.java
  7. 28
      spring-core/src/main/java/org/springframework/util/xml/TransformerUtils.java
  8. 7
      spring-messaging/src/main/java/org/springframework/messaging/converter/AbstractMessageConverter.java
  9. 13
      spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/HeadersMethodArgumentResolver.java
  10. 6
      spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/HandlerMethodArgumentResolverComposite.java
  11. 15
      spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java
  12. 13
      spring-web/src/main/java/org/springframework/web/util/HtmlCharacterEntityReferences.java

9
spring-beans/src/main/java/org/springframework/beans/factory/xml/ParserContext.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 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.
@ -44,7 +44,7 @@ public final class ParserContext { @@ -44,7 +44,7 @@ public final class ParserContext {
private BeanDefinition containingBeanDefinition;
private final Stack<ComponentDefinition> containingComponents = new Stack<ComponentDefinition>();
private final Stack<CompositeComponentDefinition> containingComponents = new Stack<CompositeComponentDefinition>();
public ParserContext(XmlReaderContext readerContext, BeanDefinitionParserDelegate delegate) {
@ -90,8 +90,7 @@ public final class ParserContext { @@ -90,8 +90,7 @@ public final class ParserContext {
}
public CompositeComponentDefinition getContainingComponent() {
return (!this.containingComponents.isEmpty() ?
(CompositeComponentDefinition) this.containingComponents.lastElement() : null);
return (!this.containingComponents.isEmpty() ? this.containingComponents.lastElement() : null);
}
public void pushContainingComponent(CompositeComponentDefinition containingComponent) {
@ -99,7 +98,7 @@ public final class ParserContext { @@ -99,7 +98,7 @@ public final class ParserContext {
}
public CompositeComponentDefinition popContainingComponent() {
return (CompositeComponentDefinition) this.containingComponents.pop();
return this.containingComponents.pop();
}
public void popAndRegisterContainingComponent() {

31
spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/CacheResolverAdapter.java vendored

@ -1,5 +1,20 @@ @@ -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 { @@ -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 { @@ -45,8 +62,10 @@ class CacheResolverAdapter implements CacheResolver {
throw new IllegalStateException("Unexpected context " + context);
}
CacheInvocationContext<?> cacheInvocationContext = (CacheInvocationContext<?>) context;
javax.cache.Cache<Object, Object> cache = target.resolveCache(cacheInvocationContext);
Assert.notNull(cache, "Cannot resolve cache for '" + context + "' using '" + target + "'");
javax.cache.Cache<Object, Object> 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));
}

4
spring-context-support/src/test/java/org/springframework/cache/jcache/interceptor/CacheResolverAdapterTests.java vendored

@ -1,5 +1,5 @@ @@ -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 { @@ -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);
}

3
spring-context/src/main/java/org/springframework/cache/interceptor/CacheResolver.java vendored

@ -1,5 +1,5 @@ @@ -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.
@ -34,6 +34,7 @@ public interface CacheResolver { @@ -34,6 +34,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<? extends Cache> resolveCaches(CacheOperationInvocationContext<?> context);

3
spring-context/src/main/java/org/springframework/validation/AbstractErrors.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 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.
@ -211,7 +211,6 @@ public abstract class AbstractErrors implements Errors, Serializable { @@ -211,7 +211,6 @@ public abstract class AbstractErrors implements Errors, Serializable {
return (!fieldErrors.isEmpty() ? fieldErrors.get(0) : null);
}
@Override
public Class<?> getFieldType(String field) {
Object value = getFieldValue(field);

13
spring-core/src/main/java/org/springframework/core/ParameterizedTypeReference.java

@ -1,5 +1,5 @@ @@ -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.
@ -24,14 +24,14 @@ import org.springframework.util.Assert; @@ -24,14 +24,14 @@ import org.springframework.util.Assert;
/**
* The purpose of this class is to enable capturing and passing a generic
* {@link Type}. In order to capture the generic type and retain it at runtime,
* you need to create a subclass as follows:
* you need to create a subclass (ideally as anonymous inline class) as follows:
*
* <pre class="code">
* ParameterizedTypeReference&lt;List&lt;String&gt;&gt; typeRef = new ParameterizedTypeReference&lt;List&lt;String&gt;&gt;() {};
* </pre>
*
* <p>The resulting {@code typeReference} instance can then be used to obtain a
* {@link Type} instance that carries parameterized type information.
* <p>The resulting {@code typeRef} instance can then be used to obtain a {@link Type}
* instance that carries the captured parameterized type information at runtime.
* For more information on "super type tokens" see the link to Neal Gafter's blog post.
*
* @author Arjen Poutsma
@ -49,8 +49,9 @@ public abstract class ParameterizedTypeReference<T> { @@ -49,8 +49,9 @@ public abstract class ParameterizedTypeReference<T> {
Type type = parameterizedTypeReferenceSubclass.getGenericSuperclass();
Assert.isInstanceOf(ParameterizedType.class, type, "Type must be a parameterized type");
ParameterizedType parameterizedType = (ParameterizedType) type;
Assert.isTrue(parameterizedType.getActualTypeArguments().length == 1, "Number of type arguments must be 1");
this.type = parameterizedType.getActualTypeArguments()[0];
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
Assert.isTrue(actualTypeArguments.length == 1, "Number of type arguments must be 1");
this.type = actualTypeArguments[0];
}
private ParameterizedTypeReference(Type type) {

28
spring-core/src/main/java/org/springframework/util/xml/TransformerUtils.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 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.
@ -22,8 +22,8 @@ import javax.xml.transform.Transformer; @@ -22,8 +22,8 @@ import javax.xml.transform.Transformer;
import org.springframework.util.Assert;
/**
* Contains common behavior relating to {@link javax.xml.transform.Transformer Transformers}, and the
* {@code javax.xml.transform} package in general.
* Contains common behavior relating to {@link javax.xml.transform.Transformer Transformers}
* and the {@code javax.xml.transform} package in general.
*
* @author Rick Evans
* @author Juergen Hoeller
@ -32,16 +32,16 @@ import org.springframework.util.Assert; @@ -32,16 +32,16 @@ import org.springframework.util.Assert;
public abstract class TransformerUtils {
/**
* The indent amount of characters if {@link #enableIndenting(javax.xml.transform.Transformer) indenting is enabled}.
* The indent amount of characters if {@link #enableIndenting indenting is enabled}.
* <p>Defaults to "2".
*/
public static final int DEFAULT_INDENT_AMOUNT = 2;
/**
* Enable indenting for the supplied {@link javax.xml.transform.Transformer}. <p>If the underlying XSLT engine is
* Xalan, then the special output key {@code indent-amount} will be also be set to a value of {@link
* #DEFAULT_INDENT_AMOUNT} characters.
*
* Enable indenting for the supplied {@link javax.xml.transform.Transformer}.
* <p>If the underlying XSLT engine is Xalan, then the special output key {@code indent-amount}
* will be also be set to a value of {@link #DEFAULT_INDENT_AMOUNT} characters.
* @param transformer the target transformer
* @see javax.xml.transform.Transformer#setOutputProperty(String, String)
* @see javax.xml.transform.OutputKeys#INDENT
@ -51,10 +51,9 @@ public abstract class TransformerUtils { @@ -51,10 +51,9 @@ public abstract class TransformerUtils {
}
/**
* Enable indenting for the supplied {@link javax.xml.transform.Transformer}. <p>If the underlying XSLT engine is
* Xalan, then the special output key {@code indent-amount} will be also be set to a value of {@link
* #DEFAULT_INDENT_AMOUNT} characters.
*
* Enable indenting for the supplied {@link javax.xml.transform.Transformer}.
* <p>If the underlying XSLT engine is Xalan, then the special output key {@code indent-amount}
* will be also be set to a value of {@link #DEFAULT_INDENT_AMOUNT} characters.
* @param transformer the target transformer
* @param indentAmount the size of the indent (2 characters, 3 characters, etc.)
* @see javax.xml.transform.Transformer#setOutputProperty(String, String)
@ -62,7 +61,9 @@ public abstract class TransformerUtils { @@ -62,7 +61,9 @@ public abstract class TransformerUtils {
*/
public static void enableIndenting(Transformer transformer, int indentAmount) {
Assert.notNull(transformer, "Transformer must not be null");
Assert.isTrue(indentAmount > -1, "The indent amount cannot be less than zero : got " + indentAmount);
if (indentAmount < 0) {
throw new IllegalArgumentException("The indent amount cannot be less than zero : got " + indentAmount);
}
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
try {
// Xalan-specific, but this is the most common XSLT engine in any case
@ -74,7 +75,6 @@ public abstract class TransformerUtils { @@ -74,7 +75,6 @@ public abstract class TransformerUtils {
/**
* Disable indenting for the supplied {@link javax.xml.transform.Transformer}.
*
* @param transformer the target transformer
* @see javax.xml.transform.OutputKeys#INDENT
*/

7
spring-messaging/src/main/java/org/springframework/messaging/converter/AbstractMessageConverter.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 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.
@ -135,8 +135,9 @@ public abstract class AbstractMessageConverter implements SmartMessageConverter @@ -135,8 +135,9 @@ public abstract class AbstractMessageConverter implements SmartMessageConverter
* @param payloadClass either byte[] or String
*/
public void setSerializedPayloadClass(Class<?> payloadClass) {
Assert.isTrue(byte[].class == payloadClass || String.class == payloadClass,
"Payload class must be byte[] or String: " + payloadClass);
if (!(byte[].class == payloadClass || String.class == payloadClass)) {
throw new IllegalArgumentException("Payload class must be byte[] or String: " + payloadClass);
}
this.serializedPayloadClass = payloadClass;
}

13
spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/HeadersMethodArgumentResolver.java

@ -1,5 +1,5 @@ @@ -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.
@ -25,7 +25,6 @@ import org.springframework.messaging.MessageHeaders; @@ -25,7 +25,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;
/**
@ -45,15 +44,12 @@ public class HeadersMethodArgumentResolver implements HandlerMethodArgumentResol @@ -45,15 +44,12 @@ 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
public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception {
Class<?> paramType = parameter.getParameterType();
if (Map.class.isAssignableFrom(paramType)) {
return message.getHeaders();
}
@ -68,7 +64,10 @@ public class HeadersMethodArgumentResolver implements HandlerMethodArgumentResol @@ -68,7 +64,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);
}
}

6
spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/HandlerMethodArgumentResolverComposite.java

@ -24,7 +24,6 @@ import java.util.concurrent.ConcurrentHashMap; @@ -24,7 +24,6 @@ import java.util.concurrent.ConcurrentHashMap;
import org.springframework.core.MethodParameter;
import org.springframework.messaging.Message;
import org.springframework.util.Assert;
/**
* Resolves method parameters by delegating to a list of registered
@ -106,9 +105,10 @@ public class HandlerMethodArgumentResolverComposite implements HandlerMethodArgu @@ -106,9 +105,10 @@ public class HandlerMethodArgumentResolverComposite implements HandlerMethodArgu
*/
@Override
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);
}

15
spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java

@ -1,5 +1,5 @@ @@ -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.
@ -554,9 +554,12 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi @@ -554,9 +554,12 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
for (int i = 0; i < resources.length; i++) {
Assert.notNull(resources[i], "Resource is null");
Assert.isTrue(resources[i].exists(), "Resource " + resources[i] + " does not exist");
InputSource inputSource = SaxResourceUtils.createInputSource(resources[i]);
Resource resource = resources[i];
Assert.notNull(resource, "Resource is null");
if (!resource.exists()) {
throw new IllegalArgumentException("Resource " + resource + " does not exist");
}
InputSource inputSource = SaxResourceUtils.createInputSource(resource);
schemaSources[i] = new SAXSource(xmlReader, inputSource);
}
SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
@ -1031,12 +1034,12 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi @@ -1031,12 +1034,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();
}

13
spring-web/src/main/java/org/springframework/web/util/HtmlCharacterEntityReferences.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 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,8 +23,6 @@ import java.util.HashMap; @@ -23,8 +23,6 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.springframework.util.Assert;
/**
* Represents a set of character entity references defined by the
* HTML 4.0 standard.
@ -87,8 +85,9 @@ class HtmlCharacterEntityReferences { @@ -87,8 +85,9 @@ class HtmlCharacterEntityReferences {
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
int referredChar = Integer.parseInt(key);
Assert.isTrue((referredChar < 1000 || (referredChar >= 8000 && referredChar < 10000)),
"Invalid reference to special HTML entity: " + referredChar);
if (!(referredChar < 1000 || (referredChar >= 8000 && referredChar < 10000))) {
throw new IllegalArgumentException("Invalid reference to special HTML entity: " + referredChar);
}
int index = (referredChar < 1000 ? referredChar : referredChar - 7000);
String reference = entityReferences.getProperty(key);
this.characterToEntityReferenceMap[index] = REFERENCE_START + reference + REFERENCE_END;
@ -119,14 +118,14 @@ class HtmlCharacterEntityReferences { @@ -119,14 +118,14 @@ class HtmlCharacterEntityReferences {
}
/**
* Return the reference mapped to the given character or {@code null}.
* Return the reference mapped to the given character, or {@code null} if none found.
*/
public String convertToReference(char character) {
return convertToReference(character, WebUtils.DEFAULT_CHARACTER_ENCODING);
}
/**
* Return the reference mapped to the given character or {@code null}.
* Return the reference mapped to the given character, or {@code null} if none found.
* @since 4.1.2
*/
public String convertToReference(char character, String encoding) {

Loading…
Cancel
Save