Search code examples
javakotlinosgiapache-felix

Expressing Java Annotation Class with Members in Kotlin


I'm new to Kotlin and currently using v1.3.31 in an OSGI bundle development POC. I'm currently using annotation processors to generate the OSGI manifest declarations and I am trying to find the Kotlin equivalent of the following:

@ObjectClassDefinition(name="Config", description = "Sample Config")
    public static @interface Config {
        @AttributeDefinition(name = "A parameter", description = "Configurable param")
        String myParameter() default "";
    }

Given that these OSGI annotations are created as:

@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface ObjectClassDefinition

/*....*/

@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
public @interface AttributeDefinition

The closest I can get in Kotlin is using the @ObjectClassDefinition annotation on a public annotation class but since Kotlin annotation classes do not support members, I cannot use the @AttributeDefinition on a class member.

  • I've tried to only use a Kotlin class or interface but the annotation processor will not allow any non-annotation class.
  • I've also tried to create a java.lang.Annotation in Kotlin to bypass this behavior, but the compiler will not allow it.

Current partially working implementation and here's is the latest working GitHub source.:

@ObjectClassDefinition(name = "Sample Kotlin servlet",
            description = "Simple Kotlin servlet with configurable properties")
    public annotation class  Config (
        val value : String = "hello"
    )

I've noticed that annotations can be particularly tricky in Kotlin. Any insight is greatly appreciated!


Solution

  • @ObjectClassDefinition(
        name = "Sample Kotlin servlet",
        description = "Simple Kotlin servlet with configurable properties")
    annotation class  Config (
      @get:AttributeDefinition(name = "A parameter", description = "Configurable param")
      val value : String = "hello")
    

    ("public" does not seem to have any effect). See this documentation for details annotation-use-site-targets