PercInstantiator.java revision 78c496fe0fac4c89993109340aec80d1afa3141f
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.perc; 17 18import java.io.ObjectInputStream; 19import java.lang.reflect.Method; 20 21import org.objenesis.ObjenesisException; 22import org.objenesis.instantiator.ObjectInstantiator; 23 24/** 25 * Instantiates a class by making a call to internal Perc private methods. It is only supposed to 26 * work on Perc JVMs. This instantiator will not call any constructors. The code was provided by 27 * Aonix Perc support team. 28 * 29 * @author Henri Tremblay 30 * @see org.objenesis.instantiator.ObjectInstantiator 31 */ 32public class PercInstantiator implements ObjectInstantiator { 33 34 private final Method newInstanceMethod; 35 36 private final Object[] typeArgs = new Object[] { null, Boolean.FALSE }; 37 38 public PercInstantiator(Class type) { 39 40 typeArgs[0] = type; 41 42 try { 43 newInstanceMethod = ObjectInputStream.class.getDeclaredMethod("newInstance", 44 new Class[] { Class.class, Boolean.TYPE }); 45 newInstanceMethod.setAccessible(true); 46 } 47 catch(RuntimeException e) { 48 throw new ObjenesisException(e); 49 } 50 catch(NoSuchMethodException e) { 51 throw new ObjenesisException(e); 52 } 53 } 54 55 public Object newInstance() { 56 try { 57 return newInstanceMethod.invoke(null, typeArgs); 58 } catch (Exception e) { 59 throw new ObjenesisException(e); 60 } 61 } 62 63} 64