Skip to content

Commit c243e65

Browse files
committed
refactor: ArtifactQualifiedName.toClass()
1 parent 9992745 commit c243e65

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

src/main/kotlin/spp/protocol/artifact/ArtifactQualifiedName.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,16 @@ data class ArtifactQualifiedName(
101101
if (identifier == other.identifier) return false
102102
return other.identifier.substringBefore("#").startsWith(identifier)
103103
}
104+
105+
fun toClass(): ArtifactQualifiedName? {
106+
return if (type == ArtifactType.CLASS) {
107+
this
108+
} else {
109+
var parent = asParent()
110+
while (parent != null && parent.type != ArtifactType.CLASS) {
111+
parent = parent.asParent()
112+
}
113+
parent
114+
}
115+
}
104116
}

src/test/kotlin/spp/protocol/artifact/ArtifactQualifiedNameTest.kt

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,51 +18,52 @@ package spp.protocol.artifact
1818

1919
import org.junit.jupiter.api.Assertions.*
2020
import org.junit.jupiter.api.Test
21+
import spp.protocol.artifact.ArtifactType.*
2122

2223
class ArtifactQualifiedNameTest {
2324

2425
@Test
2526
fun `test parent qualified name`() {
26-
val methodExpression = ArtifactQualifiedName("com.example.TestClass.fun()#22", type = ArtifactType.EXPRESSION)
27+
val methodExpression = ArtifactQualifiedName("com.example.TestClass.fun()#22", type = EXPRESSION)
2728
val method = methodExpression.asParent()
2829
assertNotNull(method)
2930
assertEquals("com.example.TestClass.fun()", method!!.identifier)
30-
assertEquals(ArtifactType.METHOD, method.type)
31+
assertEquals(METHOD, method.type)
3132

3233
val clazz = method.asParent()
3334
assertNotNull(clazz)
3435
assertEquals("com.example.TestClass", clazz!!.identifier)
35-
assertEquals(ArtifactType.CLASS, clazz.type)
36+
assertEquals(CLASS, clazz.type)
3637

37-
val fieldExpression = ArtifactQualifiedName("com.example.TestClass#22", type = ArtifactType.EXPRESSION)
38+
val fieldExpression = ArtifactQualifiedName("com.example.TestClass#22", type = EXPRESSION)
3839
val clazz2 = fieldExpression.asParent()
3940
assertNotNull(clazz2)
4041
assertEquals("com.example.TestClass", clazz2!!.identifier)
41-
assertEquals(ArtifactType.CLASS, clazz2.type)
42+
assertEquals(CLASS, clazz2.type)
4243
}
4344

4445
@Test
4546
fun `test parent qualified name with args`() {
4647
val methodExpression = ArtifactQualifiedName(
4748
"com.example.TestClass.fun(java.lang.String)#22",
48-
type = ArtifactType.EXPRESSION
49+
type = EXPRESSION
4950
)
5051
val method = methodExpression.asParent()
5152
assertNotNull(method)
5253
assertEquals("com.example.TestClass.fun(java.lang.String)", method!!.identifier)
53-
assertEquals(ArtifactType.METHOD, method.type)
54+
assertEquals(METHOD, method.type)
5455

5556
val clazz = method.asParent()
5657
assertNotNull(clazz)
5758
assertEquals("com.example.TestClass", clazz!!.identifier)
58-
assertEquals(ArtifactType.CLASS, clazz.type)
59+
assertEquals(CLASS, clazz.type)
5960
}
6061

6162
@Test
6263
fun `test is child of`() {
63-
val clazz = ArtifactQualifiedName("com.example.TestClass", type = ArtifactType.CLASS)
64-
val method = ArtifactQualifiedName("com.example.TestClass.fun()", type = ArtifactType.METHOD)
65-
val methodExpression = ArtifactQualifiedName("com.example.TestClass.fun()#22", type = ArtifactType.EXPRESSION)
64+
val clazz = ArtifactQualifiedName("com.example.TestClass", type = CLASS)
65+
val method = ArtifactQualifiedName("com.example.TestClass.fun()", type = METHOD)
66+
val methodExpression = ArtifactQualifiedName("com.example.TestClass.fun()#22", type = EXPRESSION)
6667

6768
assertTrue(methodExpression.isChildOf(clazz))
6869
assertTrue(methodExpression.isChildOf(method))
@@ -78,9 +79,9 @@ class ArtifactQualifiedNameTest {
7879

7980
@Test
8081
fun `test is parent of`() {
81-
val clazz = ArtifactQualifiedName("com.example.TestClass", type = ArtifactType.CLASS)
82-
val method = ArtifactQualifiedName("com.example.TestClass.fun()", type = ArtifactType.METHOD)
83-
val methodExpression = ArtifactQualifiedName("com.example.TestClass.fun()#22", type = ArtifactType.EXPRESSION)
82+
val clazz = ArtifactQualifiedName("com.example.TestClass", type = CLASS)
83+
val method = ArtifactQualifiedName("com.example.TestClass.fun()", type = METHOD)
84+
val methodExpression = ArtifactQualifiedName("com.example.TestClass.fun()#22", type = EXPRESSION)
8485

8586
assertTrue(clazz.isParentOf(methodExpression))
8687
assertTrue(method.isParentOf(methodExpression))
@@ -93,4 +94,30 @@ class ArtifactQualifiedNameTest {
9394
assertFalse(method.isParentOf(clazz))
9495
assertFalse(methodExpression.isParentOf(method))
9596
}
97+
98+
@Test
99+
fun `test to class`() {
100+
val expression = ArtifactQualifiedName("com.example.TestClass.fun()#22", type = EXPRESSION)
101+
val clazz = expression.toClass()
102+
assertNotNull(clazz)
103+
assertEquals("com.example.TestClass", clazz!!.identifier)
104+
105+
val method = ArtifactQualifiedName("com.example.TestClass.fun()", type = METHOD)
106+
val clazz2 = method.toClass()
107+
assertNotNull(clazz2)
108+
assertEquals("com.example.TestClass", clazz2!!.identifier)
109+
}
110+
111+
@Test
112+
fun `test to inner class`() {
113+
val expression = ArtifactQualifiedName("com.example.TestClass\$InnerClass.fun()#22", type = EXPRESSION)
114+
val clazz = expression.toClass()
115+
assertNotNull(clazz)
116+
assertEquals("com.example.TestClass\$InnerClass", clazz!!.identifier)
117+
118+
val method = ArtifactQualifiedName("com.example.TestClass\$InnerClass.fun()", type = METHOD)
119+
val clazz2 = method.toClass()
120+
assertNotNull(clazz2)
121+
assertEquals("com.example.TestClass\$InnerClass", clazz2!!.identifier)
122+
}
96123
}

0 commit comments

Comments
 (0)