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

2
  • I'm not sure what you want to do. Do you want to recode the annotation to Kotlin? Commented Jun 25, 2017 at 21:01
  • I would like to implement DateRequired annotation in Kotlin so that the call resourceInfo.resourceMethod.getAnnotation(DateRequired..) works. Commented Jun 25, 2017 at 21:05

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, that is what I tried. I am unable to refer to the class of DateRequired in the getAnnotation() call.
@DavidSoroko it has reported any error? indeed, the kotlin annotation class is transformed to java @interface like as yours. maybe your logic wrong?
see my note, looks like an IntelliJ issue.
@DavidSoroko hi, how about it after using the !! operator?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.