Thread.getContextClassLoader

1,238 views
Skip to first unread message

Rmac

unread,
Nov 27, 2013, 9:08:09 AM11/27/13
to jso...@googlegroups.com
Hi.  I've installed the latest 2.3.0 version of json-io in my Android app and I'm now seeing messages at times in the console related to the ClassLoader (see below).  Is this a problem?  Everything appears to be working fine, but the error (warning) concerns me.


11-27 07:43:40.548: W/ActivityThread(30238): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());

John DeRegnaucourt

unread,
Nov 27, 2013, 9:35:12 AM11/27/13
to jso...@googlegroups.com
I will look into this and get back to you.  What version of json-io were you running before 2.3.0?

Regards,
John


On Wed, Nov 27, 2013 at 9:08 AM, Rmac <ron...@gmail.com> wrote:
Hi.  I've installed the latest 2.3.0 version of json-io in my Android app and I'm now seeing messages at times in the console related to the ClassLoader (see below).  Is this a problem?  Everything appears to be working fine, but the error (warning) concerns me.


11-27 07:43:40.548: W/ActivityThread(30238): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());

--
You received this message because you are subscribed to the Google Groups "json-io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to json-io+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Ron

unread,
Nov 27, 2013, 10:14:49 AM11/27/13
to jso...@googlegroups.com

2.2.30

John DeRegnaucourt

unread,
Dec 10, 2013, 12:29:03 AM12/10/13
to jso...@googlegroups.com
Ron,

It looks like Android must have made an update to their code that is warning about the code that is calling Thread.getContextClassLoader().  This is benign on the Anrdoid device.  

If you want, you can change the follow method in the JsonReader:

    // loadClass() provided by: Thomas Margreiter
    private static Class loadClass(String name) throws ClassNotFoundException
    {
        String className = name;
        boolean arrayType = false;
        Class primitiveArray = null;

        while (className.startsWith("["))
        {
            arrayType = true;
            if (className.endsWith(";")) className = className.substring(0,className.length()-1);
            if (className.equals("[B")) primitiveArray = byte[].class;
            else if (className.equals("[S")) primitiveArray = short[].class;
            else if (className.equals("[I")) primitiveArray = int[].class;
            else if (className.equals("[J")) primitiveArray = long[].class;
            else if (className.equals("[F")) primitiveArray = float[].class;
            else if (className.equals("[D")) primitiveArray = double[].class;
            else if (className.equals("[Z")) primitiveArray = boolean[].class;
            else if (className.equals("[C")) primitiveArray = char[].class;
            int startpos = className.startsWith("[L") ? 2 : 1;
            className = className.substring(startpos);
        }
        Class currentClass = null;
        if (null == primitiveArray)
        {
            currentClass = Thread.currentThread().getContextClassLoader().loadClass(className);
        }

        if (arrayType)
        {
            currentClass = (null != primitiveArray) ? primitiveArray : Array.newInstance(currentClass, 0).getClass();
            while (name.startsWith("[["))
            {
                currentClass = Array.newInstance(currentClass, 0).getClass();
                name = name.substring(1);
            }
        }
        return currentClass;
    }

to:

    // loadClass() provided by: Thomas Margreiter
    private static Class loadClass(String name) throws ClassNotFoundException
    {
        return Class.forName(name);
    }

If you try this and it works fine for you (eliminates the warning), please let me know.  This is what I had originally (you might need to try/catch an exception around Class.forName()).  But Thomas Margreiter sent me the method above to get Class.forName() to work on an Android device.  This was a long time ago, and perhaps this more complicated version of Class.forName() is no longer required.

Best regards,
John

Reply all
Reply to author
Forward
0 new messages