package gnu.classpath;

import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.IdentityHashMap;

final class VMClassLoaderCache
{
    private static Field cacheField;
	
    static
    {
        AccessController.doPrivileged(new PrivilegedAction()
        {
            public Object run()
            {
                try
                {
                    cacheField = ClassLoader.class.getDeclaredField("cache");
                }
                catch (NoSuchFieldException x)
                {
                    throw (InternalError) new InternalError().initCause(x);
                }
                cacheField.setAccessible(true);
                return null;
            }
        });
    }

    static IdentityHashMap getMap(ClassLoader cl)
    {
        synchronized (cl)
        {
            try
            {
                IdentityHashMap map = (IdentityHashMap)cacheField.get(cl);
                if (map == null)
                {
                    map = new IdentityHashMap();
                    cacheField.set(cl, map);
                }
                return map;
            }
            catch (IllegalAccessException x)
            {
                throw (InternalError) new InternalError().initCause(x);
            }
        }
    }
}
