Self instantiation in static initializer

Steve Hannah <[email protected]> Mon, 25 Jan 2016 13:47:49 -0800
Newsgroups gmane.comp.java.ikvm.devel
Message-ID <CABiRg+SOf_WdRfhmV18thRjfBuxzF7-75qoUzMnv3+7oyHqUXA@mail.gmail.com>
Tried sending this before, but it was blocked because I embedded a
screenshot.

I have run into a problem trying to load classes in IKVM that include the
following pattern:

class Foo {
    static Foo instance = new Foo();

    ...

}


I'm getting errors as soon as I try to use the class at all.

System.MethodAccessException:
Attempt by method 'IKVM.Internal.ReflectUtil.isReflectionOnly(Type)' to
access method 'System.Reflection.Assembly.get_IsReflectionOnly(Type)'
failed.

The error occurs on this line
https://github.com/gluck/ikvm/blob/master/runtime/ReflectUtil.cs#L85

A first naive attempt at a fix was to just wrap that line in a try/catch.
But then I get a similar error on this line:
https://github.com/gluck/ikvm/blob/master/runtime/AssemblyClassLoader.cs#L74

Attempt by method
'IKVM.Internal.AssemblyClassLoader+AssemblyLoader..ctor(System.Reflection.Assembly)'
to access method 'System.Reflection.Assembly.GetModules(Boolean)' failed.
   at IKVM.Internal.AssemblyClassLoader.AssemblyLoader..ctor(Assembly
assembly)
   at IKVM.Internal.AssemblyClassLoader.Create(Assembly assembly)
   at IKVM.Internal.AssemblyClassLoader.FromAssembly(Assembly assembly)
   at IKVM.Internal.ClassLoaderWrapper.GetWrapperFromType(Type type)
   at IKVM.NativeCode.ikvm.runtime.Util.GetTypeWrapperFromObject(Object o)
   at java.lang.Object.toStringImpl(Object )
   at java.lang.Object.toString()
   at System.String.Concat(Object arg0, Object arg1)
   at App1.App.OnLaunched(LaunchActivatedEventArgs e)




If I change the pattern to something like:

class Foo {
    static Foo instance = null;

    static Foo getInstance() {
        if (instance == null) instance = new Foo();
        return instance;
    }

}

It works fine.


I am working with a slightly modified source base for IKVM to try to build
UWP apps.  I'm following some of the suggestions from previous threads on
the issue (e.g. -noautoserialization, -t:net-2.0, etc.. for building)

Is this a known issue?  Any suggestions on a way to work around this?

Any help appreciated.

Best regards

Steve

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140

_______________________________________________
Ikvm-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ikvm-developers