1 22 23 24 package com.mchange.v2.codegen.bean; 25 26 import java.util.*; 27 import java.io.Serializable ; 28 import java.io.IOException ; 29 import com.mchange.v2.codegen.IndentedWriter; 30 import com.mchange.v2.ser.IndirectPolicy; 31 32 public class IndirectingSerializableExtension extends SerializableExtension 33 { 34 protected String findIndirectorExpr; 35 protected String indirectorClassName; 36 37 45 public IndirectingSerializableExtension( String indirectorClassName ) 46 { 47 this.indirectorClassName = indirectorClassName; 48 this.findIndirectorExpr = "new " + indirectorClassName + "()"; 49 } 50 51 protected IndirectingSerializableExtension() 52 {} 53 54 public Collection extraSpecificImports() 55 { 56 Collection col = super.extraSpecificImports(); 57 col.add( indirectorClassName ); 58 col.add( "com.mchange.v2.ser.IndirectlySerialized" ); 59 col.add( "com.mchange.v2.ser.Indirector" ); 60 col.add( "com.mchange.v2.ser.SerializableUtils" ); 61 col.add( "java.io.NotSerializableException" ); 62 return col; 63 } 64 65 protected IndirectPolicy indirectingPolicy( Property prop, Class propType ) 66 { 67 if (Serializable .class.isAssignableFrom( propType )) 68 return IndirectPolicy.DEFINITELY_DIRECT; 69 else 70 return IndirectPolicy.INDIRECT_ON_EXCEPTION; 71 } 72 73 78 protected void writeInitializeIndirector( Property prop, Class propType, IndentedWriter iw ) throws IOException 79 {} 80 81 protected void writeExtraDeclarations(ClassInfo info, Class superclassType, Property[] props, Class [] propTypes, IndentedWriter iw) 82 throws IOException 83 {} 84 85 public void generate(ClassInfo info, Class superclassType, Property[] props, Class [] propTypes, IndentedWriter iw) 86 throws IOException 87 { 88 super.generate( info, superclassType, props, propTypes, iw); 89 writeExtraDeclarations( info, superclassType, props, propTypes, iw); 90 } 91 92 protected void writeStoreObject( Property prop, Class propType, IndentedWriter iw ) throws IOException 93 { 94 IndirectPolicy policy = indirectingPolicy( prop, propType ); 95 if (policy == IndirectPolicy.DEFINITELY_INDIRECT) 96 writeIndirectStoreObject( prop, propType, iw ); 97 else if (policy == IndirectPolicy.INDIRECT_ON_EXCEPTION) 98 { 99 iw.println("try"); 100 iw.println("{"); 101 iw.upIndent(); 102 iw.println("//test serialize"); 103 iw.println("SerializableUtils.toByteArray(" + prop.getName() + ");"); 104 super.writeStoreObject( prop, propType, iw ); 105 iw.downIndent(); 106 iw.println("}"); 107 iw.println("catch (NotSerializableException nse)"); 108 iw.println("{"); 109 iw.upIndent(); 110 writeIndirectStoreObject( prop, propType, iw ); 111 iw.downIndent(); 112 iw.println("}"); 113 } 114 else if (policy == IndirectPolicy.DEFINITELY_DIRECT) 115 super.writeStoreObject( prop, propType, iw ); 116 else 117 throw new InternalError ("indirectingPolicy() overridden to return unknown policy: " + policy); 118 } 119 120 protected void writeIndirectStoreObject( Property prop, Class propType, IndentedWriter iw ) throws IOException 121 { 122 iw.println("try"); 123 iw.println("{"); 124 iw.upIndent(); 125 126 iw.println("Indirector indirector = " + findIndirectorExpr + ';'); 127 writeInitializeIndirector( prop, propType, iw ); 128 iw.println("oos.writeObject( indirector.indirectForm( " + prop.getName() + " ) );"); 129 130 iw.downIndent(); 131 iw.println("}"); 132 iw.println("catch (IOException indirectionIOException)"); 133 iw.println("{ throw indirectionIOException; }"); 134 iw.println("catch (Exception indirectionOtherException)"); 135 iw.println("{ throw new IOException(\"Problem indirectly serializing " + prop.getName() + ": \" + indirectionOtherException.toString() ); }"); 136 } 137 138 protected void writeUnstoreObject( Property prop, Class propType, IndentedWriter iw ) throws IOException 139 { 140 IndirectPolicy policy = indirectingPolicy( prop, propType ); 141 if (policy == IndirectPolicy.DEFINITELY_INDIRECT || policy == IndirectPolicy.INDIRECT_ON_EXCEPTION) 142 { 143 iw.println("Object o = ois.readObject();"); 144 iw.println("if (o instanceof IndirectlySerialized) o = ((IndirectlySerialized) o).getObject();"); 145 iw.println("this." + prop.getName() + " = (" + prop.getSimpleTypeName() + ") o;"); 146 } 147 else if (policy == IndirectPolicy.DEFINITELY_DIRECT) 148 super.writeUnstoreObject( prop, propType, iw ); 149 else 150 throw new InternalError ("indirectingPolicy() overridden to return unknown policy: " + policy); 151 } 152 153 } 154 | Popular Tags |