~ubuntu-branches/ubuntu/quantal/cglib/quantal

« back to all changes in this revision

Viewing changes to src/proxy/net/sf/cglib/beans/BulkBean.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2009-10-08 21:23:45 UTC
  • Revision ID: james.westby@ubuntu.com-20091008212345-t2zy5hv2g8o4i40k
Tags: upstream-2.2+dfsg
ImportĀ upstreamĀ versionĀ 2.2+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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;
 
19
import java.lang.reflect.Method;
 
20
import java.lang.reflect.Modifier;
 
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 newInstance(String target, String[] getters, String[] setters, String[] types);
 
35
    }
 
36
    
 
37
    protected Class target;
 
38
    protected String[] getters, setters;
 
39
    protected Class[] types;
 
40
    
 
41
    protected BulkBean() { }
 
42
    
 
43
    abstract public void getPropertyValues(Object bean, Object[] values);
 
44
    abstract public void setPropertyValues(Object bean, Object[] values);
 
45
 
 
46
    public Object[] getPropertyValues(Object bean) {
 
47
        Object[] values = new Object[getters.length];
 
48
        getPropertyValues(bean, values);
 
49
        return values;
 
50
    }
 
51
    
 
52
    public Class[] getPropertyTypes() {
 
53
        return (Class[])types.clone();
 
54
    }
 
55
    
 
56
    public String[] getGetters() {
 
57
        return (String[])getters.clone();
 
58
    }
 
59
    
 
60
    public String[] getSetters() {
 
61
        return (String[])setters.clone();
 
62
    }
 
63
 
 
64
    public static BulkBean create(Class target, String[] getters, String[] setters, Class[] 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 target;
 
76
        private String[] getters;
 
77
        private String[] setters;
 
78
        private Class[] types;
 
79
 
 
80
        public Generator() {
 
81
            super(SOURCE);
 
82
        }
 
83
 
 
84
        public void setTarget(Class target) {
 
85
            this.target = target;
 
86
        }
 
87
 
 
88
        public void setGetters(String[] getters) {
 
89
            this.getters = getters;
 
90
        }
 
91
 
 
92
        public void setSetters(String[] setters) {
 
93
            this.setters = setters;
 
94
        }
 
95
 
 
96
        public void setTypes(Class[] types) {
 
97
            this.types = types;
 
98
        }
 
99
 
 
100
        protected ClassLoader getDefaultClassLoader() {
 
101
            return target.getClassLoader();
 
102
        }
 
103
 
 
104
        public BulkBean create() {
 
105
            setNamePrefix(target.getName());
 
106
            String targetClassName = target.getName();
 
107
            String[] typeClassNames = ReflectUtils.getNames(types);
 
108
            Object key = KEY_FACTORY.newInstance(targetClassName, getters, setters, typeClassNames);
 
109
            return (BulkBean)super.create(key);
 
110
        }
 
111
 
 
112
        public void generateClass(ClassVisitor v) throws Exception {
 
113
            new BulkBeanEmitter(v, getClassName(), target, getters, setters, types);
 
114
        }
 
115
 
 
116
        protected Object firstInstance(Class type) {
 
117
            BulkBean instance = (BulkBean)ReflectUtils.newInstance(type);
 
118
            instance.target = target;
 
119
                    
 
120
            int length = getters.length;
 
121
            instance.getters = new String[length];
 
122
            System.arraycopy(getters, 0, instance.getters, 0, length);
 
123
                    
 
124
            instance.setters = new String[length];
 
125
            System.arraycopy(setters, 0, instance.setters, 0, length);
 
126
                    
 
127
            instance.types = new Class[types.length];
 
128
            System.arraycopy(types, 0, instance.types, 0, types.length);
 
129
 
 
130
            return instance;
 
131
        }
 
132
 
 
133
        protected Object nextInstance(Object instance) {
 
134
            return instance;
 
135
        }
 
136
    }
 
137
}