BeanProperty annotation to modify scala getter/setter

739 views
Skip to first unread message

Robin Palotai

unread,
Mar 17, 2011, 7:33:44 AM3/17/11
to scala...@googlegroups.com
Hello,

As I understand, the @BeanProperty annotation just generates the appropriate getter/setter. Is there a way to make it also tunnel the standard Scala getter/setter through these generated bean getter/setters? Therefore using the Scala getter would still use the getter, which may have special behavior if proxied (I think of Hibernate and lazy loading).

-----
scala> import scala.reflect.BeanProperty
import scala.reflect.BeanProperty

scala> class X() {
     |   @BeanProperty var x: Int = _
     | }
defined class X

scala> class Y extends X {
     |   override def getX(): Int = {
     |     println("side-effect")
     |     5
     |   }
     | }
defined class Y

scala> val y = new Y
y: Y = Y@7e05c4

scala> y.getX
side-effect
res0: Int = 5

scala> y.x
res1: Int = 0
-----

What do you think?
Robin

Cay Horstmann

unread,
Mar 18, 2011, 9:53:23 AM3/18/11
to Robin Palotai, scala...@googlegroups.com
When you declare

@BeanProperty var x: Int = _

you get four distinct methods: getX, setX, x, and x_=. You cannot
override x or x_= in a subclass--you get an error "cannot override a
mutable variable". That makes it impossible for you to link the Java
getX/setX methods with the Scala counterparts.

I have to agree that this is less than ideal. When I use
@BeanProperty, I pretend the Scala getter/setter methods don't exist.
Perhaps it would be nice if I could do

@BeanProperty private var x: Int = _

so that this would be more enforceable, but that's not legal. It
probably should be, and it should generate public JavaBeans
getters/setters, which the Scala folks will find weird.

Cheers,

Cay

2011/3/17 Robin Palotai <m.palot...@gmail.com>:

Robin P

unread,
Mar 19, 2011, 5:05:24 AM3/19/11
to scala-user
I don't want to override x or x_= in a subclass, but would like the
compiler to generate x and x_= to use getX and setX respectively,
instead of directly accessing the underlying x_.

Robin

On Mar 18, 2:53 pm, Cay Horstmann <cay.horstm...@gmail.com> wrote:
> When you declare
>
> @BeanProperty var x: Int = _
>
> you get four distinct methods: getX, setX, x, and x_=. You cannot
> override x or x_= in a subclass--you get an error "cannot override a
> mutable variable". That makes it impossible for you to link the Java
> getX/setX methods with the Scala counterparts.
>
> I have to agree that this is less than ideal. When I use
> @BeanProperty, I pretend the Scala getter/setter methods don't exist.
> Perhaps it would be nice if I could do
>
> @BeanProperty private var x: Int = _
>
> so that this would be more enforceable, but that's not legal. It
> probably should be, and it should generate public JavaBeans
> getters/setters, which the Scala folks will find weird.
>
> Cheers,
>
> Cay
>
> 2011/3/17 Robin Palotai <m.palotai.ro...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages