4
4
import com .intellij .lang .annotation .Annotation ;
5
5
import com .intellij .lang .annotation .AnnotationHolder ;
6
6
import com .intellij .lang .annotation .Annotator ;
7
- import com .intellij .openapi .project .Project ;
8
7
import com .intellij .openapi .util .IconLoader ;
9
8
import com .intellij .openapi .util .NotNullLazyValue ;
10
- import com .intellij .psi .* ;
11
- import com .intellij .psi .search . GlobalSearchScope ;
12
- import com .intellij .psi .search . searches . AnnotatedElementsSearch ;
13
- import com .intellij .psi .util . PsiUtil ;
14
- import com .intellij .util .CommonProcessors ;
9
+ import com .intellij .psi .PsiClass ;
10
+ import com .intellij .psi .PsiElement ;
11
+ import com .intellij .psi .PsiIdentifier ;
12
+ import com .intellij .psi .PsiMethodCallExpression ;
13
+ import com .intellij .psi . util .PsiTypesUtil ;
15
14
import org .axonframework .intellij .ide .plugin .handler .AnnotationTypes ;
16
15
import org .axonframework .intellij .ide .plugin .handler .EventHandler ;
17
16
import org .axonframework .intellij .ide .plugin .handler .HandlerProviderManager ;
20
19
import org .jetbrains .annotations .NotNull ;
21
20
22
21
import javax .swing .*;
23
- import java .util .*;
22
+ import java .util .ArrayList ;
23
+ import java .util .Collection ;
24
+ import java .util .Set ;
25
+ import java .util .TreeSet ;
24
26
25
27
/**
26
28
* This class shows an icon in the gutter when an Axon annotation is found. The icon can be used to navigate to all
@@ -33,114 +35,103 @@ public class AxonGutterAnnotator implements Annotator {
33
35
34
36
@ Override
35
37
public void annotate (@ NotNull final PsiElement element , @ NotNull AnnotationHolder holder ) {
38
+ final PublisherProviderManager publisherManager = PublisherProviderManager .getInstance (element .getProject ());
39
+ final HandlerProviderManager handlerManager = HandlerProviderManager .getInstance (element .getProject ());
36
40
if (element instanceof PsiClass ) {
37
- tryAnnotateEventClass (holder , (PsiClass ) element );
38
- tryAnnotateCommandClass (holder , (PsiClass ) element );
41
+ tryAnnotateEventClass (holder , (PsiClass ) element , publisherManager , handlerManager );
42
+ tryAnnotateCommandClass (holder , (PsiClass ) element , handlerManager );
39
43
}
44
+
40
45
if (!(element instanceof PsiMethodCallExpression || element instanceof PsiIdentifier )) {
41
46
return ;
42
47
}
43
48
44
- final PublisherProviderManager publisherManager = PublisherProviderManager .getInstance (element .getProject ());
45
- final HandlerProviderManager handlerManager = HandlerProviderManager .getInstance (element .getProject ());
46
49
final EventPublisher publisher = publisherManager .resolveEventPublisher (element );
47
50
final EventHandler handler = handlerManager .resolveEventHandler (element .getContext ());
48
51
if (publisher != null ) {
49
- NotNullLazyValue <Collection <? extends PsiElement >> targetResolver = new NotNullLazyValue <Collection <? extends PsiElement >>() {
50
- @ NotNull
51
- @ Override
52
- protected Collection <? extends PsiElement > compute () {
53
- Set <EventHandler > handlers = handlerManager .getRepository ().findHandlers (publisher .getPublishedType ());
54
- Set <PsiEventHandlerWrapper > destinations = new TreeSet <PsiEventHandlerWrapper >();
55
- for (EventHandler eventHandler : handlers ) {
56
- PsiElement elementForAnnotation = eventHandler .getElementForAnnotation ();
57
- if (elementForAnnotation .isValid ()) {
58
- destinations .add (new PsiEventHandlerWrapper (elementForAnnotation , eventHandler ));
59
- }
60
- }
61
- return destinations ;
62
- }
63
- };
52
+ NotNullLazyValue <Collection <? extends PsiElement >> targetResolver =
53
+ createNotNullLazyValueForHandlers (handlerManager .getRepository ().findHandlers (publisher .getPublishedType ()));
64
54
Annotation gutterIconForPublisher = createGutterIconForEventHandlers (element , holder , targetResolver , handlerManager );
65
55
if (targetResolver .getValue ().isEmpty ()) {
66
56
addCreateEventHandlerQuickFixes (publisher , gutterIconForPublisher );
67
57
}
68
58
}
69
59
70
60
if (handler != null ) {
71
- createGutterIconForEventPublishers (handler .getElementForAnnotation (), holder , new NotNullLazyValue <Collection <? extends PsiElement >>() {
72
- @ NotNull
73
- @ Override
74
- protected Collection <? extends PsiElement > compute () {
75
- Collection <EventPublisher > publishers = publisherManager .getRepository ()
76
- .getPublishersFor (handler .getHandledType ());
77
- Collection <PsiElement > publishLocations = new ArrayList <PsiElement >();
78
- for (EventPublisher eventPublisher : publishers ) {
79
- PsiElement psiElement = eventPublisher .getPsiElement ();
80
- if (psiElement .isValid ()) {
81
- publishLocations .add (psiElement );
82
- }
83
- }
84
- return publishLocations ;
85
- }
86
- });
87
- }
88
- }
89
-
90
- private void tryAnnotateCommandClass (AnnotationHolder holder , PsiClass classElement ) {
91
- final Collection <PsiMethod > commandHandlerMethods =
92
- findHandlerMethod (classElement .getProject (), classElement , AnnotationTypes .COMMAND_EVENT_HANDLER .getFullyQualifiedName ());
93
- if (!commandHandlerMethods .isEmpty ()) {
94
- createGutterIconForCommandHandlers (
95
- classElement .getNameIdentifier (),
61
+ Collection <EventPublisher > publishers = publisherManager .getRepository ()
62
+ .getPublishersFor (handler .getHandledType ());
63
+ createGutterIconForEventPublishers (
64
+ handler .getElementForAnnotation (),
96
65
holder ,
97
- constantNotNullLazyValue ( commandHandlerMethods ));
66
+ createNotNullLazyValueForPublishers ( publishers ));
98
67
}
99
68
}
100
69
101
- private void tryAnnotateEventClass (AnnotationHolder holder , PsiClass classElement ) {
102
- List <AnnotationTypes > eventAnnotations = Arrays .asList (
103
- AnnotationTypes .EVENT_HANDLER ,
104
- AnnotationTypes .EVENT_SOURCING_HANDLER ,
105
- AnnotationTypes .SAGA_EVENT_HANDLER );
106
- Collection <PsiMethod > eventHandlerMethods = new ArrayList <PsiMethod >();
107
- for (AnnotationTypes eventAnnotation : eventAnnotations ) {
108
- eventHandlerMethods .addAll (findHandlerMethod (classElement .getProject (), classElement , eventAnnotation .getFullyQualifiedName ()));
109
- }
110
- if (!eventHandlerMethods .isEmpty ()) {
111
- createGutterIconForEventHandlers (
112
- classElement .getNameIdentifier (),
113
- holder ,
114
- constantNotNullLazyValue (eventHandlerMethods ),
115
- HandlerProviderManager .getInstance (classElement .getProject ()));
116
- }
117
- }
118
-
119
- public Collection <PsiMethod > findHandlerMethod (Project project , final PsiClass clazz , String handlerAnnotation ) {
120
- GlobalSearchScope scope = GlobalSearchScope .allScope (project );
121
- PsiClass handlerClass =
122
- JavaPsiFacade .getInstance (project ).findClass (handlerAnnotation , scope );
123
- CommonProcessors .CollectProcessor <PsiMethod > collectProcessor = new CommonProcessors .CollectProcessor <PsiMethod >() {
70
+ private NotNullLazyValue <Collection <? extends PsiElement >> createNotNullLazyValueForPublishers (final Collection <EventPublisher > publishers ) {
71
+ return new NotNullLazyValue <Collection <? extends PsiElement >>() {
72
+ @ NotNull
124
73
@ Override
125
- protected boolean accept (PsiMethod psiMethod ) {
126
- return psiMethod .getParameterList ().getParametersCount () > 0
127
- && clazz .isEquivalentTo (PsiUtil .resolveClassInType (psiMethod .getParameterList ().getParameters ()[0 ].getType ()));
74
+ protected Collection <? extends PsiElement > compute () {
75
+ Collection <PsiElement > publishLocations = new ArrayList <PsiElement >();
76
+ for (EventPublisher eventPublisher : publishers ) {
77
+ PsiElement psiElement = eventPublisher .getPsiElement ();
78
+ if (psiElement .isValid ()) {
79
+ publishLocations .add (psiElement );
80
+ }
81
+ }
82
+ return publishLocations ;
128
83
}
129
84
};
130
- AnnotatedElementsSearch .searchPsiMethods (handlerClass , scope ).forEach (collectProcessor );
131
- return collectProcessor .getResults ();
132
85
}
133
86
134
- private NotNullLazyValue <Collection <? extends PsiElement >> constantNotNullLazyValue (final Collection <? extends PsiElement > psiElements ) {
87
+ private NotNullLazyValue <Collection <? extends PsiElement >> createNotNullLazyValueForHandlers (final Set < EventHandler > handlers ) {
135
88
return new NotNullLazyValue <Collection <? extends PsiElement >>() {
136
89
@ NotNull
137
90
@ Override
138
91
protected Collection <? extends PsiElement > compute () {
139
- return psiElements ;
92
+ Set <PsiEventHandlerWrapper > destinations = new TreeSet <PsiEventHandlerWrapper >();
93
+ for (EventHandler eventHandler : handlers ) {
94
+ PsiElement elementForAnnotation = eventHandler .getElementForAnnotation ();
95
+ if (elementForAnnotation .isValid ()) {
96
+ destinations .add (new PsiEventHandlerWrapper (elementForAnnotation , eventHandler ));
97
+ }
98
+ }
99
+ return destinations ;
140
100
}
141
101
};
142
102
}
143
103
104
+ private void tryAnnotateCommandClass (AnnotationHolder holder , PsiClass classElement , HandlerProviderManager handlerManager ) {
105
+ Set <EventHandler > handlers =
106
+ handlerManager .getRepository ().findCommandHandlers (PsiTypesUtil .getClassType (classElement ));
107
+ if (!handlers .isEmpty ()) {
108
+ createGutterIconForCommandHandlers (
109
+ classElement .getNameIdentifier (),
110
+ holder ,
111
+ createNotNullLazyValueForHandlers (handlers ));
112
+ }
113
+ }
114
+
115
+ private void tryAnnotateEventClass (AnnotationHolder holder , PsiClass classElement ,
116
+ PublisherProviderManager publisherManager , HandlerProviderManager handlerManager ) {
117
+ Set <EventPublisher > publishers = publisherManager .getRepository ().getPublishersFor (PsiTypesUtil .getClassType (classElement ));
118
+ if (!publishers .isEmpty ()) {
119
+ createGutterIconForEventPublishers (
120
+ classElement .getNameIdentifier (),
121
+ holder ,
122
+ createNotNullLazyValueForPublishers (publishers ));
123
+ }
124
+ Set <EventHandler > handlers =
125
+ handlerManager .getRepository ().findEventHandlers (PsiTypesUtil .getClassType (classElement ));
126
+ if (!handlers .isEmpty ()) {
127
+ createGutterIconForEventHandlers (
128
+ classElement .getNameIdentifier (),
129
+ holder ,
130
+ createNotNullLazyValueForHandlers (handlers ),
131
+ handlerManager );
132
+ }
133
+ }
134
+
144
135
private void addCreateEventHandlerQuickFixes (EventPublisher publisher , Annotation gutterIconForPublisher ) {
145
136
gutterIconForPublisher .registerFix (new CreateEventHandlerQuickfix (publisher .getPublishedType (), AnnotationTypes .EVENT_HANDLER ));
146
137
gutterIconForPublisher .registerFix (new CreateEventHandlerQuickfix (publisher .getPublishedType (), AnnotationTypes .EVENT_SOURCING_HANDLER ));
@@ -170,7 +161,8 @@ private static Annotation createGutterIconForCommandHandlers(PsiElement psiEleme
170
161
}
171
162
172
163
private static Annotation createGutterIconForEventHandlers (PsiElement psiElement , AnnotationHolder holder ,
173
- NotNullLazyValue <Collection <? extends PsiElement >> targetResolver , HandlerProviderManager handlerManager ) {
164
+ NotNullLazyValue <Collection <? extends PsiElement >> targetResolver ,
165
+ HandlerProviderManager handlerManager ) {
174
166
return NavigationGutterIconBuilder .create (AxonIconOut )
175
167
.setEmptyPopupText ("No handlers found for this event" )
176
168
.setTargets (targetResolver )
0 commit comments