java 动态生成bean的案例

最近做一个需求,需求中的bean只用于生成一次json使用,所以想通过配置来动态的生成,查了一下,java还真有这个实现。

java动态的生成javabean,只能生成属性和对应的set/get方法,不能生成其他的方法。

import org.assertj.core.internal.cglib.beans.BeanGenerator;
import org.assertj.core.internal.cglib.beans.BeanMap;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
 * Created by wangpengzhi1 on 2018/1/2.
 */
public class BeanCeater {
 public static void main(String[] args) throws ClassNotFoundException {
  System.out.println("Generate JavaBean");
  Map properties = new HashMap();
  properties.put("id", Class.forName("java.lang.Integer"));
  properties.put("name", Class.forName("java.lang.String"));
  properties.put("address", Class.forName("java.lang.String"));
  Object stu = generateObject(properties);
  System.out.println("Set values");
  setValue(stu, "id", 123);
  setValue(stu, "name", "454");
  setValue(stu, "address", "789");
  System.out.println("Get values");
  System.out.println(">> " + getValue(stu, "id"));
  System.out.println(">> " + getValue(stu, "name"));
  System.out.println(">> " + getValue(stu, "address"));
  System.out.println("Show all methods");
  Method[] methods = stu.getClass().getDeclaredMethods();
  for(Method method : methods) {
   System.out.println(">> " + method.getName());
  }
  System.out.println("Show all properties");
  Field[] fields = stu.getClass().getDeclaredFields();
  for(Field field : fields) {
   System.out.println(">> " + field.getName());
  }
 }
 private static Object generateObject(Map properties) {
  BeanGenerator generator = new BeanGenerator();
  Set keySet = properties.keySet();
  for(Iterator i = keySet.iterator(); i.hasNext();) {
   String key = (String)i.next();
   generator.addProperty(key, (Class)properties.get(key));
  }
  return generator.create();
 }
 private static Object getValue(Object obj, String property) {
  BeanMap beanMap = BeanMap.create(obj);
  return beanMap.get(property);
 }
 private static void setValue(Object obj, String property, Object value) {
  BeanMap beanMap = BeanMap.create(obj);
  beanMap.put(property, value);
 }
}

代码不难懂,有需要的自己复制。

补充:spring 工具类 ReflectionUtils 获取bean所有字段

以前遇到要获取当前类以及所有父类的的field的时候,都是递归一直往上找,一直到Object ,个人觉得这种方法是不是太low了,有没有更好的办法?或者jdk其实是有这种方法的,只是我不知道,今天看了下spring中的实现,也是一样的,真没有更好的办法?

public static void doWithFields(Class clazz, ReflectionUtils.FieldCallback fc, @Nullable ReflectionUtils.FieldFilter ff) {
    Class targetClass = clazz;
 
    do {
      Field[] fields = getDeclaredFields(targetClass);
      Field[] var5 = fields;
      int var6 = fields.length;
 
      for(int var7 = 0; var7 < var6; ++var7) {
        Field field = var5[var7];
        if (ff == null || ff.matches(field)) {
          try {
            fc.doWith(field);
          } catch (IllegalAccessException var10) {
            throw new IllegalStateException("Not allowed to access field '" + field.getName() + "': " + var10);
          }
        }
      }
 
      targetClass = targetClass.getSuperclass();
    } while(targetClass != null && targetClass != Object.class); 
  }
 
  private static Field[] getDeclaredFields(Class clazz) {
    Assert.notNull(clazz, "Class must not be null");
    Field[] result = (Field[])declaredFieldsCache.get(clazz);
    if (result == null) {
      try {
        result = clazz.getDeclaredFields();
        declaredFieldsCache.put(clazz, result.length == 0 ? NO_FIELDS : result);
      } catch (Throwable var3) {
        throw new IllegalStateException("Failed to introspect Class [" + clazz.getName() + "] from ClassLoader [" + clazz.getClassLoader() + "]", var3);
      }
    }
 
    return result;
  }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

你可能感兴趣的:(java 动态生成bean的案例)