Skip to content

Commit 1e4e8fe

Browse files
committed
Support for custom event handlers (not yet recursive)
1 parent 2380414 commit 1e4e8fe

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

src/main/java/org/axonframework/intellij/ide/plugin/handler/DefaultEventHandlerProvider.java

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package org.axonframework.intellij.ide.plugin.handler;
22

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+
39
import com.intellij.openapi.project.Project;
410
import com.intellij.psi.JavaPsiFacade;
511
import com.intellij.psi.PsiAnnotation;
@@ -8,36 +14,37 @@
814
import com.intellij.psi.PsiMethod;
915
import com.intellij.psi.PsiReference;
1016
import com.intellij.psi.search.GlobalSearchScope;
11-
import com.intellij.psi.search.searches.AnnotatedElementsSearch;
1217
import com.intellij.psi.search.searches.ReferencesSearch;
1318
import com.intellij.psi.util.PsiTreeUtil;
1419
import com.intellij.util.Processor;
1520
import com.intellij.util.Query;
21+
import com.intellij.util.containers.HashMap;
1622

1723
class DefaultEventHandlerProvider implements HandlerProvider {
1824
@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) {
2026
for (final AnnotationTypes annotationType : AnnotationTypes.values()) {
2127
final PsiClass eventHandlerAnnotation = JavaPsiFacade.getInstance(project)
2228
.findClass(annotationType.getFullyQualifiedName(),
2329
GlobalSearchScope.allScope(project));
2430

2531
if (eventHandlerAnnotation != null) {
26-
final Query<PsiClass> customEventHandlers = AnnotatedElementsSearch.searchPsiClasses(eventHandlerAnnotation, scope);
32+
final Query<PsiClass> customEventHandlers = searchPsiClasses(eventHandlerAnnotation, scope);
2733
customEventHandlers.forEach(new Processor<PsiClass>() {
2834
@Override
2935
public boolean process(final PsiClass psiClass) {
30-
registerEventHandlerAnnotation(scope, registrar, eventHandlerAnnotation);
36+
registerEventHandlerAnnotation(scope, registrar, eventHandlerAnnotation, project);
3137
return true;
3238
}
3339
});
3440
}
3541

36-
registerEventHandlerAnnotation(scope, registrar, eventHandlerAnnotation);
42+
registerEventHandlerAnnotation(scope, registrar, eventHandlerAnnotation, project);
3743
}
3844
}
3945

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) {
4148
if (eventHandlerAnnotation != null) {
4249
Query<PsiReference> annotationUsages = ReferencesSearch.search(eventHandlerAnnotation, scope);
4350
annotationUsages.forEachAsync(new Processor<PsiReference>() {
@@ -47,7 +54,7 @@ public boolean process(PsiReference psiReference) {
4754
new IsMethodCondition());
4855
if (method != null) {
4956
// this doesn't say the method is annotated
50-
final PsiAnnotation annotation = locateAnnotation(method);
57+
final PsiAnnotation annotation = locateAnnotation(method, project);
5158
if (annotation != null) {
5259
Handler handler = createHandlerBasedOnAnnotation(method, annotation);
5360
if (handler != null) {
@@ -62,10 +69,10 @@ public boolean process(PsiReference psiReference) {
6269
}
6370

6471
@Override
65-
public Handler resolve(PsiElement element) {
72+
public Handler resolve(PsiElement element, final Project project) {
6673
if (element instanceof PsiMethod) {
6774
PsiMethod methodElement = (PsiMethod) element;
68-
final PsiAnnotation annotation = locateAnnotation(methodElement);
75+
final PsiAnnotation annotation = locateAnnotation(methodElement, project);
6976
if (annotation != null) {
7077
return createHandlerBasedOnAnnotation(methodElement, annotation);
7178
}
@@ -81,13 +88,38 @@ private Handler createHandlerBasedOnAnnotation(PsiMethod method, PsiAnnotation a
8188
}
8289
}
8390

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);
8795
if (annotation != null) {
8896
return annotation;
8997
}
9098
}
9199
return null;
92100
}
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+
}
93125
}

src/main/java/org/axonframework/intellij/ide/plugin/handler/HandlerProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface HandlerProvider {
88

99
void scanHandlers(Project project, GlobalSearchScope scope, Registrar registrar);
1010

11-
Handler resolve(PsiElement element);
11+
Handler resolve(PsiElement element, final Project project);
1212

1313
interface Registrar {
1414

src/main/java/org/axonframework/intellij/ide/plugin/handler/HandlerProviderManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public HandlerRepository getRepository() {
4848

4949
public Handler resolveEventHandler(PsiElement psiElement) {
5050
for (HandlerProvider eventHandlerProvider : eventHandlerProviders) {
51-
Handler handler = eventHandlerProvider.resolve(psiElement);
51+
Handler handler = eventHandlerProvider.resolve(psiElement, project);
5252
if (handler != null && psiElement.isValid()) {
5353
repository.registerHandler(handler);
5454
return handler;

0 commit comments

Comments
 (0)