KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Constructor JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20 import java.lang.reflect.Modifier JavaDoc;
21 import java.util.*;
22 import net.sf.cglib.core.*;
23 import org.objectweb.asm.ClassVisitor;
24
25 /**
26  * @author Juozas Baliuka
27  */

28 abstract public class BulkBean
29 {
30     private static final BulkBeanKey KEY_FACTORY =
31       (BulkBeanKey)KeyFactory.create(BulkBeanKey.class);
32     
33     interface BulkBeanKey {
34         public Object JavaDoc newInstance(String JavaDoc target, String JavaDoc[] getters, String JavaDoc[] setters, String JavaDoc[] types);
35     }
36     
37     protected Class JavaDoc target;
38     protected String JavaDoc[] getters, setters;
39     protected Class JavaDoc[] types;
40     
41     protected BulkBean() { }
42     
43     abstract public void getPropertyValues(Object JavaDoc bean, Object JavaDoc[] values);
44     abstract public void setPropertyValues(Object JavaDoc bean, Object JavaDoc[] values);
45
46     public Object JavaDoc[] getPropertyValues(Object JavaDoc bean) {
47         Object JavaDoc[] values = new Object JavaDoc[getters.length];
48         getPropertyValues(bean, values);
49         return values;
50     }
51     
52     public Class JavaDoc[] getPropertyTypes() {
53         return (Class JavaDoc[])types.clone();
54     }
55     
56     public String JavaDoc[] getGetters() {
57         return (String JavaDoc[])getters.clone();
58     }
59     
60     public String JavaDoc[] getSetters() {
61         return (String JavaDoc[])setters.clone();
62     }
63
64     public static BulkBean create(Class JavaDoc target, String JavaDoc[] getters, String JavaDoc[] setters, Class JavaDoc[] types) {
65         Generator gen = new Generator();
66         gen.setTarget(target);
67         gen.setGetters(getters);
68         gen.setSetters(setters);
69         gen.setTypes(types);
70         return gen.create();
71     }
72
73     public static class Generator extends AbstractClassGenerator {
74         private static final Source SOURCE = new Source(BulkBean.class.getName());
75         private Class JavaDoc target;
76         private String JavaDoc[] getters;
77         private String JavaDoc[] setters;
78         private Class JavaDoc[] types;
79
80         public Generator() {
81             super(SOURCE);
82         }
83
84         public void setTarget(Class JavaDoc target) {
85             this.target = target;
86         }
87
88         public void setGetters(String JavaDoc[] getters) {
89             this.getters = getters;
90         }
91
92         public void setSetters(String JavaDoc[] setters) {
93             this.setters = setters;
94         }
95
96         public void setTypes(Class JavaDoc[] types) {
97             this.types = types;
98         }
99
100         protected ClassLoader JavaDoc getDefaultClassLoader() {
101             return target.getClassLoader();
102         }
103
104         public BulkBean create() {
105             setNamePrefix(target.getName());
106             String JavaDoc targetClassName = target.getName();
107             String JavaDoc[] typeClassNames = ReflectUtils.getNames(types);
108             Object JavaDoc key = KEY_FACTORY.newInstance(targetClassName, getters, setters, typeClassNames);
109             return (BulkBean)super.create(key);
110         }
111
112         public void generateClass(ClassVisitor v) throws Exception JavaDoc {
113             new BulkBeanEmitter(v, getClassName(), target, getters, setters, types);
114         }
115
116         protected Object JavaDoc firstInstance(Class JavaDoc type) {
117             BulkBean instance = (BulkBean)ReflectUtils.newInstance(type);
118             instance.target = target;
119                     
120             int length = getters.length;
121             instance.getters = new String JavaDoc[length];
122             System.arraycopy(getters, 0, instance.getters, 0, length);
123                     
124             instance.setters = new String JavaDoc[length];
125             System.arraycopy(setters, 0, instance.setters, 0, length);
126                     
127             instance.types = new Class JavaDoc[types.length];
128             System.arraycopy(types, 0, instance.types, 0, types.length);
129
130             return instance;
131         }
132
133         protected Object JavaDoc nextInstance(Object JavaDoc instance) {
134             return instance;
135         }
136     }
137 }
138
Popular Tags