From a085541d26dc141fa349738b4dc0d15274e61f2f Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 28 Aug 2018 13:39:14 -0700 Subject: [PATCH] Polish --- .../condition/BeanTypeRegistry.java | 85 ++++++++++--------- .../condition/ConditionalOnBeanTests.java | 1 - .../classloader/RestartClassLoaderTests.java | 4 +- .../loader/jar/ZipInflaterInputStream.java | 9 +- ...dertowEmbeddedServletContainerFactory.java | 4 +- .../analyzer/NoSuchMethodFailureAnalyzer.java | 42 +++++---- .../support/SpringBootServletInitializer.java | 1 - ...yEmbeddedServletContainerFactoryTests.java | 6 +- .../NoSuchMethodFailureAnalyzerTests.java | 4 +- 9 files changed, 81 insertions(+), 75 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/BeanTypeRegistry.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/BeanTypeRegistry.java index fdb6819bd4..cc3aabafcc 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/BeanTypeRegistry.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/BeanTypeRegistry.java @@ -149,6 +149,40 @@ final class BeanTypeRegistry implements SmartInitializingSingleton { this.beanDefinitions.clear(); } + private RootBeanDefinition getBeanDefinition(String name) { + try { + return (RootBeanDefinition) this.beanFactory.getMergedBeanDefinition(name); + } + catch (BeanDefinitionStoreException ex) { + logIgnoredError("unresolvable metadata in bean definition", name, ex); + return null; + } + } + + private void logIgnoredError(String message, String name, Exception ex) { + if (BeanTypeRegistry.logger.isDebugEnabled()) { + BeanTypeRegistry.logger.debug("Ignoring " + message + " '" + name + "'", ex); + } + } + + private boolean requiresEagerInit(String factoryBeanName) { + return (factoryBeanName != null && this.beanFactory.isFactoryBean(factoryBeanName) + && !this.beanFactory.containsSingleton(factoryBeanName)); + } + + private void updateTypesIfNecessary() { + Iterator names = this.beanFactory.getBeanNamesIterator(); + while (names.hasNext()) { + String name = names.next(); + if (!this.beanTypes.containsKey(name)) { + addBeanType(name); + } + else { + updateBeanType(name); + } + } + } + private void addBeanType(String name) { if (this.beanFactory.containsSingleton(name)) { this.beanTypes.put(name, this.beanFactory.getType(name)); @@ -165,13 +199,17 @@ final class BeanTypeRegistry implements SmartInitializingSingleton { } } - private RootBeanDefinition getBeanDefinition(String name) { - try { - return (RootBeanDefinition) this.beanFactory.getMergedBeanDefinition(name); + private void updateBeanType(String name) { + if (this.beanFactory.isAlias(name) || this.beanFactory.containsSingleton(name)) { + return; } - catch (BeanDefinitionStoreException ex) { - logIgnoredError("unresolvable metadata in bean definition", name, ex); - return null; + RootBeanDefinition beanDefinition = getBeanDefinition(name); + if (beanDefinition == null) { + return; + } + RootBeanDefinition previous = this.beanDefinitions.put(name, beanDefinition); + if (previous != null && !beanDefinition.equals(previous)) { + addBeanTypeForNonAliasDefinition(name, beanDefinition); } } @@ -200,41 +238,6 @@ final class BeanTypeRegistry implements SmartInitializingSingleton { } } - private void logIgnoredError(String message, String name, Exception ex) { - if (BeanTypeRegistry.logger.isDebugEnabled()) { - BeanTypeRegistry.logger.debug("Ignoring " + message + " '" + name + "'", ex); - } - } - - private boolean requiresEagerInit(String factoryBeanName) { - return (factoryBeanName != null && this.beanFactory.isFactoryBean(factoryBeanName) - && !this.beanFactory.containsSingleton(factoryBeanName)); - } - - private void updateTypesIfNecessary() { - Iterator names = this.beanFactory.getBeanNamesIterator(); - while (names.hasNext()) { - String name = names.next(); - if (!this.beanTypes.containsKey(name)) { - addBeanType(name); - } - else { - if (!this.beanFactory.isAlias(name) - && !this.beanFactory.containsSingleton(name)) { - RootBeanDefinition beanDefinition = getBeanDefinition(name); - if (beanDefinition != null) { - RootBeanDefinition existingDefinition = this.beanDefinitions - .put(name, beanDefinition); - if (existingDefinition != null - && !beanDefinition.equals(existingDefinition)) { - addBeanTypeForNonAliasDefinition(name, beanDefinition); - } - } - } - } - } - } - /** * Attempt to guess the type that a {@link FactoryBean} will return based on the * generics in its method signature. diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnBeanTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnBeanTests.java index e54f3ef2ce..adbc09aa46 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnBeanTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnBeanTests.java @@ -347,7 +347,6 @@ public class ConditionalOnBeanTests { public static class ConsumingConfiguration { ConsumingConfiguration(String testBean) { - } } diff --git a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/classloader/RestartClassLoaderTests.java b/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/classloader/RestartClassLoaderTests.java index 8f06263182..69288ee97f 100644 --- a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/classloader/RestartClassLoaderTests.java +++ b/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/classloader/RestartClassLoaderTests.java @@ -214,8 +214,8 @@ public class RestartClassLoaderTests { } private List toList(Enumeration enumeration) { - return ((enumeration != null) ? Collections.list(enumeration) - : Collections.emptyList()); + return (enumeration != null) ? Collections.list(enumeration) + : Collections.emptyList(); } } diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/ZipInflaterInputStream.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/ZipInflaterInputStream.java index cdc6974d49..79a59d11e1 100644 --- a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/ZipInflaterInputStream.java +++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/ZipInflaterInputStream.java @@ -30,19 +30,16 @@ import java.util.zip.InflaterInputStream; */ class ZipInflaterInputStream extends InflaterInputStream { - private final Inflater inflater; + private int available; private boolean extraBytesWritten; - private int available; - ZipInflaterInputStream(InputStream inputStream, int size) { this(inputStream, new Inflater(true), size); } private ZipInflaterInputStream(InputStream inputStream, Inflater inflater, int size) { - super(inputStream, inflater, getInflaterBufferSize(size)); - this.inflater = inflater; + super(inputStream, new Inflater(true), getInflaterBufferSize(size)); this.available = size; } @@ -66,7 +63,7 @@ class ZipInflaterInputStream extends InflaterInputStream { @Override public void close() throws IOException { super.close(); - this.inflater.end(); + this.inf.end(); } @Override diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java index 0edfca82e7..126cf7b4e1 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java @@ -450,8 +450,8 @@ public class UndertowEmbeddedServletContainerFactory private AccessLogHandler createAccessLogHandler(HttpHandler handler, AccessLogReceiver accessLogReceiver) { createAccessLogDirectoryIfNecessary(); - String formatString = ((this.accessLogPattern != null) ? this.accessLogPattern - : "common"); + String formatString = (this.accessLogPattern != null) ? this.accessLogPattern + : "common"; return new AccessLogHandler(handler, accessLogReceiver, formatString, Undertow.class.getClassLoader()); } diff --git a/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/NoSuchMethodFailureAnalyzer.java b/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/NoSuchMethodFailureAnalyzer.java index 3ab1d65511..34d4c8f1a1 100644 --- a/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/NoSuchMethodFailureAnalyzer.java +++ b/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/NoSuchMethodFailureAnalyzer.java @@ -48,24 +48,8 @@ class NoSuchMethodFailureAnalyzer extends AbstractFailureAnalyzer candidates, URL actual) { + StringWriter description = new StringWriter(); + PrintWriter writer = new PrintWriter(description); + writer.print("An attempt was made to call the method "); + writer.print(cause.getMessage()); + writer.print(" but it does not exist. Its class, "); + writer.print(className); + writer.println(", is available from the following locations:"); + writer.println(); + for (URL candidate : candidates) { + writer.print(" "); + writer.println(candidate); + } + writer.println(); + writer.println("It was loaded from the following location:"); + writer.println(); + writer.print(" "); + writer.println(actual); + return description.toString(); + } + } diff --git a/spring-boot/src/main/java/org/springframework/boot/web/support/SpringBootServletInitializer.java b/spring-boot/src/main/java/org/springframework/boot/web/support/SpringBootServletInitializer.java index f4c2287d89..4d6348a3fe 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/support/SpringBootServletInitializer.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/support/SpringBootServletInitializer.java @@ -195,7 +195,6 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit ((ConfigurableWebEnvironment) environment) .initPropertySources(this.servletContext, null); } - } @Override diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactoryTests.java index 6268d87409..4737cb9660 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactoryTests.java @@ -383,14 +383,14 @@ public class JettyEmbeddedServletContainerFactoryTests contexts.iterator().next().addEventListener(new ServletContextListener() { @Override - public void contextInitialized(ServletContextEvent sce) { + public void contextInitialized(ServletContextEvent event) { throw new RuntimeException(); } @Override - public void contextDestroyed(ServletContextEvent sce) { - + public void contextDestroyed(ServletContextEvent event) { } + }); } diff --git a/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/NoSuchMethodFailureAnalyzerTests.java b/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/NoSuchMethodFailureAnalyzerTests.java index cf687a9745..2f158bed85 100644 --- a/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/NoSuchMethodFailureAnalyzerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/NoSuchMethodFailureAnalyzerTests.java @@ -30,7 +30,9 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; /** - * @author awilkinson + * Tests for {@link NoSuchMethodFailureAnalyzer}. + * + * @author Andy Wilkinson */ @RunWith(ModifiedClassPathRunner.class) @ClassPathOverrides("javax.servlet:servlet-api:2.5")