|
24 | 24 | */
|
25 | 25 | package java.lang.classfile;
|
26 | 26 |
|
| 27 | +import java.lang.classfile.attribute.CodeAttribute; |
| 28 | +import java.lang.classfile.instruction.LabelTarget; |
| 29 | +import java.util.ListIterator; |
| 30 | + |
27 | 31 | import jdk.internal.classfile.impl.LabelImpl;
|
28 | 32 |
|
29 | 33 | /**
|
30 | 34 | * A marker for a position within the instructions of a method body. The
|
31 |
| - * association between a label's identity and the position it represents is |
32 |
| - * managed by the entity managing the method body (a {@link CodeModel} or {@link |
33 |
| - * CodeBuilder}), not the label itself; this allows the same label to have a |
34 |
| - * meaning both in an existing method (as managed by a {@linkplain CodeModel}) |
35 |
| - * and in the transformation of that method (as managed by a {@linkplain |
36 |
| - * CodeBuilder}), while corresponding to different positions in each. When |
37 |
| - * traversing the elements of a {@linkplain CodeModel}, {@linkplain Label} |
38 |
| - * markers will be delivered at the position to which they correspond. A label |
39 |
| - * can be bound to the current position within a {@linkplain CodeBuilder} via |
40 |
| - * {@link CodeBuilder#labelBinding(Label)} or {@link CodeBuilder#with(ClassFileElement)}. |
| 35 | + * position is a cursor position in the list of instructions, similar to that |
| 36 | + * of a {@link ListIterator}. |
| 37 | + * |
| 38 | + * <h2 id="reading">Reading Labels</h2> |
| 39 | + * Labels read from {@code class} files represent positions in the {@code code} |
| 40 | + * array of a {@link CodeAttribute Code} attribute. It is associated with a |
| 41 | + * <dfn>{@index bci}</dfn>, also known as <dfn>{@index pc}</dfn>, the index into |
| 42 | + * the {@code code} array; the actual cursor position is immediately before the |
| 43 | + * given index, so a label at the beginning of the instructions has bci {@code 0}, |
| 44 | + * and a label at the end of the instructions has bci {@link CodeAttribute#codeLength |
| 45 | + * codeLength() + 1}. The bci can be inspected through {@link CodeAttribute#labelToBci |
| 46 | + * CodeAttribute::labelToBci}. |
| 47 | + * <p> |
| 48 | + * In generic {@link CodeModel}s, a label may not have a bci value; the position |
| 49 | + * of a label can be found by searching for the corresponding {@link LabelTarget} |
| 50 | + * within that model. |
| 51 | + * |
| 52 | + * <h2 id="writing">Writing Labels</h2> |
| 53 | + * Many models in {@code java.lang.classfile} refer to labels. To write a |
| 54 | + * label, a label must be obtained, it must be bound to a {@link CodeBuilder}. |
| 55 | + * <p> |
| 56 | + * To obtain a label: |
| 57 | + * <ul> |
| 58 | + * <li>Use a label read from other models. |
| 59 | + * <li>Use pre-defined labels from a {@link CodeBuilder}, such as {@link |
| 60 | + * CodeBuilder#startLabel() CodeBuilder::startLabel}, {@link CodeBuilder#endLabel |
| 61 | + * CodeBuilder::endLabel}, or {@link CodeBuilder.BlockCodeBuilder#breakLabel |
| 62 | + * BlockCodeBuilder::breakLabel}. They are already bound. |
| 63 | + * <li>Create labels with {@link CodeBuilder#newLabel CodeBuilder::newLabel} or |
| 64 | + * {@link CodeBuilder#newBoundLabel CodeBuilder::newBoundLabel}. |
| 65 | + * </ul> |
| 66 | + * <p> |
| 67 | + * A label must be bound exactly once in the {@code CodeBuilder} where it is |
| 68 | + * used; otherwise, writing fails. To bind an unbound label: |
| 69 | + * <ul> |
| 70 | + * <li>Send a {@link LabelTarget} to a {@code CodeBuilder}. |
| 71 | + * <li>Use {@link CodeBuilder#labelBinding CodeBuilder::labelBinding}. |
| 72 | + * </ul> |
| 73 | + * Note that a label read from another model is not automatically bound in a |
| 74 | + * {@code CodeBuilder}; they are separate entities and the label is bound to |
| 75 | + * different positions in them. |
41 | 76 | *
|
| 77 | + * @see CodeAttribute#labelToBci CodeAttribute::labelToBci |
| 78 | + * @see LabelTarget |
| 79 | + * @see CodeBuilder#newLabel CodeBuilder::newLabel |
| 80 | + * @see CodeBuilder#newBoundLabel CodeBuilder::newBoundLabel |
| 81 | + * @see CodeBuilder#startLabel CodeBuilder::startLabel |
| 82 | + * @see CodeBuilder#endLabel CodeBuilder::endLabel |
| 83 | + * @see CodeBuilder.BlockCodeBuilder#breakLabel BlockCodeBuilder::breakLabel |
42 | 84 | * @since 24
|
43 | 85 | */
|
44 | 86 | public sealed interface Label
|
|
0 commit comments