From 33b5bc2aae7f9dc3b89d4e93d20bf7a1e0e4dd48 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 11 Jun 2019 23:50:29 +0200 Subject: [PATCH] Polishing --- .../support/BeanDefinitionValueResolver.java | 68 +++++++++---------- .../SimpleApplicationEventMulticaster.java | 4 +- .../jmx/access/MBeanClientInterceptor.java | 5 +- .../IntegerToEnumConverterFactory.java | 2 +- .../support/StringToEnumConverterFactory.java | 2 +- .../spel/support/StandardTypeComparator.java | 8 +-- .../connection/CachingConnectionFactory.java | 8 ++- .../server/support/RouterFunctionMapping.java | 17 +++-- 8 files changed, 60 insertions(+), 54 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java index e3e77801050..8abf4cd4f42 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java @@ -281,6 +281,40 @@ class BeanDefinitionValueResolver { return value.resolveTargetType(this.beanFactory.getBeanClassLoader()); } + /** + * Resolve a reference to another bean in the factory. + */ + @Nullable + private Object resolveReference(Object argName, RuntimeBeanReference ref) { + try { + Object bean; + String refName = ref.getBeanName(); + refName = String.valueOf(doEvaluate(refName)); + if (ref.isToParent()) { + if (this.beanFactory.getParentBeanFactory() == null) { + throw new BeanCreationException( + this.beanDefinition.getResourceDescription(), this.beanName, + "Can't resolve reference to bean '" + refName + + "' in parent factory: no parent factory available"); + } + bean = this.beanFactory.getParentBeanFactory().getBean(refName); + } + else { + bean = this.beanFactory.getBean(refName); + this.beanFactory.registerDependentBean(refName, this.beanName); + } + if (bean instanceof NullBean) { + bean = null; + } + return bean; + } + catch (BeansException ex) { + throw new BeanCreationException( + this.beanDefinition.getResourceDescription(), this.beanName, + "Cannot resolve reference to bean '" + ref.getBeanName() + "' while setting " + argName, ex); + } + } + /** * Resolve an inner bean definition. * @param argName the name of the argument that the inner bean is defined for @@ -345,40 +379,6 @@ class BeanDefinitionValueResolver { return actualInnerBeanName; } - /** - * Resolve a reference to another bean in the factory. - */ - @Nullable - private Object resolveReference(Object argName, RuntimeBeanReference ref) { - try { - Object bean; - String refName = ref.getBeanName(); - refName = String.valueOf(doEvaluate(refName)); - if (ref.isToParent()) { - if (this.beanFactory.getParentBeanFactory() == null) { - throw new BeanCreationException( - this.beanDefinition.getResourceDescription(), this.beanName, - "Can't resolve reference to bean '" + refName + - "' in parent factory: no parent factory available"); - } - bean = this.beanFactory.getParentBeanFactory().getBean(refName); - } - else { - bean = this.beanFactory.getBean(refName); - this.beanFactory.registerDependentBean(refName, this.beanName); - } - if (bean instanceof NullBean) { - bean = null; - } - return bean; - } - catch (BeansException ex) { - throw new BeanCreationException( - this.beanDefinition.getResourceDescription(), this.beanName, - "Cannot resolve reference to bean '" + ref.getBeanName() + "' while setting " + argName, ex); - } - } - /** * For each element in the managed array, resolve reference if necessary. */ diff --git a/spring-context/src/main/java/org/springframework/context/event/SimpleApplicationEventMulticaster.java b/spring-context/src/main/java/org/springframework/context/event/SimpleApplicationEventMulticaster.java index 34ae5372d45..cb70ac4ea0c 100644 --- a/spring-context/src/main/java/org/springframework/context/event/SimpleApplicationEventMulticaster.java +++ b/spring-context/src/main/java/org/springframework/context/event/SimpleApplicationEventMulticaster.java @@ -130,8 +130,8 @@ public class SimpleApplicationEventMulticaster extends AbstractApplicationEventM @Override public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) { ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event)); - for (final ApplicationListener listener : getApplicationListeners(event, type)) { - Executor executor = getTaskExecutor(); + Executor executor = getTaskExecutor(); + for (ApplicationListener listener : getApplicationListeners(event, type)) { if (executor != null) { executor.execute(() -> invokeListener(listener, event)); } diff --git a/spring-context/src/main/java/org/springframework/jmx/access/MBeanClientInterceptor.java b/spring-context/src/main/java/org/springframework/jmx/access/MBeanClientInterceptor.java index c76a8bbe017..b789e8c8c00 100644 --- a/spring-context/src/main/java/org/springframework/jmx/access/MBeanClientInterceptor.java +++ b/spring-context/src/main/java/org/springframework/jmx/access/MBeanClientInterceptor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -658,6 +658,9 @@ public class MBeanClientInterceptor if (this == other) { return true; } + if (!(other instanceof MethodCacheKey)) { + return false; + } MethodCacheKey otherKey = (MethodCacheKey) other; return (this.name.equals(otherKey.name) && Arrays.equals(this.parameterTypes, otherKey.parameterTypes)); } diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/IntegerToEnumConverterFactory.java b/spring-core/src/main/java/org/springframework/core/convert/support/IntegerToEnumConverterFactory.java index e05bfe34c59..4322d8ebb32 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/IntegerToEnumConverterFactory.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/IntegerToEnumConverterFactory.java @@ -35,7 +35,7 @@ final class IntegerToEnumConverterFactory implements ConverterFactory implements Converter { + private static class IntegerToEnum implements Converter { private final Class enumType; diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java b/spring-core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java index e79f62dccd1..6a0c3ba395d 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java @@ -35,7 +35,7 @@ final class StringToEnumConverterFactory implements ConverterFactory implements Converter { + private static class StringToEnum implements Converter { private final Class enumType; diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeComparator.java b/spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeComparator.java index e2e6d11c7f9..8e48aa878c8 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeComparator.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeComparator.java @@ -26,8 +26,8 @@ import org.springframework.lang.Nullable; import org.springframework.util.NumberUtils; /** - * A simple basic {@link TypeComparator} implementation. - * It supports comparison of Numbers and types implementing Comparable. + * A basic {@link TypeComparator} implementation: supports comparison of + * {@link Number} types as well as types implementing {@link Comparable}. * * @author Andy Clement * @author Juergen Hoeller @@ -89,10 +89,10 @@ public class StandardTypeComparator implements TypeComparator { return Integer.compare(leftNumber.intValue(), rightNumber.intValue()); } else if (leftNumber instanceof Short || rightNumber instanceof Short) { - return leftNumber.shortValue() - rightNumber.shortValue(); + return Short.compare(leftNumber.shortValue(), rightNumber.shortValue()); } else if (leftNumber instanceof Byte || rightNumber instanceof Byte) { - return leftNumber.byteValue() - rightNumber.byteValue(); + return Byte.compare(leftNumber.byteValue(), rightNumber.byteValue()); } else { // Unknown Number subtypes -> best guess is double multiplication diff --git a/spring-jms/src/main/java/org/springframework/jms/connection/CachingConnectionFactory.java b/spring-jms/src/main/java/org/springframework/jms/connection/CachingConnectionFactory.java index edd19982c84..c3c1b9f39e3 100644 --- a/spring-jms/src/main/java/org/springframework/jms/connection/CachingConnectionFactory.java +++ b/spring-jms/src/main/java/org/springframework/jms/connection/CachingConnectionFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -522,7 +522,8 @@ public class CachingConnectionFactory extends SingleConnectionFactory { public boolean equals(Object other) { // Effectively checking object equality as well as toString equality. // On WebSphere MQ, Destination objects do not implement equals... - return (this == other || destinationEquals((DestinationCacheKey) other)); + return (this == other || (other instanceof DestinationCacheKey && + destinationEquals((DestinationCacheKey) other))); } @Override @@ -577,6 +578,9 @@ public class CachingConnectionFactory extends SingleConnectionFactory { if (this == other) { return true; } + if (!(other instanceof ConsumerCacheKey)) { + return false; + } ConsumerCacheKey otherKey = (ConsumerCacheKey) other; return (destinationEquals(otherKey) && ObjectUtils.nullSafeEquals(this.selector, otherKey.selector) && diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/support/RouterFunctionMapping.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/support/RouterFunctionMapping.java index bc52a4da685..33baed77493 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/support/RouterFunctionMapping.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/support/RouterFunctionMapping.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -38,6 +38,7 @@ import org.springframework.web.util.pattern.PathPattern; /** * {@code HandlerMapping} implementation that supports {@link RouterFunction RouterFunctions}. + * *

If no {@link RouterFunction} is provided at * {@linkplain #RouterFunctionMapping(RouterFunction) construction time}, this mapping * will detect all router functions in the application context, and consult them in @@ -56,8 +57,8 @@ public class RouterFunctionMapping extends AbstractHandlerMapping implements Ini /** * Create an empty {@code RouterFunctionMapping}. - *

If this constructor is used, this mapping will detect all {@link RouterFunction} instances - * available in the application context. + *

If this constructor is used, this mapping will detect all + * {@link RouterFunction} instances available in the application context. */ public RouterFunctionMapping() { } @@ -154,20 +155,18 @@ public class RouterFunctionMapping extends AbstractHandlerMapping implements Ini } @SuppressWarnings("unchecked") - private void setAttributes(Map attributes, ServerRequest serverRequest, - HandlerFunction handlerFunction) { + private void setAttributes( + Map attributes, ServerRequest serverRequest, HandlerFunction handlerFunction) { attributes.put(RouterFunctions.REQUEST_ATTRIBUTE, serverRequest); attributes.put(BEST_MATCHING_HANDLER_ATTRIBUTE, handlerFunction); - PathPattern matchingPattern = - (PathPattern) attributes.get(RouterFunctions.MATCHING_PATTERN_ATTRIBUTE); + PathPattern matchingPattern = (PathPattern) attributes.get(RouterFunctions.MATCHING_PATTERN_ATTRIBUTE); if (matchingPattern != null) { attributes.put(BEST_MATCHING_PATTERN_ATTRIBUTE, matchingPattern); } Map uriVariables = - (Map) attributes - .get(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE); + (Map) attributes.get(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE); if (uriVariables != null) { attributes.put(URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriVariables); }