Skip to content

Commit

Permalink
would tests on GH work better with strings?
Browse files Browse the repository at this point in the history
  • Loading branch information
evanchooly committed Jul 2, 2024
1 parent 26983d8 commit 1cc3a12
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package dev.morphia.critter.parser
import dev.morphia.critter.parser.generators.AddFieldAccessorMethods
import dev.morphia.critter.parser.generators.EntityAccessorGenerator
import dev.morphia.critter.parser.java.CritterClassLoader
import dev.morphia.critter.sources.DummyEntity
import dev.morphia.critter.sources.KotlinDummyEntity
import org.bson.codecs.pojo.PropertyAccessor
import org.testng.Assert.assertEquals
import org.testng.Assert.assertTrue
Expand All @@ -17,7 +15,7 @@ class TestAsmGenerator {
}

@Test(dataProvider = "classes")
fun testPropertyAccessors(type: Class<*>) {
fun testPropertyAccessors(type: String) {
val critterClassLoader = CritterClassLoader(Thread.currentThread().contextClassLoader)
val testFields =
listOf(
Expand All @@ -26,13 +24,13 @@ class TestAsmGenerator {
listOf("salary", java.lang.Long::class.java, 100_000L),
)
val bytes =
AddFieldAccessorMethods(type.name)
AddFieldAccessorMethods(type)
.update(testFields.map { l -> l[0] as String to l[1] as Class<*> }.toMap())
critterClassLoader.register(type.name, bytes)
critterClassLoader.register(type, bytes)

critterClassLoader.dump("target")

val entity = critterClassLoader.loadClass(type.name).getConstructor().newInstance()
val entity = critterClassLoader.loadClass(type).getConstructor().newInstance()

testFields.forEach { field ->
testAccessor(
Expand All @@ -47,7 +45,7 @@ class TestAsmGenerator {
}

private fun testAccessor(
type: Class<*>,
type: String,
critterClassLoader: CritterClassLoader,
entity: Any,
fieldName: String,
Expand Down Expand Up @@ -75,7 +73,10 @@ class TestAsmGenerator {
}

@DataProvider(name = "classes")
fun names(): Array<Class<out Any>> {
return arrayOf(DummyEntity::class.java, KotlinDummyEntity::class.java)
fun names(): Array<String> {
return arrayOf(
"dev.morphia.critter.sources.DummyEntity",
"dev.morphia.critter.sources.KotlinDummyEntity"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@ import org.objectweb.asm.Label
import org.objectweb.asm.Opcodes.*
import org.objectweb.asm.Type

class EntityAccessorGenerator(host: Class<*>, val fieldName: String, fieldClass: Class<*>) {
val entityType: Type = Type.getType(host)
class EntityAccessorGenerator(entity: String, val fieldName: String, fieldClass: Class<*>) {
val entityType: Type = Type.getType("L${entity.replace('.', '/')};")
val fieldType = Type.getType(fieldClass)
val wrapped = wrap(fieldType)
val classWriter = ClassWriter(0)
val accessorName =
"${host.packageName.replace('.', '/')}/__morphia/${host.simpleName}${fieldName.titleCase()}Accessor"
val accessorType = Type.getType("L$accessorName;")
val accessorName: String

val accessorType: Type

init {
val packageName = entity.substringBeforeLast(".")
val name = entity.substringAfterLast(".")
accessorName =
"${packageName.replace('.', '/')}/__morphia/$name${fieldName.titleCase()}Accessor"

accessorType = Type.getType("L$accessorName;")
}

fun dump(): ByteArray {
classWriter.visit(
Expand Down

0 comments on commit 1cc3a12

Please sign in to comment.