1 19 20 package org.openide.util.io; 21 22 import java.awt.Image ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.IOException ; 25 import java.io.ObjectOutput ; 26 import java.io.ObjectOutputStream ; 27 import java.io.OutputStream ; 28 import java.util.ArrayList ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.Set ; 34 import java.util.WeakHashMap ; 35 import java.util.logging.Logger ; 36 import org.openide.util.WeakSet; 37 38 41 44 public class NbObjectOutputStream extends ObjectOutputStream { 45 private static final String SVUID = "serialVersionUID"; private static final Set <String > alreadyReported = new WeakSet<String >(); 47 48 static { 49 alreadyReported.add("java.lang.Exception"); alreadyReported.add("java.io.IOException"); alreadyReported.add("java.util.TreeSet"); alreadyReported.add("java.awt.geom.AffineTransform"); } 55 56 private static Map <String ,Boolean > examinedClasses = new WeakHashMap <String ,Boolean >(250); 57 private final List <Class > serializing = new ArrayList <Class >(50); 58 59 63 public NbObjectOutputStream(OutputStream os) throws IOException { 64 super(os); 65 66 try { 67 enableReplaceObject(true); 68 } catch (SecurityException ex) { 69 throw (IOException ) new IOException (ex.toString()).initCause(ex); 70 } 71 } 72 73 76 public Object replaceObject(Object obj) throws IOException { 77 if (obj instanceof Image ) { 78 return null; 79 80 } 83 84 return super.replaceObject(obj); 85 } 86 87 94 public static void writeSafely(ObjectOutput oo, Object obj) 95 throws IOException { 96 ByteArrayOutputStream bos = new ByteArrayOutputStream (200); 97 98 try { 99 NbObjectOutputStream oos = new NbObjectOutputStream(bos); 100 oos.writeObject(obj); 101 oos.flush(); 102 bos.close(); 103 } catch (Exception exc) { 104 oo.writeInt(0); 107 throw new SafeException(exc); 108 } 109 110 oo.writeInt(bos.size()); 111 oo.write(bos.toByteArray()); 112 } 113 114 protected void annotateClass(Class cl) throws IOException { 115 super.annotateClass(cl); 116 117 if (cl.isArray()) { 118 return; 119 } 120 121 if (cl.isInterface()) { 122 return; 125 } 126 127 serializing.add(cl); 128 129 if (isSerialVersionUIDDeclared(cl)) { 130 return; 131 } 132 133 if (IOException .class.isAssignableFrom(cl)) { 134 return; 138 } 139 140 String classname = cl.getName(); 141 142 if (alreadyReported.add(classname)) { 143 Set <Class > serializingUniq = new HashSet <Class >(); 144 StringBuffer b = new StringBuffer ("Serializable class "); b.append(classname); 146 b.append(" does not declare serialVersionUID field. Encountered while storing: ["); 148 Iterator it = serializing.iterator(); 149 boolean first = true; 150 151 while (it.hasNext()) { 152 Class c = (Class ) it.next(); 153 154 if ((c != cl) && serializingUniq.add(c)) { 155 if (first) { 156 first = false; 157 } else { 158 b.append(", "); } 160 161 b.append(c.getName()); 162 } 163 } 164 165 b.append("] See also http://www.netbeans.org/issues/show_bug.cgi?id=19915"); 167 String file = System.getProperty("InstanceDataObject.current.file"); 169 if ((file != null) && (file.length() > 0)) { 170 b.append(" [may have been writing "); b.append(file); 172 b.append("]"); } 174 175 Logger.getAnonymousLogger().warning(b.toString()); 176 } 177 } 178 179 private static boolean isSerialVersionUIDDeclared(Class clazz) { 180 String classname = clazz.getName(); 181 Boolean okay = examinedClasses.get(classname); 182 183 if (okay == null) { 184 if (classname.equals("java.util.HashSet") || classname.equals("java.util.ArrayList")) { okay = Boolean.TRUE; 186 } else { 187 okay = Boolean.FALSE; 188 189 java.lang.reflect.Field [] flds = clazz.getDeclaredFields(); 190 191 for (int i = 0; i < flds.length; i++) { 192 if (flds[i].getName().equals(SVUID)) { 193 okay = Boolean.TRUE; 194 195 break; 196 } 197 } 198 } 199 200 examinedClasses.put(clazz.getName(), okay); 201 } 202 203 return okay.booleanValue(); 204 } 205 } 206 | Popular Tags |