Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8361102

java.lang.classfile.CodeBuilder.branch(Opcode op, Label target) doesn't throw IllegalArgumentException - if op is not of Opcode.Kind.BRANCH

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P3 P3
    • 26
    • 24, 25
    • core-libs

      The assertion "Throws: IllegalArgumentException — if op is not of Opcode.Kind.BRANCH" does not work as expected (see the attached test) for 72 instructions:
      public static void main(String[] args) {
              Opcode[] opcodeList = Arrays.stream(Opcode.values()).
                      filter(oc -> oc.kind() != Opcode.Kind.BRANCH).
                      toArray(Opcode[]::new);
              for (int i = 0, j = 0; i < opcodeList.length; i++) {
                  Opcode opcode = opcodeList[i];
                  try {
                      ClassFile.of().build(ClassDesc.of("Cls"), clsb -> {
                          clsb.withMethod("mtd", MethodTypeDesc.ofDescriptor("()I"),
                                  ClassFile.ACC_STATIC,
                                  mb -> mb.withCode(cb -> {
                                      Label LBL = cb.newLabel();
                                      cb.loadConstant(0);
                                      cb.branch(opcode, LBL);
                                      cb.loadConstant(1);
                                      cb.ireturn();
                                      cb.labelBinding(LBL);
                                      cb.loadConstant(2);
                                      cb.ireturn();
                                  }));
                      });
                  } catch (IllegalArgumentException e) {
                      continue;
                  }
                  System.out.println("%d) %s(%s) doesn't throw IllegalArgumentException as expected".
                          formatted(j++, opcode, opcode.kind()));
              }
          }

            liach Chen Liang
            lkuskov Leonid Kuskov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: