KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > beans > BeanGenerator


1 /*
2  * Copyright 2003 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.cglib.beans;
17
18 import java.beans.PropertyDescriptor JavaDoc;
19 import java.util.*;
20 import net.sf.cglib.core.*;
21 import org.objectweb.asm.ClassVisitor;
22 import org.objectweb.asm.Type;
23
24 /**
25  * @author Juozas Baliuka, Chris Nokleberg
26  */

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 JavaDoc newInstance(String JavaDoc superclass, Map props);
35     }
36
37     private Class JavaDoc superclass;
38     private Map props = new HashMap();
39     private boolean classOnly;
40
41     public BeanGenerator() {
42         super(SOURCE);
43     }
44
45     /**
46      * Set the class which the generated class will extend. The class
47      * must not be declared as final, and must have a non-private
48      * no-argument constructor.
49      * @param superclass class to extend, or null to extend Object
50      */

51     public void setSuperclass(Class JavaDoc superclass) {
52         if (superclass != null && superclass.equals(Object JavaDoc.class)) {
53             superclass = null;
54         }
55         this.superclass = superclass;
56     }
57
58     public void addProperty(String JavaDoc name, Class JavaDoc type) {
59         if (props.containsKey(name)) {
60             throw new IllegalArgumentException JavaDoc("Duplicate property name \"" + name + "\"");
61         }
62         props.put(name, Type.getType(type));
63     }
64
65     protected ClassLoader JavaDoc getDefaultClassLoader() {
66         if (superclass != null) {
67             return superclass.getClassLoader();
68         } else {
69             return null;
70         }
71     }
72
73     public Object JavaDoc create() {
74         classOnly = false;
75         return createHelper();
76     }
77
78     public Object JavaDoc createClass() {
79         classOnly = true;
80         return createHelper();
81     }
82
83     private Object JavaDoc createHelper() {
84         if (superclass != null) {
85             setNamePrefix(superclass.getName());
86         }
87         String JavaDoc superName = (superclass != null) ? superclass.getName() : "java.lang.Object";
88         Object JavaDoc key = KEY_FACTORY.newInstance(superName, props);
89         return super.create(key);
90     }
91
92     public void generateClass(ClassVisitor v) throws Exception JavaDoc {
93         int size = props.size();
94         String JavaDoc[] names = (String JavaDoc[])props.keySet().toArray(new String JavaDoc[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 JavaDoc firstInstance(Class JavaDoc type) {
112         if (classOnly) {
113             return type;
114         } else {
115             return ReflectUtils.newInstance(type);
116         }
117     }
118
119     protected Object JavaDoc nextInstance(Object JavaDoc instance) {
120         Class JavaDoc protoclass = (instance instanceof Class JavaDoc) ? (Class JavaDoc)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 JavaDoc name = (String JavaDoc)it.next();
131             gen.addProperty(name, (Class JavaDoc)props.get(name));
132         }
133     }
134
135     public static void addProperties(BeanGenerator gen, Class JavaDoc type) {
136         addProperties(gen, ReflectUtils.getBeanProperties(type));
137     }
138
139     public static void addProperties(BeanGenerator gen, PropertyDescriptor JavaDoc[] 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