Skip to content

Commit

Permalink
signature parsing working
Browse files Browse the repository at this point in the history
  • Loading branch information
evanchooly committed Sep 1, 2024
1 parent 72dba86 commit 1d07659
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import io.quarkus.gizmo.MethodCreator
import io.quarkus.gizmo.MethodDescriptor
import io.quarkus.gizmo.MethodDescriptor.ofMethod
import io.quarkus.gizmo.ResultHandle
import ksp.com.intellij.codeWithMe.ClientId.Companion.current
import org.bson.codecs.pojo.PropertyAccessor
import org.objectweb.asm.Opcodes
import org.objectweb.asm.Type
Expand All @@ -38,7 +37,7 @@ class GizmoPropertyModelGenerator private constructor(val config: MorphiaConfig,
val signature = field.signature
typeArguments =
signature?.let {
it.typeData()
typeData(it)
Type.getArgumentTypes("()$it")
} ?: arrayOf()
generatedType = "${baseName}.${propertyName.titleCase()}Model"
Expand Down Expand Up @@ -286,15 +285,23 @@ class GizmoPropertyModelGenerator private constructor(val config: MorphiaConfig,
}
}

fun String.typeData(): List<TypeData<*>> {
val bracket = indexOf('<')
fun String.typeData2(): List<TypeData<*>> {
if (!contains("<")) {
listOf(TypeData(getType(this).asClass()))
}
var current = this
var typeArguments = listOf<TypeData<*>>()

var current = this
if (bracket != -1) {
val substring = substring(indexOf("<") + 1, lastIndexOf('>'))
typeArguments += substring.typeData()
current = current.replace("<$substring>", "")
while (current.isNotEmpty()) {
if (current.contains('<') && current.indexOf('<') < current.indexOf(';')) {
val substring = substring(indexOf("<") + 1, lastIndexOf('>'))
typeArguments += substring.typeData2()
current = current.replace("<$substring>", "")
} else {
val substring = current.substringBefore(";") + ";"
typeArguments += TypeData(getType(substring).asClass())
current = current.substringAfter(";")
}
}

var // types = mutableListOf<TypeData<*>>()
Expand All @@ -320,9 +327,51 @@ fun String.typeData(): List<TypeData<*>> {
if (types.size == 1) {
types = listOf(TypeData(types[0].type, typeArguments))
}
return types
return if (types.isNotEmpty()) types else typeArguments
}

private fun Type.asClass(): Class<*> {
return Class.forName(className)
private fun String.balanced(): String {
if (!contains("<")) return ""
val start = indexOf('<') + 1
var index = start
var count = 1

while (index < length && count != 0) {
when (this[++index]) {
'>' -> count--
'<' -> count++
}
}

return substring(start, index)
}

fun typeData(input: String): List<TypeData<*>> {
if (input.isEmpty()) return emptyList()
// if (!input.contains('<')) return listOf(getType(input).typeData())
val types = mutableListOf<TypeData<*>>()

var value = input

while (value.isNotEmpty()) {
val bracket = value.indexOf('<')
if (bracket == -1 || bracket > value.indexOf(';')) {
val type = value.substringBefore(';')
types += getType("$type;").typeData()
value = value.substringAfter(';')
} else {
val paramString = value.balanced()
val parameterTypes = typeData(paramString)
value = value.replace("<$paramString>", "")

types += getType(value).typeData(parameterTypes)
value = value.substringAfter(';')
}
}
return types
}

private fun Type.typeData(typeParameters: List<TypeData<*>> = listOf()) =
TypeData(asClass(), typeParameters)

private fun Type.asClass() = Class.forName(className)
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TestGizmoGeneration {
}

@Test
fun testTypeData() {
fun testMapStringExample() {
var descString = "Ljava/util/Map<Ljava/lang/String;Ldev/morphia/critter/sources/Example;>;"
var descriptor =
descriptor(
Expand All @@ -35,18 +35,29 @@ class TestGizmoGeneration {
)

assertEquals(descriptor, descString)

var typeData = descString.typeData()[0]
var typeData = typeData(descString)[0]
assertEquals(
typeData,
Map::class.java.typeData(String::class.java.typeData(), Example::class.java.typeData())
)
}

descString =
@Test
fun testListMapStringExample() {
val descString =
"Ljava/util/List<Ljava/util/Map<Ljava/lang/String;Ldev/morphia/critter/sources/Example;>;>;"
assertEquals(descriptor(List::class.java, descriptor), descString)
val descriptor =
descriptor(
List::class.java,
descriptor(
Map::class.java,
descriptor(String::class.java),
descriptor(Example::class.java)
)
)
assertEquals(descriptor, descString)

typeData = descString.typeData()[0]
val typeData = typeData(descString)[0]
assertEquals(
typeData,
List::class
Expand All @@ -57,15 +68,21 @@ class TestGizmoGeneration {
.typeData(String::class.java.typeData(), Example::class.java.typeData())
)
)
}

descriptor =
@Test
fun testMapOfList() {
val descString =
"Ljava/util/Map<Ljava/lang/String;Ljava/util/List<Ldev/morphia/critter/sources/Example;>;>;"
val descriptor =
descriptor(
Map::class.java,
descriptor(String::class.java),
descriptor(List::class.java, descriptor(Example::class.java))
)
assertEquals(descriptor, descString)
println("**************** descString = ${descriptor}")
typeData = descriptor.typeData()[0]
val typeData = typeData(descriptor)[0]
println("**************** descString = $typeData")
assertEquals(
typeData,
Expand Down

0 comments on commit 1d07659

Please sign in to comment.