### Question:
Label { public int getOffset() { if ((flags & FLAG_RESOLVED) == 0) { throw new IllegalStateException("Label offset position has not been resolved yet"); } return bytecodeOffset; } Label(); int getOffset(); @Override String toString(); public Object info; }### Answer:
@Test public void testGetOffset() { MethodVisitor methodVisitor = new ClassWriter(0).visitMethod(Opcodes.ACC_PUBLIC, "m", "()V", null, null); methodVisitor.visitCode(); methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); Label label = new Label(); methodVisitor.visitLabel(label); assertEquals(1, label.getOffset()); }
@Test public void testGetOffset_illegalState() { Executable getOffset = () -> new Label().getOffset(); Exception exception = assertThrows(IllegalStateException.class, getOffset); assertEquals("Label offset position has not been resolved yet", exception.getMessage()); }