1 9 10 11 package JSX.magic; 12 import sun.misc.Unsafe; 13 import java.io.*; 14 import java.lang.reflect.*; 15 16 import sun.reflect.ReflectionFactory; 17 import java.security.AccessController ; 18 import java.security.PrivilegedAction ; 19 20 class MagicClass14 extends MagicClassI { 21 static ReflectionFactory reflFactory = (ReflectionFactory) 22 AccessController.doPrivileged( 23 new ReflectionFactory.GetReflectionFactoryAction()); 24 static Constructor objectConstructor; 25 static Unsafe unsafe; 26 public MagicClass14() { 27 try { 29 objectConstructor = Object .class.getDeclaredConstructor(new Class [0]); 30 } catch (NoSuchMethodException noMethod) { 31 throw new InternalError ("Object constructor not found"); 32 } 33 34 try { 36 Class osc$frClazz = Class.forName("java.io.ObjectStreamClass$FieldReflector"); 37 Field unsafeF = osc$frClazz.getDeclaredField("unsafe"); 38 unsafeF.setAccessible(true); 39 unsafe = (Unsafe) unsafeF.get(null); } catch (ClassNotFoundException noClass) { 41 throw new InternalError ("java.io.ObjectStreamClass$FieldReflector not found"); 42 } catch (NoSuchFieldException noField) { 43 throw new InternalError ("java.io.ObjectStreamClass$FieldReflector has no field 'unsafe'"); 44 } catch (IllegalAccessException noAccess) { 45 throw new InternalError ("not able to access java.io.ObjectStreamClass$FieldReflector.unsafe"); 46 } 47 48 try { 50 osc = (ObjectStreamClass) newInstance(ObjectStreamClass.class); 51 } catch (IllegalAccessException noAccess) { 52 throw new InternalError (noAccess.toString()); 53 } catch (InvocationTargetException e) { 54 e.printStackTrace(); 55 } catch (NotSerializableException e) { e.printStackTrace(); 57 } 58 } 59 60 61 62 public Object newInstance(Class currentClass) 65 throws InvocationTargetException, 66 NotSerializableException, 67 IllegalAccessException { 68 73 74 75 Constructor clCons = null; 76 if (Serializable.class.isAssignableFrom(currentClass)) { 78 clCons = getSerializableConstructor(currentClass); 79 if (clCons==null) 80 throw new InternalError ("Unable to find no-arg constructor of first non-serializable subclass of "+currentClass); 81 } else { 82 clCons = reflFactory.newConstructorForSerialization(currentClass, objectConstructor); 84 clCons.setAccessible(true); 85 } 86 87 Object obj = null; 88 try { 89 obj = clCons.newInstance(new Object [0]); 90 } catch (InstantiationException cantMake) { 91 throw new InternalError ("serialization constructor for "+currentClass+" not not able to instantiate it"); 92 } 93 return obj; 94 } 95 96 101 private static Constructor getSerializableConstructor(Class cl) { 102 Class initCl = cl; 103 while (Serializable.class.isAssignableFrom(initCl)) { 104 if ((initCl = initCl.getSuperclass()) == null) { 105 return null; 106 } 107 } 108 try { 109 Constructor cons = initCl.getDeclaredConstructor(new Class [0]); 110 int mods = cons.getModifiers(); 111 if ((mods & Modifier.PRIVATE) != 0 || 112 ((mods & (Modifier.PUBLIC | Modifier.PROTECTED)) == 0 && 113 !packageEquals(cl, initCl))) { 114 return null; 115 } 116 cons = reflFactory.newConstructorForSerialization(cl, cons); 117 cons.setAccessible(true); 118 return cons; 119 } catch (NoSuchMethodException ex) { 120 return null; 121 } 122 } 123 124 128 private static boolean packageEquals(Class cl1, Class cl2) { 129 Package pkg1 = cl1.getPackage(), pkg2 = cl2.getPackage(); 130 return ((pkg1 == pkg2) || ((pkg1 != null) && (pkg1.equals(pkg2)))); 131 } 132 133 134 135 public void setObjectFieldValue(Object parent, Field f, Class type, Object value) { 137 if (value!=null && !f.getType().isInstance(value)) throw new ClassCastException ("Expected "+f.getType()+" or subclass, got "+value.getClass()); 140 unsafe.putObject(parent, unsafe.fieldOffset(f), value); 141 } 142 143 public void setPrimitiveFieldValues(Object parent, Field f, Object value) { 144 int key = unsafe.fieldOffset(f); 146 Class type = f.getType(); if (type==int.class) unsafe.putInt(parent, key, ((Integer )value).intValue()); 149 else if (type==double.class) 150 unsafe.putDouble(parent, key, ((Double )value).doubleValue()); 151 else if (type==boolean.class) 152 unsafe.putBoolean(parent, key, ((Boolean )value).booleanValue()); 153 else if (type==byte.class) 154 unsafe.putByte(parent, key, ((Byte )value).byteValue()); 155 else if (type==char.class) 156 unsafe.putChar(parent, key, ((Character )value).charValue()); 157 else if (type==short.class) 158 unsafe.putShort(parent, key, ((Short )value).shortValue()); 159 else if (type==float.class) 160 unsafe.putFloat(parent, key, ((Float )value).floatValue()); 161 else if (type==long.class) 162 unsafe.putLong(parent, key, ((Long )value).longValue()); 163 else 164 throw new InternalError (type+" is unknown type, expected primitive"); 165 } 166 } 167
| Popular Tags
|