You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Bruno Borges <br...@gmail.com> on 2011/07/19 22:51:30 UTC

Problems with JSONWriter loading classes from Class.forName

I'm not sure about the issue, but I'm now working on a project that has a
fork of the json plugin of Struts 2.1.8.1.

The only difference is this one:

Index: src/main/java/org/apache/struts2/json/JSONWriter.java
===================================================================
--- src/main/java/org/apache/struts2/json/JSONWriter.java (revision 1148509)
+++ src/main/java/org/apache/struts2/json/JSONWriter.java (working copy)
@@ -192,9 +192,14 @@
                 Method baseAccessor = null;
                 if (clazz.getName().indexOf("$$EnhancerByCGLIB$$") > -1) {
                     try {
-                        baseAccessor = Class.forName(
-                                clazz.getName().substring(0,
clazz.getName().indexOf("$$"))).getMethod(
-                                accessor.getName(),
accessor.getParameterTypes());
+ baseAccessor = Thread
+ .currentThread()
+ .getContextClassLoader()
+ .loadClass(
+ clazz.getName().substring(0,
+ clazz.getName().indexOf("$$")))
+ .getMethod(accessor.getName(),
+ accessor.getParameterTypes());
                     } catch (Exception ex) {
                         LOG.debug(ex.getMessage(), ex);
                     }


I think the problem is that projects that use this and are installed on IBM
WebSphere, are having problems. The JAR is deployed as a SharedLib and so,
goes to the parent classloader of the Application. Using Thread's
contextClassLoader seems to fix the problem because that's the one that have
the project's class that will be exported to JSON.

I've tracked this class until trunk and haven't seen any difference about
how it loads CGLib classes.

Shouldn't JSON use the Thread's contextClassLoader, which is a bottom
classloader?

Thanks,
*Bruno Borges*
www.brunoborges.com.br
+55 21 76727099