1 5 package com.tc.object.applicator; 6 7 import com.tc.exception.TCNotSupportedMethodException; 8 import com.tc.object.ClientObjectManager; 9 import com.tc.object.TCObject; 10 import com.tc.object.TraversedReferences; 11 import com.tc.object.dna.api.DNA; 12 import com.tc.object.dna.api.DNACursor; 13 import com.tc.object.dna.api.DNAWriter; 14 import com.tc.object.dna.api.PhysicalAction; 15 import com.tc.object.dna.impl.DNAEncoding; 16 import com.tc.object.tx.optimistic.OptimisticTransactionManager; 17 import com.tc.util.Assert; 18 19 import java.io.IOException ; 20 import java.lang.reflect.AccessibleObject ; 21 import java.lang.reflect.Method ; 22 import java.util.Map ; 23 24 27 public class AccessibleObjectApplicator extends BaseApplicator { 28 private static final String DECLARING_CLASS_FIELD_NAME = "java.lang.reflect.AccessibleObject.declaringClass"; 29 private static final String ACCESSIBLE_OBJECT_NAME_FILED_NAME = "java.lang.reflect.AccessibleObject.name"; 30 private static final String OVERRIDE_FILED_NAME = "java.lang.reflect.AccessibleObject.override"; 31 private static final String PARAMETER_TYPES = "java.lang.reflect.AccessibleObject.parameterTypes"; 32 private static final String ACCESSIBLE_OBJECT_TYPE = "java.lang.reflect.AccessibleObject.type"; 33 private static final String METHOD_CLASS_NAME = Method .class.getName(); 34 35 public AccessibleObjectApplicator(DNAEncoding encoding) { 36 super(encoding); 37 } 38 39 public TraversedReferences getPortableObjects(Object pojo, TraversedReferences addTo) { 40 if (pojo instanceof Method ) { 41 addTo.addAnonymousReference(((Method ) pojo).getDeclaringClass()); 42 addTo.addAnonymousReference(((Method ) pojo).getName()); 43 addTo.addAnonymousReference(((Method ) pojo).getParameterTypes()); 44 } 45 return addTo; 46 } 47 48 public void hydrate(ClientObjectManager objectManager, TCObject tcObject, DNA dna, Object po) throws IOException , 49 IllegalArgumentException , ClassNotFoundException { 50 DNACursor cursor = dna.getCursor(); 51 Assert.eval(cursor.getActionCount() <= 1); 52 53 if (po instanceof Method ) { 54 if (cursor.next(encoding)) { 55 PhysicalAction a = (PhysicalAction) cursor.getAction(); 56 Boolean value = (Boolean ) a.getObject(); 57 ((Method )po).setAccessible(value.booleanValue()); 58 } 59 } 60 } 61 62 public void dehydrate(ClientObjectManager objectManager, TCObject tcObject, DNAWriter writer, Object pojo) { 63 Class declaringClass = null; 64 String name = null; 65 boolean override = false; 66 AccessibleObject ao = (AccessibleObject ) pojo; 67 if (ao instanceof Method ) { 68 declaringClass = ((Method ) ao).getDeclaringClass(); 69 name = ((Method ) ao).getName(); 70 override = ((Method ) ao).isAccessible(); 71 72 Class [] parameterTypes = ((Method ) ao).getParameterTypes(); 73 74 writer.addPhysicalAction(ACCESSIBLE_OBJECT_TYPE, pojo.getClass().getName()); 75 writer.addPhysicalAction(DECLARING_CLASS_FIELD_NAME, declaringClass); 76 writer.addPhysicalAction(ACCESSIBLE_OBJECT_NAME_FILED_NAME, name); 77 writer.addPhysicalAction(OVERRIDE_FILED_NAME, new Boolean (override)); 78 writer.addPhysicalAction(PARAMETER_TYPES, parameterTypes); 79 } 80 } 81 82 public Object getNewInstance(ClientObjectManager objectManager, DNA dna) throws IOException , ClassNotFoundException { 83 DNACursor cursor = dna.getCursor(); 84 Assert.assertEquals(5, cursor.getActionCount()); 85 86 cursor.next(encoding); 87 PhysicalAction a = cursor.getPhysicalAction(); 88 String objectType = (String ) a.getObject(); 89 90 cursor.next(encoding); 91 a = cursor.getPhysicalAction(); 92 Class declaringClass = (Class ) a.getObject(); 93 94 cursor.next(encoding); 95 a = cursor.getPhysicalAction(); 96 String name = (String ) a.getObject(); 97 98 cursor.next(encoding); 99 a = cursor.getPhysicalAction(); 100 Boolean override = (Boolean ) a.getObject(); 101 102 cursor.next(encoding); 103 a = cursor.getPhysicalAction(); 104 Object [] values = (Object []) a.getObject(); 105 Class [] parameterTypes = new Class [values.length]; 106 System.arraycopy(values, 0, parameterTypes, 0, values.length); 107 108 if (METHOD_CLASS_NAME.equals(objectType)) { 109 try { 110 Method m = declaringClass.getDeclaredMethod(name, parameterTypes); 111 m.setAccessible(override.booleanValue()); 112 return m; 113 } catch (NoSuchMethodException e) { 114 throw new AssertionError (e); 115 } 116 } 117 throw new AssertionError ("Object type not known."); 118 } 119 120 public Map connectedCopy(Object source, Object dest, Map visited, ClientObjectManager objectManager, 121 OptimisticTransactionManager txManager) { 122 throw new TCNotSupportedMethodException(); 123 } 124 } 125 | Popular Tags |