stevedlawrence commented on a change in pull request #279: WIP: User Defined Functions feature URL: https://github.com/apache/incubator-daffodil/pull/279#discussion_r338534774
########## File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/udf/UserDefinedFunctionService.scala ########## @@ -0,0 +1,271 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.daffodil.udf + +import collection.JavaConverters._ +import collection.mutable._; +import java.util.ServiceLoader +import java.util.ServiceConfigurationError +import org.apache.daffodil.util.Misc +import org.apache.daffodil.util.LogLevel +import org.apache.daffodil.util.Logging +import java.lang.reflect.Method +import org.apache.daffodil.dpath.NodeInfo +import java.io.ObjectOutputStream +import java.io.Serializable +import java.io.ObjectInputStream + +/** + * Loads, validates and caches (for use at schema compile time) all User Defined Functions + * and their Providers from the classpath. + * + */ +object UserDefinedFunctionService extends Logging { + lazy val evaluateMethodName = "evaluate" + + /** + * Class to make the evaluate Method serializable and to optimize our reflection lookups + * to happen once (per call) during deserialization, rather than at evaluation time. + */ + case class UserDefinedFunctionMethod( + val decClass: Class[_], + val methodName: String, + val paramTypes: Array[Class[_]]) + extends Serializable { + + @transient lazy val method: Method = { + lookupMethod + } + + def lookupMethod() = { + val m = decClass.getMethod(methodName, paramTypes: _*) + m + } + + @throws(classOf[java.io.IOException]) + private def writeObject(out: ObjectOutputStream) : Unit = { + out.defaultWriteObject() + } + + @throws(classOf[java.io.IOException]) + @throws(classOf[java.lang.ClassNotFoundException]) + private def readObject(in: ObjectInputStream): Unit = { + in.defaultReadObject() + this.method + } + } Review comment: Can you remove the writeObject since all it oes it all the default? We should probably also call "method" in the constructor to force the lookup when this is created rather than the first time the lazy "method" is used so all lookups happen at compile time or deserialization time. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services