11

I'm experimenting with Kotlin and I have a following Java-annotation

@Target( { TYPE })
@Retention(RUNTIME)
public @interface View {
    String[] url() default "";
    Class<? extends Component> parent() default Component.class;
}

and in Java-code it is used in following way

@View(url="/", parent=RootView.class)
public class FrontView extends Component {
}

How is that expressed in Kotlin? I have tried

[View(url=Array<String>("/"), parent=Class<RootView>)]
class FrontView : Component() {
}

but it does not compile. I only get type mismatch errors.

Type mismatch.  
Required: jet.Array<jet.String?>?  
Found: jet.Array<T>

and

Type mismatch
Required: java.lang.Class<out net.contextfw.web.application.component.Component?>?
Found: java.lang.Class<T>
2
  • Full docs for annotations are at kotlinlang.org/docs/reference/annotations.html Commented Dec 29, 2015 at 14:22
  • Beginner here. I had the error: "Parameters must have type annotation" for the code val isHidden:? Boolean. The correct code should be val isHidden: Boolean? (to define a Optional val). Commented Oct 17, 2018 at 14:23

1 Answer 1

8

I found the solution. The syntax seems to be like this:

[View(url=array("/"), parent=javaClass<RootView>())]
class FrontView() : Component() {
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah dropping [] would make it look cleaner.
The syntax has changed, in Kotlin now annotations use @ and are consistent with the Java syntax. Additional syntax is for targeting an annotation when there is more than one option. Full docs are now at kotlinlang.org/docs/reference/annotations.html

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.