Java 1.8 ASM modified code throws NoSuchFieldError

Hi! I use ASM to modify this code:
private Launch() {
URLClassLoader ucl = (URLClassLoader)this.getClass().getClassLoader();
classLoader = new LaunchClassLoader(ucl.getURLs());
blackboard = new HashMap();
Thread.currentThread().setContextClassLoader(classLoader);
}
private Launch() {
URLClassLoader ucl = (URLClassLoader)this.getClass().getClassLoader();
classLoader = new LaunchClassLoader(ucl.getURLs());
blackboard = new HashMap();
Thread.currentThread().setContextClassLoader(classLoader);
}
into
private Launch() {
URLClassLoader ucl = (URLClassLoader)this.getClass().getClassLoader();
classLoader = new CustomLaunchClassLoader(ucl.getURLs());
blackboard = new HashMap();
Thread.currentThread().setContextClassLoader(classLoader);
}
private Launch() {
URLClassLoader ucl = (URLClassLoader)this.getClass().getClassLoader();
classLoader = new CustomLaunchClassLoader(ucl.getURLs());
blackboard = new HashMap();
Thread.currentThread().setContextClassLoader(classLoader);
}
but when I execute it, it throws a NoSuchFieldError on the classLoader field. classLoader is a private static field.
3 Replies
JavaBot
JavaBot2y ago
This post has been reserved for your question.
Hey @tyranobast! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Tyranobast
TyranobastOP2y ago
The ASM code I use:
ClassReader cr = new ClassReader(Files.newInputStream(launchClass));
ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
ClassVisitor cv = new ClassVisitor(Opcodes.ASM5, cw) {
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
if (name.equals("<init>")) {
return new MethodVisitor(Opcodes.ASM5, super.visitMethod(access, name, desc, signature, exceptions)) {
@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
if (opcode == Opcodes.PUTSTATIC && "net/minecraft/launchwrapper/Launch".equals(owner) && "classLoader".equals(name)) {
mv.visitTypeInsn(Opcodes.NEW, "eu/damnatio/damnatioac/network/verification/CustomLaunchClassLoader");
mv.visitInsn(Opcodes.DUP);
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/net/URLClassLoader", "getURLs", "()[Ljava/net/URL;", false);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "eu/damnatio/damnatioac/network/verification/CustomLaunchClassLoader", "<init>", "([Ljava/net/URL;)V", false);
mv.visitFieldInsn(Opcodes.PUTSTATIC, "net/minecraft/launchwrapper/Launch", "classLoader", "Leu/damnatio/damnatioac/network/verification/CustomLaunchClassLoader;");
} else {
mv.visitFieldInsn(opcode, owner, name, desc);
}
}
};
}
return super.visitMethod(access, name, desc, signature, exceptions);
}
};

cr.accept(cv, 0);
ClassReader cr = new ClassReader(Files.newInputStream(launchClass));
ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
ClassVisitor cv = new ClassVisitor(Opcodes.ASM5, cw) {
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
if (name.equals("<init>")) {
return new MethodVisitor(Opcodes.ASM5, super.visitMethod(access, name, desc, signature, exceptions)) {
@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
if (opcode == Opcodes.PUTSTATIC && "net/minecraft/launchwrapper/Launch".equals(owner) && "classLoader".equals(name)) {
mv.visitTypeInsn(Opcodes.NEW, "eu/damnatio/damnatioac/network/verification/CustomLaunchClassLoader");
mv.visitInsn(Opcodes.DUP);
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/net/URLClassLoader", "getURLs", "()[Ljava/net/URL;", false);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "eu/damnatio/damnatioac/network/verification/CustomLaunchClassLoader", "<init>", "([Ljava/net/URL;)V", false);
mv.visitFieldInsn(Opcodes.PUTSTATIC, "net/minecraft/launchwrapper/Launch", "classLoader", "Leu/damnatio/damnatioac/network/verification/CustomLaunchClassLoader;");
} else {
mv.visitFieldInsn(opcode, owner, name, desc);
}
}
};
}
return super.visitMethod(access, name, desc, signature, exceptions);
}
};

cr.accept(cv, 0);
any help would be cool !!!! The decompiled Launch constructor (using FernFlower):
private Launch() {
URLClassLoader ucl =
(URLClassLoader)this.getClass().getClassLoader();
new LaunchClassLoader(ucl.getURLs());
classLoader = new CustomLaunchClassLoader(ucl.getURLs());
blackboard = new HashMap();
Thread.currentThread().setContextClassLoader(classLoader);
}
private Launch() {
URLClassLoader ucl =
(URLClassLoader)this.getClass().getClassLoader();
new LaunchClassLoader(ucl.getURLs());
classLoader = new CustomLaunchClassLoader(ucl.getURLs());
blackboard = new HashMap();
Thread.currentThread().setContextClassLoader(classLoader);
}
JavaBot
JavaBot2y ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one.
Want results from more Discord servers?
Add your server