- @BeanProperty - Generates getters and setters for a property. (Or just a getter if the bean is a val)
- @BeanInfo - Generates the associated BeanInfo for the class
Remember that if you want frameworks to be able to create the beans you need to make a 0-arg constructor and have the bean properties as fields.
Example with bean-properties as constructor arguments. This example requires Scala 2.8 because it uses BooleanBeanProperty. You can remove that and run with Scala 2.7.x.
In this example we define a bean class and use reflection to show that all the getters and setters are generated:
- scala> import scala.reflect._
- import scala.reflect._
- scala> case class MyBean(
- | @BeanProperty var mutable:String, // getter and setter should be generated
- | @BooleanBeanProperty var boolProp:Boolean, // is getter and normal setter should be generated
- | @BeanProperty val readOnly:Int) // only a getter should be generated
- defined class MyBean
- scala> val beanClass = classOf[MyBean]
- beanClass: java.lang.Class[MyBean] = class MyBean
- scala> val beanMethods = beanClass.getDeclaredMethods.filter( _.getName.matches("(get|is|set).+"))
- beanMethods: Array[java.lang.reflect.Method] = Array(public java.lang.String MyBean.getMutable(), public boolean MyBean.isBoolProp(), public int MyBean.getReadOnly(), public void MyBean.setBoolProp(boolean\
- ), public void MyBean.setMutable(java.lang.String))
- scala> println( beanMethods map (_.getName) mkString("\n"))
- getMutable
- isBoolProp
- getReadOnly
- setBoolProp
- setMutable
This next example would not run in the REPL for some reason so you must copy-paste it into a file, compile and run. Instructions:
- copy and paste code into bean.scala
- scalac bean.scala
- scala -cp . MyBean
- import scala.reflect._
- class MyBean{
- @BeanProperty
- var mutable:String = ""
- }
- object MyBean{
- def apply(mutable:String) = {
- val bean = new MyBean()
- bean.mutable = mutable
- bean
- }
- def main(args:Array[String]){
- import java.beans._
- val result = new java.io.ByteArrayOutputStream
- val encoder = new XMLEncoder(result)
- val bean = MyBean("hello")
- encoder.writeObject( bean )
- encoder.close
- println(result.toString)
- }
- }
Great article that gave me exactly what I was looking for, as a scala newbie.
ReplyDeleteI simplified the last example code a bit. Posting it for general benefit:
import scala.reflect._
class MyBean1{
@BeanProperty
var mutable:String = ""
}
object Runner{
def main(args:Array[String]){
import java.beans._
val result = new java.io.ByteArrayOutputStream
val encoder = new XMLEncoder(result)
val bean = new MyBean1
bean.mutable = "hello"
encoder.writeObject( bean )
encoder.close
println(result.toString)
}
}
Secondly, to run it from scala cmd line interpreter, ignore the Runner class and just post all the lines in its main method directly onto the interpreter.
Hope this helps.
/Ashish