KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > codegen > bean > SerializableExtension


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.codegen.bean;
25
26 import java.util.*;
27 import java.io.IOException JavaDoc;
28 import com.mchange.v2.codegen.IndentedWriter;
29
30
31 /**
32  * Note: this class pays no attention to whether users have marked any property variables as transient.
33  * In fact, it will work most efficiently if users mark ALL variables as transient... to define transient
34  * properties for this class, use the constructor which allows a user-specified set of transients.
35  */

36 public class SerializableExtension implements GeneratorExtension
37 {
38     Set transientProperties;
39     Map transientPropertyInitializers;
40
41     /**
42      * @param transientProperties a set of Strings, the names of all properties that should be considered transient and not serialized
43      * @param transientPropertyInitializers an optional Map of a subset of the transient property names to non-default initialization
44      * expressions, which should be unterminated expressions, and which will be used verbatim in
45      * the generated code.
46      */

47     public SerializableExtension(Set transientProperties, Map transientPropertyInitializers)
48     {
49     this.transientProperties = transientProperties;
50     this.transientPropertyInitializers = transientPropertyInitializers;
51     }
52
53     public SerializableExtension()
54     { this ( Collections.EMPTY_SET, null ); }
55
56
57     public Collection extraGeneralImports()
58     { return Collections.EMPTY_SET; }
59
60     public Collection extraSpecificImports()
61     {
62     Set set = new HashSet();
63     set.add( "java.io.IOException" );
64     set.add( "java.io.Serializable" );
65     set.add( "java.io.ObjectOutputStream" );
66     set.add( "java.io.ObjectInputStream" );
67     return set;
68     }
69
70     public Collection extraInterfaceNames()
71     {
72     Set set = new HashSet();
73     set.add( "Serializable" );
74     return set;
75     }
76
77     public void generate(ClassInfo info, Class JavaDoc superclassType, Property[] props, Class JavaDoc[] propTypes, IndentedWriter iw)
78     throws IOException JavaDoc
79     {
80     iw.println("private static final long serialVersionUID = 1;");
81     iw.println("private static final short VERSION = 0x0001;");
82     iw.println();
83     iw.println("private void writeObject( ObjectOutputStream oos ) throws IOException");
84     iw.println("{");
85     iw.upIndent();
86     
87     iw.println( "oos.writeShort( VERSION );" );
88
89     for( int i = 0, len = props.length; i < len; ++i )
90         {
91         Property prop = props[i];
92         if (! transientProperties.contains( prop.getName() ) )
93             {
94             Class JavaDoc propType = propTypes[i];
95             if (propType != null && propType.isPrimitive()) //primitives should always resolve, object types may not, and be null
96
{
97                 if (propType == byte.class)
98                     iw.println("oos.writeByte(" + prop.getName() + ");");
99                 else if (propType == char.class)
100                     iw.println("oos.writeChar(" + prop.getName() + ");");
101                 else if (propType == short.class)
102                     iw.println("oos.writeShort(" + prop.getName() + ");");
103                 else if (propType == int.class)
104                     iw.println("oos.writeInt(" + prop.getName() + ");");
105                 else if (propType == boolean.class)
106                     iw.println("oos.writeBoolean(" + prop.getName() + ");");
107                 else if (propType == long.class)
108                     iw.println("oos.writeLong(" + prop.getName() + ");");
109                 else if (propType == float.class)
110                     iw.println("oos.writeFloat(" + prop.getName() + ");");
111                 else if (propType == double.class)
112                     iw.println("oos.writeDouble(" + prop.getName() + ");");
113                 }
114             else
115                 writeStoreObject( prop, propType, iw );
116             }
117         }
118     generateExtraSerWriteStatements( info, superclassType, props, propTypes, iw);
119     iw.downIndent();
120     iw.println("}");
121     iw.println();
122
123     iw.println("private void readObject( ObjectInputStream ois ) throws IOException, ClassNotFoundException");
124     iw.println("{");
125     iw.upIndent();
126     iw.println("short version = ois.readShort();");
127     iw.println("switch (version)");
128     iw.println("{");
129     iw.upIndent();
130     
131     iw.println("case VERSION:");
132     iw.upIndent();
133     for( int i = 0, len = props.length; i < len; ++i )
134         {
135         Property prop = props[i];
136         if (! transientProperties.contains( prop.getName() ) )
137             {
138             Class JavaDoc propType = propTypes[i];
139             if (propType != null && propType.isPrimitive()) //if a propType is unresolvable, it ain't a primitive
140
{
141                 if (propType == byte.class)
142                     iw.println("this." + prop.getName() + " = ois.readByte();");
143                 else if (propType == char.class)
144                     iw.println("this." + prop.getName() + " = ois.readChar();");
145                 else if (propType == short.class)
146                     iw.println("this." + prop.getName() + " = ois.readShort();");
147                 else if (propType == int.class)
148                     iw.println("this." + prop.getName() + " = ois.readInt();");
149                 else if (propType == boolean.class)
150                     iw.println("this." + prop.getName() + " = ois.readBoolean();");
151                 else if (propType == long.class)
152                     iw.println("this." + prop.getName() + " = ois.readLong();");
153                 else if (propType == float.class)
154                     iw.println("this." + prop.getName() + " = ois.readFloat();");
155                 else if (propType == double.class)
156                     iw.println("this." + prop.getName() + " = ois.readDouble();");
157                 }
158             else
159                 writeUnstoreObject( prop, propType, iw );
160             }
161         else
162             {
163             String JavaDoc initializer = (String JavaDoc) transientPropertyInitializers.get( prop.getName() );
164             if (initializer != null)
165                 iw.println("this." + prop.getName() + " = " + initializer +';');
166             }
167         }
168     generateExtraSerInitializers( info, superclassType, props, propTypes, iw);
169     iw.println("break;");
170     iw.downIndent();
171     iw.println("default:");
172     iw.upIndent();
173     iw.println("throw new IOException(\"Unsupported Serialized Version: \" + version);");
174     iw.downIndent();
175
176     iw.downIndent();
177     iw.println("}");
178
179     iw.downIndent();
180     iw.println("}");
181     }
182
183     protected void writeStoreObject( Property prop, Class JavaDoc propType, IndentedWriter iw ) throws IOException JavaDoc
184     {
185     iw.println("oos.writeObject( " + prop.getName() + " );");
186     }
187
188     protected void writeUnstoreObject( Property prop, Class JavaDoc propType, IndentedWriter iw ) throws IOException JavaDoc
189     {
190     iw.println("this." + prop.getName() + " = (" + prop.getSimpleTypeName() + ") ois.readObject();");
191     }
192
193     protected void generateExtraSerWriteStatements(ClassInfo info, Class JavaDoc superclassType, Property[] props, Class JavaDoc[] propTypes, IndentedWriter iw)
194     throws IOException JavaDoc
195     {}
196
197     protected void generateExtraSerInitializers(ClassInfo info, Class JavaDoc superclassType, Property[] props, Class JavaDoc[] propTypes, IndentedWriter iw)
198     throws IOException JavaDoc
199     {}
200
201 }
202
Popular Tags