CodeGen实现代码生成器

项目背景:

在线开发平台web端需要对 输入的Sql进行校验,使提交的Sql务必可在Flink平台执行,尤其针对 自定义UDF 需要提前校验。

方案:

Flink自身支持对原生系统函数进行校验,但是 自定义UDF 由用户业务决定,无法全量支持,因此 打算采用 CodeGen 编译 自定义UDF 后,进行校验。
(Flink 内部,在 由 Sql -> DataStream 时,也使用了 CodeGen 代码生成器功能)

demo说明:

* Student 继承自 AbstractChinese, AbstractChinese 继承自 Person
* Student类并不直接使用,而是拷贝字符串文本 到 Test.java 中作为 String 使用
* Test.java为测试类

代码

Person.java

public interface Person extends Serializable {

    public String print();

}

AbstractChinese.java

public abstract class AbstractChinese implements Person {

    private static final long serialVersionUID = 1L;

    public abstract String getName();

    public abstract void setName(String name);

    public abstract int getAge();

    public abstract void setAge(int age);

}

Student.java

Student类并不直接使用,而是拷贝字符串文本 到 Test.java 中作为 String 使用。

注意:Student类文本中,Student继承AbstractChinese抽象类时,必须是 类的全路径名,否则 CodeGen 在 compile Student时,会编译不过!

public class Student extends com.wj.codegen.AbstractChinese {

    private String name;

    private int age;

    public Student() {
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int getAge() {
        return age;
    }

    @Override
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String print() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

}

Test.java

public class Test {

    private static String code = "public class Student extends com.wj.codegen.AbstractChinese { " +
            " " +
            "    private String name; " +
            " " +
            "    private int age; " +
            " " +
            "    public Student() { " +
            "    } " +
            " " +
            "    @Override " +
            "    public String getName() { " +
            "        return name; " +
            "    } " +
            " " +
            "    @Override " +
            "    public void setName(String name) { " +
            "        this.name = name; " +
            "    } " +
            " " +
            "    @Override " +
            "    public int getAge() { " +
            "        return age; " +
            "    } " +
            " " +
            "    @Override " +
            "    public void setAge(int age) { " +
            "        this.age = age; " +
            "    } " +
            " " +
            "    @Override " +
            "    public String print() { " +
            "        return \"Student{\" + " +
            "                \"name='\" + name + '\\'' + " +
            "                \", age=\" + age + " +
            "                '}'; " +
            "    } " +
            " " +
            "}";


    public static void main(String[] args) throws Exception {
        Class clazz = getClazz();

        AbstractChinese instance = clazz.newInstance();

        instance.setName("wj");
        instance.setAge(28);

        System.out.println(instance.print());
    }

    private static Class getClazz() throws Exception {
        SimpleCompiler compiler = new SimpleCompiler();
        compiler.setParentClassLoader(Test.class.getClassLoader());

        compiler.cook(code);

        return (Class) compiler.getClassLoader().loadClass("Student");
    }
}

测试结果

CodeGen实现代码生成器_第1张图片

你可能感兴趣的:(项目预研,CodeGen)