Browse Source

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 years ago
parent
commit
4d5fc47bcb
No known key found for this signature in database

+ 36
- 16
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaBytecodeRunUnit.java View File

5
  */
5
  */
6
 package org.openzen.zenscript.javabytecode;
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
 import org.objectweb.asm.ClassWriter;
8
 import org.objectweb.asm.ClassWriter;
20
 import org.objectweb.asm.Opcodes;
9
 import org.objectweb.asm.Opcodes;
21
 import org.objectweb.asm.Type;
10
 import org.objectweb.asm.Type;
29
 import org.openzen.zenscript.javashared.JavaMethod;
18
 import org.openzen.zenscript.javashared.JavaMethod;
30
 import org.openzen.zenscript.javashared.JavaParameterInfo;
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
  * @author Hoofdgebruiker
31
  * @author Hoofdgebruiker
35
  */
32
  */
36
 public class JavaBytecodeRunUnit {
33
 public class JavaBytecodeRunUnit {
145
 
142
 
146
 	public class ScriptClassLoader extends ClassLoader {
143
 	public class ScriptClassLoader extends ClassLoader {
147
 		private final Map<String, Class> customClasses = new HashMap<>();
144
 		private final Map<String, Class> customClasses = new HashMap<>();
148
-
149
-		ScriptClassLoader() {
150
-			super();
151
-		}
145
+		private final ClassLoader parent;
152
 
146
 
153
 		public ScriptClassLoader(ClassLoader parent) {
147
 		public ScriptClassLoader(ClassLoader parent) {
154
 			super(parent);
148
 			super(parent);
149
+			this.parent = parent;
155
 		}
150
 		}
156
 
151
 
157
 		@Override
152
 		@Override
158
 		public Class<?> loadClass(String name) throws ClassNotFoundException {
153
 		public Class<?> loadClass(String name) throws ClassNotFoundException {
159
 			//System.out.println("LoadClass " + name);
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
 			if (customClasses.containsKey(name))
166
 			if (customClasses.containsKey(name))
162
 				return customClasses.get(name);
167
 				return customClasses.get(name);
163
 			if (classes.containsKey(name)) {
168
 			if (classes.containsKey(name)) {
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
 	private static Class<?> loadClass(ClassLoader classLoader, String descriptor) throws ClassNotFoundException {
192
 	private static Class<?> loadClass(ClassLoader classLoader, String descriptor) throws ClassNotFoundException {
173
 		switch (descriptor) {
193
 		switch (descriptor) {
174
 			case "Z": return boolean.class;
194
 			case "Z": return boolean.class;

Loading…
Cancel
Save