Question

In Scala it is possible to use renaming in import statements. However, it doesn't work with the @BeanProperty-Annotation.

This (admittedly pretty useless) piece of code:

import java.lang.{ Deprecated => D }
import scala.beans.{ BeanProperty => B }

object Foo {
  @D
  val bar = 1;

  @B
  val baz = 2;
}

yields a compilation error for the use of @B: "implementation limitation: the BeanProperty annotation cannot be used in a type alias or renamed import". Renaming @Deprecated does work.

What is the technical reason for this limitation?

Was it helpful?

Solution

The scala compiler is architected according to the "wing it" model. In this case that means the decision as to whether to synthesize getters and setters is made before symbols are available, so it attempts to identify @BeanProperty annotations based on whether they spell out "BeanProperty".

In other words it's not a real annotation - it's a pattern match on source code.

def hasAnnotationNamed(name: TypeName) = {
  annotations exists {
    case Apply(Select(New(Ident(`name`)), _), _)     => true
    case Apply(Select(New(Select(_, `name`)), _), _) => true
    case _                                           => false
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top