Use new Java features (switch expressions, text blocks, new JDK methods)

Closes gh-29747
This commit is contained in:
Krzysztof Krason
2022-12-28 08:59:08 +01:00
committed by Sam Brannen
parent 48abd493fe
commit afb8a0d1b1
53 changed files with 498 additions and 552 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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.
@@ -128,8 +128,7 @@ abstract class AutowireUtils {
* @return the resolved value
*/
public static Object resolveAutowiringValue(Object autowiringValue, Class<?> requiredType) {
if (autowiringValue instanceof ObjectFactory && !requiredType.isInstance(autowiringValue)) {
ObjectFactory<?> factory = (ObjectFactory<?>) autowiringValue;
if (autowiringValue instanceof ObjectFactory<?> factory && !requiredType.isInstance(autowiringValue)) {
if (autowiringValue instanceof Serializable && requiredType.isInterface()) {
autowiringValue = Proxy.newProxyInstance(requiredType.getClassLoader(),
new Class<?>[] {requiredType}, new ObjectFactoryDelegatingInvocationHandler(factory));
@@ -275,22 +274,19 @@ abstract class AutowireUtils {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
switch (method.getName()) {
case "equals":
// Only consider equal when proxies are identical.
return (proxy == args[0]);
case "hashCode":
// Use hashCode of proxy.
return System.identityHashCode(proxy);
case "toString":
return this.objectFactory.toString();
}
try {
return method.invoke(this.objectFactory.getObject(), args);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
return switch (method.getName()) {
case "equals" -> (proxy == args[0]); // Only consider equal when proxies are identical.
case "hashCode" -> System.identityHashCode(proxy); // Use hashCode of proxy.
case "toString" -> this.objectFactory.toString();
default -> {
try {
yield method.invoke(this.objectFactory.getObject(), args);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
};
}
}

View File

@@ -365,8 +365,7 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
for (Map.Entry<String, Object> entry : this.beans.entrySet()) {
String beanName = entry.getKey();
Object beanInstance = entry.getValue();
if (beanInstance instanceof FactoryBean && !isFactoryType) {
FactoryBean<?> factoryBean = (FactoryBean<?>) beanInstance;
if (beanInstance instanceof FactoryBean<?> factoryBean && !isFactoryType) {
Class<?> objectType = factoryBean.getObjectType();
if ((includeNonSingletons || factoryBean.isSingleton()) &&
objectType != null && (type == null || type.isAssignableFrom(objectType))) {
@@ -409,9 +408,8 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
String beanName = entry.getKey();
Object beanInstance = entry.getValue();
// Is bean a FactoryBean?
if (beanInstance instanceof FactoryBean && !isFactoryType) {
if (beanInstance instanceof FactoryBean<?> factory && !isFactoryType) {
// Match object created by FactoryBean.
FactoryBean<?> factory = (FactoryBean<?>) beanInstance;
Class<?> objectType = factory.getObjectType();
if ((includeNonSingletons || factory.isSingleton()) &&
objectType != null && (type == null || type.isAssignableFrom(objectType))) {