1 9 10 11 package JSX.magic; 12 import java.lang.reflect.*; 13 import java.io.*; 14 15 class MagicClass13 extends MagicClassI { 16 17 private static Method allocateNewObject; 18 private static Method setObjectFieldValue; 19 private static Method setPrimitiveFieldValues; 20 private static Method getFieldIDs; 21 MagicClass13() { 22 try { 23 allocateNewObject = java.io.ObjectInputStream .class.getDeclaredMethod( 24 "allocateNewObject", 25 new Class [] {Class .class, Class .class}); 26 allocateNewObject.setAccessible(true); 27 28 setObjectFieldValue = java.io.ObjectInputStream .class.getDeclaredMethod( 29 "setObjectFieldValue", 30 new Class [] {Object .class, long.class, Class .class, Object .class}); 31 setObjectFieldValue.setAccessible(true); 32 33 setPrimitiveFieldValues = java.io.ObjectInputStream .class.getDeclaredMethod( 34 "setPrimitiveFieldValues", 35 new Class [] {Object .class, long[].class, char[].class, byte[].class}); 36 setPrimitiveFieldValues.setAccessible(true); 37 38 getFieldIDs = java.io.ObjectStreamClass .class.getDeclaredMethod( 39 "getFieldIDs", 40 new Class [] {ObjectStreamField[].class, long[].class, long[].class}); 41 getFieldIDs.setAccessible(true); 42 43 } catch (NoSuchMethodException noMethod) { 44 throw new InternalError (noMethod.toString()); 45 } 46 47 try { 48 osc = (ObjectStreamClass) newInstance(ObjectStreamClass.class); 49 } catch (NotSerializableException e) { e.printStackTrace(); 51 } 52 } 53 54 55 62 public Object newInstance(Class currentClass) 63 throws NotSerializableException { 64 65 70 71 Class currclass; 72 if (Serializable.class.isAssignableFrom(currentClass)) { 73 currclass = currentClass; if (currclass != null) { 76 while (Serializable.class.isAssignableFrom(currclass)) 77 currclass = currclass.getSuperclass(); 78 } 79 } else { 80 currclass = Object .class; } 83 84 Object [] argValues = {currentClass, currclass}; 86 try { 87 return allocateNewObject.invoke(null, argValues); 88 } catch (InvocationTargetException invokeE) { 89 throw new InternalError (invokeE.toString()); 90 } catch (IllegalAccessException noAccess) { 91 throw new InternalError (); 92 } 93 } 94 95 96 public void setObjectFieldValue(Object parent, Field f, Class type, Object value) { 97 long[] primIDs = new long[0]; 98 long[] objIDs = new long[1]; myGetFieldIDs(f, primIDs, objIDs); 100 101 Object [] argValues = {parent, new Long (objIDs[0]), type, value}; 102 try { 103 setObjectFieldValue.invoke(null, argValues); 104 } catch (InvocationTargetException invokeE) { 105 throw new InternalError (invokeE.toString()); 106 } catch (IllegalAccessException noAccess) { 107 throw new InternalError (); 108 } 109 } 110 111 112 public void setPrimitiveFieldValues(Object parent, Field f, Object value) { 113 long[] primIDs = new long[1]; long[] objIDs = new long[0]; 115 ObjectStreamField osf = myGetFieldIDs(f, primIDs, objIDs); 116 char[] typecode = {osf.getTypeCode()}; 119 ByteArrayOutputStream ba1 = new ByteArrayOutputStream(); 120 DataOutputStream ba = new DataOutputStream(ba1); 121 try { 122 switch (typecode[0]) { 123 case 'B': ba.writeByte(((Byte )value).byteValue()); break; case 'C': ba.writeChar(((Character )value).charValue()); break; case 'S': ba.writeShort(((Short )value).shortValue()); break; case 'I': ba.writeInt(((Integer )value).intValue()); break; case 'J': ba.writeLong(((Long )value).longValue()); break; case 'F': ba.writeFloat(((Float )value).floatValue()); break; case 'D': ba.writeDouble(((Double )value).doubleValue()); break; case 'Z': ba.writeBoolean(((Boolean )value).booleanValue()); break; case '[': case 'L': default: 134 throw new InternalError ("setPrimitiveFieldValues only does primitives"); 135 } 136 ba.flush(); 137 ba.close(); 138 } catch (IOException byteArrayProblem) { 139 throw new InternalError (byteArrayProblem.toString()); 140 } 141 142 Object [] argValues = {parent, primIDs, typecode, ba1.toByteArray()}; 143 try { 144 setPrimitiveFieldValues.invoke(null, argValues); 145 } catch (InvocationTargetException invokeE) { 146 throw new InternalError ("MagicClass13.setPrimitiveFieldValues() failure"); 147 } catch (IllegalAccessException noAccess) { 148 throw new InternalError (); 149 } 150 } 151 152 153 static protected ObjectStreamField myGetFieldIDs(Field f, long[] primIDs, long[] objIDs) { 154 ObjectStreamField osf = null; 155 try { 156 osf = new ObjectStreamField(f.getName(), f.getType()); 157 Field osfField = ObjectStreamField.class.getDeclaredField("field"); 158 osfField.setAccessible(true); 159 osfField.set(osf, f); 160 } catch (NoSuchFieldException noField) { 161 throw new InternalError (); 162 } catch (IllegalAccessException noAccess) { 163 throw new InternalError (); 164 } 165 ObjectStreamField[] fs = {osf}; 166 getFieldIDs(fs, primIDs, objIDs); 167 return osf; } 169 170 171 static protected void getFieldIDs(ObjectStreamField[] fs, long[] primIDs, long[] objIDs) { 172 try { 173 Object [] argValues = {fs, primIDs, objIDs}; 174 getFieldIDs.invoke(null, argValues); } catch (InvocationTargetException invokeE) { 176 throw new InternalError (invokeE.toString()); 177 } catch (IllegalAccessException noAccess) { 178 throw new InternalError (); 179 } 180 } 181 182 } 183 | Popular Tags |