0

I am implementing the dropwizard example application in Kotlin and have an issue implementing the DateRequiredFeature. The Java code code is as follows:

    @Provider
    public class DateRequiredFeature implements DynamicFeature {
        @Override
        public void configure(ResourceInfo resourceInfo, FeatureContext context) {
            if (resourceInfo.getResourceMethod().getAnnotation(DateRequired.class) != null) {
                context.register(DateNotSpecifiedFilter.class);
            }
        }
    }

with the annotation defined as:

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface DateRequired {}

For the Kotlin side I have for the feature:

    @Provider
    class DateRequiredFeature : DynamicFeature(){
        override fun configure(resourceInfo: ResourceInfo, context: FeatureContext) {
            if (resourceInfo.resourceMethod.getAnnotation(DateRequired::class.java) != null) {
                context.register(DateNotSpecifiedFilter::class.java)
            }
        }
    }

but is is not clear how to implement the corresponding DateRequired annotation so that getAnnotation() is valid. The (Java) signature for getAnnotation() is

public <T extends Annotation> T getAnnotation(Class<T> annotationClass)

Note

Looks like an IntelliJ bug, I need to explicitly import the annotation although it is in the same package:

...
import DateRequired

@Provider
class DateRequiredFeature : DynamicFeature {
    override fun configure(resourceInfo: ResourceInfo, context: FeatureContext) {
        if (resourceInfo.resourceMethod.getAnnotation(DateRequired::class.java) != null) {
            context.register(DateNotSpecifiedFilter::class.java)
        }
    }
}

then all is well using standard Kotlin annotation annotation class DateRequired

David Soroko
  • 6,773
  • 28
  • 39
  • I'm not sure what you want to do. Do you want to recode the annotation to Kotlin? – Mibac Jun 25 '17 at 21:01
  • I would like to implement `DateRequired` annotation in Kotlin so that the call `resourceInfo.resourceMethod.getAnnotation(DateRequired..)` works. – David Soroko Jun 25 '17 at 21:05

1 Answers1

4

Implementing a annotation in Kotlin is simpler than in Java. The default RetentionPolicy in Kotlin is RUNTIME. The FUNCTION,PROPERTY_GETTER and PROPERTY_SETTER are mapped to Java's ElementType.METHOD, for example:

import kotlin.annotation.AnnotationTarget.*

@Target(FUNCTION, PROPERTY_GETTER, PROPERTY_SETTER)
annotation class DateRequired;

Secondly, I have also discovered you used a Java platform class ResourceInfo which returns a java.reflect.Method instance. It might return null resulting in NullPointerException in Kotlin. You must use the NPE-lovers operator !! before calling the getAnnotation method:

resourceInfo.resourceMethod!!.getAnnotation(DateRequired::class.java)
//                         ^--- force to call

Last, the DynamicFeature is an interface. implements an interface in kotlin you must remove the parentheses ():

@Provider
class DateRequiredFeature : DynamicFeature{...}

You can see more details about annotations here.

Mibac
  • 7,250
  • 4
  • 30
  • 52
holi-java
  • 25,362
  • 6
  • 59
  • 71