2

I'm learning scala. I want to put a java annotation (let's say lombok's @NotNull) on scala method parameter. but when I write:

def a(@NotNull o : Object) = {}

I get a compilation error: trait NotNull is abstract; cannot be instantiated.

ps. I'm not asking how to ensure not null in scala. I'm asking how to use java annotation

2 Answers 2

2

That's the correct way to use a Java annotation in scala

scala> import java.lang.annotation._
...
scala> def a(@Retention(RetentionPolicy.RUNTIME) o: Object) = {}
a: (a: Object)Unit

By the way NotNull is scala specific which is the cause of the error. Martin Odersky explains this issue

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

Comments

1

There's no Lombok @NotNull; there's a Scala @NotNull and a Lombok @NonNull.

Scala's @NotNull has never been correctly implemented, and has been deprecated (with implementation removed) in 2.11. See https://issues.scala-lang.org/browse/SI-7247 . So if that was the subject, the question is probably moot.

I'd be surprised if Lombok @NonNull works in Scala source. Lombok uses a (Java) compile-time annotation processor. Since Scala compiles direct to bytecode, Java annotation processors are not invoked.

Since neither of these are of use, you might want to ask around for alternatives.

1 Comment

you're right. my ide mixed up annotations. after fix i have no more compiler errors. still haven't check if lombok works with scala

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.