首页 新闻 会员 周边 捐助

动态生成pojo类

0
悬赏园豆:100 [待解决问题]

今天领导安排了一个需求,要动态生成pojo类,有没有大佬能提供一个例子,pojo类的类名来自DataElement对象的name属性,属性在DataElement对象中的list的属性中报存。上网查了一些例子,总感觉摸不着的感觉。

!O0O!的主页 !O0O! | 初学一级 | 园豆:88
提问于:2019-04-16 14:50
< >
分享
所有回答(3)
0

写个方法 也可以 或用生成工具

cn2024 | 园豆:206 (菜鸟二级) | 2019-04-16 15:16

呃。。。

支持(0) 反对(0) !O0O! | 园豆:88 (初学一级) | 2019-04-16 15:22
0

你搜索一下,MyBatis逆向工程自动生成代码

爱听音乐的猪 | 园豆:189 (初学一级) | 2019-04-16 15:24
0

package com.java.test.dynamicpojo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Arrays;

import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

public class DynamicPOJO {
private static String classString = "/* hehha */" + "public class Student{ "
+ " private String studentId; "
+ " public String getStudentId(){ "
+ " return "小明"; "
+ " } "
+ "} ";

    private static void createStudent() throws Exception {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(null, null, null);
        ClassJavaFileManager classJavaFileManager = new ClassJavaFileManager(standardFileManager);
        StringObject stringObject = new StringObject(new URI("Student.java"), JavaFileObject.Kind.SOURCE, classString);
        JavaCompiler.CompilationTask task = compiler.getTask(null, classJavaFileManager, null, null, null,
                Arrays.asList(stringObject));
        if (task.call()) {
            ClassJavaFileObject javaFileObject = classJavaFileManager.getClassJavaFileObject();
            ClassLoader classLoader = new MyClassLoader(javaFileObject);
            Object student = classLoader.loadClass("Student").newInstance();
            System.out.println("student-->"+student);//student对象
            Method getStudetnId = student.getClass().getMethod("getStudentId");
            Object invoke = getStudetnId.invoke(student);
            System.out.println("getStudentId-->"+invoke);
        }
    }

    /**    *自定义fileManager    */ 
    static class ClassJavaFileManager extends ForwardingJavaFileManager{
        private ClassJavaFileObject classJavaFileObject;
        public ClassJavaFileManager(JavaFileManager fileManager) {
            super(fileManager);
        } 
        public ClassJavaFileObject getClassJavaFileObject() { 
            return classJavaFileObject;
        }  
        /**这个方法一定要自定义 */
        @Override  
        public JavaFileObject getJavaFileForOutput(Location location, String className,
                JavaFileObject.Kind kind, FileObject sibling) throws IOException {
            return (classJavaFileObject = new ClassJavaFileObject(className,kind)); 
        }
    }  
    
    /**     * 存储源文件     */  
    static class StringObject extends SimpleJavaFileObject{
        private String content;
        public StringObject(URI uri, Kind kind, String content) { 
            super(uri, kind);
            this.content = content;
        }   
        @Override 
        public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
            return this.content;  
        }  
    }   
    
    /**     * class文件(不需要存到文件中)     */
    static class ClassJavaFileObject extends SimpleJavaFileObject{
        ByteArrayOutputStream outputStream; 
        public ClassJavaFileObject(String className, Kind kind) { 
            super(URI.create(className + kind.extension), kind);   
            this.outputStream = new ByteArrayOutputStream(); 
        }        //这个也要实现 
        @Override      
        public OutputStream openOutputStream() throws IOException {  
            return this.outputStream;   
        }       
        public byte[] getBytes(){       
            return this.outputStream.toByteArray();    
            }   
    }   
    
    /**自定义classloader*/
    static class MyClassLoader extends ClassLoader{ 
        private ClassJavaFileObject stringObject;  
        public MyClassLoader(ClassJavaFileObject stringObject){    
            this.stringObject = stringObject;  
        }      
        @Override     
        protected Class<?> findClass(String name) throws ClassNotFoundException {
            byte[] bytes = this.stringObject.getBytes(); 
            return defineClass(name,bytes,0,bytes.length);  
        }  
    }


    public static void main(String[] args) throws Exception {
        createStudent();
    }
    

}

!O0O! | 园豆:88 (初学一级) | 2019-04-16 16:06
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册