1 16 17 18 package org.apache.commons.beanutils; 19 20 21 import java.io.IOException ; 22 import java.io.Serializable ; 23 import java.io.ObjectOutputStream ; 24 import java.io.ObjectInputStream ; 25 import java.io.StreamCorruptedException ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 30 43 44 public class DynaProperty implements Serializable { 45 46 48 56 private static final int BOOLEAN_TYPE = 1; 57 private static final int BYTE_TYPE = 2; 58 private static final int CHAR_TYPE = 3; 59 private static final int DOUBLE_TYPE = 4; 60 private static final int FLOAT_TYPE = 5; 61 private static final int INT_TYPE = 6; 62 private static final int LONG_TYPE = 7; 63 private static final int SHORT_TYPE = 8; 64 65 66 68 69 74 public DynaProperty(String name) { 75 76 this(name, Object .class); 77 78 } 79 80 81 87 public DynaProperty(String name, Class type) { 88 89 super(); 90 this.name = name; 91 this.type = type; 92 93 } 94 95 103 public DynaProperty(String name, Class type, Class contentType) { 104 105 super(); 106 this.name = name; 107 this.type = type; 108 this.contentType = contentType; 109 110 } 111 112 114 115 protected String name = null; 116 119 public String getName() { 120 return (this.name); 121 } 122 123 124 protected transient Class type = null; 125 135 public Class getType() { 136 return (this.type); 137 } 138 139 140 141 protected transient Class contentType; 142 153 public Class getContentType() { 154 return contentType; 155 } 156 157 159 160 163 public boolean isIndexed() { 164 165 if (type == null) { 166 return (false); 167 } else if (type.isArray()) { 168 return (true); 169 } else if (List .class.isAssignableFrom(type)) { 170 return (true); 171 } else { 172 return (false); 173 } 174 175 } 176 177 178 181 public boolean isMapped() { 182 183 if (type == null) { 184 return (false); 185 } else { 186 return (Map .class.isAssignableFrom(type)); 187 } 188 189 } 190 191 192 195 public String toString() { 196 197 StringBuffer sb = new StringBuffer ("DynaProperty[name="); 198 sb.append(this.name); 199 sb.append(",type="); 200 sb.append(this.type); 201 if (isMapped() || isIndexed()) { 202 sb.append(" <").append(this.contentType).append(">"); 203 } 204 sb.append("]"); 205 return (sb.toString()); 206 207 } 208 209 211 217 private void writeObject(ObjectOutputStream out) throws IOException { 218 219 writeAnyClass(this.type,out); 220 221 if (isMapped() || isIndexed()) { 222 writeAnyClass(this.contentType,out); 223 } 224 225 out.defaultWriteObject(); 227 } 228 229 232 private void writeAnyClass(Class clazz, ObjectOutputStream out) throws IOException { 233 int primitiveType = 0; 235 if (Boolean.TYPE.equals(clazz)) { 236 primitiveType = BOOLEAN_TYPE; 237 } else if (Byte.TYPE.equals(clazz)) { 238 primitiveType = BYTE_TYPE; 239 } else if (Character.TYPE.equals(clazz)) { 240 primitiveType = CHAR_TYPE; 241 } else if (Double.TYPE.equals(clazz)) { 242 primitiveType = DOUBLE_TYPE; 243 } else if (Float.TYPE.equals(clazz)) { 244 primitiveType = FLOAT_TYPE; 245 } else if (Integer.TYPE.equals(clazz)) { 246 primitiveType = INT_TYPE; 247 } else if (Long.TYPE.equals(clazz)) { 248 primitiveType = LONG_TYPE; 249 } else if (Short.TYPE.equals(clazz)) { 250 primitiveType = SHORT_TYPE; 251 } 252 253 if (primitiveType == 0) { 254 out.writeBoolean(false); 256 out.writeObject(clazz); 257 } else { 258 out.writeBoolean(true); 260 out.writeInt(primitiveType); 261 } 262 } 263 264 272 private void readObject(ObjectInputStream in) throws IOException , ClassNotFoundException { 273 274 this.type = readAnyClass(in); 275 276 if (isMapped() || isIndexed()) { 277 this.contentType = readAnyClass(in); 278 } 279 280 in.defaultReadObject(); 282 } 283 284 285 288 private Class readAnyClass(ObjectInputStream in) throws IOException , ClassNotFoundException { 289 if (in.readBoolean()) { 291 switch (in.readInt()) { 293 294 case BOOLEAN_TYPE: return Boolean.TYPE; 295 case BYTE_TYPE: return Byte.TYPE; 296 case CHAR_TYPE: return Character.TYPE; 297 case DOUBLE_TYPE: return Double.TYPE; 298 case FLOAT_TYPE: return Float.TYPE; 299 case INT_TYPE: return Integer.TYPE; 300 case LONG_TYPE: return Long.TYPE; 301 case SHORT_TYPE: return Short.TYPE; 302 default: 303 throw new StreamCorruptedException ( 305 "Invalid primitive type. " 306 + "Check version of beanutils used to serialize is compatible."); 307 308 } 309 310 } else { 311 return ((Class ) in.readObject()); 313 } 314 } 315 } | Popular Tags |