# Calling a java method that expect Class\<T\>?

**URL:** <https://discuss.kotlinlang.org/t/calling-a-java-method-that-expect-class-t/2239>\
**Category:** Support\
**Created:** [January 21, 2017, 1:47pm UTC](https://discuss.kotlinlang.org/t/calling-a-java-method-that-expect-class-t/2239 "2017-01-21T13:47:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luistrigueiros](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/luistrigueiros/32/2161_2.png) [@luistrigueiros](https://discuss.kotlinlang.org/u/luistrigueiros)\
**Post date:** [January 21, 2017, 1:47pm UTC](https://discuss.kotlinlang.org/t/calling-a-java-method-that-expect-class-t/2239/1 "2017-01-21T13:47:20Z")

</div>

I am trying to call a java method from the Apache Camel ProducerTemplate, the method is declared in java as:

`<T> T requestBodyAndHeaders(String endpointUri, Object body, Map<String, Object> headers, Class<T> type) throws CamelExecutionException;`

I my case the Class is List\<Data.Offer\> when I use the following Kotlin code:

`producerTemplate.requestBodyAndHeaders(endpoint, null, params, MutableList<Data.Offer>::class.java)`

It does not compile, I end up using the following, bur the compile is giving type casts warnings.

```
abstract class CamelBaseSupplier(val camelContext: CamelContext) : Supplier {
    open fun requestOffers(endpoint: String, params: Map<String, Any>): MutableList<*> {
        val producerTemplate = camelContext.createProducerTemplate()
        return producerTemplate.requestBodyAndHeaders(endpoint, null, params, MutableList::class.java)
    }
}

```

Thank you.

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [January 21, 2017, 7:35pm UTC](https://discuss.kotlinlang.org/t/calling-a-java-method-that-expect-class-t/2239/2 "2017-01-21T19:35:47Z")

</div>

> [@luistrigueiros](#):
>
> producerTemplate.requestBodyAndHeaders(endpoint, null, params, MutableList\<Data.Offer\>::class.java)

Replace that with this and it compiles without warning for me:

```
producerTemplate.requestBodyAndHeaders(endpoint, null, params, MutableList::class.java)

```

---

<div class="post-metadata">

**Author:** ![luistrigueiros](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/luistrigueiros/32/2161_2.png) [@luistrigueiros](https://discuss.kotlinlang.org/u/luistrigueiros)\
**Post date:** [January 22, 2017, 11:45am UTC](https://discuss.kotlinlang.org/t/calling-a-java-method-that-expect-class-t/2239/3 "2017-01-22T11:45:48Z")

</div>

Hi @dalewking:

yeap, this is what I used but the compilation working happens when I try to use this method in from a child inheritant class  
the consumer code looks like:

```
val offers = requestOffers(ENDPOINT, params) as MutableList<Data.Offer> 

```

At this point I do need the type of the list to containt Data.Offer objects so I had to use the cast.  
So my question is how to I declared that I expect the result of the call to result object of the type MutableList\<Data.Offer\> ?

Thank you, kindly

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [January 24, 2017, 3:05am UTC](https://discuss.kotlinlang.org/t/calling-a-java-method-that-expect-class-t/2239/4 "2017-01-24T03:05:55Z")

</div>

Afraid you’re running into the limitation of the JVM type system and the way that generics are implemented on the JVM and not a problem with Kotlin. You would have the exact same problem on plain Java. A class object knows absolutely nothing about any generic parameters.

It appears in this case what you want is to do the cast inside of requestOffers and suppress the unchecked cast:

```
abstract class CamelBaseSupplier(val camelContext: CamelContext) : Supplier {
    @Suppress("UNCHECKED_CAST")
    open fun requestOffers(endpoint: String, params: Map<String, Any>)
        = camelContext.createProducerTemplate()
             .requestBodyAndHeaders(endpoint, null, params, MutableList::class.java)
              as MutableList<Data.Offer>
}

```

Even if you were doing this in Java you would also have an unchecked cast due to Java’s use of type erasure for generics

---

<div class="post-metadata">

**Author:** ![luistrigueiros](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/luistrigueiros/32/2161_2.png) [@luistrigueiros](https://discuss.kotlinlang.org/u/luistrigueiros)\
**Post date:** [January 24, 2017, 10:08am UTC](https://discuss.kotlinlang.org/t/calling-a-java-method-that-expect-class-t/2239/5 "2017-01-24T10:08:24Z")

</div>

Thank you for the great insight.🙂
