Skip to content

Commit bcefab5

Browse files
committedJan 8, 2025
8342468: Improve API documentation for java.lang.classfile.constantpool
Reviewed-by: asotona
1 parent 40f0a39 commit bcefab5

31 files changed

+1070
-371
lines changed
 

‎src/java.base/share/classes/java/lang/classfile/BootstrapMethodEntry.java

+25-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,7 +25,9 @@
2525

2626
package java.lang.classfile;
2727

28+
import java.lang.classfile.attribute.BootstrapMethodsAttribute;
2829
import java.lang.classfile.constantpool.ConstantPool;
30+
import java.lang.classfile.constantpool.ConstantPoolBuilder;
2931
import java.lang.classfile.constantpool.LoadableConstantEntry;
3032
import java.lang.classfile.constantpool.MethodHandleEntry;
3133
import java.util.List;
@@ -34,22 +36,40 @@
3436

3537
/**
3638
* Models an entry in the bootstrap method table. The bootstrap method table
37-
* is stored in the {@code BootstrapMethods} attribute, but is modeled by
38-
* the {@link ConstantPool}, since the bootstrap method table is logically
39-
* part of the constant pool.
39+
* is stored in the {@link BootstrapMethodsAttribute BootstrapMethods}
40+
* attribute, but is modeled by the {@link ConstantPool}, since the bootstrap
41+
* method table is logically part of the constant pool.
42+
* <p>
43+
* A bootstrap method entry is composite:
44+
* {@snippet lang=text :
45+
* // @link substring="BootstrapMethodEntry" target="ConstantPoolBuilder#bsmEntry(MethodHandleEntry, List)" :
46+
* BootstrapMethodEntry(
47+
* MethodHandleEntry bootstrapMethod, // @link substring="bootstrapMethod" target="#bootstrapMethod"
48+
* List<LoadableConstantEntry> arguments // @link substring="arguments" target="#arguments()"
49+
* )
50+
* }
4051
*
52+
* @see ConstantPoolBuilder#bsmEntry ConstantPoolBuilder::bsmEntry
4153
* @since 24
4254
*/
4355
public sealed interface BootstrapMethodEntry
4456
permits BootstrapMethodEntryImpl {
4557

4658
/**
4759
* {@return the constant pool associated with this entry}
60+
*
61+
* @apiNote
62+
* Given a {@link ConstantPoolBuilder} {@code builder} and a {@code
63+
* BootstrapMethodEntry} {@code entry}, use {@link
64+
* ConstantPoolBuilder#canWriteDirect
65+
* builder.canWriteDirect(entry.constantPool())} instead of object equality
66+
* of the constant pool to determine if an entry is compatible.
4867
*/
4968
ConstantPool constantPool();
5069

5170
/**
52-
* {@return the index into the bootstrap method table corresponding to this entry}
71+
* {@return the index into the bootstrap method table corresponding to this
72+
* entry}
5373
*/
5474
int bsmIndex();
5575

‎src/java.base/share/classes/java/lang/classfile/ClassFile.java

+44-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -140,21 +140,56 @@ static ClassHierarchyResolverOption of(ClassHierarchyResolver classHierarchyReso
140140

141141
/**
142142
* Option describing whether to preserve the original constant pool when
143-
* transforming a classfile. Reusing the constant pool enables significant
144-
* optimizations in processing time and minimizes differences between the
145-
* original and transformed classfile, but may result in a bigger classfile
146-
* when a classfile is significantly transformed.
147-
* Default is {@code SHARED_POOL} to preserve the original constant
148-
* pool.
143+
* transforming a {@code class} file. Reusing the constant pool enables
144+
* significant optimizations in processing time and minimizes differences
145+
* between the original and transformed {@code class} files, but may result
146+
* in a bigger transformed {@code class} file when many elements of the
147+
* original {@code class} file are dropped and many original constant
148+
* pool entries become unused. Default is {@link #SHARED_POOL} to preserve
149+
* the original constant pool.
149150
*
151+
* @see ConstantPoolBuilder
152+
* @see #build(ClassEntry, ConstantPoolBuilder, Consumer)
153+
* @see #transformClass(ClassModel, ClassTransform)
150154
* @since 24
151155
*/
152156
enum ConstantPoolSharingOption implements Option {
153157

154-
/** Preserves the original constant pool when transforming classfile */
158+
/**
159+
* Preserves the original constant pool when transforming the {@code
160+
* class} file.
161+
* <p>
162+
* These two transformations below are equivalent:
163+
* {@snippet lang=java :
164+
* ClassModel originalClass = null; // @replace substring=null; replacement=...
165+
* ClassDesc resultClassName = null; // @replace substring=null; replacement=...
166+
* ClassTransform classTransform = null; // @replace substring=null; replacement=...
167+
* var resultOne = ClassFile.of(ConstantPoolSharingOption.SHARED_POOL)
168+
* .transformClass(originalClass, resultClassName, classTransform);
169+
* var resultTwo = ClassFile.of().build(resultClassName, ConstantPoolBuilder.of(originalClass),
170+
* clb -> clb.transform(originalClass, classTransform));
171+
* }
172+
*
173+
* @see ConstantPoolBuilder#of(ClassModel) ConstantPoolBuilder::of(ClassModel)
174+
*/
155175
SHARED_POOL,
156176

157-
/** Creates a new constant pool when transforming classfile */
177+
/**
178+
* Creates a new constant pool when transforming the {@code class} file.
179+
* <p>
180+
* These two transformations below are equivalent:
181+
* {@snippet lang=java :
182+
* ClassModel originalClass = null; // @replace substring=null; replacement=...
183+
* ClassDesc resultClassName = null; // @replace substring=null; replacement=...
184+
* ClassTransform classTransform = null; // @replace substring=null; replacement=...
185+
* var resultOne = ClassFile.of(ConstantPoolSharingOption.NEW_POOL)
186+
* .transformClass(originalClass, resultClassName, classTransform);
187+
* var resultTwo = ClassFile.of().build(resultClassName, ConstantPoolBuilder.of(),
188+
* clb -> clb.transform(originalClass, classTransform));
189+
* }
190+
*
191+
* @see ConstantPoolBuilder#of() ConstantPoolBuilder::of()
192+
*/
158193
NEW_POOL
159194
}
160195

‎src/java.base/share/classes/java/lang/classfile/constantpool/AnnotationConstantValueEntry.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -28,10 +28,9 @@
2828
import java.lang.constant.ConstantDesc;
2929

3030
/**
31-
* A constant pool entry that may be used by annotation constant values,
32-
* which includes the four kinds of primitive constants and UTF8 constants.
33-
* These entries are also the only entries that do not refer to other
34-
* constant pool entries.
31+
* Marker interface for constant pool entries that can represent constant values
32+
* associated with elements of annotations. They are also the only entries that
33+
* do not refer to other constant pool entries.
3534
*
3635
* @apiNote
3736
* An annotation constant value entry alone is not sufficient to determine
@@ -40,16 +39,17 @@
4039
* in {@link AnnotationValue.OfInt}.
4140
*
4241
* @see AnnotationValue.OfConstant
42+
* @jvms 4.7.16.1 The {@code element_value} structure
4343
* @sealedGraph
4444
* @since 24
4545
*/
4646
public sealed interface AnnotationConstantValueEntry extends PoolEntry
4747
permits DoubleEntry, FloatEntry, IntegerEntry, LongEntry, Utf8Entry {
4848

4949
/**
50-
* {@return the constant value} The constant value will be an {@link Integer},
51-
* {@link Long}, {@link Float}, {@link Double} for the primitive constants,
52-
* or {@link String} for UTF8 constants.
50+
* {@return the constant value} The constant value will be an {@link
51+
* Integer}, {@link Long}, {@link Float}, {@link Double} for the primitive
52+
* constants, or {@link String} for UTF8 constants.
5353
*/
5454
ConstantDesc constantValue();
5555
}

‎src/java.base/share/classes/java/lang/classfile/constantpool/ClassEntry.java

+56-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -30,33 +30,82 @@
3030
import jdk.internal.classfile.impl.AbstractPoolEntry;
3131

3232
/**
33-
* Models a {@code CONSTANT_Class_info} constant in the constant pool of a
34-
* classfile.
35-
* @jvms 4.4.1 The CONSTANT_Class_info Structure
33+
* Models a {@code CONSTANT_Class_info} structure, representing a reference
34+
* type, in the constant pool of a {@code class} file.
35+
* <p>
36+
* The use of a {@code ClassEntry} is modeled by a {@link ClassDesc} that is not
37+
* primitive. Conversions are through {@link ConstantPoolBuilder#classEntry(
38+
* ClassDesc)} and {@link #asSymbol()}.
39+
* <p>
40+
* A {@code ClassEntry} is composite:
41+
* {@snippet lang=text :
42+
* // @link substring="ClassEntry" target="ConstantPoolBuilder#classEntry(Utf8Entry)" :
43+
* ClassEntry(Utf8Entry name) // @link substring="name" target="#name"
44+
* }
45+
* where {@code name} represents:
46+
* <ul>
47+
* <li>The internal form of a binary name (JVMS {@jvms 4.2.1}), if and only if
48+
* this {@code ClassEntry} represents a class or interface, such as {@code
49+
* java/lang/String} for the {@link String} class.
50+
* <li>A field descriptor string (JVMS {@jvms 4.3.2}) representing an array type,
51+
* if and only if this {@code ClassEntry} represents an array type, such as
52+
* {@code [I} for the {@code int[]} type, or {@code [Ljava/lang/String;} for the
53+
* {@code String[]} type.
54+
* </ul>
55+
* A field descriptor string for an array type can be distinguished by its
56+
* leading {@code '['} character.
3657
*
58+
* @apiNote
59+
* The internal form of a binary name, where all occurrences of {@code .} in the
60+
* name are replaced by {@code /}, is informally known as an <dfn>{@index
61+
* "internal name"}</dfn>. This concept also applies to package names in
62+
* addition to class and interface names.
63+
*
64+
* @see ConstantPoolBuilder#classEntry ConstantPoolBuilder::classEntry
65+
* @see ClassDesc
66+
* @jvms 4.4.1 The {@code CONSTANT_Class_info} Structure
3767
* @since 24
3868
*/
3969
public sealed interface ClassEntry
4070
extends LoadableConstantEntry
4171
permits AbstractPoolEntry.ClassEntryImpl {
4272

73+
/**
74+
* {@inheritDoc}
75+
* <p>
76+
* This is equivalent to {@link #asSymbol() asSymbol()}.
77+
*/
4378
@Override
4479
default ConstantDesc constantValue() {
4580
return asSymbol();
4681
}
4782

4883
/**
49-
* {@return the UTF8 constant pool entry for the class name}
84+
* {@return the {@code Utf8Entry} referred by this structure} If the
85+
* value of the UTF8 starts with a {@code [}, this represents an array type
86+
* and the value is a descriptor string; otherwise, this represents a class
87+
* or interface and the value is the {@linkplain ##internalname internal
88+
* form} of a binary name.
89+
*
90+
* @see ConstantPoolBuilder#classEntry(Utf8Entry)
91+
* ConstantPoolBuilder::classEntry(Utf8Entry)
5092
*/
5193
Utf8Entry name();
5294

5395
/**
54-
* {@return the class name, as an internal binary name}
96+
* {@return the represented reference type, as the {@linkplain
97+
* ##internalname internal form} of a binary name or an array descriptor
98+
* string} This is a shortcut for {@link #name() name().stringValue()}.
5599
*/
56100
String asInternalName();
57101

58102
/**
59-
* {@return the class name, as a symbolic descriptor}
103+
* {@return the represented reference type, as a symbolic descriptor} The
104+
* returned descriptor is never {@linkplain ClassDesc#isPrimitive()
105+
* primitive}.
106+
*
107+
* @see ConstantPoolBuilder#classEntry(ClassDesc)
108+
* ConstantPoolBuilder::classEntry(ClassDesc)
60109
*/
61110
ClassDesc asSymbol();
62111
}

‎src/java.base/share/classes/java/lang/classfile/constantpool/ConstantDynamicEntry.java

+48-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -33,30 +33,65 @@
3333
import jdk.internal.classfile.impl.Util;
3434

3535
/**
36-
* Models a {@code CONSTANT_Dynamic_info} constant in the constant pool of a
37-
* classfile.
38-
* @jvms 4.4.10 The CONSTANT_Dynamic_info and CONSTANT_InvokeDynamic_info Structures
36+
* Models a {@code CONSTANT_Dynamic_info} structure, representing a <dfn>{@index
37+
* "dynamically-computed constant"}</dfn>, in the constant pool of a {@code
38+
* class} file.
39+
* <p>
40+
* The use of a {@code ConstantDynamicEntry} is modeled by a {@link
41+
* DynamicConstantDesc}. Conversions are through {@link #asSymbol()} and {@link
42+
* ConstantPoolBuilder#constantDynamicEntry(DynamicConstantDesc)}.
43+
* <p>
44+
* A dynamic constant entry is composite:
45+
* {@snippet lang=text :
46+
* // @link substring="ConstantDynamicEntry" target="ConstantPoolBuilder#constantDynamicEntry(BootstrapMethodEntry, NameAndTypeEntry)" :
47+
* ConstantDynamicEntry(
48+
* BootstrapMethodEntry bootstrap, // @link substring="bootstrap" target="#bootstrap()"
49+
* NameAndTypeEntry nameAndType // @link substring="nameAndType" target="#nameAndType()"
50+
* )
51+
* }
52+
* where {@link #type() nameAndType.type()} is a {@linkplain #typeSymbol()
53+
* field descriptor} string.
3954
*
55+
* @apiNote
56+
* A dynamically-computed constant is frequently called a <dfn>{@index "dynamic
57+
* constant"}</dfn>, or a <dfn>{@index "condy"}</dfn>, from the abbreviation of
58+
* "constant dynamic".
59+
*
60+
* @see ConstantPoolBuilder#constantDynamicEntry
61+
* ConstantPoolBuilder::constantDynamicEntry
62+
* @see DynamicConstantDesc
63+
* @see java.lang.invoke##condycon Dynamically-computed constants
64+
* @jvms 4.4.10 The {@code CONSTANT_Dynamic_info} and {@code
65+
* CONSTANT_InvokeDynamic_info} Structures
4066
* @since 24
4167
*/
4268
public sealed interface ConstantDynamicEntry
4369
extends DynamicConstantPoolEntry, LoadableConstantEntry
4470
permits AbstractPoolEntry.ConstantDynamicEntryImpl {
4571

4672
/**
47-
* {@return a symbolic descriptor for the dynamic constant's type}
73+
* {@return a symbolic descriptor for the {@linkplain #type() field type} of
74+
* this dynamically-computed constant}
4875
*/
4976
default ClassDesc typeSymbol() {
5077
return Util.fieldTypeSymbol(type());
5178
}
5279

80+
/**
81+
* {@inheritDoc}
82+
* <p>
83+
* This is equivalent to {@link #asSymbol() asSymbol()}.
84+
*/
5385
@Override
5486
default ConstantDesc constantValue() {
5587
return asSymbol();
5688
}
5789

5890
/**
59-
* {@return the symbolic descriptor for the {@code invokedynamic} constant}
91+
* {@return a symbolic descriptor for this dynamically-computed constant}
92+
*
93+
* @see ConstantPoolBuilder#constantDynamicEntry(DynamicConstantDesc)
94+
* ConstantPoolBuilder::constantDynamicEntry(DynamicConstantDesc)
6095
*/
6196
default DynamicConstantDesc<?> asSymbol() {
6297
return DynamicConstantDesc.ofNamed(bootstrap().bootstrapMethod().asSymbol(),
@@ -68,10 +103,15 @@ default DynamicConstantDesc<?> asSymbol() {
68103
}
69104

70105
/**
71-
* {@return the type of the constant}
106+
* {@inheritDoc}
107+
*
108+
* @apiNote
109+
* The data type of a dynamically-computed constant depends on its
110+
* {@linkplain #type() descriptor}, while the data type of all other
111+
* constants can be determined by their {@linkplain #tag() constant type}.
72112
*/
73113
@Override
74114
default TypeKind typeKind() {
75-
return TypeKind.fromDescriptor(type().stringValue());
115+
return TypeKind.fromDescriptor(type());
76116
}
77117
}

0 commit comments

Comments
 (0)
Please sign in to comment.