1 package polyglot.util; 2 3 import polyglot.main.Report; 4 import polyglot.types.*; 5 6 import java.io.*; 7 import java.util.*; 8 9 10 public class TypeOutputStream extends ObjectOutputStream 11 { 12 protected TypeSystem ts; 13 protected Set roots; 14 protected Map placeHolders; 15 16 public TypeOutputStream( OutputStream out, TypeSystem ts, Type root) 17 throws IOException 18 { 19 super( out); 20 21 this.ts = ts; 22 this.roots = ts.getTypeEncoderRootSet(root); 23 this.placeHolders = new HashMap(); 24 25 if (Report.should_report(Report.serialize, 2)) { 26 Report.report(2, "Began TypeOutputStream with roots: " + roots); 27 } 28 29 enableReplaceObject( true); 30 } 31 32 protected Object placeHolder(TypeObject o) { 33 Object k = new IdentityKey(o); 34 Object p = placeHolders.get(k); 35 if (p == null) { 36 p = ts.placeHolder(o, roots); 37 placeHolders.put(k, p); 38 } 39 return p; 40 } 41 42 protected Object replaceObject(Object o) throws IOException 43 { 44 if (roots.contains(o)) { 45 if (Report.should_report(Report.serialize, 2)) { 46 Report.report(2, "+ In roots: " + o + " : " + o.getClass()); 47 } 48 return o; 49 } 50 else if (o instanceof TypeObject) { 51 Object r = placeHolder((TypeObject) o); 52 if (Report.should_report(Report.serialize, 2)) { 53 if (r != o) { 54 Report.report(2, "+ Replacing: " + o + " : " + o.getClass() 55 + " with " + r); 56 } 57 else { 58 Report.report(2, "+ " + o + " : " + o.getClass()); 59 } 60 } 61 return r; 62 } 63 else { 64 if (Report.should_report(Report.serialize, 2)) { 65 Report.report(2, "+ " + o + " : " + o.getClass()); 66 } 67 return o; 68 } 69 } 70 } 71 | Popular Tags |