1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| public class MyClassLoader extends ClassLoader {
public MyClassLoader() { super(null); }
@Override protected Class<?> findClass(String name) throws ClassNotFoundException { System.out.println("findClass: "+name); try { ClassReader reader = new ClassReader(name); ClassWriter writer = new ClassWriter(reader, 0); RemoveFinalFlagClassVisitor classVisitor = new RemoveFinalFlagClassVisitor(writer); reader.accept(classVisitor, ClassReader.SKIP_CODE); byte[] bytes = writer.toByteArray(); return defineClass(name, bytes, 0, bytes.length); } catch (IOException e) { e.printStackTrace(); } return null; }
}
class RemoveFinalFlagClassVisitor extends ClassVisitor {
public RemoveFinalFlagClassVisitor(ClassVisitor cv) { super(Opcodes.ASM5, cv); }
@Override public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { if((access & Opcodes.ACC_FINAL) == Opcodes.ACC_FINAL) { access = access ^ Opcodes.ACC_FINAL; }
super.visit(version, access, name, signature, superName, interfaces); }
@Override public MethodVisitor visitMethod(int i, String s, String s1, String s2, String[] strings) {
if (i==17){ return super.visitMethod(1, s, s1, s2, strings); } return super.visitMethod(i, s, s1, s2, strings); } }
|