1 4 package com.tc.object; 5 6 import com.tc.object.bytecode.Manageable; 7 import com.tc.object.dna.api.DNA; 8 import com.tc.util.UnsafeUtil; 9 10 import java.io.IOException ; 11 import java.lang.reflect.Array ; 12 import java.lang.reflect.Constructor ; 13 import java.lang.reflect.InvocationTargetException ; 14 15 18 public class TCObjectFactoryImpl implements TCObjectFactory { 19 private final static Object [] EMPTY_OBJECT_ARRAY = new Object [] {}; 20 21 private ClientObjectManager objectManager; 22 private final TCClassFactory clazzFactory; 23 24 public TCObjectFactoryImpl(TCClassFactory clazzFactory) { 25 this.clazzFactory = clazzFactory; 26 } 27 28 public void setObjectManager(ClientObjectManager objectManager) { 29 this.objectManager = objectManager; 30 } 31 32 public TCObject getNewInstance(ObjectID id, Object peer, Class clazz) { 33 TCClass tcc = clazzFactory.getOrCreate(clazz, objectManager); 34 TCObject rv = tcc.createTCObject(id, peer); 35 36 if (peer instanceof Manageable) { 37 ((Manageable) peer).__tc_managed(rv); 38 } 39 40 return rv; 41 } 42 43 public TCObject getNewInstance(ObjectID id, Class clazz) { 44 return getNewInstance(id, null, clazz); 45 } 46 47 public Object getNewPeerObject(TCClass type, DNA dna) throws IOException , ClassNotFoundException { 48 return type.getNewInstanceFromNonDefaultConstructor(dna); 49 } 50 51 public Object getNewPeerObject(TCClass type, Object parent) throws IllegalArgumentException , SecurityException , 52 InstantiationException , IllegalAccessException , InvocationTargetException , NoSuchMethodException { 53 return getNewPeerObject(type.getConstructor(), type, parent); 55 } 56 57 public Object getNewArrayInstance(TCClass type, int size) { 58 return Array.newInstance(type.getComponentType(), size); 59 } 60 61 public Object getNewPeerObject(TCClass type) throws IllegalArgumentException , InstantiationException , 62 IllegalAccessException , InvocationTargetException , SecurityException , NoSuchMethodException { 63 Constructor ctor = type.getConstructor(); 64 if (ctor == null) throw new AssertionError ("type:" + type.getName()); 65 return getNewPeerObject(ctor, EMPTY_OBJECT_ARRAY); 66 } 67 68 private Object getNewPeerObject(Constructor ctor, Object [] args) throws IllegalArgumentException , 69 InstantiationException , IllegalAccessException , InvocationTargetException { 70 final Object rv; 71 72 Thread thread = Thread.currentThread(); 74 ClassLoader cl = thread.getContextClassLoader(); 75 try { 76 thread.setContextClassLoader(ctor.getDeclaringClass().getClassLoader()); 77 rv = ctor.newInstance(args); 78 } finally { 79 thread.setContextClassLoader(cl); 80 } 81 return rv; 82 } 83 84 private Object getNewPeerObject(Constructor ctor, TCClass type, Object parent) throws IllegalArgumentException , 85 InstantiationException , IllegalAccessException , InvocationTargetException { 86 final Object rv; 87 88 Thread thread = Thread.currentThread(); 90 ClassLoader cl = thread.getContextClassLoader(); 91 try { 92 thread.setContextClassLoader(ctor.getDeclaringClass().getClassLoader()); 93 rv = ctor.newInstance(EMPTY_OBJECT_ARRAY); 94 UnsafeUtil.setField(rv, type.getParentField(), parent); 95 } finally { 96 thread.setContextClassLoader(cl); 97 } 98 return rv; 99 } 100 101 } | Popular Tags |