From c4ec6aea6825011fd0f400d58b3c1fdf5d4fe002 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 11 Nov 2019 17:18:52 +0100 Subject: [PATCH] Fix Checkstyle violations See gh-23784 --- .../ApplicationListenerMethodAdapter.java | 4 +- .../AnnotationDrivenEventListenerTests.java | 48 +++++++++---------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java b/spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java index 9cef8e78d4..9b6a0619a1 100644 --- a/spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java +++ b/spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java @@ -295,9 +295,11 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe @Nullable protected Object doInvoke(Object... args) { Object bean = getTargetBean(); + // Detect package-protected NullBean instance through equals(null) check if (bean.equals(null)) { - return null; + return null; } + ReflectionUtils.makeAccessible(this.method); try { return this.method.invoke(bean, args); 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 fbe6a67d2e..1462146b9b 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 @@ -16,8 +16,6 @@ package org.springframework.context.event; -import java.io.Closeable; -import java.io.IOException; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -627,12 +625,13 @@ public class AnnotationDrivenEventListenerTests { } @Test - public void missingBeanDoesntCrash() { - load(MissingEventListener.class); - context.getBean(UseMissingEventListener.class); - context.getBean(ApplicationEventMulticaster.class).multicastEvent(new TestEvent(this)); + public void missingListenerBeanIgnored() { + load(MissingEventListener.class); + context.getBean(UseMissingEventListener.class); + context.getBean(ApplicationEventMulticaster.class).multicastEvent(new TestEvent(this)); } + private void load(Class... classes) { List> allClasses = new ArrayList<>(); allClasses.add(BasicConfiguration.class); @@ -1087,31 +1086,32 @@ public class AnnotationDrivenEventListenerTests { } @Configuration - @Import({UseMissingEventListener.class}) + @Import(UseMissingEventListener.class) public static class MissingEventListener { - @Bean - public MyEventListener missing() { - return null; - } + + @Bean + public MyEventListener missing() { + return null; + } } @Component - public static class MyEventListener implements Closeable { - @EventListener - public void hear(TestEvent e) { - throw new AssertionError(); - } + public static class MyEventListener { - @Override - public void close() throws IOException {} + @EventListener + public void hear(TestEvent e) { + throw new AssertionError(); + } } public static class UseMissingEventListener { - @Inject - public UseMissingEventListener(Optional notHere) { - if (notHere.isPresent()) { - throw new AssertionError(); - } - } + + @Inject + public UseMissingEventListener(Optional notHere) { + if (notHere.isPresent()) { + throw new AssertionError(); + } + } } + }