Object

org.clapper.classutil

ScalaObjectToBean

Related Doc: package classutil

Permalink

object ScalaObjectToBean

ScalaObjectToBean contains functions that allow you to map a Scala object into a read-only Java bean or merely generate an interface for such a bean. The functions take a Scala object or class (depending), locate the Scala accessors (using simple heuristics defined in ClassUtil), and generate a new interface or object with additional Java Bean get and set methods for the accessors.

This kind of wrapping is an alternative to using the @BeanProperty annotation on classes, so it is useful for mapping case classes into Java Beans, or for mapping classes from other APIs into Java Beans without having to extend them.

ScalaObjectToBean uses the following heuristics to determine which fields to map.

First, it recognizes that any Scala val or var is really a getter method returning some type. That is:

val x: Int = 0
var y: Int = 10

is compiled down to the equivalent of the following Java code:

private int _x = 0;
private int _y = 10;

public int x() { return _x; }
public int y() { return _y; }
public void y_$eq(int newY) { _y = newY; }

So, the mapper looks for Scala getter methods that take no parameters and return some non-void (i.e., non-Unit) value, and it looks for Scala setter methods that take one parameter, return void (Unit) and have names ending in "_$eq". Then, from that set of methods, the mapper discards:

- Methods starting with "get" - Methods that have a corresponding "get" method. In the above example, if there's a getX() method that returns an int, the mapper will assume that it's the bean version of x(), and it will ignore x(). - Methods that aren't public. - Any method in java.lang.Object. - Any method in scala.Product.

If there are any methods in the remaining set, then the mapper returns a new wrapper object that contains Java Bean versions of those methods; otherwise, the mapper returns the original Scala object. The resulting bean delegates its calls to the original object, instead of capturing the object's method values at the time the bean is called. That way, if the underlying Scala object's methods return different values for each call, the bean will reflect those changes.

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. ScalaObjectToBean
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. def apply(obj: Any, className: String, recurse: Boolean): AnyRef

    Permalink

    Transform an object into an object.

    Transform an object into an object. The class name will be generated, will be in the org.clapper.classutil package, and will have a class name prefix of ScalaObjectToBean_.

    obj

    the Scala object

    className

    the desired class name

    recurse

    true to recursively map nested maps, false otherwise. Recursively mapped maps will have generated class names.

    returns

    an instantiated object representing the map

  5. def apply(obj: Any, recurse: Boolean = true): AnyRef

    Permalink

    Transform an object into an object.

    Transform an object into an object. The class name will be generated, will be in the org.clapper.classutil package, and will have a class name prefix of ScalaObjectBean_.

    obj

    the Scala object

    recurse

    true to recursively map nested maps, false otherwise

    returns

    an instantiated object representing the map

  6. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  7. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  8. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  10. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  11. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  12. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  13. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  14. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  15. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  16. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  17. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  18. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  19. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  22. def withResultMapper(obj: Any, className: String = "", recurse: Boolean = true)(cb: (String, AnyRef) ⇒ AnyRef): AnyRef

    Permalink

    Transform an object into an object.

    Transform an object into an object. The class name will be generated, will be in the org.clapper.classutil package, and will have a class name prefix of ScalaObjectToBean_. This version of the call allows you to intercept each method call and possibly transform the results.

    Note that the cb parameter will be receiving an AnyRef result (i.e., java.lang.Object). That means any Scala primitives are really boxed Java values. In addition, it's important that the method return an object. For instance, consider wrapping the following class in a bean:

    case class Foo(x: Int, y: Double)

    To post-process a bean generated from that class, you might use code like the following:

    val bean = ScalaObjectToBean.withResultMapper(Foo(10, 20.0)) { (name, res) =>
      (name, res) match {
        case ("getX", n: java.lang.Integer) => new Integer(n * 100)
        case ("getY", d: java.lang.Double)  => new java.lang.Double(d / 10.0)
        case _                              => res
      }
    }

    By contrast, the following will not compile, because (a) it attempts to pattern match against Int, and (b) it attempts to return a Scala Double.

    val bean = ScalaObjectToBean.withResultMapper(Foo(10, 20.0)) { (name, res) =>
      (name, res) match {
        case ("getX", n: Int) => new Integer(n * 100)
        //               ^ pattern type is incompatible with expected type
    
        case ("getY", d: java.lang.Double) => d / 10.0
        //                                      ^ the result type of an
        //                                        implicit conversaion must
        //                                        be more specific than AnyRef
        case _ => res
      }
    }

    Note, too, that if recurse is true (the default), the result value passed to your post-call function will be a java.lang.reflect.Proxy for any non-primitive, non-String object.

    case class Foo(i: Int)
    case class Bar(name: String, foo: Foo)
    val bean = ScalaObjectToBean(Bar("quux", Foo(10)) { (name, res) =>
      (name, res) match {
        case ("getName", n) => // n will be a String
        case ("getFoo", f)  => // f will be a java.lang.reflect.Proxy
        case ("name",   n)  => // n will be a String
        case ("foo",    f)  => // f will be a java.lang.reflect.Proxy
      }
    }
    obj

    the Scala object

    className

    the desired class name, or an empty string to generate a default. (Empty string is the default.)

    recurse

    true to recursively map nested maps, false otherwise. Recursively mapped maps will have generated class names. Defaults to false.

    cb

    If defined, this parameter is a function that is invoked with the result of any method call, along with the called method's name. It can be used to transform the result before the result is returned.

    returns

    an instantiated object representing the map

Inherited from AnyRef

Inherited from Any

Ungrouped