KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > ReflectionUtil


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
11
12 /**
13  * A wrapper for unsafe usage in class like Atomic Variables, ReentrantLock, etc.
14  */

15 public class ReflectionUtil {
16   private static final Constructor JavaDoc refConstructor;
17   private static final ReflectionFactory rf = ReflectionFactory.getReflectionFactory();
18
19   static {
20     try {
21       refConstructor = Object JavaDoc.class.getDeclaredConstructor(new Class JavaDoc[0]);
22     } catch (NoSuchMethodException JavaDoc e) {
23       throw new TCRuntimeException(e);
24     }
25   }
26
27   private ReflectionUtil() {
28     // Disallow any object to be instantiated.
29
}
30
31   public static Constructor JavaDoc newConstructor(Class JavaDoc clazz, Class JavaDoc logicalSuperClass) {
32     Constructor JavaDoc useConstructor = refConstructor;
33     if (logicalSuperClass != null) {
34       try {
35         useConstructor = logicalSuperClass.getDeclaredConstructor(new Class JavaDoc[0]);
36       } catch (SecurityException JavaDoc e) {
37         throw new TCRuntimeException(e);
38       } catch (NoSuchMethodException JavaDoc e) {
39         throw new TCRuntimeException(e);
40       }
41     }
42     return rf.newConstructorForSerialization(clazz, useConstructor);
43   }
44
45 }
46
Popular Tags