Source Code Cross Referenced for BulkBeanEmitter.java in  » Byte-Code » cglib » net » sf » cglib » beans » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Byte Code » cglib » net.sf.cglib.beans 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2003,2004 The Apache Software Foundation
003:         *
004:         *  Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         *  Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package net.sf.cglib.beans;
017:
018:        import java.lang.reflect.Constructor;
019:        import java.lang.reflect.Method;
020:        import java.lang.reflect.Modifier;
021:        import java.util.*;
022:        import net.sf.cglib.core.*;
023:        import org.objectweb.asm.ClassVisitor;
024:        import org.objectweb.asm.Type;
025:
026:        class BulkBeanEmitter extends ClassEmitter {
027:            private static final Signature GET_PROPERTY_VALUES = TypeUtils
028:                    .parseSignature("void getPropertyValues(Object, Object[])");
029:            private static final Signature SET_PROPERTY_VALUES = TypeUtils
030:                    .parseSignature("void setPropertyValues(Object, Object[])");
031:            private static final Signature CSTRUCT_EXCEPTION = TypeUtils
032:                    .parseConstructor("Throwable, int");
033:            private static final Type BULK_BEAN = TypeUtils
034:                    .parseType("net.sf.cglib.beans.BulkBean");
035:            private static final Type BULK_BEAN_EXCEPTION = TypeUtils
036:                    .parseType("net.sf.cglib.beans.BulkBeanException");
037:
038:            public BulkBeanEmitter(ClassVisitor v, String className,
039:                    Class target, String[] getterNames, String[] setterNames,
040:                    Class[] types) {
041:                super (v);
042:
043:                Method[] getters = new Method[getterNames.length];
044:                Method[] setters = new Method[setterNames.length];
045:                validate(target, getterNames, setterNames, types, getters,
046:                        setters);
047:
048:                begin_class(Constants.V1_2, Constants.ACC_PUBLIC, className,
049:                        BULK_BEAN, null, Constants.SOURCE_FILE);
050:                EmitUtils.null_constructor(this );
051:                generateGet(target, getters);
052:                generateSet(target, setters);
053:                end_class();
054:            }
055:
056:            private void generateGet(final Class target, final Method[] getters) {
057:                CodeEmitter e = begin_method(Constants.ACC_PUBLIC,
058:                        GET_PROPERTY_VALUES, null);
059:                if (getters.length >= 0) {
060:                    e.load_arg(0);
061:                    e.checkcast(Type.getType(target));
062:                    Local bean = e.make_local();
063:                    e.store_local(bean);
064:                    for (int i = 0; i < getters.length; i++) {
065:                        if (getters[i] != null) {
066:                            MethodInfo getter = ReflectUtils
067:                                    .getMethodInfo(getters[i]);
068:                            e.load_arg(1);
069:                            e.push(i);
070:                            e.load_local(bean);
071:                            e.invoke(getter);
072:                            e.box(getter.getSignature().getReturnType());
073:                            e.aastore();
074:                        }
075:                    }
076:                }
077:                e.return_value();
078:                e.end_method();
079:            }
080:
081:            private void generateSet(final Class target, final Method[] setters) {
082:                // setPropertyValues
083:                CodeEmitter e = begin_method(Constants.ACC_PUBLIC,
084:                        SET_PROPERTY_VALUES, null);
085:                if (setters.length > 0) {
086:                    Local index = e.make_local(Type.INT_TYPE);
087:                    e.push(0);
088:                    e.store_local(index);
089:                    e.load_arg(0);
090:                    e.checkcast(Type.getType(target));
091:                    e.load_arg(1);
092:                    Block handler = e.begin_block();
093:                    int lastIndex = 0;
094:                    for (int i = 0; i < setters.length; i++) {
095:                        if (setters[i] != null) {
096:                            MethodInfo setter = ReflectUtils
097:                                    .getMethodInfo(setters[i]);
098:                            int diff = i - lastIndex;
099:                            if (diff > 0) {
100:                                e.iinc(index, diff);
101:                                lastIndex = i;
102:                            }
103:                            e.dup2();
104:                            e.aaload(i);
105:                            e
106:                                    .unbox(setter.getSignature()
107:                                            .getArgumentTypes()[0]);
108:                            e.invoke(setter);
109:                        }
110:                    }
111:                    handler.end();
112:                    e.return_value();
113:                    e.catch_exception(handler, Constants.TYPE_THROWABLE);
114:                    e.new_instance(BULK_BEAN_EXCEPTION);
115:                    e.dup_x1();
116:                    e.swap();
117:                    e.load_local(index);
118:                    e
119:                            .invoke_constructor(BULK_BEAN_EXCEPTION,
120:                                    CSTRUCT_EXCEPTION);
121:                    e.athrow();
122:                } else {
123:                    e.return_value();
124:                }
125:                e.end_method();
126:            }
127:
128:            private static void validate(Class target, String[] getters,
129:                    String[] setters, Class[] types, Method[] getters_out,
130:                    Method[] setters_out) {
131:                int i = -1;
132:                if (setters.length != types.length
133:                        || getters.length != types.length) {
134:                    throw new BulkBeanException(
135:                            "accessor array length must be equal type array length",
136:                            i);
137:                }
138:                try {
139:                    for (i = 0; i < types.length; i++) {
140:                        if (getters[i] != null) {
141:                            Method method = ReflectUtils.findDeclaredMethod(
142:                                    target, getters[i], null);
143:                            if (method.getReturnType() != types[i]) {
144:                                throw new BulkBeanException("Specified type "
145:                                        + types[i]
146:                                        + " does not match declared type "
147:                                        + method.getReturnType(), i);
148:                            }
149:                            if (Modifier.isPrivate(method.getModifiers())) {
150:                                throw new BulkBeanException(
151:                                        "Property is private", i);
152:                            }
153:                            getters_out[i] = method;
154:                        }
155:                        if (setters[i] != null) {
156:                            Method method = ReflectUtils.findDeclaredMethod(
157:                                    target, setters[i],
158:                                    new Class[] { types[i] });
159:                            if (Modifier.isPrivate(method.getModifiers())) {
160:                                throw new BulkBeanException(
161:                                        "Property is private", i);
162:                            }
163:                            setters_out[i] = method;
164:                        }
165:                    }
166:                } catch (NoSuchMethodException e) {
167:                    throw new BulkBeanException(
168:                            "Cannot find specified property", i);
169:                }
170:            }
171:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.