This page is no longer maintained — Please continue to the home page at www.scala-lang.org

A few questions about annotations

2 replies
ericacm
Joined: 2011-05-14,
User offline. Last seen 42 years 45 weeks ago.

Hi all:

I have a property in my class like:

@Value("${service.enabled:false}")
var enabled: Boolean = _

The Value annotation is
@Target(value={FIELD,METHOD,PARAMETER})

When looking at the bytecode the annotation is applied to the private
field generated by the compiler, but not the accessor methods. This
is fine by me but I would like to know if we should we expect this
going forward?

Next, I apply the following annotation to the same property:

@Value("${service.enabled:false}")
@ManagedAttribute
var enabled: Boolean = _

The ManagedAttribute annotation is
@Target(value=METHOD)

The annotation ends up getting applied to the generated private field,
just like the @Value annotation. This seems like a bug. Is it?
The Java compiler will complain if I try to annotate a field with
@ManagedAttribute.

If I do this:

@Value("${service.enabled:false}")
@ManagedAttribute
@BeanProperty
var enabled: Boolean = _

then 2 sets of accessors are generated along with the private field.
The @ManagedAttribute annotation continues to get applied to the
private field.

Thanks,

Iulian Dragos
Joined: 2008-12-18,
User offline. Last seen 42 years 45 weeks ago.
Re: A few questions about annotations

On Sat, May 14, 2011 at 4:25 AM, ericacm wrote:
> Hi all:
>
> I have a property in my class like:
>
> @Value("${service.enabled:false}")
> var enabled: Boolean = _
>
> The Value annotation is
> @Target(value={FIELD,METHOD,PARAMETER})
>
> When looking at the bytecode the annotation is applied to the private
> field generated by the compiler, but not the accessor methods.  This
> is fine by me but I would like to know if we should we expect this
> going forward?

It has already. You need to use annotations from
scala.annotation.target. You should annotate your annotation with
@field, @getter or @setter, or a combination of them. More info here:

http://www.scala-lang.org/archives/downloads/distrib/files/nightly/docs/...

cheers,
iulian

>
> Next, I apply the following annotation to the same property:
>
> @Value("${service.enabled:false}")
> @ManagedAttribute
> var enabled: Boolean = _
>
> The ManagedAttribute annotation is
> @Target(value=METHOD)
>
> The annotation ends up getting applied to the generated private field,
> just like the @Value annotation.   This seems like a bug.   Is it?
> The Java compiler will complain if I try to annotate a field with
> @ManagedAttribute.
>
> If I do this:
>
> @Value("${service.enabled:false}")
> @ManagedAttribute
> @BeanProperty
> var enabled: Boolean = _
>
> then 2 sets of accessors are generated along with the private field.
> The @ManagedAttribute annotation continues to get applied to the
> private field.
>
> Thanks,
>
>

ericacm
Joined: 2011-05-14,
User offline. Last seen 42 years 45 weeks ago.
Re: A few questions about annotations
Thanks!  Works like a charm.

-- Eric



On Sat, May 14, 2011 at 6:59 AM, iulian dragos <jaguarul [at] gmail [dot] com> wrote:
On Sat, May 14, 2011 at 4:25 AM, ericacm <ericacm [at] gmail [dot] com> wrote:
> Hi all:
>
> I have a property in my class like:
>
> @Value("${service.enabled:false}")
> var enabled: Boolean = _
>
> The Value annotation is
> @Target(value={FIELD,METHOD,PARAMETER})
>
> When looking at the bytecode the annotation is applied to the private
> field generated by the compiler, but not the accessor methods.  This
> is fine by me but I would like to know if we should we expect this
> going forward?

It has already. You need to use annotations from
scala.annotation.target. You should annotate your annotation with
@field, @getter or @setter, or a combination of them. More info here:

http://www.scala-lang.org/archives/downloads/distrib/files/nightly/docs/library/index.html#scala.annotation.target.package

cheers,
iulian

>
> Next, I apply the following annotation to the same property:
>
> @Value("${service.enabled:false}")
> @ManagedAttribute
> var enabled: Boolean = _
>
> The ManagedAttribute annotation is
> @Target(value=METHOD)
>
> The annotation ends up getting applied to the generated private field,
> just like the @Value annotation.   This seems like a bug.   Is it?
> The Java compiler will complain if I try to annotate a field with
> @ManagedAttribute.
>
> If I do this:
>
> @Value("${service.enabled:false}")
> @ManagedAttribute
> @BeanProperty
> var enabled: Boolean = _
>
> then 2 sets of accessors are generated along with the private field.
> The @ManagedAttribute annotation continues to get applied to the
> private field.
>
> Thanks,
>
>



--
« Je déteste la montagne, ça cache le paysage »
Alphonse Allais

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland