Polishing

This commit is contained in:
Juergen Hoeller 2016-03-25 23:28:45 +01:00
parent 862d10c128
commit 94cb7786c8
7 changed files with 37 additions and 33 deletions

View File

@ -777,4 +777,5 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
return (this.cacheOperation.hashCode() * 31 + this.methodCacheKey.hashCode()); return (this.cacheOperation.hashCode() * 31 + this.methodCacheKey.hashCode());
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -27,7 +27,7 @@ import org.springframework.util.ObjectUtils;
* *
* @author Costin Leau * @author Costin Leau
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 4.2.0 * @since 4.2
* @see CachedExpressionEvaluator * @see CachedExpressionEvaluator
*/ */
public final class AnnotatedElementKey { public final class AnnotatedElementKey {
@ -36,12 +36,13 @@ public final class AnnotatedElementKey {
private final Class<?> targetClass; private final Class<?> targetClass;
/** /**
* Create a new instance with the specified {@link AnnotatedElement} and * Create a new instance with the specified {@link AnnotatedElement} and
* optional target {@link Class}. * optional target {@link Class}.
*/ */
public AnnotatedElementKey(AnnotatedElement element, Class<?> targetClass) { public AnnotatedElementKey(AnnotatedElement element, Class<?> targetClass) {
Assert.notNull(element, "AnnotatedElement must be set."); Assert.notNull(element, "AnnotatedElement must not be null");
this.element = element; this.element = element;
this.targetClass = targetClass; this.targetClass = targetClass;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,13 +28,14 @@ import org.springframework.util.ObjectUtils;
* are defined on {@link java.lang.reflect.AnnotatedElement}. * are defined on {@link java.lang.reflect.AnnotatedElement}.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 4.2.0 * @since 4.2
* @see AnnotatedElementKey * @see AnnotatedElementKey
*/ */
public abstract class CachedExpressionEvaluator { public abstract class CachedExpressionEvaluator {
private final SpelExpressionParser parser; private final SpelExpressionParser parser;
/** /**
* Create a new instance with the specified {@link SpelExpressionParser}. * Create a new instance with the specified {@link SpelExpressionParser}.
*/ */
@ -50,6 +51,7 @@ public abstract class CachedExpressionEvaluator {
this(new SpelExpressionParser()); this(new SpelExpressionParser());
} }
/** /**
* Return the {@link SpelExpressionParser} to use. * Return the {@link SpelExpressionParser} to use.
*/ */
@ -57,6 +59,7 @@ public abstract class CachedExpressionEvaluator {
return this.parser; return this.parser;
} }
/** /**
* Return the {@link Expression} for the specified SpEL value * Return the {@link Expression} for the specified SpEL value
* <p>Parse the expression if it hasn't been already. * <p>Parse the expression if it hasn't been already.
@ -66,6 +69,7 @@ public abstract class CachedExpressionEvaluator {
*/ */
protected Expression getExpression(Map<ExpressionKey, Expression> cache, protected Expression getExpression(Map<ExpressionKey, Expression> cache,
AnnotatedElementKey elementKey, String expression) { AnnotatedElementKey elementKey, String expression) {
ExpressionKey expressionKey = createKey(elementKey, expression); ExpressionKey expressionKey = createKey(elementKey, expression);
Expression expr = cache.get(expressionKey); Expression expr = cache.get(expressionKey);
if (expr == null) { if (expr == null) {
@ -79,6 +83,7 @@ public abstract class CachedExpressionEvaluator {
return new ExpressionKey(elementKey, expression); return new ExpressionKey(elementKey, expression);
} }
protected static class ExpressionKey { protected static class ExpressionKey {
private final AnnotatedElementKey key; private final AnnotatedElementKey key;
@ -105,7 +110,7 @@ public abstract class CachedExpressionEvaluator {
@Override @Override
public int hashCode() { public int hashCode() {
return this.key.hashCode() * 29 + (this.expression != null ? this.expression.hashCode() : 0); return this.key.hashCode() + (this.expression != null ? this.expression.hashCode() * 29 : 0);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,8 +25,8 @@ import org.springframework.util.ObjectUtils;
/** /**
* A method-based {@link org.springframework.expression.EvaluationContext} that * A method-based {@link org.springframework.expression.EvaluationContext} that
* provides explicit support for method-based invocations. * provides explicit support for method-based invocations.
* <p> *
* Expose the actual method arguments using the following aliases: * <p>Expose the actual method arguments using the following aliases:
* <ol> * <ol>
* <li>pX where X is the index of the argument (p0 for the first argument)</li> * <li>pX where X is the index of the argument (p0 for the first argument)</li>
* <li>aX where X is the index of the argument (a1 for the second argument)</li> * <li>aX where X is the index of the argument (a1 for the second argument)</li>
@ -34,7 +34,7 @@ import org.springframework.util.ObjectUtils;
* </ol> * </ol>
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 4.2.0 * @since 4.2
*/ */
public class MethodBasedEvaluationContext extends StandardEvaluationContext { public class MethodBasedEvaluationContext extends StandardEvaluationContext {
@ -46,6 +46,7 @@ public class MethodBasedEvaluationContext extends StandardEvaluationContext {
private boolean paramLoaded = false; private boolean paramLoaded = false;
public MethodBasedEvaluationContext(Object rootObject, Method method, Object[] args, public MethodBasedEvaluationContext(Object rootObject, Method method, Object[] args,
ParameterNameDiscoverer paramDiscoverer) { ParameterNameDiscoverer paramDiscoverer) {
@ -55,6 +56,7 @@ public class MethodBasedEvaluationContext extends StandardEvaluationContext {
this.paramDiscoverer = paramDiscoverer; this.paramDiscoverer = paramDiscoverer;
} }
@Override @Override
public Object lookupVariable(String name) { public Object lookupVariable(String name) {
Object variable = super.lookupVariable(name); Object variable = super.lookupVariable(name);

View File

@ -47,6 +47,7 @@ public class JsonPathResultMatchers {
private String prefix; private String prefix;
/** /**
* Protected constructor. * Protected constructor.
* <p>Use {@link MockMvcResultMatchers#jsonPath(String, Object...)} or * <p>Use {@link MockMvcResultMatchers#jsonPath(String, Object...)} or
@ -65,7 +66,7 @@ public class JsonPathResultMatchers {
* <p>Use this method if the JSON payloads are prefixed to avoid * <p>Use this method if the JSON payloads are prefixed to avoid
* Cross Site Script Inclusion (XSSI) attacks. * Cross Site Script Inclusion (XSSI) attacks.
* @param prefix the string prefix prepended to the actual JSON payload * @param prefix the string prefix prepended to the actual JSON payload
* @since 4.3.0 * @since 4.3
*/ */
public JsonPathResultMatchers prefix(String prefix) { public JsonPathResultMatchers prefix(String prefix) {
this.prefix = prefix; this.prefix = prefix;
@ -252,14 +253,14 @@ public class JsonPathResultMatchers {
String content = result.getResponse().getContentAsString(); String content = result.getResponse().getContentAsString();
if (StringUtils.hasLength(this.prefix)) { if (StringUtils.hasLength(this.prefix)) {
try { try {
String reason = String.format("Expected a JSON payload prefixed with \"%s\" but found: %s", this.prefix, String reason = String.format("Expected a JSON payload prefixed with \"%s\" but found: %s",
StringUtils.quote(content.substring(0, this.prefix.length()))); this.prefix, StringUtils.quote(content.substring(0, this.prefix.length())));
MatcherAssert.assertThat(reason, content, StringStartsWith.startsWith(this.prefix)); MatcherAssert.assertThat(reason, content, StringStartsWith.startsWith(this.prefix));
return content.substring(this.prefix.length()); return content.substring(this.prefix.length());
} }
catch (StringIndexOutOfBoundsException oobe) { catch (StringIndexOutOfBoundsException oobe) {
String message = "JSON prefix \"" + this.prefix + "\" not found, exception: "; throw new AssertionError(
throw new AssertionError(message + oobe.getMessage()); "JSON prefix \"" + this.prefix + "\" not found, exception: " + oobe.getMessage());
} }
} }
else { else {

View File

@ -71,14 +71,11 @@ import org.springframework.web.util.WebUtils;
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 3.2 * @since 3.2
*
* @see #handleException(Exception, WebRequest) * @see #handleException(Exception, WebRequest)
* @see org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver * @see org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver
*/ */
public abstract class ResponseEntityExceptionHandler { public abstract class ResponseEntityExceptionHandler {
protected final Log logger = LogFactory.getLog(getClass());
/** /**
* Log category to use when no mapped handler is found for a request. * Log category to use when no mapped handler is found for a request.
* @see #pageNotFoundLogger * @see #pageNotFoundLogger
@ -86,11 +83,16 @@ public abstract class ResponseEntityExceptionHandler {
public static final String PAGE_NOT_FOUND_LOG_CATEGORY = "org.springframework.web.servlet.PageNotFound"; public static final String PAGE_NOT_FOUND_LOG_CATEGORY = "org.springframework.web.servlet.PageNotFound";
/** /**
* Additional logger to use when no mapped handler is found for a request. * Specific logger to use when no mapped handler is found for a request.
* @see #PAGE_NOT_FOUND_LOG_CATEGORY * @see #PAGE_NOT_FOUND_LOG_CATEGORY
*/ */
protected static final Log pageNotFoundLogger = LogFactory.getLog(PAGE_NOT_FOUND_LOG_CATEGORY); protected static final Log pageNotFoundLogger = LogFactory.getLog(PAGE_NOT_FOUND_LOG_CATEGORY);
/**
* Common logger for use in subclasses.
*/
protected final Log logger = LogFactory.getLog(getClass());
/** /**
* Provides handling for standard Spring MVC exceptions. * Provides handling for standard Spring MVC exceptions.
@ -115,9 +117,7 @@ public abstract class ResponseEntityExceptionHandler {
NoHandlerFoundException.class NoHandlerFoundException.class
}) })
public final ResponseEntity<Object> handleException(Exception ex, WebRequest request) { public final ResponseEntity<Object> handleException(Exception ex, WebRequest request) {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
if (ex instanceof NoSuchRequestHandlingMethodException) { if (ex instanceof NoSuchRequestHandlingMethodException) {
HttpStatus status = HttpStatus.NOT_FOUND; HttpStatus status = HttpStatus.NOT_FOUND;
return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException) ex, headers, status, request); return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException) ex, headers, status, request);
@ -202,7 +202,6 @@ public abstract class ResponseEntityExceptionHandler {
if (HttpStatus.INTERNAL_SERVER_ERROR.equals(status)) { if (HttpStatus.INTERNAL_SERVER_ERROR.equals(status)) {
request.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, ex, WebRequest.SCOPE_REQUEST); request.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, ex, WebRequest.SCOPE_REQUEST);
} }
return new ResponseEntity<Object>(body, headers, status); return new ResponseEntity<Object>(body, headers, status);
} }
@ -242,7 +241,6 @@ public abstract class ResponseEntityExceptionHandler {
if (!supportedMethods.isEmpty()) { if (!supportedMethods.isEmpty()) {
headers.setAllow(supportedMethods); headers.setAllow(supportedMethods);
} }
return handleExceptionInternal(ex, null, headers, status, request); return handleExceptionInternal(ex, null, headers, status, request);
} }
@ -443,8 +441,8 @@ public abstract class ResponseEntityExceptionHandler {
* @return a {@code ResponseEntity} instance * @return a {@code ResponseEntity} instance
* @since 4.0 * @since 4.0
*/ */
protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, protected ResponseEntity<Object> handleNoHandlerFoundException(
HttpStatus status, WebRequest request) { NoHandlerFoundException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
return handleExceptionInternal(ex, null, headers, status, request); return handleExceptionInternal(ex, null, headers, status, request);
} }

View File

@ -162,7 +162,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
/** /**
* Configure the {@link ResourceHttpMessageConverter} to use. * Configure the {@link ResourceHttpMessageConverter} to use.
* <p>By default a {@link ResourceHttpMessageConverter} will be configured. * <p>By default a {@link ResourceHttpMessageConverter} will be configured.
* @since 4.3.0 * @since 4.3
*/ */
public void setResourceHttpMessageConverter(ResourceHttpMessageConverter resourceHttpMessageConverter) { public void setResourceHttpMessageConverter(ResourceHttpMessageConverter resourceHttpMessageConverter) {
this.resourceHttpMessageConverter = resourceHttpMessageConverter; this.resourceHttpMessageConverter = resourceHttpMessageConverter;
@ -172,7 +172,6 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
return this.resourceHttpMessageConverter; return this.resourceHttpMessageConverter;
} }
/** /**
* Configure a {@code ContentNegotiationManager} to determine the media types * Configure a {@code ContentNegotiationManager} to determine the media types
* for resources being served. If the manager contains a path * for resources being served. If the manager contains a path
@ -185,7 +184,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
* settings is used to create the manager. See the Javadoc of * settings is used to create the manager. See the Javadoc of
* {@code ContentNegotiationManagerFactoryBean} for details * {@code ContentNegotiationManagerFactoryBean} for details
* @param contentNegotiationManager the manager to use * @param contentNegotiationManager the manager to use
* @since 4.3.0 * @since 4.3
*/ */
public void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager) { public void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager) {
this.contentNegotiationManager = contentNegotiationManager; this.contentNegotiationManager = contentNegotiationManager;
@ -335,10 +334,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
this.resourceHttpMessageConverter.write(rangeResource, mediaType, outputMessage); this.resourceHttpMessageConverter.write(rangeResource, mediaType, outputMessage);
} }
catch (IllegalArgumentException ex) { catch (IllegalArgumentException ex) {
Long contentLength = resource.contentLength();
if (contentLength != null) {
response.addHeader("Content-Range", "bytes */" + resource.contentLength()); response.addHeader("Content-Range", "bytes */" + resource.contentLength());
}
response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE); response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
} }
} }
@ -468,7 +464,6 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
*/ */
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
protected MediaType getMediaType(HttpServletRequest request, Resource resource) { protected MediaType getMediaType(HttpServletRequest request, Resource resource) {
// For backwards compatibility // For backwards compatibility
MediaType mediaType = getMediaType(resource); MediaType mediaType = getMediaType(resource);
if (mediaType != null) { if (mediaType != null) {
@ -535,6 +530,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes"); response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
} }
@Override @Override
public String toString() { public String toString() {
return "ResourceHttpRequestHandler [locations=" + getLocations() + ", resolvers=" + getResourceResolvers() + "]"; return "ResourceHttpRequestHandler [locations=" + getLocations() + ", resolvers=" + getResourceResolvers() + "]";