@@ -23,6 +23,15 @@ public MyClassLoader(ClassLoader parent, boolean tryToUseParent)
2323 _cfgUseParentLoader = tryToUseParent ;
2424 }
2525
26+ @ Override
27+ public Class <?> loadClass (String name ) throws ClassNotFoundException {
28+ try {
29+ return super .loadClass (name );
30+ } catch (ClassNotFoundException e ) {
31+ return getClass ().getClassLoader ().loadClass (name );
32+ }
33+ }
34+
2635 /**
2736 * Helper method called to check whether it is acceptable to create a new
2837 * class in package that given class is part of.
@@ -63,47 +72,44 @@ public Class<?> loadAndResolve(ClassName className, byte[] byteCode)
6372 if (old != null ) {
6473 return old ;
6574 }
66-
67- Class <?> impl ;
6875
6976 // Important: bytecode is generated with a template name (since bytecode itself
7077 // is used for checksum calculation) -- must be replaced now, however
7178 replaceName (byteCode , className .getSlashedTemplate (), className .getSlashedName ());
7279
7380 // First: let's try calling it directly on parent, to be able to access protected/package-access stuff:
74- if (_cfgUseParentLoader ) {
75- ClassLoader cl = getParent ();
76- // if we have parent, that is
77- if (cl != null ) {
78- try {
79- Method method = ClassLoader .class .getDeclaredMethod ("defineClass" ,
80- new Class [] {String .class , byte [].class , int .class ,
81- int .class });
82- method .setAccessible (true );
83- return (Class <?>)method .invoke (getParent (),
84- className .getDottedName (), byteCode , 0 , byteCode .length );
85- } catch (Exception e ) {
86- // Should we handle this somehow?
87- }
81+ if (_cfgUseParentLoader && getParent () != null ) {
82+ try {
83+ Method method = ClassLoader .class .getDeclaredMethod ("defineClass" ,
84+ new Class [] {String .class , byte [].class , int .class ,
85+ int .class });
86+ method .setAccessible (true );
87+ return (Class <?>)method .invoke (getParent (),
88+ className .getDottedName (), byteCode , 0 , byteCode .length );
89+ } catch (Exception e ) {
90+ // Should we handle this somehow?
8891 }
8992 }
9093
9194 // but if that doesn't fly, try to do it from our own class loader
92-
95+ return resolveFromThisClassLoader (className , byteCode );
96+ }
97+
98+ private Class <?> resolveFromThisClassLoader (ClassName className , byte [] byteCode ) {
9399 try {
94- impl = defineClass (className .getDottedName (), byteCode , 0 , byteCode .length );
100+ Class <?> impl = defineClass (className .getDottedName (), byteCode , 0 , byteCode .length );
101+ // important: must also resolve the class...
102+ resolveClass (impl );
103+ return impl ;
95104 } catch (LinkageError e ) {
96105 Throwable t = e ;
97106 while (t .getCause () != null ) {
98107 t = t .getCause ();
99108 }
100109 throw new IllegalArgumentException ("Failed to load class '" +className +"': " +t .getMessage (), t );
101110 }
102- // important: must also resolve the class...
103- resolveClass (impl );
104- return impl ;
105111 }
106-
112+
107113 public static int replaceName (byte [] byteCode ,
108114 String from , String to )
109115 {
0 commit comments