Skip to content

Commit cde7f1f

Browse files
authored
Merge pull request #25 from HaHaWTH/bouncedpad
Rewrite ClassInheritanceProvider
2 parents dc36ba6 + 9c9559d commit cde7f1f

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed
Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package catserver.server.remapper;
22

33
import net.md_5.specialsource.provider.InheritanceProvider;
4+
import net.minecraft.launchwrapper.Launch;
5+
import org.objectweb.asm.ClassReader;
6+
import org.objectweb.asm.ClassVisitor;
7+
import org.objectweb.asm.Opcodes;
48

9+
import java.io.InputStream;
510
import java.util.Collection;
611
import java.util.HashSet;
712

@@ -11,26 +16,59 @@ public class ClassInheritanceProvider implements InheritanceProvider {
1116
@Override
1217
public Collection<String> getParents(String className) {
1318
className = ReflectionTransformer.remapper.map(className);
19+
try (InputStream is = Launch.classLoader.getResourceAsStream((className + ".class"))) {
20+
if (is == null) {
21+
return null;
22+
}
23+
24+
ClassReader reader = new ClassReader(is);
25+
ParentVisitor visitor = new ParentVisitor();
26+
27+
reader.accept(visitor, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
28+
29+
Collection<String> parents = new HashSet<>();
1430

15-
try {
16-
Collection<String> parents = new HashSet<String>();
17-
Class<?> reference = Class.forName(className.replace('/', '.'), false, this.getClass().getClassLoader()/*RemappedMethods.loader*/);
18-
Class<?> extend = reference.getSuperclass();
19-
if (extend != null) {
20-
parents.add(reverseMap(extend));
31+
if (visitor.getSuperName() != null && !visitor.getSuperName().equals("java/lang/Object")) {
32+
parents.add(reverseMap(visitor.getSuperName()));
2133
}
2234

23-
for (Class<?> inter : reference.getInterfaces()) {
35+
for (String inter : visitor.getInterfaces()) {
2436
if (inter != null) {
2537
parents.add(reverseMap(inter));
2638
}
2739
}
2840

2941
return parents;
42+
3043
} catch (Exception e) {
31-
// Empty catch block
44+
System.err.println("Failed to read class for parents: " + className);
3245
}
46+
3347
return null;
3448
}
3549

50+
private static class ParentVisitor extends ClassVisitor {
51+
private String superName;
52+
private String[] interfaces;
53+
54+
public ParentVisitor() {
55+
super(Opcodes.ASM9);
56+
}
57+
58+
@Override
59+
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
60+
this.superName = superName;
61+
this.interfaces = interfaces;
62+
super.visit(version, access, name, signature, superName, interfaces);
63+
}
64+
65+
public String getSuperName() {
66+
return superName;
67+
}
68+
69+
public String[] getInterfaces() {
70+
return interfaces == null ? new String[0] : interfaces;
71+
}
72+
}
73+
3674
}

0 commit comments

Comments
 (0)