Migrate code to Java 17 style.
Use var instead of explicit local types where applicable. Use pattern variable instead instanceof and cast. Prefer loops and nullable types over Stream and Optional. Convert classes to records where applicable. See #2465
This commit is contained in:
committed by
Jens Schauder
parent
d4036ec0a9
commit
c735b58607
@@ -44,7 +44,7 @@ public final class Accessor {
|
||||
|
||||
Assert.notNull(method, "Method must not be null!");
|
||||
|
||||
PropertyDescriptor descriptor = BeanUtils.findPropertyForMethod(method);
|
||||
var descriptor = BeanUtils.findPropertyForMethod(method);
|
||||
|
||||
if (descriptor == null) {
|
||||
throw new IllegalArgumentException(String.format("Invoked method %s is no accessor method!", method));
|
||||
|
||||
@@ -55,9 +55,9 @@ public class DefaultMethodInvokingMethodInterceptor implements MethodInterceptor
|
||||
*/
|
||||
public static boolean hasDefaultMethods(Class<?> interfaceClass) {
|
||||
|
||||
Method[] methods = ReflectionUtils.getAllDeclaredMethods(interfaceClass);
|
||||
var methods = ReflectionUtils.getAllDeclaredMethods(interfaceClass);
|
||||
|
||||
for (Method method : methods) {
|
||||
for (var method : methods) {
|
||||
if (method.isDefault()) {
|
||||
return true;
|
||||
}
|
||||
@@ -74,21 +74,21 @@ public class DefaultMethodInvokingMethodInterceptor implements MethodInterceptor
|
||||
@Override
|
||||
public Object invoke(@SuppressWarnings("null") MethodInvocation invocation) throws Throwable {
|
||||
|
||||
Method method = invocation.getMethod();
|
||||
var method = invocation.getMethod();
|
||||
|
||||
if (!method.isDefault()) {
|
||||
return invocation.proceed();
|
||||
}
|
||||
|
||||
Object[] arguments = invocation.getArguments();
|
||||
Object proxy = ((ProxyMethodInvocation) invocation).getProxy();
|
||||
var arguments = invocation.getArguments();
|
||||
var proxy = ((ProxyMethodInvocation) invocation).getProxy();
|
||||
|
||||
return getMethodHandle(method).bindTo(proxy).invokeWithArguments(arguments);
|
||||
}
|
||||
|
||||
private MethodHandle getMethodHandle(Method method) throws Exception {
|
||||
|
||||
MethodHandle handle = methodHandleCache.get(method);
|
||||
var handle = methodHandleCache.get(method);
|
||||
|
||||
if (handle == null) {
|
||||
|
||||
@@ -139,7 +139,7 @@ public class DefaultMethodInvokingMethodInterceptor implements MethodInterceptor
|
||||
|
||||
private Lookup getLookup(Class<?> declaringClass, Method privateLookupIn) {
|
||||
|
||||
Lookup lookup = MethodHandles.lookup();
|
||||
var lookup = MethodHandles.lookup();
|
||||
|
||||
try {
|
||||
return (Lookup) privateLookupIn.invoke(MethodHandles.class, declaringClass, lookup);
|
||||
@@ -168,7 +168,7 @@ public class DefaultMethodInvokingMethodInterceptor implements MethodInterceptor
|
||||
throw new IllegalStateException("Could not obtain MethodHandles.lookup constructor!");
|
||||
}
|
||||
|
||||
Constructor<Lookup> constructor = this.constructor.get();
|
||||
var constructor = this.constructor.get();
|
||||
|
||||
return constructor.newInstance(method.getDeclaringClass()).unreflectSpecial(method, method.getDeclaringClass());
|
||||
}
|
||||
@@ -212,7 +212,7 @@ public class DefaultMethodInvokingMethodInterceptor implements MethodInterceptor
|
||||
private static MethodHandle doLookup(Method method, Lookup lookup)
|
||||
throws NoSuchMethodException, IllegalAccessException {
|
||||
|
||||
MethodType methodType = MethodType.methodType(method.getReturnType(), method.getParameterTypes());
|
||||
var methodType = MethodType.methodType(method.getReturnType(), method.getParameterTypes());
|
||||
|
||||
if (Modifier.isStatic(method.getModifiers())) {
|
||||
return lookup.findStatic(method.getDeclaringClass(), method.getName(), methodType);
|
||||
@@ -243,7 +243,7 @@ public class DefaultMethodInvokingMethodInterceptor implements MethodInterceptor
|
||||
*/
|
||||
public static MethodHandleLookup getMethodHandleLookup() {
|
||||
|
||||
for (MethodHandleLookup it : MethodHandleLookup.values()) {
|
||||
for (var it : MethodHandleLookup.values()) {
|
||||
|
||||
if (it.isAvailable()) {
|
||||
return it;
|
||||
@@ -258,7 +258,7 @@ public class DefaultMethodInvokingMethodInterceptor implements MethodInterceptor
|
||||
|
||||
try {
|
||||
|
||||
Constructor<Lookup> constructor = Lookup.class.getDeclaredConstructor(Class.class);
|
||||
var constructor = Lookup.class.getDeclaredConstructor(Class.class);
|
||||
ReflectionUtils.makeAccessible(constructor);
|
||||
|
||||
return constructor;
|
||||
|
||||
@@ -17,7 +17,6 @@ package org.springframework.data.projection;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
@@ -34,7 +33,6 @@ import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.core.log.LogMessage;
|
||||
import org.springframework.core.type.MethodMetadata;
|
||||
import org.springframework.data.type.MethodsMetadata;
|
||||
import org.springframework.data.type.classreading.MethodsMetadataReader;
|
||||
import org.springframework.data.type.classreading.MethodsMetadataReaderFactory;
|
||||
import org.springframework.data.util.StreamUtils;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -117,7 +115,7 @@ class DefaultProjectionInformation implements ProjectionInformation {
|
||||
*/
|
||||
private static boolean hasDefaultGetter(PropertyDescriptor descriptor) {
|
||||
|
||||
Method method = descriptor.getReadMethod();
|
||||
var method = descriptor.getReadMethod();
|
||||
|
||||
return method != null && method.isDefault();
|
||||
}
|
||||
@@ -167,13 +165,13 @@ class DefaultProjectionInformation implements ProjectionInformation {
|
||||
*/
|
||||
private Stream<PropertyDescriptor> collectDescriptors() {
|
||||
|
||||
Stream<PropertyDescriptor> allButDefaultGetters = Arrays.stream(BeanUtils.getPropertyDescriptors(type)) //
|
||||
var allButDefaultGetters = Arrays.stream(BeanUtils.getPropertyDescriptors(type)) //
|
||||
.filter(it -> !hasDefaultGetter(it));
|
||||
|
||||
Stream<PropertyDescriptor> ownDescriptors = metadata.map(it -> filterAndOrder(allButDefaultGetters, it))
|
||||
var ownDescriptors = metadata.map(it -> filterAndOrder(allButDefaultGetters, it))
|
||||
.orElse(allButDefaultGetters);
|
||||
|
||||
Stream<PropertyDescriptor> superTypeDescriptors = metadata.map(this::fromMetadata) //
|
||||
var superTypeDescriptors = metadata.map(this::fromMetadata) //
|
||||
.orElseGet(this::fromType) //
|
||||
.flatMap(it -> new PropertyDescriptorSource(it).collectDescriptors());
|
||||
|
||||
@@ -191,7 +189,7 @@ class DefaultProjectionInformation implements ProjectionInformation {
|
||||
private static Stream<PropertyDescriptor> filterAndOrder(Stream<PropertyDescriptor> source,
|
||||
MethodsMetadata metadata) {
|
||||
|
||||
Map<String, Integer> orderedMethods = getMethodOrder(metadata);
|
||||
var orderedMethods = getMethodOrder(metadata);
|
||||
|
||||
if (orderedMethods.isEmpty()) {
|
||||
return source;
|
||||
@@ -232,8 +230,8 @@ class DefaultProjectionInformation implements ProjectionInformation {
|
||||
|
||||
try {
|
||||
|
||||
MethodsMetadataReaderFactory factory = new MethodsMetadataReaderFactory(type.getClassLoader());
|
||||
MethodsMetadataReader metadataReader = factory.getMetadataReader(ClassUtils.getQualifiedName(type));
|
||||
var factory = new MethodsMetadataReaderFactory(type.getClassLoader());
|
||||
var metadataReader = factory.getMetadataReader(ClassUtils.getQualifiedName(type));
|
||||
|
||||
return Optional.of(metadataReader.getMethodsMetadata());
|
||||
|
||||
@@ -268,7 +266,7 @@ class DefaultProjectionInformation implements ProjectionInformation {
|
||||
*/
|
||||
private static Map<String, Integer> getMethodOrder(MethodsMetadata metadata) {
|
||||
|
||||
List<String> methods = metadata.getMethods() //
|
||||
var methods = metadata.getMethods() //
|
||||
.stream() //
|
||||
.map(MethodMetadata::getMethodName) //
|
||||
.distinct() //
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package org.springframework.data.projection;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
@@ -50,13 +49,13 @@ class MapAccessingMethodInterceptor implements MethodInterceptor {
|
||||
@Override
|
||||
public Object invoke(@SuppressWarnings("null") MethodInvocation invocation) throws Throwable {
|
||||
|
||||
Method method = invocation.getMethod();
|
||||
var method = invocation.getMethod();
|
||||
|
||||
if (ReflectionUtils.isObjectMethod(method)) {
|
||||
return invocation.proceed();
|
||||
}
|
||||
|
||||
Accessor accessor = new Accessor(method);
|
||||
var accessor = new Accessor(method);
|
||||
|
||||
if (accessor.isGetter()) {
|
||||
return map.get(accessor.getPropertyName());
|
||||
|
||||
@@ -70,11 +70,11 @@ class ProjectingMethodInterceptor implements MethodInterceptor {
|
||||
public Object invoke(@SuppressWarnings("null") @NonNull MethodInvocation invocation) throws Throwable {
|
||||
|
||||
TypeInformation<?> type = ClassTypeInformation.fromReturnTypeOf(invocation.getMethod());
|
||||
TypeInformation<?> resultType = type;
|
||||
TypeInformation<?> typeToReturn = type;
|
||||
var resultType = type;
|
||||
var typeToReturn = type;
|
||||
|
||||
Object result = delegate.invoke(invocation);
|
||||
boolean applyWrapper = false;
|
||||
var result = delegate.invoke(invocation);
|
||||
var applyWrapper = false;
|
||||
|
||||
if (NullableWrapperConverters.supports(type.getType())
|
||||
&& (result == null || !NullableWrapperConverters.supports(result.getClass()))) {
|
||||
@@ -98,7 +98,7 @@ class ProjectingMethodInterceptor implements MethodInterceptor {
|
||||
return null;
|
||||
}
|
||||
|
||||
Class<?> targetType = type.getType();
|
||||
var targetType = type.getType();
|
||||
|
||||
if (type.isCollectionLike() && !ClassUtils.isPrimitiveArray(targetType)) {
|
||||
return projectCollectionElements(asCollection(result), type);
|
||||
@@ -127,9 +127,9 @@ class ProjectingMethodInterceptor implements MethodInterceptor {
|
||||
*/
|
||||
private Object projectCollectionElements(Collection<?> sources, TypeInformation<?> type) {
|
||||
|
||||
Class<?> rawType = type.getType();
|
||||
TypeInformation<?> componentType = type.getComponentType();
|
||||
Collection<Object> result = CollectionFactory.createCollection(rawType.isArray() ? List.class : rawType,
|
||||
var rawType = type.getType();
|
||||
var componentType = type.getComponentType();
|
||||
var result = CollectionFactory.createCollection(rawType.isArray() ? List.class : rawType,
|
||||
componentType != null ? componentType.getType() : null, sources.size());
|
||||
|
||||
for (Object source : sources) {
|
||||
@@ -153,7 +153,7 @@ class ProjectingMethodInterceptor implements MethodInterceptor {
|
||||
*/
|
||||
private Map<Object, Object> projectMapValues(Map<?, ?> sources, TypeInformation<?> type) {
|
||||
|
||||
Map<Object, Object> result = CollectionFactory.createMap(type.getType(), sources.size());
|
||||
var result = CollectionFactory.createMap(type.getType(), sources.size());
|
||||
|
||||
for (Entry<?, ?> source : sources.entrySet()) {
|
||||
result.put(source.getKey(), getProjection(source.getValue(), type.getRequiredMapValueType().getType()));
|
||||
|
||||
@@ -57,13 +57,13 @@ class PropertyAccessingMethodInterceptor implements MethodInterceptor {
|
||||
@Override
|
||||
public Object invoke(@SuppressWarnings("null") MethodInvocation invocation) throws Throwable {
|
||||
|
||||
Method method = invocation.getMethod();
|
||||
var method = invocation.getMethod();
|
||||
|
||||
if (ReflectionUtils.isObjectMethod(method)) {
|
||||
return invocation.proceed();
|
||||
}
|
||||
|
||||
PropertyDescriptor descriptor = BeanUtils.findPropertyForMethod(method);
|
||||
var descriptor = BeanUtils.findPropertyForMethod(method);
|
||||
|
||||
if (descriptor == null) {
|
||||
throw new IllegalStateException("Invoked method is not a property accessor!");
|
||||
|
||||
@@ -111,7 +111,7 @@ class ProxyProjectionFactory implements ProjectionFactory, BeanClassLoaderAware
|
||||
return (T) source;
|
||||
}
|
||||
|
||||
ProxyFactory factory = new ProxyFactory();
|
||||
var factory = new ProxyFactory();
|
||||
factory.setTarget(source);
|
||||
factory.setOpaque(true);
|
||||
factory.setInterfaces(projectionType, TargetAware.class);
|
||||
@@ -178,7 +178,7 @@ class ProxyProjectionFactory implements ProjectionFactory, BeanClassLoaderAware
|
||||
*/
|
||||
private MethodInterceptor getMethodInterceptor(Object source, Class<?> projectionType) {
|
||||
|
||||
MethodInterceptor propertyInvocationInterceptor = getFactoryFor(source, projectionType)
|
||||
var propertyInvocationInterceptor = getFactoryFor(source, projectionType)
|
||||
.createMethodInterceptor(source, projectionType);
|
||||
|
||||
return new ProjectingMethodInterceptor(this,
|
||||
@@ -194,7 +194,7 @@ class ProxyProjectionFactory implements ProjectionFactory, BeanClassLoaderAware
|
||||
*/
|
||||
private MethodInterceptorFactory getFactoryFor(Object source, Class<?> projectionType) {
|
||||
|
||||
for (MethodInterceptorFactory factory : factories) {
|
||||
for (var factory : factories) {
|
||||
if (factory.supports(source, projectionType)) {
|
||||
return factory;
|
||||
}
|
||||
|
||||
@@ -16,11 +16,11 @@
|
||||
package org.springframework.data.projection;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
@@ -95,7 +95,7 @@ public class SpelAwareProxyProjectionFactory extends ProxyProjectionFactory impl
|
||||
|
||||
Assert.notNull(type, "Type must not be null!");
|
||||
|
||||
AnnotationDetectionMethodCallback<Value> callback = new AnnotationDetectionMethodCallback<>(Value.class);
|
||||
var callback = new AnnotationDetectionMethodCallback<Value>(Value.class);
|
||||
ReflectionUtils.doWithMethods(type, callback);
|
||||
|
||||
return callback.hasFoundAnnotation();
|
||||
@@ -118,7 +118,7 @@ public class SpelAwareProxyProjectionFactory extends ProxyProjectionFactory impl
|
||||
return false;
|
||||
}
|
||||
|
||||
Method readMethod = descriptor.getReadMethod();
|
||||
var readMethod = descriptor.getReadMethod();
|
||||
|
||||
if (readMethod == null) {
|
||||
return false;
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package org.springframework.data.projection;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -28,7 +27,6 @@ import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.expression.BeanFactoryResolver;
|
||||
import org.springframework.context.expression.MapAccessor;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.data.annotation.PersistenceConstructor;
|
||||
import org.springframework.expression.EvaluationContext;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.ParserContext;
|
||||
@@ -78,7 +76,7 @@ class SpelEvaluatingMethodInterceptor implements MethodInterceptor {
|
||||
Assert.notNull(parser, "SpelExpressionParser must not be null!");
|
||||
Assert.notNull(targetInterface, "Target interface must not be null!");
|
||||
|
||||
StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
|
||||
var evaluationContext = new StandardEvaluationContext();
|
||||
|
||||
if (target instanceof Map) {
|
||||
evaluationContext.addPropertyAccessor(new MapAccessor());
|
||||
@@ -109,9 +107,9 @@ class SpelEvaluatingMethodInterceptor implements MethodInterceptor {
|
||||
|
||||
Map<Integer, Expression> expressions = new HashMap<>();
|
||||
|
||||
for (Method method : targetInterface.getMethods()) {
|
||||
for (var method : targetInterface.getMethods()) {
|
||||
|
||||
Value value = AnnotationUtils.findAnnotation(method, Value.class);
|
||||
var value = AnnotationUtils.findAnnotation(method, Value.class);
|
||||
if (value == null) {
|
||||
continue;
|
||||
}
|
||||
@@ -134,7 +132,7 @@ class SpelEvaluatingMethodInterceptor implements MethodInterceptor {
|
||||
@Override
|
||||
public Object invoke(@SuppressWarnings("null") MethodInvocation invocation) throws Throwable {
|
||||
|
||||
Expression expression = expressions.get(invocation.getMethod().hashCode());
|
||||
var expression = expressions.get(invocation.getMethod().hashCode());
|
||||
|
||||
if (expression == null) {
|
||||
return delegate.invoke(invocation);
|
||||
@@ -181,12 +179,10 @@ class SpelEvaluatingMethodInterceptor implements MethodInterceptor {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(o instanceof TargetWrapper)) {
|
||||
if (!(o instanceof TargetWrapper that)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
TargetWrapper that = (TargetWrapper) o;
|
||||
|
||||
if (!ObjectUtils.nullSafeEquals(target, that.target)) {
|
||||
return false;
|
||||
}
|
||||
@@ -200,7 +196,7 @@ class SpelEvaluatingMethodInterceptor implements MethodInterceptor {
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = ObjectUtils.nullSafeHashCode(target);
|
||||
var result = ObjectUtils.nullSafeHashCode(target);
|
||||
result = 31 * result + ObjectUtils.nullSafeHashCode(args);
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user