# Using generic annotation classes from Java

**URL:** <https://discuss.kotlinlang.org/t/using-generic-annotation-classes-from-java/4332>\
**Category:** Language Design\
**Created:** [August 25, 2017, 8:35am UTC](https://discuss.kotlinlang.org/t/using-generic-annotation-classes-from-java/4332 "2017-08-25T08:35:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![erickok](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/erickok/32/2849_2.png) [@erickok](https://discuss.kotlinlang.org/u/erickok)\
**Post date:** [August 25, 2017, 8:35am UTC](https://discuss.kotlinlang.org/t/using-generic-annotation-classes-from-java/4332/1 "2017-08-25T08:35:18Z")

</div>

Is it possible to use annotation classes from Java if they have generics?

Kotlin (as opposed to Java) supports generics for annotation classes (Java’s `@interface`).

```
interface Contract
class ContractImpl: Contract

annotation class Annotated<C: Contract>(val contract: KClass<C>)

@Annotated<ContractImpl>(ContractImpl::class)
fun function() {}

```

However, when I try to use this annotation from Java, I get an error “Incompatible types. Found: 'java.lang.Class, required: ‘java.lang.Class’”

```
@Annotated(contract = ContractImpl.class)
void function() {}

```

Since I cannot do `@Annotated<ContractImpl>` in Java, is there any way to use this annotation?

---

<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:** [August 26, 2017, 1:12pm UTC](https://discuss.kotlinlang.org/t/using-generic-annotation-classes-from-java/4332/2 "2017-08-26T13:12:40Z")

</div>

> [@erickok](#):
>
> “Incompatible types. Found: 'java.lang.Class, required: ‘java.lang.Class’”

For any one looking into this the actual error is:

```
Incompatible types. Found: 'java.lang.Class<ContractImpl>, required: ‘java.lang.Class<C>'

```

---

<div class="post-metadata">

**Author:** ![madmax1028](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/madmax1028/32/3335_2.png) [@madmax1028](https://discuss.kotlinlang.org/u/madmax1028)\
**Post date:** [August 26, 2017, 1:31pm UTC](https://discuss.kotlinlang.org/t/using-generic-annotation-classes-from-java/4332/3 "2017-08-26T13:31:27Z")

</div>

Wouldn’t it be easier to just declare annotation like this?

```auto
annotation class Annotated(val contract: KClass<out Contract>)

@Annotated(ContractImpl::class)
fun function() {}

```

---

<div class="post-metadata">

**Author:** ![erickok](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/erickok/32/2849_2.png) [@erickok](https://discuss.kotlinlang.org/u/erickok)\
**Post date:** [August 28, 2017, 7:22am UTC](https://discuss.kotlinlang.org/t/using-generic-annotation-classes-from-java/4332/4 "2017-08-28T07:22:17Z")

</div>

Well that works aroudn the problem at best but my real world case is closer to

```
annotation class Annotated<C: Contract>(val contract1: KClass<C>, val contract2: KClass<C>)

```

which gives extra restrictions on the type of `contract1` and `contract2`, but I do not see how to use it from Java?
