[groovy] @ListenerList: fix empty custom name processing, add tests

This commit is contained in:
Daniil Ovchinnikov
2016-11-07 21:11:37 +03:00
parent c81f48ae21
commit 040979f5b1
2 changed files with 106 additions and 3 deletions
@@ -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
@@ -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<MyCoolListener> 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<MyCoolListener> 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<MyCoolListener> 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', '''\