# Annotation for local variables is not retained in class file

**URL:** https://discuss.kotlinlang.org/t/annotation-for-local-variables-is-not-retained-in-class-file/9739
**Category:** Support
**Created:** [October 8, 2018, 10:54am UTC](https://discuss.kotlinlang.org/t/annotation-for-local-variables-is-not-retained-in-class-file/9739 "2018-10-08T10:54:36Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jacobwu](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@jacobwu](https://discuss.kotlinlang.org/u/jacobwu)
#### Post date: [October 8, 2018, 10:54am UTC](https://discuss.kotlinlang.org/t/annotation-for-local-variables-is-not-retained-in-class-file/9739/1 "2018-10-08T10:54:36Z")

</div>

I just found that after compilation, the annotations for exception variable of catch block (actually also for all local varialbes) are not retained. For example, for the code below:  
try { } catch (@MyAnnotation e: Exception) { }

the @MyAnnotation is not present in class file. I read that Java retains annotations for local variables since 8. I am using Maven and kotlin plugin, and have set jvmTarget to 1.8. The Kotlin version that I use (and also the kotlin maven plugin version) is 1.2.30. Is there anything that I missed?

---

<div class="post-metadata">

### Author: ![Alexey.Belkov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/alexey.belkov/32/5215_2.png) [@Alexey.Belkov](https://discuss.kotlinlang.org/u/Alexey.Belkov)
#### Post date: [October 15, 2018, 9:47am UTC](https://discuss.kotlinlang.org/t/annotation-for-local-variables-is-not-retained-in-class-file/9739/2 "2018-10-15T09:47:37Z")

</div>

Hello, have you tried to decompile some Java sources with local variable annotations? For example:

Ann.java

```auto
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.LOCAL_VARIABLE)
public @interface Ann {
}

```

JavaTest.java

```auto
public class JavaTest {
    public static void main(String[] args) {
        @Ann String s = "hello";
    }
}

```

Invoke `javap -l -v -c -s JavaTest.class` on the class file - there are no mentions of the `Ann` annotation.

---

<div class="post-metadata">

### Author: ![mbogdanov](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@mbogdanov](https://discuss.kotlinlang.org/u/mbogdanov)
#### Post date: [October 15, 2018, 10:28am UTC](https://discuss.kotlinlang.org/t/annotation-for-local-variables-is-not-retained-in-class-file/9739/3 "2018-10-15T10:28:48Z")

</div>

According to specification "An annotation on a local variable declaration is never retained in the binary representation. "  
[https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.6.4.2](https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.6.4.2)

---

<div class="post-metadata">

### Author: ![jacobwu](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@jacobwu](https://discuss.kotlinlang.org/u/jacobwu)
#### Post date: [October 23, 2018, 6:11am UTC](https://discuss.kotlinlang.org/t/annotation-for-local-variables-is-not-retained-in-class-file/9739/4 "2018-10-23T06:11:26Z")

</div>

Thank you for suggestion, really appreciate that.

---

<div class="post-metadata">

### Author: ![jacobwu](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@jacobwu](https://discuss.kotlinlang.org/u/jacobwu)
#### Post date: [October 23, 2018, 6:12am UTC](https://discuss.kotlinlang.org/t/annotation-for-local-variables-is-not-retained-in-class-file/9739/5 "2018-10-23T06:12:36Z")

</div>

Thank you for the info, I knew before 8 they are not retained, but thought it’s solved in 8. Thanks for making it clear it’s not.
