#655 - Replace Java8Utils by utilizing Spring 5's Optional support.
Since the baseline for the next version is Java 8 there is no need to check if Optionals are on the classpath. Original pull request: #657.
This commit is contained in:
committed by
Oliver Gierke
parent
cf3ab53b5a
commit
6d952b1abf
@@ -139,12 +139,10 @@ class AnnotatedParametersParameterAccessor {
|
||||
|
||||
Assert.notNull(parameter, "MethodParameter must not be null!");
|
||||
|
||||
boolean isOptional = Java8Utils.isJava8Optional(parameter.getParameterType());
|
||||
|
||||
this.parameter = parameter;
|
||||
this.value = value;
|
||||
this.attribute = attribute;
|
||||
this.parameterTypeDescriptor = TypeDescriptor.nested(parameter, isOptional ? 1 : 0);
|
||||
this.parameterTypeDescriptor = TypeDescriptor.nested(parameter, parameter.isOptional() ? 1 : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -45,6 +45,7 @@ import org.springframework.hateoas.core.MethodParameters;
|
||||
import org.springframework.hateoas.mvc.AnnotatedParametersParameterAccessor.BoundMethodParameter;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
@@ -299,7 +300,7 @@ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory<Co
|
||||
|
||||
RequestParam annotation = parameter.getParameterAnnotation(RequestParam.class);
|
||||
|
||||
if (Java8Utils.isJava8Optional(parameter.getParameterType())) {
|
||||
if (parameter.isOptional()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -318,13 +319,13 @@ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory<Co
|
||||
|
||||
RequestParam annotation = parameter.getParameterAnnotation(RequestParam.class);
|
||||
|
||||
value = Java8Utils.unwrapJava8Optional(value);
|
||||
value = ObjectUtils.unwrapOptional(value);
|
||||
|
||||
if (value != null) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (!annotation.required() || Java8Utils.isJava8Optional(parameter.getParameterType())) {
|
||||
if (!annotation.required() || parameter.isOptional()) {
|
||||
return SKIP_VALUE;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
/*
|
||||
* Copyright 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.
|
||||
* 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.hateoas.mvc;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Utilities to reflectively work with JDK 8's {@link Optional} on JDK 6.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
final class Java8Utils {
|
||||
|
||||
private static boolean OPTIONAL_PRESENT = ClassUtils.isPresent("java.util.Optional",
|
||||
OptionalValueAccessor.class.getClassLoader());
|
||||
|
||||
/**
|
||||
* Returns whether the given type is the JDK 8's {@link Optional}.
|
||||
*
|
||||
* @param type must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
static boolean isJava8Optional(Class<?> type) {
|
||||
|
||||
Assert.notNull(type, "Type must not be null!");
|
||||
|
||||
return OPTIONAL_PRESENT && OptionalValueAccessor.isOptional(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given source value is a JDK 8 {@link Optional}.
|
||||
*
|
||||
* @param source can be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
static boolean isJava8Optional(Object source) {
|
||||
return OPTIONAL_PRESENT && OptionalValueAccessor.isOptional(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unwraps the value contained in a JDK 8 {@link Optional} if the value is one.
|
||||
*
|
||||
* @param source can be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
static Object unwrapJava8Optional(Object source) {
|
||||
return OPTIONAL_PRESENT && isJava8Optional(source) ? OptionalValueAccessor.unwrapOptional(source) : source;
|
||||
}
|
||||
|
||||
private static class OptionalValueAccessor {
|
||||
|
||||
static boolean isOptional(Class<?> type) {
|
||||
return Optional.class.isAssignableFrom(type);
|
||||
}
|
||||
|
||||
static boolean isOptional(Object source) {
|
||||
return Optional.class.isInstance(source);
|
||||
}
|
||||
|
||||
static Object unwrapOptional(Object source) {
|
||||
return ((Optional<?>) source).orElse(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user