1 4 package com.tc.object.applicator; 5 6 import com.tc.exception.TCRuntimeException; 7 import com.tc.object.ClientObjectManager; 8 import com.tc.object.TCClass; 9 import com.tc.object.TCObject; 10 import com.tc.object.bytecode.JavaUtilConcurrentHashMapSegmentAdapter; 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.util.Assert; 17 18 import java.io.IOException ; 19 import java.lang.reflect.Field ; 20 import java.lang.reflect.InvocationTargetException ; 21 import java.lang.reflect.Method ; 22 23 public class ConcurrentHashMapSegmentApplicator extends PhysicalApplicator { 24 private static final String TABLE_LENGTH_FIELD_NAME = "java.util.concurrent.ConcurrentHashMap$Segment.capacity"; 25 private static final String TABLE_FIELD_NAME = "table"; 26 27 private final TCClass clazz; 28 29 public ConcurrentHashMapSegmentApplicator(TCClass clazz, DNAEncoding encoding) { 30 super(clazz, encoding); 31 this.clazz = clazz; 32 } 33 34 public void hydrate(ClientObjectManager objectManager, TCObject tcObject, DNA dna, Object po) throws IOException , 35 ClassNotFoundException { 36 DNACursor cursor = dna.getCursor(); 37 String fieldName; 38 Object fieldValue; 39 40 Integer capacity = null; 41 42 while (cursor.next(encoding)) { 43 PhysicalAction a = cursor.getPhysicalAction(); 44 Assert.eval(a.isTruePhysical()); 45 fieldName = a.getFieldName(); 46 fieldValue = a.getObject(); 47 if (TABLE_LENGTH_FIELD_NAME.equals(fieldName)) { 48 capacity = (Integer )fieldValue; 49 } else { 50 tcObject.setValue(fieldName, fieldValue); 51 } 52 } 53 initializeTable(capacity, po); 54 } 55 56 private void initializeTable(Integer capacity, Object pojo) { 57 Assert.assertNotNull(capacity); 58 Class peerClass = clazz.getPeerClass(); 59 try { 60 Method method = peerClass.getDeclaredMethod(JavaUtilConcurrentHashMapSegmentAdapter.INITIAL_TABLE_METHOD_NAME, new Class []{Integer.TYPE}); 61 method.setAccessible(true); 62 method.invoke(pojo, new Object []{capacity}); 63 } catch (SecurityException e) { 64 throw new TCRuntimeException(e); 65 } catch (NoSuchMethodException e) { 66 throw new TCRuntimeException(e); 67 } catch (IllegalArgumentException e) { 68 throw new TCRuntimeException(e); 69 } catch (IllegalAccessException e) { 70 throw new TCRuntimeException(e); 71 } catch (InvocationTargetException e) { 72 throw new TCRuntimeException(e); 73 } 74 } 75 76 private Field getTableField() { 77 Class peerClass = clazz.getPeerClass(); 78 try { 79 Field field = peerClass.getDeclaredField(TABLE_FIELD_NAME); 80 field.setAccessible(true); 81 return field; 82 } catch (NoSuchFieldException e) { 83 throw new TCRuntimeException(e); 84 } 85 } 86 87 public void dehydrate(ClientObjectManager objectManager, TCObject tcObject, DNAWriter writer, Object pojo) { 88 super.dehydrate(objectManager, tcObject, writer, pojo); 89 90 try { 91 Field field = getTableField(); 92 Object [] tableArray = (Object [])field.get(pojo); 93 int tableLength = tableArray.length; 94 writer.addPhysicalAction(TABLE_LENGTH_FIELD_NAME, new Integer (tableLength)); 95 } catch (SecurityException e) { 96 throw new TCRuntimeException(e); 97 } catch (IllegalArgumentException e) { 98 throw new TCRuntimeException(e); 99 } catch (IllegalAccessException e) { 100 throw new TCRuntimeException(e); 101 } 102 103 } 104 105 } 106 | Popular Tags |