src/main/java/com/cedarsoftware/util/io/MetaUtils.java
public static Object newInstance(Class c)
    {
        if (unmodifiableSortedMap.getClass().isAssignableFrom(c))
        {
            return new TreeMap();
        }
        if (unmodifiableMap.getClass().isAssignableFrom(c))
        {
            return new LinkedHashMap();
        }
        if (unmodifiableSortedSet.getClass().isAssignableFrom(c))
        {
            return new TreeSet();
        }
        if (unmodifiableSet.getClass().isAssignableFrom(c))
        {
            return new LinkedHashSet();
        }
        if (unmodifiableCollection.getClass().isAssignableFrom(c))
        {
            return new ArrayList();
        }

        if (c.isInterface())
        {
            throw new JsonIoException("Cannot instantiate unknown interface: " + c.getName());
        }

        // Constructor not cached, go find a constructor
        Object[] constructorInfo = constructors.get(c);
        if (constructorInfo != null)
        {   // Constructor was cached
            Constructor constructor = (Constructor) constructorInfo[0];

            if (constructor == null && useUnsafe)
            {   // null constructor --> set to null when object instantiated with unsafe.allocateInstance()
                try
                {
                    return unsafe.allocateInstance(c);
                }
                catch (Exception e)
                {
                    // Should never happen, as the code that fetched the constructor was able to instantiate it once already
                    throw new JsonIoException("Could not instantiate " + c.getName(), e);
                }
            }

            if (constructor == null)
            {
                throw new JsonIoException("No constructor found to instantiate " + c.getName());
            }

            Boolean useNull = (Boolean) constructorInfo[1];
            Class[] paramTypes = constructor.getParameterTypes();
            if (paramTypes == null || paramTypes.length == 0)
            {
                try
                {
                    return constructor.newInstance();
                }
                catch (Exception e)
                {   // Should never happen, as the code that fetched the constructor was able to instantiate it once already
                    throw new JsonIoException("Could not instantiate " + c.getName(), e);
                }
            }
            Object[] values = fillArgs(paramTypes, useNull);
            try
            {
                return constructor.newInstance(values);
            }
            catch (Exception e)
            {   // Should never happen, as the code that fetched the constructor was able to instantiate it once already
                throw new JsonIoException("Could not instantiate " + c.getName(), e);
            }
        }

        Object[] ret = newInstanceEx(c);
        constructors.put(c, new Object[]{ret[1], ret[2]});
        return ret[0];
    }
public static Object newInstance(Class c)
    {
        if (unmodifiableSortedMap.getClass().isAssignableFrom(c))
        {
            return new TreeMap();
        }
        if (unmodifiableMap.getClass().isAssignableFrom(c))
        {
            return new LinkedHashMap();
        }
        if (unmodifiableSortedSet.getClass().isAssignableFrom(c))
        {
            return new TreeSet();
        }
        if (unmodifiableSet.getClass().isAssignableFrom(c))
        {
            return new LinkedHashSet();
        }
        if (unmodifiableCollection.getClass().isAssignableFrom(c))
        {
            return new ArrayList();
        }

        if (c.isInterface())
        {
            throw new JsonIoException("Cannot instantiate unknown interface: " + c.getName());
        }

        // Constructor not cached, go find a constructor
        Object[] constructorInfo = constructors.get(c);
        if (constructorInfo != null)
        {   // Constructor was cached
            Constructor constructor = (Constructor) constructorInfo[0];

            if (constructor == null && useUnsafe)
            {   // null constructor --> set to null when object instantiated with unsafe.allocateInstance()
                try
                {
                    return unsafe.allocateInstance(c);
                }
                catch (Exception e)
                {
                    // Should never happen, as the code that fetched the constructor was able to instantiate it once already
                    throw new JsonIoException("Could not instantiate " + c.getName(), e);
                }
            }

            if (constructor == null)
            {
                throw new JsonIoException("No constructor found to instantiate " + c.getName());
            }

            Boolean useNull = (Boolean) constructorInfo[1];
            Class[] paramTypes = constructor.getParameterTypes();
            if (paramTypes == null || paramTypes.length == 0)
            {
                try
                {
                    return constructor.newInstance();
                }
                catch (Exception e)
                {   // Should never happen, as the code that fetched the constructor was able to instantiate it once already
                    throw new JsonIoException("Could not instantiate " + c.getName(), e);
                }
            }
            Object[] values = fillArgs(paramTypes, useNull);
            try
            {
                return constructor.newInstance(values);
            }
            catch (Exception e)
            {   // Should never happen, as the code that fetched the constructor was able to instantiate it once already
                throw new JsonIoException("Could not instantiate " + c.getName(), e);
            }
        }

        Object[] ret = newInstanceEx(c);
        constructors.put(c, new Object[]{ret[1], ret[2]});
        return ret[0];
    }
public static Object newInstance ( Class c ) { if ( unmodifiableSortedMap . getClass ( ) . isAssignableFrom ( c ) ) { return new TreeMap ( ) ; } if ( unmodifiableMap . getClass ( ) . isAssignableFrom ( c ) ) { return new LinkedHashMap ( ) ; } if ( unmodifiableSortedSet . getClass ( ) . isAssignableFrom ( c ) ) { return new TreeSet ( ) ; } if ( unmodifiableSet . getClass ( ) . isAssignableFrom ( c ) ) { return new LinkedHashSet ( ) ; } if ( unmodifiableCollection . getClass ( ) . isAssignableFrom ( c ) ) { return new ArrayList ( ) ; } if ( c . isInterface ( ) ) { throw new JsonIoException ( "Cannot instantiate unknown interface: " + c . getName ( ) ) ; } // Constructor not cached, go find a constructor Object [ ] constructorInfo = constructors . get ( c ) ; if ( constructorInfo != null ) { // Constructor was cached Constructor constructor = ( Constructor ) constructorInfo [ 0 ] ; if ( constructor == null && useUnsafe ) { // null constructor --> set to null when object instantiated with unsafe.allocateInstance() try { return unsafe . allocateInstance ( c ) ; } catch ( Exception e ) { // Should never happen, as the code that fetched the constructor was able to instantiate it once already throw new JsonIoException ( "Could not instantiate " + c . getName ( ) , e ) ; } } if ( constructor == null ) { throw new JsonIoException ( "No constructor found to instantiate " + c . getName ( ) ) ; } Boolean useNull = ( Boolean ) constructorInfo [ 1 ] ; Class [ ] paramTypes = constructor . getParameterTypes ( ) ; if ( paramTypes == null || paramTypes . length == 0 ) { try { return constructor . newInstance ( ) ; } catch ( Exception e ) { // Should never happen, as the code that fetched the constructor was able to instantiate it once already throw new JsonIoException ( "Could not instantiate " + c . getName ( ) , e ) ; } } Object [ ] values = fillArgs ( paramTypes , useNull ) ; try { return constructor . newInstance ( values ) ; } catch ( Exception e ) { // Should never happen, as the code that fetched the constructor was able to instantiate it once already throw new JsonIoException ( "Could not instantiate " + c . getName ( ) , e ) ; } } Object [ ] ret = newInstanceEx ( c ) ; constructors . put ( c , new Object [ ] { ret [ 1 ] , ret [ 2 ] } ) ; return ret [ 0 ] ; }
<p>C language malloc() for Java
</p><p>
Create a new instance of the passed in Class.  This method will make a valiant effort to instantiate
the passed in Class, including calling all of its constructors until successful.  The order they
are tried are public with the fewest arguments first to private with the most arguments.  If, after
exhausting all constructors, then it will attempt using the 'unsafe allocate' from Sun.  This step is
optional - by default it will use this if on a Sun (Oracle) JVM unless MetaUtil.setUseUnsafe(false) is called.
</p><p>
This method will handle common interfaces, such as Collection, Map, etc. which commonly show up in
parameterized types.  Any other interface passed to this method will cause a JsonIoException to be thrown.
</p><p>
To improve performance, when called a 2nd time for the same Class, the constructor that was successfully
used to construct the instance will be retrieved from an internal cache.
</p>
@param c Class to instantiate
@return an instance of the instantiated class.  This instance is intended to have its fields 'stuffed' by
direct assignment, not called via setter methods.
@throws JsonIoException if it cannot instantiate the passed in class.
<p > C language malloc () for Java < / p > <p > Create a new instance of the passed in Class . This method will make a valiant effort to instantiate the passed in Class including calling all of its constructors until successful . The order they are tried are public with the fewest arguments first to private with the most arguments . If after exhausting all constructors then it will attempt using the unsafe allocate from Sun . This step is optional - by default it will use this if on a Sun ( Oracle ) JVM unless MetaUtil . setUseUnsafe ( false ) is called . < / p > <p > This method will handle common interfaces such as Collection Map etc . which commonly show up in parameterized types . Any other interface passed to this method will cause a JsonIoException to be thrown . < / p > <p > To improve performance when called a 2nd time for the same Class the constructor that was successfully used to construct the instance will be retrieved from an internal cache . < / p >