18
18
import java .util .stream .Stream ;
19
19
20
20
import org .eclipse .jdt .core .dom .Annotation ;
21
+ import org .eclipse .jdt .core .dom .IMethodBinding ;
21
22
import org .eclipse .jdt .core .dom .ITypeBinding ;
23
+ import org .eclipse .jdt .core .dom .MethodDeclaration ;
22
24
import org .eclipse .jdt .core .dom .TypeDeclaration ;
23
25
import org .eclipse .lsp4j .Location ;
24
26
import org .eclipse .lsp4j .SymbolKind ;
25
27
import org .eclipse .lsp4j .WorkspaceSymbol ;
26
28
import org .eclipse .lsp4j .jsonrpc .messages .Either ;
27
- import org .eclipse .lsp4j .jsonrpc .messages .Tuple ;
28
- import org .eclipse .lsp4j .jsonrpc .messages .Tuple .Two ;
29
29
import org .slf4j .Logger ;
30
30
import org .slf4j .LoggerFactory ;
31
31
import org .springframework .ide .vscode .boot .java .Annotations ;
32
+ import org .springframework .ide .vscode .boot .java .events .EventListenerIndexElement ;
32
33
import org .springframework .ide .vscode .boot .java .handlers .AbstractSymbolProvider ;
33
34
import org .springframework .ide .vscode .boot .java .handlers .EnhancedSymbolInformation ;
34
35
import org .springframework .ide .vscode .boot .java .utils .ASTUtils ;
39
40
import org .springframework .ide .vscode .commons .protocol .spring .Bean ;
40
41
import org .springframework .ide .vscode .commons .protocol .spring .InjectionPoint ;
41
42
import org .springframework .ide .vscode .commons .util .BadLocationException ;
43
+ import org .springframework .ide .vscode .commons .util .text .DocumentRegion ;
42
44
import org .springframework .ide .vscode .commons .util .text .TextDocument ;
43
45
44
46
/**
@@ -53,12 +55,7 @@ public class ComponentSymbolProvider extends AbstractSymbolProvider {
53
55
protected void addSymbolsPass1 (Annotation node , ITypeBinding annotationType , Collection <ITypeBinding > metaAnnotations , SpringIndexerJavaContext context , TextDocument doc ) {
54
56
try {
55
57
if (node != null && node .getParent () != null && node .getParent () instanceof TypeDeclaration ) {
56
- Two <EnhancedSymbolInformation , Bean > result = createSymbol (node , annotationType , metaAnnotations , doc );
57
-
58
- EnhancedSymbolInformation enhancedSymbol = result .getFirst ();
59
- Bean beanDefinition = result .getSecond ();
60
- context .getGeneratedSymbols ().add (new CachedSymbol (context .getDocURI (), context .getLastModified (), enhancedSymbol ));
61
- context .getBeans ().add (new CachedBean (context .getDocURI (), beanDefinition ));
58
+ createSymbol (node , annotationType , metaAnnotations , context , doc );
62
59
}
63
60
else if (Annotations .NAMED_ANNOTATIONS .contains (annotationType .getQualifiedName ())) {
64
61
WorkspaceSymbol symbol = DefaultSymbolProvider .provideDefaultSymbol (node , doc );
@@ -71,7 +68,7 @@ else if (Annotations.NAMED_ANNOTATIONS.contains(annotationType.getQualifiedName(
71
68
}
72
69
}
73
70
74
- protected Tuple . Two < EnhancedSymbolInformation , Bean > createSymbol (Annotation node , ITypeBinding annotationType , Collection <ITypeBinding > metaAnnotations , TextDocument doc ) throws BadLocationException {
71
+ protected void createSymbol (Annotation node , ITypeBinding annotationType , Collection <ITypeBinding > metaAnnotations , SpringIndexerJavaContext context , TextDocument doc ) throws BadLocationException {
75
72
String annotationTypeName = annotationType .getName ();
76
73
77
74
Collection <String > metaAnnotationNames = metaAnnotations .stream ()
@@ -81,7 +78,7 @@ protected Tuple.Two<EnhancedSymbolInformation, Bean> createSymbol(Annotation nod
81
78
TypeDeclaration type = (TypeDeclaration ) node .getParent ();
82
79
83
80
String beanName = BeanUtils .getBeanNameFromComponentAnnotation (node , type );
84
- ITypeBinding beanType = getBeanType ( type );
81
+ ITypeBinding beanType = type . resolveBinding ( );
85
82
86
83
Location location = new Location (doc .getUri (), doc .toRange (node .getStartPosition (), node .getLength ()));
87
84
@@ -107,8 +104,49 @@ protected Tuple.Two<EnhancedSymbolInformation, Bean> createSymbol(Annotation nod
107
104
.toArray (AnnotationMetadata []::new );
108
105
109
106
Bean beanDefinition = new Bean (beanName , beanType .getQualifiedName (), location , injectionPoints , supertypes , annotations , isConfiguration );
107
+
108
+ // event listener - create child element, if necessary
109
+ ITypeBinding inTypeHierarchy = ASTUtils .findInTypeHierarchy (type , doc , beanType , Set .of (Annotations .APPLICATION_LISTENER ));
110
+ if (inTypeHierarchy != null ) {
111
+
112
+ MethodDeclaration handleEventMethod = findHandleEventMethod (type );
113
+ if (handleEventMethod != null ) {
114
+
115
+ IMethodBinding methodBinding = handleEventMethod .resolveBinding ();
116
+ ITypeBinding [] parameterTypes = methodBinding .getParameterTypes ();
117
+ if (parameterTypes != null && parameterTypes .length == 1 ) {
118
+
119
+ ITypeBinding eventType = parameterTypes [0 ];
120
+ String eventTypeFq = eventType .getQualifiedName ();
121
+
122
+ DocumentRegion nodeRegion = ASTUtils .nodeRegion (doc , handleEventMethod .getName ());
123
+ Location handleMethodLocation = new Location (doc .getUri (), nodeRegion .asRange ());
124
+
125
+ Collection <Annotation > annotationsOnHandleEventMethod = ASTUtils .getAnnotations (handleEventMethod );
126
+ AnnotationMetadata [] handleEventMethodAnnotations = ASTUtils .getAnnotationsMetadata (annotationsOnHandleEventMethod , doc );
127
+
128
+ EventListenerIndexElement eventElement = new EventListenerIndexElement (eventTypeFq , handleMethodLocation , handleEventMethodAnnotations );
129
+ beanDefinition .addChild (eventElement );
130
+ }
131
+ }
132
+ }
133
+
134
+ context .getGeneratedSymbols ().add (new CachedSymbol (context .getDocURI (), context .getLastModified (), new EnhancedSymbolInformation (symbol )));
135
+ context .getBeans ().add (new CachedBean (context .getDocURI (), beanDefinition ));
136
+ }
110
137
111
- return Tuple .two (new EnhancedSymbolInformation (symbol ), beanDefinition );
138
+ private MethodDeclaration findHandleEventMethod (TypeDeclaration type ) {
139
+ MethodDeclaration [] methods = type .getMethods ();
140
+
141
+ for (MethodDeclaration method : methods ) {
142
+ IMethodBinding binding = method .resolveBinding ();
143
+ String name = binding .getName ();
144
+
145
+ if (name != null && name .equals ("onApplicationEvent" )) {
146
+ return method ;
147
+ }
148
+ }
149
+ return null ;
112
150
}
113
151
114
152
protected String beanLabel (String searchPrefix , String annotationTypeName , Collection <String > metaAnnotationNames , String beanName , String beanType ) {
@@ -137,9 +175,5 @@ protected String beanLabel(String searchPrefix, String annotationTypeName, Colle
137
175
symbolLabel .append (beanType );
138
176
return symbolLabel .toString ();
139
177
}
140
-
141
- private ITypeBinding getBeanType (TypeDeclaration type ) {
142
- return type .resolveBinding ();
143
- }
144
178
145
179
}
0 commit comments