Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement wildcard types #1510

Open
wants to merge 1 commit into
base: release-4.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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