1 16 package net.sf.cglib.beans; 17 18 import java.beans.PropertyDescriptor ; 19 import java.util.*; 20 import net.sf.cglib.core.*; 21 import org.objectweb.asm.ClassVisitor; 22 import org.objectweb.asm.Type; 23 24 27 public class BeanGenerator extends AbstractClassGenerator 28 { 29 private static final Source SOURCE = new Source(BeanGenerator.class.getName()); 30 private static final BeanGeneratorKey KEY_FACTORY = 31 (BeanGeneratorKey)KeyFactory.create(BeanGeneratorKey.class); 32 33 interface BeanGeneratorKey { 34 public Object newInstance(String superclass, Map props); 35 } 36 37 private Class superclass; 38 private Map props = new HashMap(); 39 private boolean classOnly; 40 41 public BeanGenerator() { 42 super(SOURCE); 43 } 44 45 51 public void setSuperclass(Class superclass) { 52 if (superclass != null && superclass.equals(Object .class)) { 53 superclass = null; 54 } 55 this.superclass = superclass; 56 } 57 58 public void addProperty(String name, Class type) { 59 if (props.containsKey(name)) { 60 throw new IllegalArgumentException ("Duplicate property name \"" + name + "\""); 61 } 62 props.put(name, Type.getType(type)); 63 } 64 65 protected ClassLoader getDefaultClassLoader() { 66 if (superclass != null) { 67 return superclass.getClassLoader(); 68 } else { 69 return null; 70 } 71 } 72 73 public Object create() { 74 classOnly = false; 75 return createHelper(); 76 } 77 78 public Object createClass() { 79 classOnly = true; 80 return createHelper(); 81 } 82 83 private Object createHelper() { 84 if (superclass != null) { 85 setNamePrefix(superclass.getName()); 86 } 87 String superName = (superclass != null) ? superclass.getName() : "java.lang.Object"; 88 Object key = KEY_FACTORY.newInstance(superName, props); 89 return super.create(key); 90 } 91 92 public void generateClass(ClassVisitor v) throws Exception { 93 int size = props.size(); 94 String [] names = (String [])props.keySet().toArray(new String [size]); 95 Type[] types = new Type[size]; 96 for (int i = 0; i < size; i++) { 97 types[i] = (Type)props.get(names[i]); 98 } 99 ClassEmitter ce = new ClassEmitter(v); 100 ce.begin_class(Constants.V1_2, 101 Constants.ACC_PUBLIC, 102 getClassName(), 103 superclass != null ? Type.getType(superclass) : Constants.TYPE_OBJECT, 104 null, 105 null); 106 EmitUtils.null_constructor(ce); 107 EmitUtils.add_properties(ce, names, types); 108 ce.end_class(); 109 } 110 111 protected Object firstInstance(Class type) { 112 if (classOnly) { 113 return type; 114 } else { 115 return ReflectUtils.newInstance(type); 116 } 117 } 118 119 protected Object nextInstance(Object instance) { 120 Class protoclass = (instance instanceof Class ) ? (Class )instance : instance.getClass(); 121 if (classOnly) { 122 return protoclass; 123 } else { 124 return ReflectUtils.newInstance(protoclass); 125 } 126 } 127 128 public static void addProperties(BeanGenerator gen, Map props) { 129 for (Iterator it = props.keySet().iterator(); it.hasNext();) { 130 String name = (String )it.next(); 131 gen.addProperty(name, (Class )props.get(name)); 132 } 133 } 134 135 public static void addProperties(BeanGenerator gen, Class type) { 136 addProperties(gen, ReflectUtils.getBeanProperties(type)); 137 } 138 139 public static void addProperties(BeanGenerator gen, PropertyDescriptor [] descriptors) { 140 for (int i = 0; i < descriptors.length; i++) { 141 gen.addProperty(descriptors[i].getName(), descriptors[i].getPropertyType()); 142 } 143 } 144 } 145 | Popular Tags |