Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.libg.classloaders; |
| 2 | |
| 3 | import java.lang.reflect.*; |
| 4 | import java.net.*; |
| 5 | |
| 6 | public class URLClassLoaderWrapper { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 7 | final URLClassLoader loader; |
| 8 | final Method addURL; |
| 9 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 10 | public URLClassLoaderWrapper(ClassLoader loader) throws Exception { |
| 11 | this.loader = (URLClassLoader) loader; |
| 12 | addURL = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); |
| 13 | addURL.setAccessible(true); |
| 14 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 15 | |
| 16 | public void addURL(URL url) throws Exception { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 17 | try { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 18 | addURL.invoke(loader, url); |
| 19 | } |
| 20 | catch (InvocationTargetException ite) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 21 | throw (Exception) ite.getTargetException(); |
| 22 | } |
| 23 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 24 | |
| 25 | public Class< ? > loadClass(String name) throws Exception { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 26 | return loader.loadClass(name); |
| 27 | } |
| 28 | } |