Skip to content

Commit

Permalink
Implement wildcard types
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmveel committed May 31, 2023
1 parent ad6a964 commit 746b3e8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ing.baker.types

import java.lang.reflect.ParameterizedType
import java.lang.reflect.WildcardType

import com.ing.baker.types.Converters.readJavaType
import com.ing.baker.types.modules._
Expand All @@ -20,6 +21,7 @@ class TypeAdapter(private val modules: Map[Class[_], TypeModule]) {
val clazz: Class[_] = javaType match {
case c: Class[_] => c
case genericType: ParameterizedType => getBaseClass(genericType)
case wildcardType: WildcardType => getBaseClass(wildcardType)
case _ => throw new IllegalArgumentException(s"Incompatible type: $javaType")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.ing.baker

import java.lang.reflect.ParameterizedType

import java.lang.reflect.{ParameterizedType, WildcardType}
import scala.annotation.tailrec
import scala.reflect.runtime.universe
Expand All @@ -19,6 +21,7 @@ package object types {
def getBaseClass(javaType: java.lang.reflect.Type): Class[_] = javaType match {
case c: Class[_] => c
case t: ParameterizedType => getBaseClass(t.getRawType)
case w: WildcardType => getBaseClass(w.getUpperBounds().head)
case _ => throw new IllegalArgumentException(s"Unsupported type: $javaType")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,35 @@ class KotlinDslTest {
}
}

@Test
fun `wildcard type impl`() {


val recipe = recipe("WildcardRecipe") {
interaction<WildcardInteraction>()
}

with(recipe) {
assertEquals("WildcardRecipe", name())
assertEquals(1, interactions().size())
}

with(recipe.interactions().toList().apply(0)) {
assertEquals("WildcardInteraction", name())

assertEquals(1, inputIngredients().size())
assertEquals("hello", inputIngredients().toList().apply(0).name())

assertEquals(1, output().size())
assertEquals("WildcardIngredient", output().toList().apply(0).name())
assertEquals(1, output().toList().apply(0).providedIngredients().size())
assertEquals("wildcardMap", output().toList().apply(0).providedIngredients().toList().apply(0).name())
assertEquals("Map[List[CharArray]]", output().toList().apply(0).providedIngredients().toList().apply(0).ingredientType().toString())

}

}

object Events {
class OrderPlaced(val items: List<Ingredients.Item>)
class PaymentInformationReceived(val paymentInformation: Ingredients.PaymentInformation)
Expand Down Expand Up @@ -498,6 +527,12 @@ class KotlinDslTest {
): Response
}

interface WildcardInteraction: Interaction {
data class WildcardIngredient(val wildcardMap: Map<String, List<String>>)

fun apply(hello:String): WildcardIngredient
}

interface KotlinInteractionWithAnnotations : Interaction {
interface Outcome
data class Person(val name: String, val age: Int) : Outcome
Expand Down

0 comments on commit 746b3e8

Please sign in to comment.