1/**
2 * Copyright 2006-2017 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.gcj;
17
18import org.objenesis.ObjenesisException;
19import org.objenesis.instantiator.SerializationInstantiatorHelper;
20import org.objenesis.instantiator.annotations.Instantiator;
21import org.objenesis.instantiator.annotations.Typology;
22
23/**
24 * Instantiates a class by making a call to internal GCJ private methods. It is only supposed to
25 * work on GCJ JVMs. This instantiator will create classes in a way compatible with serialization,
26 * calling the first non-serializable superclass' no-arg constructor.
27 *
28 * @author Leonardo Mesquita
29 * @see org.objenesis.instantiator.ObjectInstantiator
30 */
31@Instantiator(Typology.SERIALIZATION)
32public class GCJSerializationInstantiator<T> extends GCJInstantiatorBase<T> {
33   private Class<? super T> superType;
34
35   public GCJSerializationInstantiator(Class<T> type) {
36      super(type);
37      this.superType = SerializationInstantiatorHelper.getNonSerializableSuperClass(type);
38   }
39
40   @Override
41   public T newInstance() {
42      try {
43         return type.cast(newObjectMethod.invoke(dummyStream, type, superType));
44      }
45      catch(Exception e) {
46         throw new ObjenesisException(e);
47      }
48   }
49
50}
51