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 */
16package org.objenesis.instantiator.sun;
17
18import java.io.ObjectInputStream;
19import java.lang.reflect.Method;
20
21import org.objenesis.ObjenesisException;
22import org.objenesis.instantiator.ObjectInstantiator;
23
24/**
25 * Base class for Sun 1.3 based instantiators. It initializes reflection access to static method
26 * ObjectInputStream.allocateNewObject.
27 *
28 * @author Leonardo Mesquita
29 */
30public abstract class Sun13InstantiatorBase implements ObjectInstantiator {
31   static Method allocateNewObjectMethod = null;
32
33   private static void initialize() {
34      if(allocateNewObjectMethod == null) {
35         try {
36            allocateNewObjectMethod = ObjectInputStream.class.getDeclaredMethod(
37               "allocateNewObject", new Class[] {Class.class, Class.class});
38            allocateNewObjectMethod.setAccessible(true);
39         }
40         catch(RuntimeException e) {
41            throw new ObjenesisException(e);
42         }
43         catch(NoSuchMethodException e) {
44            throw new ObjenesisException(e);
45         }
46      }
47   }
48
49   protected final Class type;
50
51   public Sun13InstantiatorBase(Class type) {
52      this.type = type;
53      initialize();
54   }
55
56   public abstract Object newInstance();
57
58}
59