private static void visit(Method method, byte[] bytecodes, MethodVisitor methodVisitor, boolean withLineNumbers) {
    ReferenceType type = method.declaringType();

    assert type.virtualMachine().canGetConstantPool();
    BufferExposingByteArrayOutputStream bytes = new BufferExposingByteArrayOutputStream();
    try (DataOutputStream dos = new DataOutputStream(bytes)) {
      writeClassHeader(dos, type.constantPoolCount(), type.constantPool());
    }
    catch (IOException e) {
      throw new RuntimeException(e);
    }
    ClassReader reader = new ClassReader(bytes.getInternalBuffer(), 0, bytes.size());

    ClassWriter writer = new ClassWriter(reader, 0);
    String superName = null;
    String[] interfaces = null;
    if (type instanceof ClassType) {
      ClassType superClass = ((ClassType)type).superclass();
      superName = superClass != null ? superClass.name() : null;
      interfaces = ((ClassType)type).interfaces().stream().map(ReferenceType::name).toArray(String[]::new);
    }
    else if (type instanceof InterfaceType) {
      interfaces = ((InterfaceType)type).superinterfaces().stream().map(ReferenceType::name).toArray(String[]::new);
    }
    writer.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, type.name(), type.signature(), superName, interfaces);
    Attribute bootstrapMethods = createBootstrapMethods(reader, writer);
    if (bootstrapMethods != null) {
      writer.visitAttribute(bootstrapMethods);
    }
    MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC, method.name(), method.signature(), method.signature(), null);
    mv.visitAttribute(createCode(writer, method, bytecodes, withLineNumbers));

    InstructionOffsetReader insnOffsReader = methodVisitor instanceof InstructionOffsetReader
                                             ? (InstructionOffsetReader)methodVisitor
                                             : null;

    new ClassReader(writer.toByteArray()) {
      @Override
      protected void readBytecodeInstructionOffset(int bytecodeOffset) {
        if (insnOffsReader != null) {
          insnOffsReader.readBytecodeInstructionOffset(bytecodeOffset);
        }
      }
    }.accept(new ClassVisitor(Opcodes.API_VERSION) {
      @Override
      public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
        assert name.equals(method.name());
        return methodVisitor;
      }
    }, 0);
  }
private static void visit(Method method, byte[] bytecodes, MethodVisitor methodVisitor, boolean withLineNumbers)