Reintroduce explicit local variable types.

We now reinstate local variable types instead of var as general var usage removes contextual detail that cannot be omitted generally.

See #2465
This commit is contained in:
Mark Paluch
2022-03-28 08:55:29 +02:00
parent c85c4ef2ed
commit 0e5e869cbf
249 changed files with 1662 additions and 1507 deletions

View File

@@ -44,7 +44,7 @@ public final class Accessor {
Assert.notNull(method, "Method must not be null!");
var descriptor = BeanUtils.findPropertyForMethod(method);
PropertyDescriptor descriptor = BeanUtils.findPropertyForMethod(method);
if (descriptor == null) {
throw new IllegalArgumentException(String.format("Invoked method %s is no accessor method!", method));

View File

@@ -55,9 +55,9 @@ public class DefaultMethodInvokingMethodInterceptor implements MethodInterceptor
*/
public static boolean hasDefaultMethods(Class<?> interfaceClass) {
var methods = ReflectionUtils.getAllDeclaredMethods(interfaceClass);
Method[] methods = ReflectionUtils.getAllDeclaredMethods(interfaceClass);
for (var method : methods) {
for (Method method : methods) {
if (method.isDefault()) {
return true;
}
@@ -70,21 +70,21 @@ public class DefaultMethodInvokingMethodInterceptor implements MethodInterceptor
@Override
public Object invoke(@SuppressWarnings("null") MethodInvocation invocation) throws Throwable {
var method = invocation.getMethod();
Method method = invocation.getMethod();
if (!method.isDefault()) {
return invocation.proceed();
}
var arguments = invocation.getArguments();
var proxy = ((ProxyMethodInvocation) invocation).getProxy();
Object[] arguments = invocation.getArguments();
Object proxy = ((ProxyMethodInvocation) invocation).getProxy();
return getMethodHandle(method).bindTo(proxy).invokeWithArguments(arguments);
}
private MethodHandle getMethodHandle(Method method) throws Exception {
var handle = methodHandleCache.get(method);
MethodHandle handle = methodHandleCache.get(method);
if (handle == null) {
@@ -127,7 +127,7 @@ public class DefaultMethodInvokingMethodInterceptor implements MethodInterceptor
private Lookup getLookup(Class<?> declaringClass, Method privateLookupIn) {
var lookup = MethodHandles.lookup();
Lookup lookup = MethodHandles.lookup();
try {
return (Lookup) privateLookupIn.invoke(MethodHandles.class, declaringClass, lookup);
@@ -152,7 +152,7 @@ public class DefaultMethodInvokingMethodInterceptor implements MethodInterceptor
throw new IllegalStateException("Could not obtain MethodHandles.lookup constructor!");
}
var constructor = this.constructor.get();
Constructor<Lookup> constructor = this.constructor.get();
return constructor.newInstance(method.getDeclaringClass()).unreflectSpecial(method, method.getDeclaringClass());
}
@@ -184,7 +184,7 @@ public class DefaultMethodInvokingMethodInterceptor implements MethodInterceptor
private static MethodHandle doLookup(Method method, Lookup lookup)
throws NoSuchMethodException, IllegalAccessException {
var methodType = MethodType.methodType(method.getReturnType(), method.getParameterTypes());
MethodType methodType = MethodType.methodType(method.getReturnType(), method.getParameterTypes());
if (Modifier.isStatic(method.getModifiers())) {
return lookup.findStatic(method.getDeclaringClass(), method.getName(), methodType);
@@ -215,7 +215,7 @@ public class DefaultMethodInvokingMethodInterceptor implements MethodInterceptor
*/
public static MethodHandleLookup getMethodHandleLookup() {
for (var it : MethodHandleLookup.values()) {
for (MethodHandleLookup it : MethodHandleLookup.values()) {
if (it.isAvailable()) {
return it;
@@ -230,7 +230,7 @@ public class DefaultMethodInvokingMethodInterceptor implements MethodInterceptor
try {
var constructor = Lookup.class.getDeclaredConstructor(Class.class);
Constructor<Lookup> constructor = Lookup.class.getDeclaredConstructor(Class.class);
ReflectionUtils.makeAccessible(constructor);
return constructor;

View File

@@ -17,6 +17,7 @@ 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;
@@ -33,6 +34,7 @@ import org.springframework.beans.BeanUtils;
import org.springframework.core.log.LogMessage;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.MethodMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
import org.springframework.data.util.StreamUtils;
import org.springframework.util.Assert;
@@ -103,7 +105,7 @@ class DefaultProjectionInformation implements ProjectionInformation {
*/
private static boolean hasDefaultGetter(PropertyDescriptor descriptor) {
var method = descriptor.getReadMethod();
Method method = descriptor.getReadMethod();
return method != null && method.isDefault();
}
@@ -153,13 +155,13 @@ class DefaultProjectionInformation implements ProjectionInformation {
*/
private Stream<PropertyDescriptor> collectDescriptors() {
var allButDefaultGetters = Arrays.stream(BeanUtils.getPropertyDescriptors(type)) //
Stream<PropertyDescriptor> allButDefaultGetters = Arrays.stream(BeanUtils.getPropertyDescriptors(type)) //
.filter(it -> !hasDefaultGetter(it));
var ownDescriptors = metadata.map(it -> filterAndOrder(allButDefaultGetters, it))
Stream<PropertyDescriptor> ownDescriptors = metadata.map(it -> filterAndOrder(allButDefaultGetters, it))
.orElse(allButDefaultGetters);
var superTypeDescriptors = metadata.map(this::fromMetadata) //
Stream<PropertyDescriptor> superTypeDescriptors = metadata.map(this::fromMetadata) //
.orElseGet(this::fromType) //
.flatMap(it -> new PropertyDescriptorSource(it).collectDescriptors());
@@ -177,7 +179,7 @@ class DefaultProjectionInformation implements ProjectionInformation {
private static Stream<PropertyDescriptor> filterAndOrder(Stream<PropertyDescriptor> source,
AnnotationMetadata metadata) {
var orderedMethods = getMethodOrder(metadata);
Map<String, Integer> orderedMethods = getMethodOrder(metadata);
if (orderedMethods.isEmpty()) {
return source;
@@ -218,8 +220,8 @@ class DefaultProjectionInformation implements ProjectionInformation {
try {
var factory = new SimpleMetadataReaderFactory(type.getClassLoader());
var metadataReader = factory.getMetadataReader(ClassUtils.getQualifiedName(type));
SimpleMetadataReaderFactory factory = new SimpleMetadataReaderFactory(type.getClassLoader());
MetadataReader metadataReader = factory.getMetadataReader(ClassUtils.getQualifiedName(type));
return Optional.of(metadataReader.getAnnotationMetadata());
@@ -254,7 +256,7 @@ class DefaultProjectionInformation implements ProjectionInformation {
*/
private static Map<String, Integer> getMethodOrder(AnnotationMetadata metadata) {
var methods = metadata.getDeclaredMethods() //
List<String> methods = metadata.getDeclaredMethods() //
.stream() //
.map(MethodMetadata::getMethodName) //
.distinct() //

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.projection;
import java.lang.reflect.Method;
import java.util.Map;
import org.aopalliance.intercept.MethodInterceptor;
@@ -45,13 +46,13 @@ class MapAccessingMethodInterceptor implements MethodInterceptor {
@Override
public Object invoke(@SuppressWarnings("null") MethodInvocation invocation) throws Throwable {
var method = invocation.getMethod();
Method method = invocation.getMethod();
if (ReflectionUtils.isObjectMethod(method)) {
return invocation.proceed();
}
var accessor = new Accessor(method);
Accessor accessor = new Accessor(method);
if (accessor.isGetter()) {
return map.get(accessor.getPropertyName());

View File

@@ -66,11 +66,11 @@ class ProjectingMethodInterceptor implements MethodInterceptor {
public Object invoke(@SuppressWarnings("null") @NonNull MethodInvocation invocation) throws Throwable {
TypeInformation<?> type = ClassTypeInformation.fromReturnTypeOf(invocation.getMethod());
var resultType = type;
var typeToReturn = type;
TypeInformation<?> resultType = type;
TypeInformation<?> typeToReturn = type;
var result = delegate.invoke(invocation);
var applyWrapper = false;
Object result = delegate.invoke(invocation);
boolean applyWrapper = false;
if (NullableWrapperConverters.supports(type.getType())
&& (result == null || !NullableWrapperConverters.supports(result.getClass()))) {
@@ -94,7 +94,7 @@ class ProjectingMethodInterceptor implements MethodInterceptor {
return null;
}
var targetType = type.getType();
Class<?> targetType = type.getType();
if (type.isCollectionLike() && !ClassUtils.isPrimitiveArray(targetType)) {
return projectCollectionElements(asCollection(result), type);
@@ -123,9 +123,9 @@ class ProjectingMethodInterceptor implements MethodInterceptor {
*/
private Object projectCollectionElements(Collection<?> sources, TypeInformation<?> type) {
var rawType = type.getType();
var componentType = type.getComponentType();
var result = CollectionFactory.createCollection(rawType.isArray() ? List.class : rawType,
Class<?> rawType = type.getType();
TypeInformation<?> componentType = type.getComponentType();
Collection<Object> result = CollectionFactory.createCollection(rawType.isArray() ? List.class : rawType,
componentType != null ? componentType.getType() : null, sources.size());
for (Object source : sources) {
@@ -149,7 +149,7 @@ class ProjectingMethodInterceptor implements MethodInterceptor {
*/
private Map<Object, Object> projectMapValues(Map<?, ?> sources, TypeInformation<?> type) {
var result = CollectionFactory.createMap(type.getType(), sources.size());
Map<Object, Object> result = CollectionFactory.createMap(type.getType(), sources.size());
for (Entry<?, ?> source : sources.entrySet()) {
result.put(source.getKey(), getProjection(source.getValue(), type.getRequiredMapValueType().getType()));

View File

@@ -53,13 +53,13 @@ class PropertyAccessingMethodInterceptor implements MethodInterceptor {
@Override
public Object invoke(@SuppressWarnings("null") MethodInvocation invocation) throws Throwable {
var method = invocation.getMethod();
Method method = invocation.getMethod();
if (ReflectionUtils.isObjectMethod(method)) {
return invocation.proceed();
}
var descriptor = BeanUtils.findPropertyForMethod(method);
PropertyDescriptor descriptor = BeanUtils.findPropertyForMethod(method);
if (descriptor == null) {
throw new IllegalStateException("Invoked method is not a property accessor!");

View File

@@ -103,7 +103,7 @@ class ProxyProjectionFactory implements ProjectionFactory, BeanClassLoaderAware
return (T) source;
}
var factory = new ProxyFactory();
ProxyFactory factory = new ProxyFactory();
factory.setTarget(source);
factory.setOpaque(true);
factory.setInterfaces(projectionType, TargetAware.class);
@@ -162,7 +162,7 @@ class ProxyProjectionFactory implements ProjectionFactory, BeanClassLoaderAware
*/
private MethodInterceptor getMethodInterceptor(Object source, Class<?> projectionType) {
var propertyInvocationInterceptor = getFactoryFor(source, projectionType)
MethodInterceptor propertyInvocationInterceptor = getFactoryFor(source, projectionType)
.createMethodInterceptor(source, projectionType);
return new ProjectingMethodInterceptor(this,
@@ -178,7 +178,7 @@ class ProxyProjectionFactory implements ProjectionFactory, BeanClassLoaderAware
*/
private MethodInterceptorFactory getFactoryFor(Object source, Class<?> projectionType) {
for (var factory : factories) {
for (MethodInterceptorFactory factory : factories) {
if (factory.supports(source, projectionType)) {
return factory;
}

View File

@@ -16,6 +16,7 @@
package org.springframework.data.projection;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -87,7 +88,7 @@ public class SpelAwareProxyProjectionFactory extends ProxyProjectionFactory impl
Assert.notNull(type, "Type must not be null!");
var callback = new AnnotationDetectionMethodCallback<Value>(Value.class);
AnnotationDetectionMethodCallback<Value> callback = new AnnotationDetectionMethodCallback<Value>(Value.class);
ReflectionUtils.doWithMethods(type, callback);
return callback.hasFoundAnnotation();
@@ -106,7 +107,7 @@ public class SpelAwareProxyProjectionFactory extends ProxyProjectionFactory impl
return false;
}
var readMethod = descriptor.getReadMethod();
Method readMethod = descriptor.getReadMethod();
if (readMethod == null) {
return false;

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.projection;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -76,7 +77,7 @@ class SpelEvaluatingMethodInterceptor implements MethodInterceptor {
Assert.notNull(parser, "SpelExpressionParser must not be null!");
Assert.notNull(targetInterface, "Target interface must not be null!");
var evaluationContext = new StandardEvaluationContext();
StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
if (target instanceof Map) {
evaluationContext.addPropertyAccessor(new MapAccessor());
@@ -107,9 +108,9 @@ class SpelEvaluatingMethodInterceptor implements MethodInterceptor {
Map<Integer, Expression> expressions = new HashMap<>();
for (var method : targetInterface.getMethods()) {
for (Method method : targetInterface.getMethods()) {
var value = AnnotationUtils.findAnnotation(method, Value.class);
Value value = AnnotationUtils.findAnnotation(method, Value.class);
if (value == null) {
continue;
}
@@ -128,7 +129,7 @@ class SpelEvaluatingMethodInterceptor implements MethodInterceptor {
@Override
public Object invoke(@SuppressWarnings("null") MethodInvocation invocation) throws Throwable {
var expression = expressions.get(invocation.getMethod().hashCode());
Expression expression = expressions.get(invocation.getMethod().hashCode());
if (expression == null) {
return delegate.invoke(invocation);
@@ -184,7 +185,7 @@ class SpelEvaluatingMethodInterceptor implements MethodInterceptor {
@Override
public int hashCode() {
var result = ObjectUtils.nullSafeHashCode(target);
int result = ObjectUtils.nullSafeHashCode(target);
result = 31 * result + ObjectUtils.nullSafeHashCode(args);
return result;
}