Selaa lähdekoodia

Diry hack to use findLoadedClass on the parent loader

Lokking at you ForgeModLoader that needs to reinitialize about every class it comes across
kindlich 6 vuotta sitten
vanhempi
commit
4d5fc47bcb
No known key found for this signature in database

+ 36
- 16
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaBytecodeRunUnit.java Näytä tiedosto

@@ -5,17 +5,6 @@
5 5
  */
6 6
 package org.openzen.zenscript.javabytecode;
7 7
 
8
-import java.io.File;
9
-import java.io.FileOutputStream;
10
-import java.io.IOException;
11
-import java.lang.reflect.InvocationTargetException;
12
-import java.util.ArrayList;
13
-import java.util.Collections;
14
-import java.util.HashMap;
15
-import java.util.List;
16
-import java.util.Map;
17
-import java.util.logging.Level;
18
-import java.util.logging.Logger;
19 8
 import org.objectweb.asm.ClassWriter;
20 9
 import org.objectweb.asm.Opcodes;
21 10
 import org.objectweb.asm.Type;
@@ -29,8 +18,16 @@ import org.openzen.zenscript.javashared.JavaClass;
29 18
 import org.openzen.zenscript.javashared.JavaMethod;
30 19
 import org.openzen.zenscript.javashared.JavaParameterInfo;
31 20
 
21
+import java.io.File;
22
+import java.io.FileOutputStream;
23
+import java.io.IOException;
24
+import java.lang.reflect.InvocationTargetException;
25
+import java.lang.reflect.Method;
26
+import java.util.*;
27
+import java.util.logging.Level;
28
+import java.util.logging.Logger;
29
+
32 30
 /**
33
- *
34 31
  * @author Hoofdgebruiker
35 32
  */
36 33
 public class JavaBytecodeRunUnit {
@@ -145,19 +142,27 @@ public class JavaBytecodeRunUnit {
145 142
 
146 143
 	public class ScriptClassLoader extends ClassLoader {
147 144
 		private final Map<String, Class> customClasses = new HashMap<>();
148
-
149
-		ScriptClassLoader() {
150
-			super();
151
-		}
145
+		private final ClassLoader parent;
152 146
 
153 147
 		public ScriptClassLoader(ClassLoader parent) {
154 148
 			super(parent);
149
+			this.parent = parent;
155 150
 		}
156 151
 
157 152
 		@Override
158 153
 		public Class<?> loadClass(String name) throws ClassNotFoundException {
159 154
 			//System.out.println("LoadClass " + name);
160 155
 
156
+			if (findLoadedClassMethod != null) {
157
+				try {
158
+					Class<?> clazz = (Class<?>) findLoadedClassMethod.invoke(this.parent, name);
159
+					if (clazz != null)
160
+						return clazz;
161
+				} catch (IllegalAccessException | InvocationTargetException e) {
162
+					e.printStackTrace();
163
+				}
164
+			}
165
+
161 166
 			if (customClasses.containsKey(name))
162 167
 				return customClasses.get(name);
163 168
 			if (classes.containsKey(name)) {
@@ -169,6 +174,21 @@ public class JavaBytecodeRunUnit {
169 174
 		}
170 175
 	}
171 176
 
177
+
178
+	static {
179
+		Method method;
180
+		try {
181
+			method = ClassLoader.class.getDeclaredMethod("findLoadedClass", String.class);
182
+			method.setAccessible(true);
183
+		} catch (NoSuchMethodException e) {
184
+			e.printStackTrace();
185
+			method = null;
186
+		}
187
+		findLoadedClassMethod = method;
188
+	}
189
+
190
+	private static final Method findLoadedClassMethod;
191
+
172 192
 	private static Class<?> loadClass(ClassLoader classLoader, String descriptor) throws ClassNotFoundException {
173 193
 		switch (descriptor) {
174 194
 			case "Z": return boolean.class;

Loading…
Peruuta
Tallenna