View Javadoc

1   // TestUtils.java
2   // http://blog.dhananjaynene.com/archives/8
3   
4   package org.sf.jlaunchpad.util;
5   
6   import java.lang.reflect.Method;
7   import java.net.URL;
8   import java.net.URLClassLoader;
9   
10  public class TestUtils
11  
12  {
13  
14    public static void addURL(Class<?> clazz)  {
15  
16      try  {
17  
18        URLClassLoader sysloader = (URLClassLoader) ClassLoader
19  
20            .getSystemClassLoader();
21  
22        Class<URLClassLoader> sysclass = URLClassLoader.class;
23  
24  
25        String path = sysloader.getResource(
26  
27            clazz.getCanonicalName().replace('.', '/') + ".class")
28  
29            .getPath();
30  
31        int lastSlash = path.lastIndexOf('/');
32  
33        path = path.substring(lastSlash + 1);
34  
35        URL url = new URL("file://" + path);
36  
37  
38        Method method = sysclass.getDeclaredMethod("addURL", URL.class);
39  
40        method.setAccessible(true);
41  
42        method.invoke(sysloader, new Object[] {url});
43  
44      }
45  
46      catch (Throwable t)
47  
48      {
49  
50        t.printStackTrace();
51  
52      }
53  
54    }
55  
56  }