views:

91

answers:

1

Annotating constructor parameters seems to do nothing when compiled to bytecode. I get no compiler warnings either.

The following works. getAnnotations for the name field returns javax.annotation.Nullable.

class Person {
    @Nullable var name: String = _;
}

The following doesn't, neither with val or var.

class Person(@Nullable var name: String)

This is probably not intentional, so is there something I am missing or should I go file a bug report?

+9  A: 

You need to specify what should get annotated when you specify annotations on constructor parameters.

To do that annotate your annotation with one ore more annotations from scala.annotation.target, e.g. getter, setter or as in your case field:

import annotation.target.field

class Person(@(Nullable @field) var name: String)

You can also use type aliases for that:

type NullableField = Nullable @field

class Person(@NullableField var name: String)
Moritz
More information can be found here: http://lampsvn.epfl.ch/trac/scala/ticket/3421 and http://lampsvn.epfl.ch/trac/scala/ticket/3596
michael.kebe
Thanks, works like a charm!
ponzao