KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > easymock > classextension > internal > SunClassInstantiator


1 /*
2  * Copyright (c) 2003-2004 OFFIS. This program is made available under the terms of
3  * the MIT License.
4  */

5 package org.easymock.classextension.internal;
6
7 import java.lang.reflect.Constructor JavaDoc;
8 import java.lang.reflect.InvocationTargetException JavaDoc;
9
10 import sun.reflect.ReflectionFactory;
11
12 /**
13  * Instantiate a class for Sun JVM.
14  * <p>
15  * The implementation doesn't call any constructor just like serialization is
16  * doing.
17  */

18 public class SunClassInstantiator implements IClassInstantiator {
19
20     public Object JavaDoc newInstance(Class JavaDoc type) throws InstantiationException JavaDoc {
21         try {
22             Constructor JavaDoc customConstructor = getMungedConstructor(type);
23             Object JavaDoc newValue = customConstructor.newInstance(new Object JavaDoc[0]);
24             return newValue;
25             // ///CLOVER:OFF
26
} catch (NoSuchMethodException JavaDoc e) {
27             throw new RuntimeException JavaDoc("Failed to instantiate "
28                     + type.getName() + "'s mock: " + e.getMessage());
29         } catch (IllegalAccessException JavaDoc e) {
30             throw new RuntimeException JavaDoc("Failed to instantiate "
31                     + type.getName() + "'s mock: " + e.getMessage());
32         } catch (InvocationTargetException JavaDoc e) {
33             throw new RuntimeException JavaDoc("Failed to instantiate "
34                     + type.getName() + "'s mock: " + e.getMessage());
35         }
36         // ///CLOVER:ON
37
}
38
39     private Constructor JavaDoc getMungedConstructor(Class JavaDoc type)
40             throws NoSuchMethodException JavaDoc {
41         Constructor JavaDoc javaLangObjectConstructor = Object JavaDoc.class
42                 .getDeclaredConstructor(new Class JavaDoc[0]);
43         Constructor JavaDoc customConstructor = ReflectionFactory
44                 .getReflectionFactory().newConstructorForSerialization(type,
45                         javaLangObjectConstructor);
46         return customConstructor;
47     }
48 }
49
Popular Tags