Avoid defensive checks against Java 8 API (java.util.Optional etc)

This commit also fixes broken javadoc links and code references.

Issue: SPR-13188
This commit is contained in:
Juergen Hoeller
2016-07-05 02:08:59 +02:00
parent adb935db79
commit 51252ebbca
98 changed files with 290 additions and 1232 deletions

View File

@@ -74,7 +74,7 @@ import org.springframework.web.util.WebUtils;
* <li>It can use any {@link HandlerMapping} implementation - pre-built or provided as part
* of an application - to control the routing of requests to handler objects. Default is
* {@link org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping} and
* {@link org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping}.
* {@link org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping}.
* HandlerMapping objects can be defined as beans in the servlet's application context,
* implementing the HandlerMapping interface, overriding the default HandlerMapping if
* present. HandlerMappings can be given any bean name (they are tested by type).
@@ -84,7 +84,7 @@ import org.springframework.web.util.WebUtils;
* {@link org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter}, for Spring's
* {@link org.springframework.web.HttpRequestHandler} and
* {@link org.springframework.web.servlet.mvc.Controller} interfaces, respectively. A default
* {@link org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter}
* {@link org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter}
* will be registered as well. HandlerAdapter objects can be added as beans in the
* application context, overriding the default HandlerAdapters. Like HandlerMappings,
* HandlerAdapters can be given any bean name (they are tested by type).
@@ -92,7 +92,7 @@ import org.springframework.web.util.WebUtils;
* <li>The dispatcher's exception resolution strategy can be specified via a
* {@link HandlerExceptionResolver}, for example mapping certain exceptions to error pages.
* Default are
* {@link org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver},
* {@link org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver},
* {@link org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver}, and
* {@link org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver}.
* These HandlerExceptionResolvers can be overridden through the application context.
@@ -131,7 +131,7 @@ import org.springframework.web.util.WebUtils;
* {@code HandlerAdapter} (for method-level annotations) is present in the dispatcher.</b>
* This is the case by default. However, if you are defining custom {@code HandlerMappings}
* or {@code HandlerAdapters}, then you need to make sure that a corresponding custom
* {@code DefaultAnnotationHandlerMapping} and/or {@code AnnotationMethodHandlerAdapter}
* {@code RequestMappingHandlerMapping} and/or {@code RequestMappingHandlerAdapter}
* is defined as well - provided that you intend to use {@code @RequestMapping}.
*
* <p><b>A web application can define any number of DispatcherServlets.</b>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2016 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,7 +24,7 @@ import javax.servlet.http.HttpServletRequest;
*
* <p>This class can be implemented by application developers, although this is not
* necessary, as {@link org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping}
* and {@link org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping}
* and {@link org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping}
* are included in the framework. The former is the default if no
* HandlerMapping bean is registered in the application context.
*
@@ -49,7 +49,7 @@ import javax.servlet.http.HttpServletRequest;
* @see org.springframework.core.Ordered
* @see org.springframework.web.servlet.handler.AbstractHandlerMapping
* @see org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
* @see org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
* @see org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
*/
public interface HandlerMapping {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2016 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.
@@ -28,9 +28,9 @@ import org.springframework.util.StringUtils;
*
* <p>This is the default implementation used by the
* {@link org.springframework.web.servlet.DispatcherServlet}, along with
* {@link org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping}
* (on Java 5 and higher). Alternatively, {@link SimpleUrlHandlerMapping} allows for
* customizing a handler mapping declaratively.
* {@link org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping}.
* Alternatively, {@link SimpleUrlHandlerMapping} allows for customizing a
* handler mapping declaratively.
*
* <p>The mapping is from URL to bean name. Thus an incoming URL "/foo" would map
* to a handler named "/foo", or to "/foo /foo2" in case of multiple mappings to

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");
* you may not use this file except in compliance with the License.
@@ -25,7 +25,6 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.context.i18n.LocaleContext;
import org.springframework.context.i18n.SimpleLocaleContext;
import org.springframework.context.i18n.TimeZoneAwareLocaleContext;
import org.springframework.lang.UsesJava7;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleContextResolver;
import org.springframework.web.servlet.LocaleResolver;
@@ -242,7 +241,6 @@ public class CookieLocaleResolver extends CookieGenerator implements LocaleConte
* @return the corresponding {@code Locale} instance
* @since 4.3
*/
@UsesJava7
protected Locale parseLocaleValue(String locale) {
return (isLanguageTagCompliant() ? Locale.forLanguageTag(locale) : StringUtils.parseLocaleString(locale));
}
@@ -256,7 +254,6 @@ public class CookieLocaleResolver extends CookieGenerator implements LocaleConte
* @return a String representation for the given locale
* @since 4.3
*/
@UsesJava7
protected String toLocaleValue(Locale locale) {
return (isLanguageTagCompliant() ? locale.toLanguageTag() : locale.toString());
}

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");
* you may not use this file except in compliance with the License.
@@ -24,7 +24,6 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.lang.UsesJava7;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
@@ -183,7 +182,6 @@ public class LocaleChangeInterceptor extends HandlerInterceptorAdapter {
* @return the corresponding {@code Locale} instance
* @since 4.3
*/
@UsesJava7
protected Locale parseLocaleValue(String locale) {
return (isLanguageTagCompliant() ? Locale.forLanguageTag(locale) : StringUtils.parseLocaleString(locale));
}

View File

@@ -1,84 +0,0 @@
/*
* Copyright 2002-2015 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.web.servlet.mvc.method.annotation;
import java.util.concurrent.CompletionStage;
import java.util.function.Consumer;
import java.util.function.Function;
import org.springframework.core.MethodParameter;
import org.springframework.lang.UsesJava8;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.context.request.async.WebAsyncUtils;
import org.springframework.web.method.support.AsyncHandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;
/**
* Handles return values of type {@link CompletionStage} (implemented by
* {@link java.util.concurrent.CompletableFuture} for example).
*
* @author Sebastien Deleuze
* @since 4.2
*
* @deprecated as of 4.3 {@link DeferredResultMethodReturnValueHandler} supports
* CompletionStage return values via an adapter mechanism.
*/
@Deprecated
@UsesJava8
public class CompletionStageReturnValueHandler implements AsyncHandlerMethodReturnValueHandler {
@Override
public boolean supportsReturnType(MethodParameter returnType) {
return CompletionStage.class.isAssignableFrom(returnType.getParameterType());
}
@Override
public boolean isAsyncReturnValue(Object returnValue, MethodParameter returnType) {
return (returnValue != null && returnValue instanceof CompletionStage);
}
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
if (returnValue == null) {
mavContainer.setRequestHandled(true);
return;
}
final DeferredResult<Object> deferredResult = new DeferredResult<Object>();
WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult, mavContainer);
@SuppressWarnings("unchecked")
CompletionStage<Object> future = (CompletionStage<Object>) returnValue;
future.thenAccept(new Consumer<Object>() {
@Override
public void accept(Object result) {
deferredResult.setResult(result);
}
});
future.exceptionally(new Function<Throwable, Object>() {
@Override
public Object apply(Throwable ex) {
deferredResult.setErrorResult(ex);
return null;
}
});
}
}

View File

@@ -22,9 +22,7 @@ import java.util.concurrent.CompletionStage;
import java.util.function.BiFunction;
import org.springframework.core.MethodParameter;
import org.springframework.lang.UsesJava8;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.context.request.NativeWebRequest;
@@ -50,9 +48,7 @@ public class DeferredResultMethodReturnValueHandler implements AsyncHandlerMetho
this.adapterMap = new HashMap<Class<?>, DeferredResultAdapter>(5);
this.adapterMap.put(DeferredResult.class, new SimpleDeferredResultAdapter());
this.adapterMap.put(ListenableFuture.class, new ListenableFutureAdapter());
if (ClassUtils.isPresent("java.util.concurrent.CompletionStage", getClass().getClassLoader())) {
this.adapterMap.put(CompletionStage.class, new CompletionStageAdapter());
}
this.adapterMap.put(CompletionStage.class, new CompletionStageAdapter());
}
@@ -114,6 +110,7 @@ public class DeferredResultMethodReturnValueHandler implements AsyncHandlerMetho
}
}
/**
* Adapter for {@code ListenableFuture} return values.
*/
@@ -137,10 +134,10 @@ public class DeferredResultMethodReturnValueHandler implements AsyncHandlerMetho
}
}
/**
* Adapter for {@code CompletionStage} return values.
*/
@UsesJava8
private static class CompletionStageAdapter implements DeferredResultAdapter {
@Override

View File

@@ -1,76 +0,0 @@
/*
* Copyright 2002-2016 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.web.servlet.mvc.method.annotation;
import org.springframework.core.MethodParameter;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.context.request.async.WebAsyncUtils;
import org.springframework.web.method.support.AsyncHandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;
/**
* Handles return values of type
* {@link org.springframework.util.concurrent.ListenableFuture}.
*
* @author Rossen Stoyanchev
* @since 4.1
*
* @deprecated as of 4.3 {@link DeferredResultMethodReturnValueHandler} supports
* ListenableFuture return values via an adapter mechanism.
*/
@Deprecated
public class ListenableFutureReturnValueHandler implements AsyncHandlerMethodReturnValueHandler {
@Override
public boolean supportsReturnType(MethodParameter returnType) {
return ListenableFuture.class.isAssignableFrom(returnType.getParameterType());
}
@Override
public boolean isAsyncReturnValue(Object returnValue, MethodParameter returnType) {
return (returnValue != null && returnValue instanceof ListenableFuture);
}
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
if (returnValue == null) {
mavContainer.setRequestHandled(true);
return;
}
final DeferredResult<Object> deferredResult = new DeferredResult<Object>();
WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult, mavContainer);
ListenableFuture<?> future = (ListenableFuture<?>) returnValue;
future.addCallback(new ListenableFutureCallback<Object>() {
@Override
public void onSuccess(Object result) {
deferredResult.setResult(result);
}
@Override
public void onFailure(Throwable ex) {
deferredResult.setErrorResult(ex);
}
});
}
}

View File

@@ -38,8 +38,8 @@ import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.Factory;
import org.springframework.cglib.proxy.MethodProxy;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.MethodParameter;
import org.springframework.core.MethodIntrospector;
import org.springframework.core.MethodParameter;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.SynthesizingMethodParameter;
@@ -778,7 +778,6 @@ public class MvcUriComponentsBuilder {
}
/**
* @see #MethodArgumentBuilder(Class, Method)
* @deprecated as of 4.2, this is deprecated in favor of alternative constructors
* that accept a controllerType argument
*/

View File

@@ -24,7 +24,6 @@ import javax.servlet.http.HttpServletRequest;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.lang.UsesJava8;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.WebDataBinder;
@@ -158,7 +157,13 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM
}
}
if (parameter.isOptional()) {
arg = OptionalResolver.resolveValue(arg);
if (arg == null || (arg instanceof Collection && ((Collection) arg).isEmpty()) ||
(arg instanceof Object[] && ((Object[]) arg).length == 0)) {
arg = Optional.empty();
}
else {
arg = Optional.of(arg);
}
}
return arg;
@@ -177,20 +182,4 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM
return partName;
}
/**
* Inner class to avoid hard-coded dependency on Java 8 Optional type...
*/
@UsesJava8
private static class OptionalResolver {
public static Object resolveValue(Object value) {
if (value == null || (value instanceof Collection && ((Collection) value).isEmpty()) ||
(value instanceof Object[] && ((Object[]) value).length == 0)) {
return Optional.empty();
}
return Optional.of(value);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -28,7 +28,6 @@ import javax.servlet.http.HttpSession;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpMethod;
import org.springframework.lang.UsesJava8;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.ServletWebRequest;
@@ -131,7 +130,6 @@ public class ServletRequestMethodArgumentResolver implements HandlerMethodArgume
/**
* Inner class to avoid a hard-coded dependency on Java 8's {@link java.time.ZoneId}.
*/
@UsesJava8
private static class ZoneIdResolver {
public static Object resolveZoneId(HttpServletRequest request) {