GH-1348: navigation for events improved

GH-1461: hierarchy of events taken into account when looking for references from listeners, too

GH-1462: event listener index nodes now not created twice for type and annotation anymore
This commit is contained in:
Martin Lippert
2025-02-04 16:06:32 +01:00
parent 4f675ccebb
commit 0b6b6dff9c
9 changed files with 165 additions and 58 deletions

View File

@@ -131,4 +131,49 @@ public class EventsReferencesProviderTest {
assertTrue(references.contains(expectedLocation1));
}
@Test
public void testEventPublisherFindsAllListenersIncludingThoseFromListenersWithoutAnnotation() throws Exception {
String tempJavaDocUri = directory.toPath().resolve("src/main/java/com/example/events/demo/CustomApplicationEventPublisher.java").toUri().toString();
Editor editor = harness.newEditor(LanguageId.JAVA, """
package com.example.events.demo;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
@Component
public class CustomApplcationEventPublisher {
private ApplicationEventPublisher publisher;
public CustomApplcationEventPublisher(ApplicationEventPublisher publisher) {
this.publisher = publisher;
}
public void foo() {
this.publisher.pub<*>lishEvent(new CustomApplicationEvent(null));
}
}""", tempJavaDocUri);
List<? extends Location> references = editor.getReferences();
assertEquals(3, references.size());
String expectedDefinitionUri1 = directory.toPath().resolve("src/main/java/com/example/events/demo/EventListenerPerInterface.java").toUri().toString();
Location expectedLocation1 = new Location(expectedDefinitionUri1, new Range(new Position(10, 13), new Position(10, 31)));
assertTrue(references.contains(expectedLocation1));
String expectedDefinitionUri2 = directory.toPath().resolve("src/main/java/com/example/events/demo/EventListenerPerAnnotation.java").toUri().toString();
Location expectedLocation2 = new Location(expectedDefinitionUri2, new Range(new Position(10, 13), new Position(10, 24)));
assertTrue(references.contains(expectedLocation2));
String expectedDefinitionUri3 = directory.toPath().resolve("src/main/java/com/example/events/demo/EventListenerPerInterfaceAndBeanMethod.java").toUri().toString();
Location expectedLocation3 = new Location(expectedDefinitionUri3, new Range(new Position(9, 13), new Position(9, 24)));
assertTrue(references.contains(expectedLocation3));
}
}

View File

@@ -12,6 +12,7 @@ package org.springframework.ide.vscode.boot.java.events.test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -133,6 +134,11 @@ public class SpringIndexerEventsTest {
Bean[] beans = springIndex.getBeansOfDocument(docUri);
assertEquals(1, beans.length);
DocumentElement document = springIndex.getDocument(docUri);
List<SpringIndexElement> docChildren = document.getChildren();
assertEquals(1, docChildren.size());
assertTrue(docChildren.get(0) instanceof Bean);
Bean listenerComponentBean = Arrays.stream(beans).filter(bean -> bean.getName().equals("eventListenerPerInterface")).findFirst().get();
assertEquals("com.example.events.demo.EventListenerPerInterface", listenerComponentBean.getType());
@@ -148,6 +154,13 @@ public class SpringIndexerEventsTest {
assertNotNull(location);
assertEquals(docUri, location.getUri());
assertEquals(new Range(new Position(10, 13), new Position(10, 31)), location.getRange());
List<EventListenerIndexElement> doubleCheckEventListenerNodes = springIndex.getNodesOfType(EventListenerIndexElement.class).stream()
.filter(eventListener -> eventListener.getLocation().getUri().equals(docUri))
.toList();
assertEquals(1, doubleCheckEventListenerNodes.size());
assertSame(listenerElement, doubleCheckEventListenerNodes.get(0));
}
@Test
@@ -161,13 +174,13 @@ public class SpringIndexerEventsTest {
List<SpringIndexElement> children = document.getChildren();
EventListenerIndexElement listenerElement = children.stream().filter(element -> element instanceof EventListenerIndexElement).map(element -> (EventListenerIndexElement) element).findFirst().get();
assertEquals("org.springframework.context.ApplicationEvent", listenerElement.getEventType());
assertEquals("com.example.events.demo.CustomApplicationEvent", listenerElement.getEventType());
assertEquals("com.example.events.demo.EventListenerPerInterfaceAndBeanMethod", listenerElement.getContainerBeanType());
Location location = listenerElement.getLocation();
assertNotNull(location);
assertEquals(docUri, location.getUri());
assertEquals(new Range(new Position(8, 13), new Position(8, 31)), location.getRange());
assertEquals(new Range(new Position(7, 13), new Position(7, 31)), location.getRange());
}
@Test

View File

@@ -0,0 +1,13 @@
package com.example.events.demo;
import org.springframework.context.ApplicationEvent;
public class CustomApplicationEvent extends ApplicationEvent {
private static final long serialVersionUID = 1L;
public CustomApplicationEvent(Object source) {
super(source);
}
}

View File

@@ -0,0 +1,19 @@
package com.example.events.demo;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
@Component
public class CustomApplicationEventPublisher {
private ApplicationEventPublisher publisher;
public CustomApplicationEventPublisher(ApplicationEventPublisher publisher) {
this.publisher = publisher;
}
public void foo() {
this.publisher.publishEvent(new CustomApplicationEvent(null));
}
}

View File

@@ -1,12 +1,11 @@
package com.example.events.demo;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
public class EventListenerPerInterfaceAndBeanMethod implements ApplicationListener<ApplicationEvent> {
public class EventListenerPerInterfaceAndBeanMethod implements ApplicationListener<CustomApplicationEvent> {
@Override
public void onApplicationEvent(ApplicationEvent event) {
public void onApplicationEvent(CustomApplicationEvent event) {
System.out.println("Event received via listener implementation and bean method: " + event);
}