1 5 package org.easymock.classextension.internal; 6 7 import java.lang.reflect.Constructor ; 8 import java.lang.reflect.InvocationTargetException ; 9 10 import sun.reflect.ReflectionFactory; 11 12 18 public class SunClassInstantiator implements IClassInstantiator { 19 20 public Object newInstance(Class type) throws InstantiationException { 21 try { 22 Constructor customConstructor = getMungedConstructor(type); 23 Object newValue = customConstructor.newInstance(new Object [0]); 24 return newValue; 25 } catch (NoSuchMethodException e) { 27 throw new RuntimeException ("Failed to instantiate " 28 + type.getName() + "'s mock: " + e.getMessage()); 29 } catch (IllegalAccessException e) { 30 throw new RuntimeException ("Failed to instantiate " 31 + type.getName() + "'s mock: " + e.getMessage()); 32 } catch (InvocationTargetException e) { 33 throw new RuntimeException ("Failed to instantiate " 34 + type.getName() + "'s mock: " + e.getMessage()); 35 } 36 } 38 39 private Constructor getMungedConstructor(Class type) 40 throws NoSuchMethodException { 41 Constructor javaLangObjectConstructor = Object .class 42 .getDeclaredConstructor(new Class [0]); 43 Constructor customConstructor = ReflectionFactory 44 .getReflectionFactory().newConstructorForSerialization(type, 45 javaLangObjectConstructor); 46 return customConstructor; 47 } 48 } 49 | Popular Tags |