diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/transformations/listenerList/ListenerListTransformationSupport.kt b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/transformations/listenerList/ListenerListTransformationSupport.kt index 7d01db74bbbe..622a74b8d7b1 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/transformations/listenerList/ListenerListTransformationSupport.kt +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/transformations/listenerList/ListenerListTransformationSupport.kt @@ -15,6 +15,7 @@ */ package org.jetbrains.plugins.groovy.transformations.listenerList +import com.intellij.openapi.util.text.StringUtil import com.intellij.psi.PsiClassType import com.intellij.psi.PsiModifier import com.intellij.psi.PsiType @@ -34,7 +35,8 @@ class ListenerListTransformationSupport : AstTransformationSupport { val annotation = PsiImplUtil.getAnnotation(field, listenerListFqn) ?: continue val listenerType = field.getListenerType() as? PsiClassType ?: continue val listenerClass = listenerType.resolve() ?: continue - val name = GrAnnotationUtil.getString(annotation.findDeclaredDetachedValue("name")) ?: listenerClass.name ?: continue + val declaredName = GrAnnotationUtil.getString(annotation.findDeclaredDetachedValue("name")) + val name = StringUtil.nullize(declaredName) ?: listenerClass.name ?: continue context += context.memberBuilder.method("add${name.capitalize()}") { addModifier(GrModifierFlags.PUBLIC_MASK) returnType = PsiType.VOID diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/listenerList/ListenerListTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/listenerList/ListenerListTest.groovy index 1c63b2ec5c17..964f17a0fcf4 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/listenerList/ListenerListTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/transformations/listenerList/ListenerListTest.groovy @@ -44,7 +44,7 @@ def bean = new Bean() bean.addMyCoolListener {} bean.removeMyCoolListener() {} bean.getMyCoolListeners() -bean.fireSomeCoolStuffHappened(null) +bean.fireSomeCoolStuffHappened(new WowEvent()) ''' enableInspections(GrUnresolvedAccessInspection) checkHighlighting() @@ -56,7 +56,7 @@ class Main { bean.addMyCoolListener(e -> {}); bean.removeMyCoolListener(e -> {}); MyCoolListener[] actionListeners = bean.getMyCoolListeners(); - bean.fireSomeCoolStuffHappened(null); + bean.fireSomeCoolStuffHappened(new WowEvent()); } } ''' @@ -64,6 +64,107 @@ class Main { } } + void 'test with empty custom name'() { + fixture.with { + addFileToProject 'Bean.groovy', '''\ +class WowEvent {} +interface MyCoolListener { + void someCoolStuffHappened(WowEvent e) +} +class Bean { + @groovy.beans.ListenerList(name = "") + List listeners +} +''' + + configureByText '_.groovy', '''\ +def bean = new Bean() +bean.addMyCoolListener {} +bean.removeMyCoolListener() {} +bean.getMyCoolListeners() +bean.fireSomeCoolStuffHappened(new WowEvent()) +''' + enableInspections(GrUnresolvedAccessInspection) + checkHighlighting() + + configureByText 'Main.java', '''\ +class Main { + void foo() { + Bean bean = new Bean(); + bean.addMyCoolListener(e -> {}); + bean.removeMyCoolListener(e -> {}); + MyCoolListener[] actionListeners = bean.getMyCoolListeners(); + bean.fireSomeCoolStuffHappened(new WowEvent()); + } +} +''' + checkHighlighting() + } + } + + void 'test with custom name'() { + fixture.with { + addFileToProject 'Bean.groovy', '''\ +class WowEvent {} +interface MyCoolListener { + void someCoolStuffHappened(WowEvent e) +} +class Bean { + @groovy.beans.ListenerList(name = "awesomeListener") + List listeners +} +''' + + configureByText '_.groovy', '''\ +def bean = new Bean() +bean.addAwesomeListener {} +bean.removeAwesomeListener() {} +bean.getAwesomeListeners() +bean.fireSomeCoolStuffHappened(new WowEvent()) +''' + enableInspections(GrUnresolvedAccessInspection) + checkHighlighting() + + configureByText 'Main.java', '''\ +class Main { + void foo() { + Bean bean = new Bean(); + bean.addAwesomeListener(e -> {}); + bean.removeAwesomeListener(e -> {}); + MyCoolListener[] actionListeners = bean.getAwesomeListeners(); + bean.fireSomeCoolStuffHappened(new WowEvent()); + } +} +''' + checkHighlighting() + } + } + + void 'test with spaces custom name'() { + fixture.with { + addFileToProject 'Bean.groovy', '''\ +class WowEvent {} +interface MyCoolListener { + void someCoolStuffHappened(WowEvent e) +} +class Bean { + @groovy.beans.ListenerList(name = " ") + List listeners +} +''' + + configureByText '_.groovy', '''\ +def bean = new Bean() +bean.'add ' {} +bean.'remove '() {} +bean.'get s'() +bean.fireSomeCoolStuffHappened(new WowEvent()) +''' + enableInspections(GrUnresolvedAccessInspection) + checkHighlighting() + } + } + void 'test errors highlighting'() { fixture.with { configureByText '_.groovy', '''\