01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.util;
05:
06: import sun.reflect.ReflectionFactory;
07:
08: import com.tc.exception.TCRuntimeException;
09:
10: import java.lang.reflect.Constructor;
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 refConstructor;
17: private static final ReflectionFactory rf = ReflectionFactory
18: .getReflectionFactory();
19:
20: static {
21: try {
22: refConstructor = Object.class
23: .getDeclaredConstructor(new Class[0]);
24: } catch (NoSuchMethodException e) {
25: throw new TCRuntimeException(e);
26: }
27: }
28:
29: private ReflectionUtil() {
30: // Disallow any object to be instantiated.
31: }
32:
33: public static Constructor newConstructor(Class clazz,
34: Class logicalSuperClass) {
35: Constructor useConstructor = refConstructor;
36: if (logicalSuperClass != null) {
37: try {
38: useConstructor = logicalSuperClass
39: .getDeclaredConstructor(new Class[0]);
40: } catch (SecurityException e) {
41: throw new TCRuntimeException(e);
42: } catch (NoSuchMethodException e) {
43: throw new TCRuntimeException(e);
44: }
45: }
46: return rf.newConstructorForSerialization(clazz, useConstructor);
47: }
48:
49: }
|