I’m currently using Scala with JSF, of which the two play pretty well together. However at times JSF needs to re-instantiate (via Class.newInstance) a data structure, like a list. For example in a managed bean I have:
@BeanProperty
var countries: java.util.List[String] = List("US").asJava
Which works fine until you get to JSF’s process validation phase where it runs into java.lang.InstantiationException:
java.lang.InstantiationException: scala.collection.JavaConversions$SeqWrapper
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
at com.sun.faces.renderkit.html_basic.MenuRenderer.createCollection(MenuRenderer.java:906)
at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectManyValuesForModel(MenuRenderer.java:366)
at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectManyValue(MenuRenderer.java:128)
at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:314)
at org.primefaces.component.selectcheckboxmenu.SelectCheckboxMenuRenderer.getConvertedValue(SelectCheckboxMenuRenderer.java:34)
...
From a fundamental level it makes sense that Wrappers may not be re-instantiated from scratch, so using JavaConverters won’t work well here. My question is there library that already provides a complete Data Structure mapping/conversion without wrappers? If not I’ll just write my own internal ones.
Use a Java
ArrayListas thevarand then useJavaConverters/JavaConversionsin your code to manipulate. That’s the usual approach I use for APIs like Hibernate, JAX-WS, JSR-303, etc. that need Java collections.or
If you really want to just convert back and forth and not wrap it’s easy enough without creating your own classes:
But then you have to worry about the cost of copying and about when synchronization needs to happen. Wrapping/decorating just a lot more convenient.