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

Annotations being copied to property accessor methods

2 replies
Richard Wallace
Joined: 2009-07-14,
User offline. Last seen 42 years 45 weeks ago.

Hello,

I've run into a bit of a problem while trying to use JAXB to annotate
and serialize objects in Scala. Here's my class

@XmlRootElement{val name="catalog"}
class CatalogRepresentation(
@XmlElement private val name: String
) {
// no-arg ctor for JAXB
def this() = this("")
}

When trying to serialize this class with JAXB, I get an error stating,
"JAXB annotation is placed on a method that is not a JAXB property".

I had no clue what this meant, so I tried looking at the bytecode
generated by the Scala compiler. This class seems to turn into the
following Java equivalent

@XmlRootElement{val name="catalog"}
public class CatalogRepresentation {
@XmlElement private String name;

// omitting ctors for brevity

@XmlElement
public String name() { return name; }
}

It's the @XmlElement annotation on the name() method that is tripping
up JAXB. I guess I'm wondering if copying the field annotation to the
Scala accessor is really the right thing to do or should this be fixed
in the Scala compiler? Also, can anyone think of any workarounds that
don't involve having to override the autogenerated accessors, which I
can do but is a real PITA.

Thanks,
Rich

rytz
Joined: 2008-07-01,
User offline. Last seen 45 weeks 5 days ago.
Re: Annotations being copied to property accessor methods
Hi Richard,
this issue is currently being discussed in another thread on the scala (not scala-user) mailing list:http://www.nabble.com/-scala--field-annotations%2C-getters-setters-and-BeanProperty-td24970781.html
Most likely, we will change the compiler and no longer copy the annotations to the getters / setters by default.
Lukas


On Tue, Aug 18, 2009 at 06:58, Richard Wallace <rwallace [at] thewallacepack [dot] net> wrote:
Hello,

I've run into a bit of a problem while trying to use JAXB to annotate
and serialize objects in Scala.  Here's my class

@XmlRootElement{val name="catalog"}
class CatalogRepresentation(
 @XmlElement private val name: String
) {
 // no-arg ctor for JAXB
 def this() = this("")
}

When trying to serialize this class with JAXB, I get an error stating,
"JAXB annotation is placed on a method that is not a JAXB property".

I had no clue what this meant, so I tried looking at the bytecode
generated by the Scala compiler.  This class seems to turn into the
following Java equivalent

@XmlRootElement{val name="catalog"}
public class CatalogRepresentation {
 @XmlElement private String name;

 // omitting ctors for brevity

 @XmlElement
 public String name() { return name; }
}

It's the @XmlElement annotation on the name() method that is tripping
up JAXB.  I guess I'm wondering if copying the field annotation to the
Scala accessor is really the right thing to do or should this be fixed
in the Scala compiler?  Also, can anyone think of any workarounds that
don't involve having to override the autogenerated accessors, which I
can do but is a real PITA.

Thanks,
Rich

Richard Wallace
Joined: 2009-07-14,
User offline. Last seen 42 years 45 weeks ago.
Re: Annotations being copied to property accessor methods

Ah geez. I saw that thread and completely missed the point of it and
the implications it has on my situation. I feel like such a shmuck.
:p

Thanks,
Rich

On Tue, Aug 18, 2009 at 12:35 AM, Lukas Rytz wrote:
> Hi Richard,
> this issue is currently being discussed in another thread on the scala (not
> scala-user) mailing list:
> http://www.nabble.com/-scala--field-annotations%2C-getters-setters-and-B...
> Most likely, we will change the compiler and no longer copy the annotations
> to the getters / setters by default.
> Lukas
>
>
> On Tue, Aug 18, 2009 at 06:58, Richard Wallace
> wrote:
>>
>> Hello,
>>
>> I've run into a bit of a problem while trying to use JAXB to annotate
>> and serialize objects in Scala.  Here's my class
>>
>> @XmlRootElement{val name="catalog"}
>> class CatalogRepresentation(
>>  @XmlElement private val name: String
>> ) {
>>  // no-arg ctor for JAXB
>>  def this() = this("")
>> }
>>
>> When trying to serialize this class with JAXB, I get an error stating,
>> "JAXB annotation is placed on a method that is not a JAXB property".
>>
>> I had no clue what this meant, so I tried looking at the bytecode
>> generated by the Scala compiler.  This class seems to turn into the
>> following Java equivalent
>>
>> @XmlRootElement{val name="catalog"}
>> public class CatalogRepresentation {
>>  @XmlElement private String name;
>>
>>  // omitting ctors for brevity
>>
>>  @XmlElement
>>  public String name() { return name; }
>> }
>>
>> It's the @XmlElement annotation on the name() method that is tripping
>> up JAXB.  I guess I'm wondering if copying the field annotation to the
>> Scala accessor is really the right thing to do or should this be fixed
>> in the Scala compiler?  Also, can anyone think of any workarounds that
>> don't involve having to override the autogenerated accessors, which I
>> can do but is a real PITA.
>>
>> Thanks,
>> Rich
>
>

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