1 4 package com.tc.util; 5 6 import sun.reflect.ReflectionFactory; 7 8 import com.tc.exception.TCRuntimeException; 9 10 import java.lang.reflect.Constructor ; 11 12 15 public class ReflectionUtil { 16 private static final Constructor refConstructor; 17 private static final ReflectionFactory rf = ReflectionFactory.getReflectionFactory(); 18 19 static { 20 try { 21 refConstructor = Object .class.getDeclaredConstructor(new Class [0]); 22 } catch (NoSuchMethodException e) { 23 throw new TCRuntimeException(e); 24 } 25 } 26 27 private ReflectionUtil() { 28 } 30 31 public static Constructor newConstructor(Class clazz, Class logicalSuperClass) { 32 Constructor useConstructor = refConstructor; 33 if (logicalSuperClass != null) { 34 try { 35 useConstructor = logicalSuperClass.getDeclaredConstructor(new Class [0]); 36 } catch (SecurityException e) { 37 throw new TCRuntimeException(e); 38 } catch (NoSuchMethodException e) { 39 throw new TCRuntimeException(e); 40 } 41 } 42 return rf.newConstructorForSerialization(clazz, useConstructor); 43 } 44 45 } 46 | Popular Tags |