在运行时创建简单的POJO类(字节码)(动态) [英] Create simple POJO classes (bytecode) at runtime (dynamically)

查看:136
本文介绍了在运行时创建简单的POJO类(字节码)(动态)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况..

我正在编写一些工具,对数据库运行用户输入的查询并返回结果..

I am writing some tool that run user-entered query against the database and return the result..

最简单的方法是将结果返回为: List< String []> 但我需要更进一步。

The simplest way is to return the result as: List<String[]> but I need to take this a step further.

我需要创建(在运行时)一些带有名称的POJO(或DTO),并为它创建字段和setter以及getter。使用返回的数据填充它,然后使用生成的 .class 文件将其返回给用户...

I need to create (at runtime) some POJO (or DTO) with some name and create fields and setters and getters for it and populate it with the data returned and then return it to the user among with the .class file generated...

所以这里的想法是如何在运行时创建简单的类(字节码)(动态)
我做了一个基本的搜索,发现很多lib 包括Apache BCEL 但我认为我需要更简单的东西...

So the idea here is How to Create simple class(bytecode) at runtime (dynamically) I do a basic search and found many lib including Apache BCEL But I think I need something more simpler...

你怎么看待这个?

谢谢。

推荐答案

使用getter创建一个简单的POJO如果您使用 CGLib ,则很容易设置:

Creating a simple POJO with getters and setters is easy if you use CGLib:

public static Class<?> createBeanClass(
    /* fully qualified class name */
    final String className,
    /* bean properties, name -> type */
    final Map<String, Class<?>> properties){

    final BeanGenerator beanGenerator = new BeanGenerator();

    /* use our own hard coded class name instead of a real naming policy */
    beanGenerator.setNamingPolicy(new NamingPolicy(){
        @Override public String getClassName(final String prefix,
            final String source, final Object key, final Predicate names){
            return className;
        }});
    BeanGenerator.addProperties(beanGenerator, properties);
    return (Class<?>) beanGenerator.createClass();
}

测试代码:

public static void main(final String[] args) throws Exception{
    final Map<String, Class<?>> properties =
        new HashMap<String, Class<?>>();
    properties.put("foo", Integer.class);
    properties.put("bar", String.class);
    properties.put("baz", int[].class);

    final Class<?> beanClass =
        createBeanClass("some.ClassName", properties);
    System.out.println(beanClass);
    for(final Method method : beanClass.getDeclaredMethods()){
        System.out.println(method);
    }

}

输出:


class some.ClassName

public int [] some.ClassName.getBaz()

public void some.ClassName.setBaz(int [])

public java.lang.Integer some.ClassName.getFoo()

public void some.ClassName.setFoo( java.lang.Integer)

public java.lang.String some.ClassName.getBar()

public void some.ClassName.setBar(java.lang.String)

class some.ClassName
public int[] some.ClassName.getBaz()
public void some.ClassName.setBaz(int[])
public java.lang.Integer some.ClassName.getFoo()
public void some.ClassName.setFoo(java.lang.Integer)
public java.lang.String some.ClassName.getBar()
public void some.ClassName.setBar(java.lang.String)

但问题是:你无法对这些方法进行编码,因为它们在编译时不存在,所以我不这样做知道这对你有什么好处。

But the problem is: you have no way of coding against these methods, as they don't exist at compile time, so I don't know what good this will do you.

这篇关于在运行时创建简单的POJO类(字节码)(动态)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆