1 18 package org.apache.geronimo.interop.rmi.iiop; 19 20 import java.io.*; 21 import java.util.*; 22 import java.lang.reflect.*; 23 24 28 29 public class GetField extends java.io.ObjectInputStream.GetField 30 { 31 32 private final ObjectStreamClass desc; 33 34 private final byte[] primVals; 35 36 private final Object [] objVals; 37 38 private final int[] objHandles; 39 40 private int primDataSize = 0; 41 private int numObjFields = 0; 42 private ObjectStreamField[] _fields = null; 43 44 private static Method setOffsetMethod; 45 46 static 47 { 48 try 49 { 50 Class osFieldClass = java.io.ObjectStreamField .class; 51 Class [] params = new Class [1]; 52 params[0] = int.class; 53 setOffsetMethod = osFieldClass.getDeclaredMethod("setOffset", params); 54 setOffsetMethod.setAccessible(true); 55 } 56 catch (Throwable t) 57 { 58 t.printStackTrace(); 59 } 60 } 61 62 66 GetField(ObjectStreamClass desc) 67 { 68 this.desc = desc; 69 computeOffsets(); 70 primVals = new byte[primDataSize]; 71 objVals = new Object [numObjFields]; 72 objHandles = new int[objVals.length]; 73 } 74 75 80 public ObjectStreamClass getObjectStreamClass() 81 { 82 return desc; 83 } 84 85 96 public boolean defaulted(String name) throws IOException 97 { 98 int fieldOffset = getFieldOffset(name, null); 99 boolean result; 100 if(fieldOffset < 0) 101 { 102 result = false; 103 } 104 else 105 { 106 result = true; 107 } 108 return result; 109 } 110 111 123 public boolean get(String name, boolean val) throws IOException 124 { 125 int off = getFieldOffset(name, Boolean.TYPE); 126 return (off >= 0) ? Bits.getBoolean(primVals, off) : val; 127 } 128 129 141 public byte get(String name, byte val) throws IOException 142 { 143 int off = getFieldOffset(name, Byte.TYPE); 144 return (off >= 0) ? primVals[off] : val; 145 } 146 147 159 public char get(String name, char val) throws IOException 160 { 161 int off = getFieldOffset(name, Character.TYPE); 162 return (off >= 0) ? Bits.getChar(primVals, off) : val; 163 } 164 165 177 public short get(String name, short val) throws IOException 178 { 179 int off = getFieldOffset(name, Short.TYPE); 180 return (off >= 0) ? Bits.getShort(primVals, off) : val; 181 } 182 183 195 public int get(String name, int val) throws IOException 196 { 197 int off = getFieldOffset(name, Integer.TYPE); 198 return (off >= 0) ? Bits.getInt(primVals, off) : val; 199 } 200 201 213 public long get(String name, long val) throws IOException 214 { 215 int off = getFieldOffset(name, Long.TYPE); 216 return (off >= 0) ? Bits.getLong(primVals, off) : val; 217 } 218 219 231 public float get(String name, float val) throws IOException 232 { 233 int off = getFieldOffset(name, Float.TYPE); 234 return (off >= 0) ? Bits.getFloat(primVals, off) : val; 235 } 236 237 249 public double get(String name, double val) throws IOException 250 { 251 int off = getFieldOffset(name, Double.TYPE); 252 return (off >= 0) ? Bits.getDouble(primVals, off) : val; 253 } 254 255 267 public Object get(String name, Object val) throws IOException 268 { 269 int off = getFieldOffset(name, Object .class); 270 if (off >= 0) 271 { 272 return objVals[off]; 273 } 274 else 275 { 276 return val; 277 } 278 } 279 280 281 284 void readFields(ObjectInputStream oi) throws IOException 285 { 286 org.apache.geronimo.interop.rmi.iiop.ObjectInputStream in = 287 (org.apache.geronimo.interop.rmi.iiop.ObjectInputStream)oi; 288 289 in._cdrInput.read_align(4, 4); 290 291 for(int i = 0; i < primVals.length; i++) 293 { 294 primVals[i] = in.readByte(); 295 } 296 297 java.io.ObjectStreamField [] fields = desc.getFields(); 299 int numPrimFields = fields.length - objVals.length; 300 for (int i = 0; i < objVals.length; i++) 301 { 302 objVals[i] = in.readObject(ValueType.getInstance(fields[numPrimFields + i].getType())); 303 } 304 } 305 306 private int getFieldOffset(String name, Class type) 307 { 308 ObjectStreamField field = getField(name, type); 309 if (field == null) 310 { 311 throw new IllegalArgumentException ("no such field: " + name + " of type: " + type.getName()); 312 } 313 return field.getOffset(); 314 } 315 316 private ObjectStreamField getField(String name, Class type) 317 { 318 if(type == null) 319 { 320 for(int i = 0; i < _fields.length; i++) 322 { 323 if(_fields[i].getName().equals(name)) 324 { 325 return _fields[i]; 326 } 327 } 328 return (ObjectStreamField)null; 329 } 330 else if(type == java.lang.Object .class) 331 { 332 for(int i = 0; i < _fields.length; i++) 334 { 335 if(_fields[i].getName().equals(name) && !_fields[i].getType().isPrimitive()) 336 { 337 return _fields[i]; 338 } 339 } 340 return (ObjectStreamField)null; 341 } 342 else 343 { 344 for(int i = 0; i < _fields.length; i++) 345 { 346 if(_fields[i].getName().equals(name) && _fields[i].getType().equals(type)) 347 { 348 return _fields[i]; 349 } 350 } 351 return (ObjectStreamField)null; 352 } 353 } 354 355 private void computeOffsets() 356 { 357 try 358 { 359 computeFieldOffsets(); 360 } 361 catch(Exception e) 362 { 363 throw new RuntimeException (org.apache.geronimo.interop.util.ExceptionUtil.causedBy(e)); 364 } 365 } 366 367 private void computeFieldOffsets() throws Exception 368 { 369 primDataSize = 0; 370 numObjFields = 0; 371 int firstObjIndex = -1; 372 java.io.ObjectStreamField [] fields = desc.getFields(); 373 _fields = new ObjectStreamField[fields.length]; 374 Object [] args = new Object [1]; 375 376 for (int i = 0; i < fields.length; i++) 377 { 378 java.io.ObjectStreamField f = fields[i]; 379 _fields[i] = new ObjectStreamField(fields[i].getName(), fields[i].getType()); 380 ObjectStreamField _f = _fields[i]; 381 382 switch (f.getTypeCode()) 383 { 384 case 'Z': 385 case 'B': 386 args[0] = new Integer (primDataSize++); 387 setOffsetMethod.invoke(_f, args); 388 break; 389 390 case 'C': 391 case 'S': 392 args[0] = new Integer (primDataSize); 393 setOffsetMethod.invoke(_f, args); 394 primDataSize += 2; 395 break; 396 397 case 'I': 398 case 'F': 399 args[0] = new Integer (primDataSize); 400 setOffsetMethod.invoke(_f, args); 401 primDataSize += 4; 402 break; 403 404 case 'J': 405 case 'D': 406 args[0] = new Integer (primDataSize); 407 setOffsetMethod.invoke(_f, args); 408 primDataSize += 8; 409 break; 410 411 case '[': 412 case 'L': 413 args[0] = new Integer (numObjFields++); 414 setOffsetMethod.invoke(_f, args); 415 if (firstObjIndex == -1) 416 { 417 firstObjIndex = i; 418 } 419 break; 420 421 default: 422 break; 423 } 424 } 425 if (firstObjIndex != -1 && firstObjIndex + numObjFields != fields.length) 426 { 427 } 429 } 430 } 431 | Popular Tags |