|
1 | 1 | package the.bytecode.club.bytecodeviewer.api;
|
2 | 2 |
|
3 | 3 | import java.io.File;
|
| 4 | +import java.io.IOException; |
4 | 5 | import java.net.URL;
|
5 | 6 | import java.net.URLClassLoader;
|
6 | 7 | import java.util.ArrayList;
|
@@ -62,50 +63,60 @@ public static URLClassLoader getClassLoaderInstance() {
|
62 | 63 | return cl;
|
63 | 64 | }
|
64 | 65 |
|
| 66 | + |
| 67 | + |
65 | 68 | /**
|
66 | 69 | * Re-instances the URLClassLoader and loads a jar to it.
|
67 |
| - * @param path |
| 70 | + * |
| 71 | + * @param nodeList |
| 72 | + * The list of ClassNodes to be loaded |
68 | 73 | * @return The loaded classes into the new URLClassLoader instance
|
69 | 74 | * @author Cafebabe
|
| 75 | + * @throws IOException |
| 76 | + * @throws ClassNotFoundException |
70 | 77 | */
|
71 |
| - public static List<Class<?>> loadClassesIntoClassLoader() { |
72 |
| - try { |
73 |
| - File f = new File( |
74 |
| - the.bytecode.club.bytecodeviewer.BytecodeViewer.tempDirectory + |
75 |
| - the.bytecode.club.bytecodeviewer.BytecodeViewer.fs + |
76 |
| - "loaded_temp.jar"); |
77 |
| - JarUtils.saveAsJar(BytecodeViewer.getLoadedClasses(), f.getAbsolutePath()); |
78 |
| - JarFile jarFile = new JarFile(""+f.getAbsolutePath()); |
79 |
| - Enumeration<JarEntry> e = jarFile.entries(); |
80 |
| - URL[] urls = { new URL("jar:file:" + ""+f.getAbsolutePath()+"!/") }; |
81 |
| - cl = URLClassLoader.newInstance(urls); |
82 |
| - List<Class<?>> ret = new ArrayList<Class<?>>(); |
83 |
| - |
84 |
| - while (e.hasMoreElements()) |
85 |
| - { |
86 |
| - JarEntry je = (JarEntry) e.nextElement(); |
87 |
| - if(je.isDirectory() || !je.getName().endsWith(".class")) |
88 |
| - continue; |
89 |
| - String className = je.getName().replace("/", ".").replace(".class", ""); |
90 |
| - className = className.replace('/', '.'); |
91 |
| - try{ |
92 |
| - ret.add(cl.loadClass(className)); |
93 |
| - } |
94 |
| - catch(Exception e2) |
95 |
| - { |
96 |
| - new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e2); |
97 |
| - } |
98 |
| - } |
99 |
| - jarFile.close(); |
100 |
| - |
101 |
| - return ret; |
102 |
| - } |
103 |
| - catch(Exception e) |
104 |
| - { |
105 |
| - new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e); |
106 |
| - } |
107 |
| - return null; |
108 |
| - } |
| 78 | + @SuppressWarnings("deprecation") |
| 79 | + public static List<Class<?>> loadClassesIntoClassLoader( |
| 80 | + ArrayList<ClassNode> nodeList) throws IOException, |
| 81 | + ClassNotFoundException { |
| 82 | + |
| 83 | + File f = new File( |
| 84 | + the.bytecode.club.bytecodeviewer.BytecodeViewer.tempDirectory |
| 85 | + + the.bytecode.club.bytecodeviewer.BytecodeViewer.fs |
| 86 | + + "loaded_temp.jar"); |
| 87 | + JarUtils.saveAsJarClassesOnly(nodeList, f.getAbsolutePath()); |
| 88 | + |
| 89 | + JarFile jarFile = new JarFile("" + f.getAbsolutePath()); |
| 90 | + Enumeration<JarEntry> e = jarFile.entries(); |
| 91 | + cl = URLClassLoader.newInstance(new URL[]{ f.toURL() }); |
| 92 | + List<Class<?>> ret = new ArrayList<Class<?>>(); |
| 93 | + |
| 94 | + while (e.hasMoreElements()) { |
| 95 | + JarEntry je = (JarEntry) e.nextElement(); |
| 96 | + if (je.isDirectory() || !je.getName().endsWith(".class")) |
| 97 | + continue; |
| 98 | + String className = je.getName().replace("/", ".").replace(".class", ""); |
| 99 | + className = className.replace('/', '.'); |
| 100 | + ret.add(cl.loadClass(className)); |
| 101 | + |
| 102 | + } |
| 103 | + jarFile.close(); |
| 104 | + |
| 105 | + return ret; |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + /** |
| 111 | + * Re-instances the URLClassLoader and loads a jar to it. |
| 112 | + * @return The loaded classes into the new URLClassLoader instance |
| 113 | + * @author Cafebabe |
| 114 | + * @throws IOException |
| 115 | + * @throws ClassNotFoundException |
| 116 | + */ |
| 117 | + public static List<Class<?>> loadAllClassesIntoClassLoader() throws ClassNotFoundException, IOException { |
| 118 | + return loadClassesIntoClassLoader(getLoadedClasses()); |
| 119 | + } |
109 | 120 |
|
110 | 121 | /**
|
111 | 122 | * Creates a new instance of the ClassNode loader.
|
|
0 commit comments