All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.objenesis.instantiator.jrockit.JRockitLegacyInstantiator Maven / Gradle / Ivy

There is a newer version: 2.0.2-beta
Show newest version
/**
 * COPYRIGHT & LICENSE
 *
 * This code is Copyright (c) 2006 BEA Systems, inc. It is provided free, as-is and without any warranties for the purpose of
 * inclusion in Objenesis or any other open source project with a FSF approved license, as long as this notice is not
 * removed. There are no limitations on modifying or repackaging the code apart from this. 
 *
 * BEA does not guarantee that the code works, and provides no support for it. Use at your own risk.
 *
 * Originally developed by Leonardo Mesquita. Copyright notice added by Henrik St?hl, BEA JRockit Product Manager.
 *  
 */

package org.objenesis.instantiator.jrockit;

import java.lang.reflect.Method;

import org.objenesis.ObjenesisException;
import org.objenesis.instantiator.ObjectInstantiator;

/**
 * Instantiates a class by making a call to internal JRockit private methods. It is only supposed to
 * work on JRockit 1.4.2 JVMs prior to release R25.1. From release R25.1 on, JRockit supports
 * sun.reflect.ReflectionFactory, making this "trick" unnecessary. This instantiator will not call
 * any constructors.
 * 
 * @author Leonardo Mesquita
 * @see org.objenesis.instantiator.ObjectInstantiator
 * @see org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator
 */
public class JRockitLegacyInstantiator implements ObjectInstantiator {
   private static Method safeAllocObjectMethod = null;

   private static void initialize() {
      if(safeAllocObjectMethod == null) {
         Class memSystem;
         try {
            memSystem = Class.forName("jrockit.vm.MemSystem");
            safeAllocObjectMethod = memSystem.getDeclaredMethod("safeAllocObject",
               new Class[] {Class.class});
            safeAllocObjectMethod.setAccessible(true);
         }
         catch(Exception e) {
            throw new ObjenesisException(e);
         }
      }
   }

   private Class type;

   public JRockitLegacyInstantiator(Class type) {
      initialize();
      this.type = type;
   }

   public Object newInstance() {      
      try {
         return safeAllocObjectMethod.invoke(null, new Object[] {type});
      }
      catch(Exception e) {
         throw new ObjenesisException(e);
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy