前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Create dynamic proxy persistently in Java and ABAP

Create dynamic proxy persistently in Java and ABAP

作者头像
Jerry Wang
发布2019-12-17 18:45:33
2530
发布2019-12-17 18:45:33
举报

In Implement CGLIB in ABAP I explain how to create a transient proxy class via dynamically creating sub class. Suppose for your use case you need a persistent proxy class instead, you can try this approach below. For how to achieve it in ABAP, see detail in my blog Create dynamic proxy persistently in Java and ABAP.

代码语言:javascript
复制
class HelloWorldImp implements IHelloWorld {
    public void print() {
        System.out.println("Hello World");
    }
}

public class DynamicProxyDemo implements Serializable {
    private static final long serialVersionUID = 1L;

    public static void main(String[] arg) throws Exception {
        Class<?> c = getProxyClass();
        Constructor<?> constructor = c.getConstructor(IHelloWorld.class);
        IHelloWorld helloWorldImpl = new HelloWorldImp();
        IHelloWorld helloWorld = (IHelloWorld) constructor.newInstance(helloWorldImpl);

        helloWorld.print();
    }

    private static String getSourceCode() {
        String src = "package dynamicproxy;\n\n"
                + "public class DynamicProxy implements IHelloWorld\n" + "{\n"
                + "\tIHelloWorld helloWorld;\n\n"
                + "\tpublic DynamicProxy(IHelloWorld helloWorld)\n" + "\t{\n"
                + "\t\tthis.helloWorld = helloWorld;\n" + "\t}\n\n"
                + "\tpublic void print()\n" + "\t{\n"
                + "\t\tSystem.out.println(\"Before Hello World!\");\n"
                + "\t\thelloWorld.print();\n"
                + "\t\tSystem.out.println(\"After Hello World!\");\n" + "\t}\n"
                + "}";
        return src;
    }

    private static String createJavaFile(String sourceCode) {
        // TODO: avoid using absolute file path in productive code
        String fileName = "C:\\Users\\i042416\\git\\JavaTwoPlusTwoEquals5\\src\\dynamicproxy\\DynamicProxy.java";
        File javaFile = new File(fileName);
        Writer writer;
        try {
            writer = new FileWriter(javaFile);
            writer.write(sourceCode);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName;
    }

    private static void compile(String fileName) {
        try {
            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            StandardJavaFileManager sjfm = compiler.getStandardFileManager(null, null, null);
            Iterable<? extends JavaFileObject> iter = sjfm.getJavaFileObjects(fileName);
            CompilationTask ct = compiler.getTask(null, sjfm, null, null, null, iter);
            ct.call();
            sjfm.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static Class<?> loadClass() {
        URL[] urls;
        Class<?> c = null;
        try {
            urls = new URL[] { (new URL("file:\\"
                    + "C:\\Users\\i042416\\git\\JavaTwoPlusTwoEquals5\\src\\")) };
            URLClassLoader ul = new URLClassLoader(urls);
            c = ul.loadClass("dynamicproxy.DynamicProxy");
            System.out.println("Class loaded successfully: " + c.getName());
        } catch (MalformedURLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return c;
    }

    private static Class<?> getProxyClass() {
        String sourceCode = getSourceCode();
        String javaFile = createJavaFile(sourceCode);
        compile(javaFile);
        return loadClass();
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-12-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档