From e4c57a9b613dddee69adce53ce672aa0e6783023 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 13 Nov 2019 16:53:23 +0100 Subject: [PATCH] Minor internal refinements (backported from master) --- .../AbstractAutowireCapableBeanFactory.java | 19 +++++--------- ...ReloadableResourceBundleMessageSource.java | 26 ++++++++++++++----- .../AnnotationDrivenEventListenerTests.java | 9 ++++--- .../handler/EventSourceTransportHandler.java | 10 +++---- .../handler/HtmlFileTransportHandler.java | 4 +-- .../handler/WebSocketTransportHandler.java | 4 +-- .../handler/XhrPollingTransportHandler.java | 4 +-- .../handler/XhrStreamingTransportHandler.java | 9 +++---- 8 files changed, 46 insertions(+), 39 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java index c11bce1865..20383c0e77 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java @@ -645,7 +645,6 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac @Nullable protected Class predictBeanType(String beanName, RootBeanDefinition mbd, Class... typesToMatch) { Class targetType = determineTargetType(beanName, mbd, typesToMatch); - // Apply SmartInstantiationAwareBeanPostProcessors to predict the // eventual type after a before-instantiation shortcut. if (targetType != null && !mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { @@ -1364,34 +1363,28 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the // state of the bean before properties are set. This can be used, for example, // to support styles of field injection. - boolean continueWithPropertyPopulation = true; - if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) { - continueWithPropertyPopulation = false; - break; + return; } } } } - if (!continueWithPropertyPopulation) { - return; - } - PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null); - if (mbd.getResolvedAutowireMode() == AUTOWIRE_BY_NAME || mbd.getResolvedAutowireMode() == AUTOWIRE_BY_TYPE) { + int resolvedAutowireMode = mbd.getResolvedAutowireMode(); + if (resolvedAutowireMode == AUTOWIRE_BY_NAME || resolvedAutowireMode == AUTOWIRE_BY_TYPE) { MutablePropertyValues newPvs = new MutablePropertyValues(pvs); // Add property values based on autowire by name if applicable. - if (mbd.getResolvedAutowireMode() == AUTOWIRE_BY_NAME) { + if (resolvedAutowireMode == AUTOWIRE_BY_NAME) { autowireByName(beanName, mbd, bw, newPvs); } // Add property values based on autowire by type if applicable. - if (mbd.getResolvedAutowireMode() == AUTOWIRE_BY_TYPE) { + if (resolvedAutowireMode == AUTOWIRE_BY_TYPE) { autowireByType(beanName, mbd, bw, newPvs); } pvs = newPvs; @@ -1495,7 +1488,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac if (Object.class != pd.getPropertyType()) { MethodParameter methodParam = BeanUtils.getWriteMethodParameter(pd); // Do not allow eager init for type matching in case of a prioritized post-processor. - boolean eager = !PriorityOrdered.class.isInstance(bw.getWrappedInstance()); + boolean eager = !(bw.getWrappedInstance() instanceof PriorityOrdered); DependencyDescriptor desc = new AutowireByTypeDependencyDescriptor(methodParam, eager); Object autowiredArgument = resolveDependency(desc, beanName, autowiredBeanNames, converter); if (autowiredArgument != null) { diff --git a/spring-context/src/main/java/org/springframework/context/support/ReloadableResourceBundleMessageSource.java b/spring-context/src/main/java/org/springframework/context/support/ReloadableResourceBundleMessageSource.java index d65869b14f..5ef83f004b 100644 --- a/spring-context/src/main/java/org/springframework/context/support/ReloadableResourceBundleMessageSource.java +++ b/spring-context/src/main/java/org/springframework/context/support/ReloadableResourceBundleMessageSource.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. @@ -237,6 +237,7 @@ public class ReloadableResourceBundleMessageSource extends AbstractResourceBased if (mergedHolder != null) { return mergedHolder; } + Properties mergedProps = newProperties(); long latestTimestamp = -1; String[] basenames = StringUtils.toStringArray(getBasenameSet()); @@ -253,6 +254,7 @@ public class ReloadableResourceBundleMessageSource extends AbstractResourceBased } } } + mergedHolder = new PropertiesHolder(mergedProps, latestTimestamp); PropertiesHolder existing = this.cachedMergedProperties.putIfAbsent(locale, mergedHolder); if (existing != null) { @@ -279,18 +281,28 @@ public class ReloadableResourceBundleMessageSource extends AbstractResourceBased return filenames; } } + + // Filenames for given Locale List filenames = new ArrayList<>(7); filenames.addAll(calculateFilenamesForLocale(basename, locale)); - if (isFallbackToSystemLocale() && !locale.equals(Locale.getDefault())) { - List fallbackFilenames = calculateFilenamesForLocale(basename, Locale.getDefault()); - for (String fallbackFilename : fallbackFilenames) { - if (!filenames.contains(fallbackFilename)) { - // Entry for fallback locale that isn't already in filenames list. - filenames.add(fallbackFilename); + + // Filenames for default Locale, if any + if (isFallbackToSystemLocale()) { + Locale defaultLocale = Locale.getDefault(); + if (!locale.equals(defaultLocale)) { + List fallbackFilenames = calculateFilenamesForLocale(basename, defaultLocale); + for (String fallbackFilename : fallbackFilenames) { + if (!filenames.contains(fallbackFilename)) { + // Entry for fallback locale that isn't already in filenames list. + filenames.add(fallbackFilename); + } } } } + + // Filename for default bundle file filenames.add(basename); + if (localeMap == null) { localeMap = new ConcurrentHashMap<>(); Map> existing = this.cachedFilenames.putIfAbsent(basename, localeMap); diff --git a/spring-context/src/test/java/org/springframework/context/event/AnnotationDrivenEventListenerTests.java b/spring-context/src/test/java/org/springframework/context/event/AnnotationDrivenEventListenerTests.java index edb82e00a3..631e135a47 100644 --- a/spring-context/src/test/java/org/springframework/context/event/AnnotationDrivenEventListenerTests.java +++ b/spring-context/src/test/java/org/springframework/context/event/AnnotationDrivenEventListenerTests.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. @@ -794,6 +794,7 @@ public class AnnotationDrivenEventListenerTests { @EventListener @Async + @Override public void handleAsync(AnotherTestEvent event) { assertTrue(!Thread.currentThread().getName().equals(event.content)); this.eventCollector.addEvent(this, event); @@ -820,6 +821,7 @@ public class AnnotationDrivenEventListenerTests { @EventListener @Async + @Override public void handleAsync(AnotherTestEvent event) { assertTrue(!Thread.currentThread().getName().equals(event.content)); this.eventCollector.addEvent(this, event); @@ -902,7 +904,6 @@ public class AnnotationDrivenEventListenerTests { } - @EventListener @Retention(RetentionPolicy.RUNTIME) public @interface ConditionalEvent { @@ -934,18 +935,20 @@ public class AnnotationDrivenEventListenerTests { super.handle(event); } - @Override @EventListener(condition = "#payload.startsWith('OK')") + @Override public void handleString(String payload) { super.handleString(payload); } @ConditionalEvent("#root.event.timestamp > #p0") + @Override public void handleTimestamp(Long timestamp) { collectEvent(timestamp); } @ConditionalEvent("@conditionEvaluator.valid(#p0)") + @Override public void handleRatio(Double ratio) { collectEvent(ratio); } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/EventSourceTransportHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/EventSourceTransportHandler.java index 4cc8da5d2e..90b56c378c 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/EventSourceTransportHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/EventSourceTransportHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 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. @@ -30,7 +30,7 @@ import org.springframework.web.socket.sockjs.transport.TransportType; import org.springframework.web.socket.sockjs.transport.session.StreamingSockJsSession; /** - * A TransportHandler for sending messages via Server-Sent events: + * A TransportHandler for sending messages via Server-Sent Events: * https://dev.w3.org/html5/eventsource/. * * @author Rossen Stoyanchev @@ -50,7 +50,7 @@ public class EventSourceTransportHandler extends AbstractHttpSendingTransportHan @Override public boolean checkSessionType(SockJsSession session) { - return session instanceof EventSourceStreamingSockJsSession; + return (session instanceof EventSourceStreamingSockJsSession); } @Override @@ -66,7 +66,7 @@ public class EventSourceTransportHandler extends AbstractHttpSendingTransportHan } - private class EventSourceStreamingSockJsSession extends StreamingSockJsSession { + private static class EventSourceStreamingSockJsSession extends StreamingSockJsSession { public EventSourceStreamingSockJsSession(String sessionId, SockJsServiceConfig config, WebSocketHandler wsHandler, Map attributes) { @@ -76,7 +76,7 @@ public class EventSourceTransportHandler extends AbstractHttpSendingTransportHan @Override protected byte[] getPrelude(ServerHttpRequest request) { - return new byte[] { '\r', '\n' }; + return new byte[] {'\r', '\n'}; } } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/HtmlFileTransportHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/HtmlFileTransportHandler.java index 6473af39ca..d39f51bbf9 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/HtmlFileTransportHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/HtmlFileTransportHandler.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. @@ -93,7 +93,7 @@ public class HtmlFileTransportHandler extends AbstractHttpSendingTransportHandle @Override public boolean checkSessionType(SockJsSession session) { - return session instanceof HtmlFileStreamingSockJsSession; + return (session instanceof HtmlFileStreamingSockJsSession); } @Override diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/WebSocketTransportHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/WebSocketTransportHandler.java index e4d67b0217..60423319dc 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/WebSocketTransportHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/WebSocketTransportHandler.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. @@ -107,7 +107,7 @@ public class WebSocketTransportHandler extends AbstractTransportHandler @Override public boolean checkSessionType(SockJsSession session) { - return session instanceof WebSocketServerSockJsSession; + return (session instanceof WebSocketServerSockJsSession); } @Override diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/XhrPollingTransportHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/XhrPollingTransportHandler.java index 5e98adc360..95a065801f 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/XhrPollingTransportHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/XhrPollingTransportHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 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. @@ -54,7 +54,7 @@ public class XhrPollingTransportHandler extends AbstractHttpSendingTransportHand @Override public boolean checkSessionType(SockJsSession session) { - return session instanceof PollingSockJsSession; + return (session instanceof PollingSockJsSession); } @Override diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/XhrStreamingTransportHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/XhrStreamingTransportHandler.java index 07f32132b9..f947643205 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/XhrStreamingTransportHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/XhrStreamingTransportHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 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. @@ -17,6 +17,7 @@ package org.springframework.web.socket.sockjs.transport.handler; import java.nio.charset.StandardCharsets; +import java.util.Arrays; import java.util.Map; import org.springframework.http.MediaType; @@ -41,9 +42,7 @@ public class XhrStreamingTransportHandler extends AbstractHttpSendingTransportHa private static final byte[] PRELUDE = new byte[2049]; static { - for (int i = 0; i < 2048; i++) { - PRELUDE[i] = 'h'; - } + Arrays.fill(PRELUDE, (byte) 'h'); PRELUDE[2048] = '\n'; } @@ -60,7 +59,7 @@ public class XhrStreamingTransportHandler extends AbstractHttpSendingTransportHa @Override public boolean checkSessionType(SockJsSession session) { - return session instanceof XhrStreamingSockJsSession; + return (session instanceof XhrStreamingSockJsSession); } @Override