From 3446f1ce26bdb18747982f6737d2db5987f50fe0 Mon Sep 17 00:00:00 2001 From: Christian Date: Sat, 20 Apr 2013 23:31:22 -0400 Subject: [PATCH] Fix scala supporting SidedProxy. It should work now. --- .../cpw/mods/fml/common/FMLModContainer.java | 25 +-------- .../cpw/mods/fml/common/ILanguageAdapter.java | 52 +++++++++++++++++++ .../cpw/mods/fml/common/ProxyInjector.java | 8 +-- .../modloader/ModLoaderModContainer.java | 3 +- 4 files changed, 60 insertions(+), 28 deletions(-) create mode 100644 fml/common/cpw/mods/fml/common/ILanguageAdapter.java diff --git a/fml/common/cpw/mods/fml/common/FMLModContainer.java b/fml/common/cpw/mods/fml/common/FMLModContainer.java index 8aac5129e0..7471db23e6 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 0000000000..6aae70b66a --- /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 d78ae56cbf..977bd27079 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 16d5386a80..568ffb2ae0 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) {