@@ -7,7 +7,6 @@ package org.jetbrains.kotlin.light.classes.symbol.base
7
7
8
8
import com.intellij.openapi.project.Project
9
9
import com.intellij.psi.*
10
- import junit.framework.TestCase
11
10
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfigurator
12
11
import org.jetbrains.kotlin.asJava.LightClassTestCommon
13
12
import org.jetbrains.kotlin.light.classes.symbol.modifierLists.SymbolLightClassModifierList
@@ -30,93 +29,101 @@ abstract class AbstractSymbolLightClassesParentingTest(
30
29
val ktFile = ktFiles.first()
31
30
val lightClass = findLightClass(fqName, ktFile.project)
32
31
33
- lightClass?.accept(createLightAnnotationVisitor (testServices.assertions))
32
+ lightClass?.accept(createLightElementsVisitor (testServices.assertions))
34
33
}
35
34
36
35
override fun getRenderResult (ktFile : KtFile , ktFiles : List <KtFile >, testDataFile : Path , module : TestModule , project : Project ): String {
37
36
throw IllegalStateException (" This test is not rendering light elements" )
38
37
}
39
38
40
- private fun createLightAnnotationVisitor (assertions : AssertionsService ) = object : JavaElementVisitor () {
41
- private val declarationStack = ArrayDeque <PsiModifierListOwner >()
39
+ private fun createLightElementsVisitor (assertions : AssertionsService ) = object : JavaElementVisitor () {
40
+ private val declarationStack = ArrayDeque <PsiElement >()
42
41
43
- override fun visitClass (aClass : PsiClass ? ) {
44
- if (aClass == null ) return
45
- checkDeclarationParent(aClass)
46
- declarationStack.addLast(aClass)
42
+ private fun <T : PsiElement > checkParentAndVisitChildren (declaration : T ? , action : T .(visitor: JavaElementVisitor ) -> Unit = {}) {
43
+ if (declaration == null ) return
44
+ checkDeclarationParent(declaration)
47
45
48
- aClass.annotations.forEach { it.accept(this ) }
46
+ declarationStack.addLast(declaration)
47
+ try {
48
+ if (declaration is PsiModifierListOwner ) {
49
+ declaration.modifierList?.accept(this )
50
+ }
49
51
50
- aClass.fields.forEach { it.accept( this ) }
51
- aClass.methods.forEach { it. accept(this ) }
52
- aClass.innerClasses.forEach { it.accept( this ) }
52
+ if (declaration is PsiParameterListOwner ) {
53
+ declaration.parameterList. accept(this )
54
+ }
53
55
54
- aClass.typeParameterList?.typeParameters?.forEach { it.accept(this ) }
56
+ if (declaration is PsiTypeParameterListOwner ) {
57
+ declaration.typeParameterList?.accept(this )
58
+ }
55
59
56
- declarationStack.removeLast()
60
+ declaration.action(this )
61
+ } finally {
62
+ val removed = declarationStack.removeLast()
63
+ assertions.assertEquals(declaration, removed)
64
+ }
57
65
}
58
66
59
- override fun visitField (field : PsiField ? ) {
60
- if (field == null ) return
61
- checkDeclarationParent(field)
62
- declarationStack.addLast(field)
63
-
64
- field.annotations.forEach { it.accept(this ) }
65
-
66
- field.type.annotations.forEach { it.accept(this ) }
67
+ override fun visitModifierList (list : PsiModifierList ? ) {
68
+ checkParentAndVisitChildren(list) { visitor ->
69
+ annotations.forEach { it.accept(visitor) }
70
+ }
71
+ }
67
72
68
- declarationStack.removeLast()
73
+ override fun visitParameterList (list : PsiParameterList ? ) {
74
+ checkParentAndVisitChildren(list) { visitor ->
75
+ parameters.forEach { it.accept(visitor) }
76
+ }
69
77
}
70
78
71
- override fun visitMethod (method : PsiMethod ? ) {
72
- if (method == null ) return
73
- checkDeclarationParent(method)
74
- declarationStack.addLast(method)
79
+ override fun visitTypeParameterList (list : PsiTypeParameterList ? ) {
80
+ checkParentAndVisitChildren(list) { visitor ->
81
+ typeParameters.forEach { it.accept(visitor) }
82
+ }
83
+ }
75
84
76
- method.annotations.forEach { it.accept(this ) }
85
+ override fun visitClass (aClass : PsiClass ? ) {
86
+ checkParentAndVisitChildren(aClass) { visitor ->
87
+ annotations.forEach { it.accept(visitor) }
77
88
78
- method.returnType?.annotations?.forEach { it.accept(this ) }
79
- method.parameterList.parameters.forEach { it.accept(this ) }
89
+ fields.forEach { it.accept(visitor) }
90
+ methods.forEach { it.accept(visitor) }
91
+ innerClasses.forEach { it.accept(visitor) }
92
+ }
93
+ }
80
94
81
- method.typeParameterList?.typeParameters?.forEach { it.accept(this ) }
95
+ override fun visitField (field : PsiField ? ) {
96
+ checkParentAndVisitChildren(field) { visitor ->
97
+ annotations.forEach { it.accept(visitor) }
82
98
83
- declarationStack.removeLast()
99
+ type.annotations.forEach { it.accept(visitor) }
100
+ }
84
101
}
85
102
86
- override fun visitParameter (parameter : PsiParameter ? ) {
87
- if (parameter == null ) return
88
- checkDeclarationParent(parameter)
89
- declarationStack.addLast(parameter)
103
+ override fun visitMethod (method : PsiMethod ? ) {
104
+ checkParentAndVisitChildren(method) { visitor ->
105
+ annotations.forEach { it.accept(visitor) }
90
106
91
- parameter.annotations.forEach { it.accept(this ) }
107
+ returnType?.annotations?.forEach { it.accept(visitor) }
108
+ }
109
+ }
92
110
93
- declarationStack.removeLast()
111
+ override fun visitParameter (parameter : PsiParameter ? ) {
112
+ checkParentAndVisitChildren(parameter) { visitor ->
113
+ annotations.forEach { it.accept(visitor) }
114
+ }
94
115
}
95
116
96
117
override fun visitTypeParameter (classParameter : PsiTypeParameter ? ) {
97
- if (classParameter == null ) return
98
- checkDeclarationParent(classParameter)
99
- declarationStack.addLast(classParameter)
100
-
101
- classParameter.annotations.forEach { it.accept(this ) }
102
-
103
- declarationStack.removeLast()
118
+ checkParentAndVisitChildren(classParameter) { visitor ->
119
+ annotations.forEach { it.accept(visitor) }
120
+ }
104
121
}
105
122
106
123
private fun checkDeclarationParent (declaration : PsiElement ) {
107
124
val expectedParent = declarationStack.lastOrNull() ? : return
108
- val parent = when (declaration) {
109
- is PsiParameter -> {
110
- val parameterList = declaration.parent as PsiParameterList
111
- parameterList.parent
112
- }
113
- is PsiTypeParameter -> {
114
- val parameterList = declaration.parent as PsiTypeParameterList
115
- parameterList.parent
116
- }
117
- else -> declaration.parent
118
- }
119
- assertions.assertNotNull(parent) { " Parent should not be null for ${declaration::class } with text ${declaration.text} " }
125
+ val parent = declaration.parent
126
+ assertions.assertNotNull(parent) { " Parent should not be null for ${declaration::class } with text ${declaration.text} " }
120
127
assertions.assertEquals(expectedParent, parent) {
121
128
" Unexpected parent for ${declaration::class } with text ${declaration.text} "
122
129
}
@@ -126,17 +133,24 @@ abstract class AbstractSymbolLightClassesParentingTest(
126
133
if (annotation == null ) return
127
134
128
135
val owner = annotation.owner
129
- TestCase .assertNotNull(owner)
136
+ assertions.assertNotNull(owner)
137
+
130
138
val lastDeclaration = declarationStack.last()
131
- TestCase .assertEquals(lastDeclaration.modifierList, owner)
132
- when (lastDeclaration) {
139
+ val psiModifierListOwner = if (lastDeclaration is PsiModifierListOwner ) {
140
+ assertions.assertEquals(lastDeclaration.modifierList, owner)
141
+ lastDeclaration
142
+ } else {
143
+ (lastDeclaration as PsiModifierList ).parent
144
+ }
145
+
146
+ when (psiModifierListOwner) {
133
147
is PsiClass ,
134
148
is PsiParameter ->
135
- TestCase .assertTrue(owner is SymbolLightClassModifierList <* >)
149
+ assertions .assertTrue(owner is SymbolLightClassModifierList <* >)
136
150
137
151
is PsiField ,
138
152
is PsiMethod ->
139
- TestCase .assertTrue(owner is SymbolLightMemberModifierList <* >)
153
+ assertions .assertTrue(owner is SymbolLightMemberModifierList <* >)
140
154
141
155
else ->
142
156
throw IllegalStateException (" Unexpected annotation owner kind: ${lastDeclaration::class .java} " )
0 commit comments