1
1
package org .axonframework .intellij .ide .plugin .handler ;
2
2
3
+ import static com .intellij .psi .search .GlobalSearchScope .projectScope ;
4
+ import static com .intellij .psi .search .searches .AnnotatedElementsSearch .searchPsiClasses ;
5
+
6
+ import java .util .Map ;
7
+
8
+
3
9
import com .intellij .openapi .project .Project ;
4
10
import com .intellij .psi .JavaPsiFacade ;
5
11
import com .intellij .psi .PsiAnnotation ;
8
14
import com .intellij .psi .PsiMethod ;
9
15
import com .intellij .psi .PsiReference ;
10
16
import com .intellij .psi .search .GlobalSearchScope ;
11
- import com .intellij .psi .search .searches .AnnotatedElementsSearch ;
12
17
import com .intellij .psi .search .searches .ReferencesSearch ;
13
18
import com .intellij .psi .util .PsiTreeUtil ;
14
19
import com .intellij .util .Processor ;
15
20
import com .intellij .util .Query ;
21
+ import com .intellij .util .containers .HashMap ;
16
22
17
23
class DefaultEventHandlerProvider implements HandlerProvider {
18
24
@ Override
19
- public void scanHandlers (Project project , final GlobalSearchScope scope , final Registrar registrar ) {
25
+ public void scanHandlers (final Project project , final GlobalSearchScope scope , final Registrar registrar ) {
20
26
for (final AnnotationTypes annotationType : AnnotationTypes .values ()) {
21
27
final PsiClass eventHandlerAnnotation = JavaPsiFacade .getInstance (project )
22
28
.findClass (annotationType .getFullyQualifiedName (),
23
29
GlobalSearchScope .allScope (project ));
24
30
25
31
if (eventHandlerAnnotation != null ) {
26
- final Query <PsiClass > customEventHandlers = AnnotatedElementsSearch . searchPsiClasses (eventHandlerAnnotation , scope );
32
+ final Query <PsiClass > customEventHandlers = searchPsiClasses (eventHandlerAnnotation , scope );
27
33
customEventHandlers .forEach (new Processor <PsiClass >() {
28
34
@ Override
29
35
public boolean process (final PsiClass psiClass ) {
30
- registerEventHandlerAnnotation (scope , registrar , eventHandlerAnnotation );
36
+ registerEventHandlerAnnotation (scope , registrar , eventHandlerAnnotation , project );
31
37
return true ;
32
38
}
33
39
});
34
40
}
35
41
36
- registerEventHandlerAnnotation (scope , registrar , eventHandlerAnnotation );
42
+ registerEventHandlerAnnotation (scope , registrar , eventHandlerAnnotation , project );
37
43
}
38
44
}
39
45
40
- private void registerEventHandlerAnnotation (final GlobalSearchScope scope , final Registrar registrar , final PsiClass eventHandlerAnnotation ) {
46
+ private void registerEventHandlerAnnotation (final GlobalSearchScope scope , final Registrar registrar ,
47
+ final PsiClass eventHandlerAnnotation , final Project project ) {
41
48
if (eventHandlerAnnotation != null ) {
42
49
Query <PsiReference > annotationUsages = ReferencesSearch .search (eventHandlerAnnotation , scope );
43
50
annotationUsages .forEachAsync (new Processor <PsiReference >() {
@@ -47,7 +54,7 @@ public boolean process(PsiReference psiReference) {
47
54
new IsMethodCondition ());
48
55
if (method != null ) {
49
56
// this doesn't say the method is annotated
50
- final PsiAnnotation annotation = locateAnnotation (method );
57
+ final PsiAnnotation annotation = locateAnnotation (method , project );
51
58
if (annotation != null ) {
52
59
Handler handler = createHandlerBasedOnAnnotation (method , annotation );
53
60
if (handler != null ) {
@@ -62,10 +69,10 @@ public boolean process(PsiReference psiReference) {
62
69
}
63
70
64
71
@ Override
65
- public Handler resolve (PsiElement element ) {
72
+ public Handler resolve (PsiElement element , final Project project ) {
66
73
if (element instanceof PsiMethod ) {
67
74
PsiMethod methodElement = (PsiMethod ) element ;
68
- final PsiAnnotation annotation = locateAnnotation (methodElement );
75
+ final PsiAnnotation annotation = locateAnnotation (methodElement , project );
69
76
if (annotation != null ) {
70
77
return createHandlerBasedOnAnnotation (methodElement , annotation );
71
78
}
@@ -81,13 +88,38 @@ private Handler createHandlerBasedOnAnnotation(PsiMethod method, PsiAnnotation a
81
88
}
82
89
}
83
90
84
- private PsiAnnotation locateAnnotation (PsiMethod element ) {
85
- for (AnnotationTypes annotationType : AnnotationTypes .values ()) {
86
- PsiAnnotation annotation = element .getModifierList ().findAnnotation (annotationType .getFullyQualifiedName ());
91
+ private PsiAnnotation locateAnnotation (PsiMethod element , final Project project ) {
92
+ final Map <String , String > fullyQualifiedNames = getFullyQualifiedNames (project );
93
+ for (String fullyQualifiedName : fullyQualifiedNames .keySet ()) {
94
+ PsiAnnotation annotation = element .getModifierList ().findAnnotation (fullyQualifiedName );
87
95
if (annotation != null ) {
88
96
return annotation ;
89
97
}
90
98
}
91
99
return null ;
92
100
}
101
+
102
+ private Map <String , String > getFullyQualifiedNames (final Project project ) {
103
+ final HashMap <String , String > annotations = new HashMap <String , String >();
104
+
105
+ for (AnnotationTypes annotationType : AnnotationTypes .values ()) {
106
+ annotations .put (annotationType .getFullyQualifiedName (), annotationType .getAnnotation ());
107
+
108
+ final PsiClass eventHandlerAnnotation = JavaPsiFacade .getInstance (project )
109
+ .findClass (annotationType .getFullyQualifiedName (),
110
+ GlobalSearchScope .allScope (project ));
111
+
112
+ if (eventHandlerAnnotation != null ) {
113
+ final Query <PsiClass > customEventHandlers = searchPsiClasses (eventHandlerAnnotation , projectScope (project ));
114
+ customEventHandlers .forEach (new Processor <PsiClass >() {
115
+ @ Override
116
+ public boolean process (final PsiClass psiClass ) {
117
+ annotations .put (psiClass .getQualifiedName (), psiClass .getName ());
118
+ return true ;
119
+ }
120
+ });
121
+ }
122
+ }
123
+ return annotations ;
124
+ }
93
125
}
0 commit comments