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