Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 6.50 KB | None | 0 0
  1. package org.extrasapache.myfaces.codi.examples.ebean.view.jsf
  2.  
  3. import javax.el.{ELContext, ELResolver}
  4. import java.beans.FeatureDescriptor
  5. import java.lang.reflect.Method
  6.  
  7. import collection.JavaConversions._
  8. import collection.mutable.HashSet
  9. import org.extrasapache.myfaces.codi.examples.ebean.support.lang.ReflectUtil
  10.  
  11. /**
  12.  *
  13.  * @author Werner Punz (latest modification by $Author$)
  14.  * @version $Revision$ $Date$
  15.  *
  16.  * A custom el resolver which needs to do following things
  17.  * call the getters and setters of scala according to the scala convention of
  18.  * attr() and attr_$eq(value)
  19.  *
  20.  * transform the incoming or outgoing lists, maps and arrays
  21.  * from the scala maps an arrays to their java representations
  22.  * (if needed, since the el works with introspection alone
  23.  * it might work without the conversion)
  24.  *
  25.  * delegate the rest to the other el resolvers in the chain
  26.  */
  27.  
  28. class ScalaELResolver extends ELResolver {
  29.  
  30.   def getCommonPropertyType(elContext: ELContext, o: AnyRef): Class[_] = {
  31.     o.getClass
  32.   }
  33.  
  34.   def getFeatureDescriptors(elContext: ELContext, base: AnyRef): java.util.Iterator[FeatureDescriptor] = {
  35.  
  36.     if (base.isInstanceOf[scala.ScalaObject]) {
  37.       //no scala object we forward it to another el
  38.       //resolver
  39.       null
  40.     } else {
  41.       val ret = new HashSet[FeatureDescriptor]
  42.       //We have to iterate over all properties of the base and return it
  43.       //as feature descriptor instance
  44.       val methods: Array[Method] = base.getClass().getMethods
  45.       val alreadyProcessed = new HashSet[String]
  46.       for (method <- methods if (!alreadyProcessed.contains(method.getName.replaceAll("\\_eq\\$", "")))) {
  47.         //note every attribute of a scala object
  48.         //is set as protected or private
  49.         //with two encapsulating functions
  50.         var methodName: String = method.getName.replaceAll("\\_\\$eq", "")
  51.         alreadyProcessed += methodName
  52.         //TODO we probably have to work the return values in
  53.         ret += makeDescriptor(methodName, methodName, base.getClass)
  54.       }
  55.  
  56.       asJavaSet[FeatureDescriptor](ret).iterator
  57.     }
  58.  
  59.   }
  60.  
  61.   /**
  62.    * backported from myfaces
  63.    */
  64.   private def makeDescriptor(name: String, description: String,
  65.                              objectType: Class[_]): FeatureDescriptor = {
  66.     val fd = new FeatureDescriptor()
  67.     fd.setValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME, true)
  68.     fd.setValue(ELResolver.TYPE, objectType)
  69.     fd.setName(name)
  70.     fd.setDisplayName(name)
  71.     fd.setShortDescription(description)
  72.     fd.setExpert(false)
  73.     fd.setHidden(false)
  74.     fd.setPreferred(true)
  75.     fd
  76.   }
  77.  
  78.   def getType(elContext: ELContext, base: AnyRef, prop: AnyRef): Class[_] = {
  79.     if (prop == "groupName") {
  80.       println("debugpoint found")
  81.     }
  82.     if (base == null || !base.isInstanceOf[scala.ScalaObject]) null
  83.     else if (base != null && prop == null) null
  84.     else {
  85.       var methods = ReflectUtil.getAllMethods(base.getClass(), prop.asInstanceOf[String], 0)
  86.       if(methods != null && methods.size > 0) {
  87.         elContext.setPropertyResolved(true)
  88.         methods.iterator.next.getReturnType
  89.       } else {
  90.         //lets delegate the analysis into the subsequent sections of the chain
  91.         null
  92.       }
  93.     }
  94.   }
  95.  
  96.   def getValue(elContext: ELContext, base: AnyRef, prop: AnyRef): AnyRef = {
  97.      if (prop == "groupName") {
  98.       println("debugpoint found")
  99.     }
  100.     if (!(base != null && base.isInstanceOf[scala.ScalaObject])) {
  101.       null
  102.     } else {
  103.       var methods = ReflectUtil.getAllMethods(base.getClass(), prop.asInstanceOf[String], 0)
  104.       if (methods != null && methods.size > 0) {
  105.         var res = ReflectUtil.executeMethod(base, prop.asInstanceOf[String])
  106.         elContext.setPropertyResolved(true)
  107.         res
  108.       } else {
  109.         null
  110.       }
  111.     }
  112.   }
  113.  
  114.   def isReadOnly(elContext: ELContext, base: AnyRef, prop: AnyRef): Boolean = {
  115.     if (!(base != null && base.isInstanceOf[scala.ScalaObject])) {
  116.       true
  117.     } else {
  118.       def methodName: String = prop.asInstanceOf[String]
  119.       def setterName = methodName + "_$eq"
  120.       if (base.getClass.getMethod(setterName) != null) {
  121.         elContext.setPropertyResolved(true)
  122.         false
  123.       } else {
  124.         val m: Method = base.getClass.getMethod(methodName)
  125.         val paramTypes = m.getParameterTypes
  126.         val ret = !(paramTypes != null && paramTypes.length > 0)
  127.         elContext.setPropertyResolved(true)
  128.         ret
  129.       }
  130.     }
  131.   }
  132.  
  133.   def setValue(elContext: ELContext, base: AnyRef, prop: AnyRef, value: AnyRef) {
  134.     if (prop == "groupType") {
  135.       println("debugpoint found")
  136.     }
  137.     if (base != null && base.isInstanceOf[scala.ScalaObject]) {
  138.       def methodName: String = prop.asInstanceOf[String]
  139.       def setterName = methodName + "_$eq"
  140.       val setMethod = ReflectUtil.getAllMethods(base.getClass(), methodName, 1)
  141.       val setterMethod = ReflectUtil.getAllMethods(base.getClass(), setterName, 1)
  142.  
  143.  
  144.       if (setMethod != null && setMethod.size > 0) {
  145.         val transformedValue = getValueType(setMethod.iterator.next, value)
  146.         ReflectUtil.executeMethod(base, methodName, transformedValue)
  147.         elContext.setPropertyResolved(true)
  148.       } else if (setterMethod != null && setterMethod.size > 0) {
  149.         val transformedValue = getValueType(setterMethod.iterator.next, value)
  150.         ReflectUtil.executeMethod(base, setterName, transformedValue)
  151.         elContext.setPropertyResolved(true)
  152.       }
  153.  
  154.     }
  155.   }
  156.  
  157.   def getValueType(method: Method, theVal: AnyRef): AnyRef = {
  158.     if (!theVal.isInstanceOf[String]) {
  159.       theVal
  160.     } else {
  161.       val strVal = theVal.asInstanceOf[String]
  162.       val paramTypes: Array[Class[_]] = method.getParameterTypes()
  163.       val initParam = paramTypes.apply(0)
  164.       if (initParam == classOf[String]) {
  165.         theVal
  166.       } else if (initParam == classOf[Int]) {
  167.         strVal.toInt.asInstanceOf[AnyRef]
  168.       } else if (initParam == classOf[Long]) {
  169.         strVal.toLong.asInstanceOf[AnyRef]
  170.       } else if (initParam == classOf[Float]) {
  171.         strVal.toFloat.asInstanceOf[AnyRef]
  172.       } else if (initParam == classOf[Double]) {
  173.         strVal.toDouble.asInstanceOf[AnyRef]
  174.       } else if (initParam == classOf[Boolean]) {
  175.         strVal.toBoolean.asInstanceOf[AnyRef]
  176.       } else if (initParam == classOf[Byte]) {
  177.         strVal.toByte.asInstanceOf[AnyRef]
  178.       } else if (initParam == classOf[Char]) {
  179.         strVal.toCharArray.apply(0).asInstanceOf[AnyRef]
  180.       } else {
  181.         theVal
  182.       }
  183.     }
  184.   }
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement