Source Code Cross Referenced for DynaProperty.java in  » Library » Apache-commons-beanutils-1.8.0-BETA-src » org » apache » commons » beanutils » 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 » Library » Apache commons beanutils 1.8.0 BETA src » org.apache.commons.beanutils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.commons.beanutils;
019:
020:        import java.io.IOException;
021:        import java.io.Serializable;
022:        import java.io.ObjectOutputStream;
023:        import java.io.ObjectInputStream;
024:        import java.io.StreamCorruptedException;
025:        import java.util.List;
026:        import java.util.Map;
027:
028:        /**
029:         * <p>The metadata describing an individual property of a DynaBean.</p>
030:         *
031:         * <p>The meta contains an <em>optional</em> content type property ({@link #getContentType})
032:         * for use by mapped and iterated properties. 
033:         * A mapped or iterated property may choose to indicate the type it expects.
034:         * The DynaBean implementation may choose to enforce this type on its entries.
035:         * Alternatively, an implementatin may choose to ignore this property.
036:         * All keys for maps must be of type String so no meta data is needed for map keys.</p>
037:         *
038:         * @author Craig R. McClanahan
039:         * @version $Revision: 555824 $ $Date: 2007-07-13 01:27:15 +0100 (Fri, 13 Jul 2007) $
040:         */
041:
042:        public class DynaProperty implements  Serializable {
043:
044:            // ----------------------------------------------------------- Constants
045:
046:            /*
047:             * There are issues with serializing primitive class types on certain JVM versions
048:             * (including java 1.3).
049:             * This class uses a custom serialization implementation that writes an integer
050:             * for these primitive class.
051:             * This list of constants are the ones used in serialization.
052:             * If these values are changed, then older versions will no longer be read correctly
053:             */
054:            private static final int BOOLEAN_TYPE = 1;
055:            private static final int BYTE_TYPE = 2;
056:            private static final int CHAR_TYPE = 3;
057:            private static final int DOUBLE_TYPE = 4;
058:            private static final int FLOAT_TYPE = 5;
059:            private static final int INT_TYPE = 6;
060:            private static final int LONG_TYPE = 7;
061:            private static final int SHORT_TYPE = 8;
062:
063:            // ----------------------------------------------------------- Constructors
064:
065:            /**
066:             * Construct a property that accepts any data type.
067:             *
068:             * @param name Name of the property being described
069:             */
070:            public DynaProperty(String name) {
071:
072:                this (name, Object.class);
073:
074:            }
075:
076:            /**
077:             * Construct a property of the specified data type.
078:             *
079:             * @param name Name of the property being described
080:             * @param type Java class representing the property data type
081:             */
082:            public DynaProperty(String name, Class type) {
083:
084:                super ();
085:                this .name = name;
086:                this .type = type;
087:                if (type != null && type.isArray()) {
088:                    this .contentType = type.getComponentType();
089:                }
090:
091:            }
092:
093:            /**
094:             * Construct an indexed or mapped <code>DynaProperty</code> that supports (pseudo)-introspection
095:             * of the content type.
096:             *
097:             * @param name Name of the property being described
098:             * @param type Java class representing the property data type
099:             * @param contentType Class that all indexed or mapped elements are instances of
100:             */
101:            public DynaProperty(String name, Class type, Class contentType) {
102:
103:                super ();
104:                this .name = name;
105:                this .type = type;
106:                this .contentType = contentType;
107:
108:            }
109:
110:            // ------------------------------------------------------------- Properties
111:
112:            /** Property name */
113:            protected String name = null;
114:
115:            /**
116:             * Get the name of this property.
117:             * @return the name of the property
118:             */
119:            public String getName() {
120:                return (this .name);
121:            }
122:
123:            /** Property type */
124:            protected transient Class type = null;
125:
126:            /**
127:             * <p>Gets the Java class representing the data type of the underlying property
128:             * values.</p>
129:             * 
130:             * <p>There are issues with serializing primitive class types on certain JVM versions
131:             * (including java 1.3).
132:             * Therefore, this field <strong>must not be serialized using the standard methods</strong>.</p>
133:             * 
134:             * <p><strong>Please leave this field as <code>transient</code></strong></p>
135:             *
136:             * @return the property type
137:             */
138:            public Class getType() {
139:                return (this .type);
140:            }
141:
142:            /** The <em>(optional)</em> type of content elements for indexed <code>DynaProperty</code> */
143:            protected transient Class contentType;
144:
145:            /**
146:             * Gets the <em>(optional)</em> type of the indexed content for <code>DynaProperty</code>'s
147:             * that support this feature.
148:             *
149:             * <p>There are issues with serializing primitive class types on certain JVM versions
150:             * (including java 1.3).
151:             * Therefore, this field <strong>must not be serialized using the standard methods</strong>.</p>
152:             *
153:             * @return the Class for the content type if this is an indexed <code>DynaProperty</code> 
154:             * and this feature is supported. Otherwise null.
155:             */
156:            public Class getContentType() {
157:                return contentType;
158:            }
159:
160:            // --------------------------------------------------------- Public Methods
161:
162:            /**
163:             * Does this property represent an indexed value (ie an array or List)?
164:             *
165:             * @return <code>true</code> if the property is indexed (i.e. is a List or
166:             * array), otherwise <code>false</code>
167:             */
168:            public boolean isIndexed() {
169:
170:                if (type == null) {
171:                    return (false);
172:                } else if (type.isArray()) {
173:                    return (true);
174:                } else if (List.class.isAssignableFrom(type)) {
175:                    return (true);
176:                } else {
177:                    return (false);
178:                }
179:
180:            }
181:
182:            /**
183:             * Does this property represent a mapped value (ie a Map)?
184:             *
185:             * @return <code>true</code> if the property is a Map
186:             * otherwise <code>false</code>
187:             */
188:            public boolean isMapped() {
189:
190:                if (type == null) {
191:                    return (false);
192:                } else {
193:                    return (Map.class.isAssignableFrom(type));
194:                }
195:
196:            }
197:
198:            /**
199:             * Checks this instance against the specified Object for equality. Overrides the
200:             * default refererence test for equality provided by {@link java.lang.Object#equals(Object)}  
201:             * @param obj The object to compare to
202:             * @return <code>true</code> if object is a dyna property with the same name
203:             * type and content type, otherwise <code>false</code>
204:             */
205:            public boolean equals(final Object obj) {
206:
207:                boolean result = false;
208:
209:                result = (obj == this );
210:
211:                if ((!result) && obj instanceof  DynaProperty) {
212:                    final DynaProperty that = (DynaProperty) obj;
213:                    result = ((this .name == null) ? (that.name == null)
214:                            : (this .name.equals(that.name)))
215:                            && ((this .type == null) ? (that.type == null)
216:                                    : (this .type.equals(that.type)))
217:                            && ((this .contentType == null) ? (that.contentType == null)
218:                                    : (this .contentType
219:                                            .equals(that.contentType)));
220:                }
221:
222:                return result;
223:            }
224:
225:            /**
226:             * @return the hashcode for this dyna property
227:             * @see java.lang.Object#hashCode
228:             */
229:            public int hashCode() {
230:
231:                int result = 1;
232:
233:                result = result * 31 + ((name == null) ? 0 : name.hashCode());
234:                result = result * 31 + ((type == null) ? 0 : type.hashCode());
235:                result = result * 31
236:                        + ((contentType == null) ? 0 : contentType.hashCode());
237:
238:                return result;
239:            }
240:
241:            /**
242:             * Return a String representation of this Object.
243:             * @return a String representation of the dyna property
244:             */
245:            public String toString() {
246:
247:                StringBuffer sb = new StringBuffer("DynaProperty[name=");
248:                sb.append(this .name);
249:                sb.append(",type=");
250:                sb.append(this .type);
251:                if (isMapped() || isIndexed()) {
252:                    sb.append(" <").append(this .contentType).append(">");
253:                }
254:                sb.append("]");
255:                return (sb.toString());
256:
257:            }
258:
259:            // --------------------------------------------------------- Serialization helper methods
260:
261:            /**
262:             * Writes this object safely.
263:             * There are issues with serializing primitive class types on certain JVM versions
264:             * (including java 1.3).
265:             * This method provides a workaround.
266:             */
267:            private void writeObject(ObjectOutputStream out) throws IOException {
268:
269:                writeAnyClass(this .type, out);
270:
271:                if (isMapped() || isIndexed()) {
272:                    writeAnyClass(this .contentType, out);
273:                }
274:
275:                // write out other values
276:                out.defaultWriteObject();
277:            }
278:
279:            /**
280:             * Write a class using safe encoding to workaround java 1.3 serialization bug.
281:             */
282:            private void writeAnyClass(Class clazz, ObjectOutputStream out)
283:                    throws IOException {
284:                // safely write out any class
285:                int primitiveType = 0;
286:                if (Boolean.TYPE.equals(clazz)) {
287:                    primitiveType = BOOLEAN_TYPE;
288:                } else if (Byte.TYPE.equals(clazz)) {
289:                    primitiveType = BYTE_TYPE;
290:                } else if (Character.TYPE.equals(clazz)) {
291:                    primitiveType = CHAR_TYPE;
292:                } else if (Double.TYPE.equals(clazz)) {
293:                    primitiveType = DOUBLE_TYPE;
294:                } else if (Float.TYPE.equals(clazz)) {
295:                    primitiveType = FLOAT_TYPE;
296:                } else if (Integer.TYPE.equals(clazz)) {
297:                    primitiveType = INT_TYPE;
298:                } else if (Long.TYPE.equals(clazz)) {
299:                    primitiveType = LONG_TYPE;
300:                } else if (Short.TYPE.equals(clazz)) {
301:                    primitiveType = SHORT_TYPE;
302:                }
303:
304:                if (primitiveType == 0) {
305:                    // then it's not a primitive type
306:                    out.writeBoolean(false);
307:                    out.writeObject(clazz);
308:                } else {
309:                    // we'll write out a constant instead
310:                    out.writeBoolean(true);
311:                    out.writeInt(primitiveType);
312:                }
313:            }
314:
315:            /**
316:             * Reads field values for this object safely.
317:             * There are issues with serializing primitive class types on certain JVM versions
318:             * (including java 1.3).
319:             * This method provides a workaround.
320:             *
321:             * @throws StreamCorruptedException when the stream data values are outside expected range 
322:             */
323:            private void readObject(ObjectInputStream in) throws IOException,
324:                    ClassNotFoundException {
325:
326:                this .type = readAnyClass(in);
327:
328:                if (isMapped() || isIndexed()) {
329:                    this .contentType = readAnyClass(in);
330:                }
331:
332:                // read other values
333:                in.defaultReadObject();
334:            }
335:
336:            /**
337:             * Reads a class using safe encoding to workaround java 1.3 serialization bug.
338:             */
339:            private Class readAnyClass(ObjectInputStream in)
340:                    throws IOException, ClassNotFoundException {
341:                // read back type class safely 
342:                if (in.readBoolean()) {
343:                    // it's a type constant
344:                    switch (in.readInt()) {
345:
346:                    case BOOLEAN_TYPE:
347:                        return Boolean.TYPE;
348:                    case BYTE_TYPE:
349:                        return Byte.TYPE;
350:                    case CHAR_TYPE:
351:                        return Character.TYPE;
352:                    case DOUBLE_TYPE:
353:                        return Double.TYPE;
354:                    case FLOAT_TYPE:
355:                        return Float.TYPE;
356:                    case INT_TYPE:
357:                        return Integer.TYPE;
358:                    case LONG_TYPE:
359:                        return Long.TYPE;
360:                    case SHORT_TYPE:
361:                        return Short.TYPE;
362:                    default:
363:                        // something's gone wrong
364:                        throw new StreamCorruptedException(
365:                                "Invalid primitive type. "
366:                                        + "Check version of beanutils used to serialize is compatible.");
367:
368:                    }
369:
370:                } else {
371:                    // it's another class
372:                    return ((Class) in.readObject());
373:                }
374:            }
375:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.