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 ef2c0e6 commit 1dd2638
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 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,6 +1,7 @@
package com.ing.baker

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

import scala.reflect.runtime.universe

Expand All @@ -18,11 +19,14 @@ 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 t @ _ => throw new IllegalArgumentException(s"Unsupported type: $javaType")
}

def getTypeParameter(javaType: java.lang.reflect.Type, index: Int): java.lang.reflect.Type = {
javaType.asInstanceOf[ParameterizedType].getActualTypeArguments()(index)
def getTypeParameter(javaType: java.lang.reflect.Type, index: Int): java.lang.reflect.Type = javaType match {
case t: ParameterizedType => t.getActualTypeArguments()(index)
case w: WildcardType => getTypeParameter(w.getUpperBounds().head, index)
case _ => throw new IllegalArgumentException(s"Unsupported type: $javaType")
}

def isAssignableToBaseClass(javaType: java.lang.reflect.Type, base: Class[_]) = base.isAssignableFrom(getBaseClass(javaType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,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 @@ -450,6 +479,12 @@ class KotlinDslTest {
fun apply(metaData: String, reservedItems: List<ProductAgreement>): Result
}

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 1dd2638

Please sign in to comment.