diff --git a/fml/common/cpw/mods/fml/common/FMLModContainer.java b/fml/common/cpw/mods/fml/common/FMLModContainer.java index 8aac5129e..7471db23e 100644 --- a/fml/common/cpw/mods/fml/common/FMLModContainer.java +++ b/fml/common/cpw/mods/fml/common/FMLModContainer.java @@ -108,34 +108,13 @@ public class FMLModContainer implements ModContainer private String modLanguage; private ILanguageAdapter languageAdapter; - public static interface ILanguageAdapter { - public Object getNewInstance(FMLModContainer container, Class objectClass, ClassLoader classLoader) throws Exception; - } - - public static class ScalaAdapter implements ILanguageAdapter { - @Override - public Object getNewInstance(FMLModContainer container, Class scalaObjectClass, ClassLoader classLoader) throws Exception - { - System.out.println("Scala class : "+ scalaObjectClass); - Class sObjectClass = Class.forName(scalaObjectClass.getName()+"$",true,classLoader); - return sObjectClass.getField("MODULE$").get(null); - } - } - - public static class JavaAdapter implements ILanguageAdapter { - @Override - public Object getNewInstance(FMLModContainer container, Class objectClass, ClassLoader classLoader) throws Exception - { - return objectClass.newInstance(); - } - } public FMLModContainer(String className, File modSource, Map modDescriptor) { this.className = className; this.source = modSource; this.descriptor = modDescriptor; this.modLanguage = (String) modDescriptor.get("modLanguage"); - this.languageAdapter = "scala".equals(modLanguage) ? new ScalaAdapter() : new JavaAdapter(); + this.languageAdapter = "scala".equals(modLanguage) ? new ILanguageAdapter.ScalaAdapter() : new ILanguageAdapter.JavaAdapter(); } private ILanguageAdapter getLanguageAdapter() @@ -489,7 +468,7 @@ public class FMLModContainer implements ModContainer { eventBus.post(new FMLFingerprintViolationEvent(source.isDirectory(), source, ImmutableSet.copyOf(this.sourceFingerprints), expectedFingerprint)); } - ProxyInjector.inject(this, event.getASMHarvestedData(), FMLCommonHandler.instance().getSide()); + ProxyInjector.inject(this, event.getASMHarvestedData(), FMLCommonHandler.instance().getSide(), getLanguageAdapter()); processFieldAnnotations(event.getASMHarvestedData()); } catch (Throwable e) diff --git a/fml/common/cpw/mods/fml/common/ILanguageAdapter.java b/fml/common/cpw/mods/fml/common/ILanguageAdapter.java new file mode 100644 index 000000000..6aae70b66 --- /dev/null +++ b/fml/common/cpw/mods/fml/common/ILanguageAdapter.java @@ -0,0 +1,52 @@ +package cpw.mods.fml.common; + +import java.lang.reflect.Field; + +public interface ILanguageAdapter { + public Object getNewInstance(FMLModContainer container, Class objectClass, ClassLoader classLoader) throws Exception; + public boolean supportsStatics(); + public void setProxy(Field target, Class proxyTarget, Object proxy) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException; + + public static class ScalaAdapter implements ILanguageAdapter { + @Override + public Object getNewInstance(FMLModContainer container, Class scalaObjectClass, ClassLoader classLoader) throws Exception + { + Class sObjectClass = Class.forName(scalaObjectClass.getName()+"$",true,classLoader); + return sObjectClass.getField("MODULE$").get(null); + } + + @Override + public boolean supportsStatics() + { + return false; + } + + @Override + public void setProxy(Field target, Class proxyTarget, Object proxy) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException + { + Field field = proxyTarget.getField("INSTANCE"); + Object scalaObject = field.get(null); + target.set(scalaObject, proxy); + } + } + public static class JavaAdapter implements ILanguageAdapter { + @Override + public Object getNewInstance(FMLModContainer container, Class objectClass, ClassLoader classLoader) throws Exception + { + return objectClass.newInstance(); + } + + @Override + public boolean supportsStatics() + { + return true; + } + + @Override + public void setProxy(Field target, Class proxyTarget, Object proxy) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, + SecurityException + { + target.set(null, proxy); + } + } +} \ No newline at end of file diff --git a/fml/common/cpw/mods/fml/common/ProxyInjector.java b/fml/common/cpw/mods/fml/common/ProxyInjector.java index d78ae56cb..977bd2707 100644 --- a/fml/common/cpw/mods/fml/common/ProxyInjector.java +++ b/fml/common/cpw/mods/fml/common/ProxyInjector.java @@ -5,7 +5,7 @@ * are made available under the terms of the GNU Lesser Public License v2.1 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html - * + * * Contributors: * cpw - implementation */ @@ -27,7 +27,7 @@ import cpw.mods.fml.relauncher.Side; */ public class ProxyInjector { - public static void inject(ModContainer mod, ASMDataTable data, Side side) + public static void inject(ModContainer mod, ASMDataTable data, Side side, ILanguageAdapter languageAdapter) { FMLLog.fine("Attempting to inject @SidedProxy classes into %s", mod.getModId()); Set targets = data.getAnnotationsFor(mod).get(SidedProxy.class.getName()); @@ -49,7 +49,7 @@ public class ProxyInjector String targetType = side.isClient() ? target.getAnnotation(SidedProxy.class).clientSide() : target.getAnnotation(SidedProxy.class).serverSide(); Object proxy=Class.forName(targetType, true, mcl).newInstance(); - if ((target.getModifiers() & Modifier.STATIC) == 0 ) + if (languageAdapter.supportsStatics() && (target.getModifiers() & Modifier.STATIC) == 0 ) { FMLLog.severe("Attempted to load a proxy type %s into %s.%s, but the field is not static", targetType, targ.getClassName(), targ.getObjectName()); throw new LoaderException(); @@ -59,7 +59,7 @@ public class ProxyInjector FMLLog.severe("Attempted to load a proxy type %s into %s.%s, but the types don't match", targetType, targ.getClassName(), targ.getObjectName()); throw new LoaderException(); } - target.set(null, proxy); + languageAdapter.setProxy(target, proxyTarget, proxy); } catch (Exception e) { diff --git a/fml/common/cpw/mods/fml/common/modloader/ModLoaderModContainer.java b/fml/common/cpw/mods/fml/common/modloader/ModLoaderModContainer.java index 16d5386a8..568ffb2ae 100644 --- a/fml/common/cpw/mods/fml/common/modloader/ModLoaderModContainer.java +++ b/fml/common/cpw/mods/fml/common/modloader/ModLoaderModContainer.java @@ -40,6 +40,7 @@ import com.google.common.eventbus.Subscribe; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.FMLLog; +import cpw.mods.fml.common.ILanguageAdapter; import cpw.mods.fml.common.LoadController; import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.LoaderException; @@ -496,7 +497,7 @@ public class ModLoaderModContainer implements ModContainer { dummyHandler.setBaseMod(mod); } - ProxyInjector.inject(this, event.getASMHarvestedData(), FMLCommonHandler.instance().getSide()); + ProxyInjector.inject(this, event.getASMHarvestedData(), FMLCommonHandler.instance().getSide(), new ILanguageAdapter.JavaAdapter()); } catch (Exception e) {