ExecClassクラスの改修
改修項目
- String型からDate型への変換を追加
- InvocationTargetExceptionの例外処理
package ews import java.lang.reflect.{Field,InvocationTargetException,Method,Type} import java.math.BigDecimal import java.util.{Calendar,Date} import java.text.SimpleDateFormat import java.sql.{CallableStatement, PreparedStatement, ResultSet,Timestamp} import javax.persistence.{Column,Entity,Id,Table,Lob,UniqueConstraint,SequenceGenerator} import seedo.Util /** * The function is executed specifying the class. */ class ExecClass(className:String ) { val myClass = Class.forName(className) // The class is gotten from the class name. val Obj = myClass.newInstance.asInstanceOf[AnyRef] // create Object val clazz:Class[_] = Obj.getClass // Class of Bean /** * It executes it specifying the type of the argument of the function. */ def execStr(methodName:String,value:String) : AnyRef = { try { // The function with the Int in the argument is called. val v = value.toInt return exec(methodName,v) } catch { case e:Exception => {} } try { // The function with the Long in the argument is called. val v = value.toLong return exec(methodName,v) } catch { case e:Exception => {} } try { // The function with the Double in the argument is called. val v = value.toDouble return exec(methodName,v) } catch { case e:Exception => {} } val DATE_PATTERN = "yyyy/MM/dd" val sdf = new SimpleDateFormat(DATE_PATTERN) try { val v = sdf.parse(value) return exec(methodName,v) } catch { case e:Exception => {} } return exec(methodName,value) } /** * The function with the argument is executed. */ def exec(methodName:String,value:Any) : AnyRef = { if(value == null){ val method:Method = clazz.getMethod(methodName) return method.invoke(Obj) } // The function is executed specifying the type of the argument of the function. if(value.isInstanceOf[Int]){// The function with the Int in the argument is called. val method = clazz.getMethod(methodName, classOf[Int]) val v = new Integer(value.asInstanceOf[Int]) try { return method.invoke(Obj, v) } catch { case e:InvocationTargetException => { throw new Exception(e.getCause) } } } else if(value.isInstanceOf[Long]){// The function with the Long in the argument is called. val method = clazz.getMethod(methodName, classOf[Long]) val v = new java.lang.Float(value.asInstanceOf[Long]) try { return method.invoke(Obj, v) } catch { case e:InvocationTargetException => { throw new Exception(e.getCause) } } } else if(value.isInstanceOf[Float]){// The function with the Float in the argument is called. val method = clazz.getMethod(methodName, classOf[Float]) val v = new java.lang.Float(value.asInstanceOf[Float]) try { return method.invoke(Obj, v) } catch { case e:InvocationTargetException => { throw new Exception(e.getCause) } } } else if(value.isInstanceOf[Double]){// The function with the Double in the argument is called. val method = clazz.getMethod(methodName, classOf[Double]) val v = new java.lang.Double(value.asInstanceOf[Double]) try { return method.invoke(Obj, v) } catch { case e:InvocationTargetException => { throw new Exception(e.getCause) } } } else if(value.isInstanceOf[Char]){// The function with the Char in the argument is called. val method = clazz.getMethod(methodName, classOf[Char]) val v = new Character(value.asInstanceOf[Char]) try { return method.invoke(Obj, v) } catch { case e:InvocationTargetException => { throw new Exception(e.getCause) } } } else { // other class var valClass:Class[_] = value.asInstanceOf[AnyRef].getClass val method = clazz.getMethod(methodName, valClass) try { return method.invoke(Obj, value.asInstanceOf[AnyRef]) } catch { case e:InvocationTargetException => { throw new Exception(e.getCause) } } } } /** * The function is executed. */ def exec(methodName:String) : AnyRef ={ if(methodName == null) return null try { val method:Method = clazz.getMethod(methodName) return method.invoke(Obj) } catch { case e:InvocationTargetException => { throw new Exception(e.getCause) } case e:Exception => { throw new Exception(e) } } } }
改修したExecClassクラスをテストするためのHelloWorld3 クラス。
- 引数なしtb関数に置換元を格納するtag変数からspanタグを取り出し、HelloWorld文字列に追加
- 整数を引数に持つtb関数にパラメータの値を、HelloWorld文字列に追加
- Date型を引数に持つtb関数を追加
package ews import java.util.{HashMap,Date} import scala.collection.JavaConversions._ import scala.xml._ /** * A class that's instantiated early and run. It allows the application * to modify Seedo's environment */ class HelloWorld3 { var tag:String = null // Replaced original tag def setTag(tg:String): Unit = {tag = tg} var parameterMap:HashMap[String,Array[String]] = null // Parameter from request of servlet def setParameterMap(pm:HashMap[String,Array[String]]) : Unit = {parameterMap = pm} // Function without argument def tb : String = { var xml : Node = null var str : String = "" if(tag != null){ xml = XML.loadString(tag) val l = xml \\ "span" l.foreach(v=> str += "" + v) } "HelloWorld " + new Date + str } def tb(no:Int) : String = { var str : String = " parameterMap: " if(parameterMap!=null){ println(parameterMap.size) parameterMap.foreach(v => { str += v._1 + "=" v._2.foreach(w => str += w + "," ) }) } "HelloWorld Int [" + no + "] " + new Date + str } def tb(no:Double) : String = { "HelloWorld Double [" + no + "] " + new Date } def tb(no:Date) : String = { "HelloWorld " + no.getClass + " [" + no + "] " + new Date } def tb(no:String) : String = { "HelloWorld " + no.getClass + " [" + no + "] " + new Date } }
HTMLファイルは、helloworld3.html
<!doctype html> <HTML> <HEAD> <title>Home</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </HEAD> <BODY> <h2>Example3</h2> The current time is <div class="seedo:ews.HelloWorld3:tb"> <div>The current time is <span class="seedo:ews.HelloWorld3:tb:999999999">now</span>.</div> <div>The current time is <span class="seedo:ews.HelloWorld3:tb:こんばんわ">now</span>.</div> </div>. </BODY> </HTML>
結果
コメントを残す
コメントを投稿するにはログインしてください。