piotrek
piotrek

Reputation: 14530

java annotation on scala method parameter

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

Upvotes: 2

Views: 1360

Answers (2)

Ed Staub
Ed Staub

Reputation: 15690

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.

Upvotes: 1

loki
loki

Reputation: 2311

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

Upvotes: 2

Related Questions