4040import java .lang .reflect .Field ;
4141import java .lang .reflect .InvocationTargetException ;
4242import java .util .ArrayList ;
43+ import java .util .Collection ;
4344import java .util .HashMap ;
4445import java .util .List ;
4546import java .util .logging .Level ;
@@ -198,9 +199,13 @@ public static Savable fromName(String className)
198199 * @throws ClassNotFoundException thrown if the class name is not in the classpath.
199200 * @throws SecurityException thrown if the filter rejects the class name.
200201 */
201- public static Savable fromName (String className , SavableClassFilter classFilter )
202- throws ClassNotFoundException , IllegalAccessException ,
203- InstantiationException , InvocationTargetException {
202+ public static Savable fromName (String className , SavableClassFilter classFilter )
203+ throws ClassNotFoundException , IllegalAccessException , InstantiationException , InvocationTargetException {
204+ return fromName (className , classFilter , null );
205+ }
206+
207+ public static Savable fromName (String className , SavableClassFilter classFilter , Collection <ClassLoader > additionalClassLoaders )
208+ throws ClassNotFoundException , IllegalAccessException , InstantiationException , InvocationTargetException {
204209 className = remapClass (className );
205210 if (classFilter == null ) {
206211 throw new NullPointerException ("classFilter" );
@@ -209,7 +214,7 @@ public static Savable fromName(String className, SavableClassFilter classFilter)
209214 throw new SecurityException ("Savable class rejected by filter: " + className );
210215 }
211216
212- Constructor noArgConstructor = findNoArgConstructor (className );
217+ Constructor noArgConstructor = findNoArgConstructor (className , additionalClassLoaders );
213218 noArgConstructor .setAccessible (true );
214219 try {
215220 return (Savable ) noArgConstructor .newInstance ();
@@ -226,6 +231,7 @@ public static Savable fromName(String className, SavableClassFilter classFilter)
226231 }
227232 }
228233
234+
229235 /**
230236 * @deprecated use {@link #fromName(java.lang.String)} instead
231237 */
@@ -263,12 +269,32 @@ public static Savable fromName(String className, List<ClassLoader> loaders) thro
263269 * @return the pre-existing constructor (not null)
264270 */
265271 @ SuppressWarnings ("unchecked" )
266- private static Constructor findNoArgConstructor (String className )
272+ private static Constructor findNoArgConstructor (String className , Collection < ClassLoader > additionalClassLoaders )
267273 throws ClassNotFoundException , InstantiationException {
268- Class clazz = Class .forName (className );
274+ Class clazz = null ;
275+
276+ try {
277+ clazz = Class .forName (className );
278+ } catch (ClassNotFoundException e ) {
279+ if (additionalClassLoaders != null ) {
280+ for (ClassLoader classLoader : additionalClassLoaders ) {
281+ try {
282+ clazz = Class .forName (className , true , classLoader );
283+ break ;
284+ } catch (ClassNotFoundException ex ) {
285+ // ignore
286+ }
287+ }
288+ }
289+ if (clazz == null ) {
290+ throw e ;
291+ }
292+ }
293+
269294 if (!SavableClassUtil .isImplementingSavable (clazz )) {
270295 throw new InstantiationException ("Class " + className + " does not implement Savable." );
271296 }
297+
272298 Constructor result ;
273299 try {
274300 result = clazz .getDeclaredConstructor ();
0 commit comments