1 22 23 24 package com.mchange.v2.codegen.bean; 25 26 import java.util.*; 27 import java.io.IOException ; 28 import com.mchange.v2.codegen.IndentedWriter; 29 30 31 36 public class SerializableExtension implements GeneratorExtension 37 { 38 Set transientProperties; 39 Map transientPropertyInitializers; 40 41 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 superclassType, Property[] props, Class [] propTypes, IndentedWriter iw) 78 throws IOException 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 propType = propTypes[i]; 95 if (propType != null && propType.isPrimitive()) { 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 propType = propTypes[i]; 139 if (propType != null && propType.isPrimitive()) { 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 initializer = (String ) 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 propType, IndentedWriter iw ) throws IOException 184 { 185 iw.println("oos.writeObject( " + prop.getName() + " );"); 186 } 187 188 protected void writeUnstoreObject( Property prop, Class propType, IndentedWriter iw ) throws IOException 189 { 190 iw.println("this." + prop.getName() + " = (" + prop.getSimpleTypeName() + ") ois.readObject();"); 191 } 192 193 protected void generateExtraSerWriteStatements(ClassInfo info, Class superclassType, Property[] props, Class [] propTypes, IndentedWriter iw) 194 throws IOException 195 {} 196 197 protected void generateExtraSerInitializers(ClassInfo info, Class superclassType, Property[] props, Class [] propTypes, IndentedWriter iw) 198 throws IOException 199 {} 200 201 } 202 | Popular Tags |