    001package cpw.mods.fml.common;
    002
    003import java.lang.reflect.Field;
    004
    005public interface ILanguageAdapter {
    006    public Object getNewInstance(FMLModContainer container, Class<?> objectClass, ClassLoader classLoader) throws Exception;
    007    public boolean supportsStatics();
    008    public void setProxy(Field target, Class<?> proxyTarget, Object proxy) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException;
    009
    010    public static class ScalaAdapter implements ILanguageAdapter {
    011        @Override
    012        public Object getNewInstance(FMLModContainer container, Class<?> scalaObjectClass, ClassLoader classLoader) throws Exception
    013        {
    014            Class<?> sObjectClass = Class.forName(scalaObjectClass.getName()+"$",true,classLoader);
    015            return sObjectClass.getField("MODULE$").get(null);
    016        }
    017
    018        @Override
    019        public boolean supportsStatics()
    020        {
    021            return false;
    022        }
    023
    024        @Override
    025        public void setProxy(Field target, Class<?> proxyTarget, Object proxy) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException
    026        {
    027            Field field = proxyTarget.getField("INSTANCE");
    028            Object scalaObject = field.get(null);
    029            target.set(scalaObject, proxy);
    030        }
    031    }
    032    public static class JavaAdapter implements ILanguageAdapter {
    033        @Override
    034        public Object getNewInstance(FMLModContainer container, Class<?> objectClass, ClassLoader classLoader) throws Exception
    035        {
    036            return objectClass.newInstance();
    037        }
    038
    039        @Override
    040        public boolean supportsStatics()
    041        {
    042            return true;
    043        }
    044
    045        @Override
    046        public void setProxy(Field target, Class<?> proxyTarget, Object proxy) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException,
    047                SecurityException
    048        {
    049            target.set(null, proxy);
    050        }
    051    }
    052}