bug icon indicating copy to clipboard operation
bug copied to clipboard

Java annotations are not applied correctly classes defined within traits

Open stringbean opened this issue 6 years ago • 1 comments

There are some edge cases for the targeting of annotations using the meta-annotations where they are not getting applied correctly for classes defined within traits.

According to the scaladocs for scala.annotation.meta:

The target meta-annotations can be put on the annotation type when instantiating the annotation. In the following example, the annotation @Id will be added only to the bean getter getX.

import javax.persistence.Id
class A {
  @(Id @beanGetter) @BeanProperty val x = 0
}

When the meta-annotations are used on classes defined in standalone compilation units or within objects everything behaves correctly. If the annotations are applied to classes defined within traits they get applied to extra methods that shouldn't be. In Scala 2.11.12 and below this only affected a couple of scenarios but in 2.12.0 and newer the behaviour has regressed significantly.

I've created a test harness that applies the different meta-annotations and outputs where the annotations were actually applied. The harness can be found in this gist.

A summary of the results can be seen below:

Test Case Expected 2.11 2.12 2.13
TestBean.defaultTarget C C C C
TestBean.fieldTarget F F F F
TestBean.getterTarget M M M M
TestBean.beanPropertyDefault C C C C
TestBean.beanPropertyField F F F F
TestBean.beanPropertyGetter M M M M
TestBean.beanPropertyBeanGetter B B B B
WrappedBean.defaultTarget C - M M
WrappedBean.fieldTarget F F FMC FMC
WrappedBean.getterTarget M M M M
WrappedBean.beanPropertyDefault C - MB MB
WrappedBean.beanPropertyField F FC FMCB FMCB
WrappedBean.beanPropertyGetter M MC MC MC
WrappedBean.beanPropertyBeanGetter B B B B

Key:

  • C: constructor
  • F: field
  • M: getter method
  • B: bean getter method

stringbean avatar Nov 01 '19 11:11 stringbean

(not sure if the situation is any different or better in Scala 3)

SethTisue avatar Feb 24 '25 23:02 SethTisue