1/**
2 * Copyright 2006-2013 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.objenesis.instantiator.sun;
18import java.lang.reflect.Constructor;
19import java.lang.reflect.InvocationTargetException;
20import java.lang.reflect.Method;
21
22import org.objenesis.ObjenesisException;
23import org.objenesis.instantiator.ObjectInstantiator;
24
25
26/**
27 * Helper methods providing access to {@link sun.reflect.ReflectionFactory} via reflection, for use
28 * by the {@link ObjectInstantiator}s that use it.
29 *
30 * @author Henri Tremblay
31 */
32class SunReflectionFactoryHelper {
33
34   public static Constructor newConstructorForSerialization(Class type, Constructor constructor) {
35      Class reflectionFactoryClass = getReflectionFactoryClass();
36      Object reflectionFactory = createReflectionFactory(reflectionFactoryClass);
37
38      Method newConstructorForSerializationMethod = getNewConstructorForSerializationMethod(
39         reflectionFactoryClass);
40
41      try {
42         return (Constructor) newConstructorForSerializationMethod.invoke(
43            reflectionFactory, new Object[] {type, constructor});
44      }
45      catch(IllegalArgumentException e) {
46         throw new ObjenesisException(e);
47      }
48      catch(IllegalAccessException e) {
49         throw new ObjenesisException(e);
50      }
51      catch(InvocationTargetException e) {
52         throw new ObjenesisException(e);
53      }
54   }
55
56   private static Class getReflectionFactoryClass() {
57      try {
58         return Class.forName("sun.reflect.ReflectionFactory");
59      }
60      catch(ClassNotFoundException e) {
61         throw new ObjenesisException(e);
62      }
63   }
64
65   private static Object createReflectionFactory(Class reflectionFactoryClass) {
66      try {
67         Method method = reflectionFactoryClass.getDeclaredMethod(
68            "getReflectionFactory", new Class[] {});
69         return method.invoke(null, new Object[] {});
70      }
71      catch(NoSuchMethodException e) {
72         throw new ObjenesisException(e);
73      }
74      catch(IllegalAccessException e) {
75         throw new ObjenesisException(e);
76      }
77      catch(IllegalArgumentException e) {
78         throw new ObjenesisException(e);
79      }
80      catch(InvocationTargetException e) {
81         throw new ObjenesisException(e);
82      }
83   }
84
85   private static Method getNewConstructorForSerializationMethod(Class reflectionFactoryClass) {
86      try {
87         return reflectionFactoryClass.getDeclaredMethod(
88            "newConstructorForSerialization", new Class[] {Class.class, Constructor.class});
89      }
90      catch(NoSuchMethodException e) {
91         throw new ObjenesisException(e);
92      }
93   }
94}
95