Skip to content

Instantly share code, notes, and snippets.

@wuchong
Last active June 29, 2017 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wuchong/4d6fc04c131ebcb544665f6518ac0a5e to your computer and use it in GitHub Desktop.
Save wuchong/4d6fc04c131ebcb544665f6518ac0a5e to your computer and use it in GitHub Desktop.
/*
* 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.flink.table.expressions.utils
import org.apache.flink.api.common.functions.{Function, MapFunction}
import org.apache.flink.table.api.{TableConfig, Types}
import org.apache.flink.table.codegen.{CodeGenerator, Compiler, GeneratedFunction}
import org.apache.flink.util.InstantiationUtil
object CompileTest {
// TestCompiler that uses current class loader
class TestCompiler2[F <: Function, T <: Any] extends Compiler[F] {
def compile(genFunc: GeneratedFunction[F, T]): Class[F] =
compile(getClass.getClassLoader, genFunc.name, genFunc.code)
}
def main(args: Array[String]): Unit = {
val compiler = new TestCompiler2[MapFunction[String, String], String]()
val generator = new CodeGenerator(new TableConfig, false, Types.STRING)
val cody =
s"""
|String res = ${generator.input1Term} + " world!";
|return res;
""".stripMargin
val genFunc = generator.generateFunction[MapFunction[String, String], String](
"TestFunction",
classOf[MapFunction[String, String]],
cody,
Types.STRING)
val clazz = compiler.compile(genFunc)
val obj2 = clazz.newInstance()
val janinoCL = obj2.getClass.getClassLoader
// print: TestFunction$0
println(obj2.getClass.getCanonicalName)
// print: org.codehaus.janino.ByteArrayClassLoader@341b80b2
println(janinoCL)
val bytes = InstantiationUtil.serializeObject(obj2)
val map: MapFunction[String, String] = InstantiationUtil.deserializeObject(bytes, janinoCL)
// print: hello world!
println(map.map("hello"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment