Skip to content

Commit a9f5e76

Browse files
committed
8335905: CompoundElement API cleanup
Reviewed-by: asotona
1 parent 6f325db commit a9f5e76

34 files changed

+73
-82
lines changed

src/java.base/share/classes/java/lang/classfile/ClassFileBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2024, 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
@@ -89,7 +89,7 @@ default void transform(CompoundElement<E> model, ClassFileTransform<?, E, B> tra
8989
B builder = (B) this;
9090
var resolved = transform.resolve(builder);
9191
resolved.startHandler().run();
92-
model.forEachElement(resolved.consumer());
92+
model.forEach(resolved.consumer());
9393
resolved.endHandler().run();
9494
}
9595
}

src/java.base/share/classes/java/lang/classfile/ClassModel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2024, 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
@@ -37,7 +37,7 @@
3737

3838
/**
3939
* Models a classfile. The contents of the classfile can be traversed via
40-
* a streaming view (e.g., {@link #elements()}), or via random access (e.g.,
40+
* a streaming view, or via random access (e.g.,
4141
* {@link #flags()}), or by freely mixing the two.
4242
*
4343
* @since 22

src/java.base/share/classes/java/lang/classfile/CodeModel.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2024, 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
@@ -36,8 +36,7 @@
3636

3737
/**
3838
* Models the body of a method (the {@code Code} attribute). The instructions
39-
* of the method body are accessed via a streaming view (e.g., {@link
40-
* #elements()}).
39+
* of the method body are accessed via a streaming view.
4140
*
4241
* @since 22
4342
*/

src/java.base/share/classes/java/lang/classfile/CompoundElement.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2024, 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
@@ -40,7 +40,7 @@
4040
* class. When encountering a {@linkplain CompoundElement}, clients have the
4141
* option to treat the element as a single entity (e.g., an entire method)
4242
* or to traverse the contents of that element with the methods in this class
43-
* (e.g., {@link #elements()}, {@link #forEachElement(Consumer)}, etc.)
43+
* (e.g., {@link #forEach(Consumer)}, etc.)
4444
* @param <E> the element type
4545
*
4646
* @sealedGraph
@@ -55,23 +55,16 @@ public sealed interface CompoundElement<E extends ClassFileElement>
5555
* compound element
5656
* @param consumer the handler
5757
*/
58-
void forEachElement(Consumer<E> consumer);
59-
60-
/**
61-
* {@return an {@link Iterable} describing all the elements contained in this
62-
* compound element}
63-
*/
64-
default Iterable<E> elements() {
65-
return elementList();
66-
}
58+
@Override
59+
void forEach(Consumer<? super E> consumer);
6760

6861
/**
6962
* {@return an {@link Iterator} describing all the elements contained in this
7063
* compound element}
7164
*/
7265
@Override
7366
default Iterator<E> iterator() {
74-
return elements().iterator();
67+
return elementList().iterator();
7568
}
7669

7770
/**
@@ -91,7 +84,7 @@ default Stream<E> elementStream() {
9184
*/
9285
default List<E> elementList() {
9386
List<E> list = new ArrayList<>();
94-
forEachElement(new Consumer<>() {
87+
forEach(new Consumer<>() {
9588
@Override
9689
public void accept(E e) {
9790
list.add(e);

src/java.base/share/classes/java/lang/classfile/FieldModel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2024, 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
@@ -35,7 +35,7 @@
3535

3636
/**
3737
* Models a field. The contents of the field can be traversed via
38-
* a streaming view (e.g., {@link #elements()}), or via random access (e.g.,
38+
* a streaming view, or via random access (e.g.,
3939
* {@link #flags()}), or by freely mixing the two.
4040
*
4141
* @since 22

src/java.base/share/classes/java/lang/classfile/MethodModel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2024, 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
@@ -35,7 +35,7 @@
3535

3636
/**
3737
* Models a method. The contents of the method can be traversed via
38-
* a streaming view (e.g., {@link #elements()}), or via random access (e.g.,
38+
* a streaming view, or via random access (e.g.,
3939
* {@link #flags()}), or by freely mixing the two.
4040
*
4141
* @since 22

src/java.base/share/classes/jdk/internal/classfile/impl/AbstractUnboundModel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2024, 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
@@ -45,7 +45,7 @@ public AbstractUnboundModel(List<E> elements) {
4545
}
4646

4747
@Override
48-
public void forEachElement(Consumer<E> consumer) {
48+
public void forEach(Consumer<? super E> consumer) {
4949
elements.forEach(consumer);
5050
}
5151

src/java.base/share/classes/jdk/internal/classfile/impl/BufferedCodeBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public void writeTo(DirectMethodBuilder builder) {
201201
builder.withCode(new Consumer<>() {
202202
@Override
203203
public void accept(CodeBuilder cb) {
204-
forEachElement(cb);
204+
forEach(cb);
205205
}
206206
});
207207
}

src/java.base/share/classes/jdk/internal/classfile/impl/BufferedMethodBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2024, 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
@@ -200,7 +200,7 @@ public void writeTo(DirectClassBuilder builder) {
200200
builder.withMethod(methodName(), methodType(), methodFlags(), new Consumer<>() {
201201
@Override
202202
public void accept(MethodBuilder mb) {
203-
forEachElement(mb);
203+
forEach(mb);
204204
}
205205
});
206206
}

src/java.base/share/classes/jdk/internal/classfile/impl/ClassImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public List<Attribute<?>> attributes() {
156156
// ClassModel
157157

158158
@Override
159-
public void forEachElement(Consumer<ClassElement> consumer) {
159+
public void forEach(Consumer<? super ClassElement> consumer) {
160160
consumer.accept(flags());
161161
consumer.accept(ClassFileVersion.of(majorVersion(), minorVersion()));
162162
superclass().ifPresent(new Consumer<ClassEntry>() {

src/java.base/share/classes/jdk/internal/classfile/impl/ClassRemapperImpl.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,10 @@ public void accept(ClassBuilder clb, ClassElement cle) {
9797
switch (cle) {
9898
case FieldModel fm ->
9999
clb.withField(fm.fieldName().stringValue(), map(
100-
fm.fieldTypeSymbol()), fb ->
101-
fm.forEachElement(asFieldTransform().resolve(fb).consumer()));
100+
fm.fieldTypeSymbol()), fb -> fb.transform(fm, asFieldTransform()));
102101
case MethodModel mm ->
103102
clb.withMethod(mm.methodName().stringValue(), mapMethodDesc(
104-
mm.methodTypeSymbol()), mm.flags().flagsMask(), mb ->
105-
mm.forEachElement(asMethodTransform().resolve(mb).consumer()));
103+
mm.methodTypeSymbol()), mm.flags().flagsMask(), mb -> mb.transform(mm, asMethodTransform()));
106104
case Superclass sc ->
107105
clb.withSuperclass(map(sc.superclassEntry().asSymbol()));
108106
case Interfaces ins ->

src/java.base/share/classes/jdk/internal/classfile/impl/CodeImpl.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.ArrayList;
2828
import java.util.Collections;
2929
import java.util.List;
30+
import java.util.Objects;
3031
import java.util.Optional;
3132
import java.util.function.Consumer;
3233

@@ -149,7 +150,7 @@ public void writeTo(BufWriter buf) {
149150
new Consumer<CodeBuilder>() {
150151
@Override
151152
public void accept(CodeBuilder cb) {
152-
forEachElement(cb);
153+
forEach(cb);
153154
}
154155
},
155156
(SplitConstantPool)buf.constantPool(),
@@ -166,7 +167,8 @@ public Optional<MethodModel> parent() {
166167
}
167168

168169
@Override
169-
public void forEachElement(Consumer<CodeElement> consumer) {
170+
public void forEach(Consumer<? super CodeElement> consumer) {
171+
Objects.requireNonNull(consumer);
170172
inflateMetadata();
171173
boolean doLineNumbers = (lineNumbers != null);
172174
generateCatchTargets(consumer);
@@ -329,7 +331,7 @@ private void inflateTypeAnnotations() {
329331
findAttribute(Attributes.runtimeInvisibleTypeAnnotations()).ifPresent(RuntimeInvisibleTypeAnnotationsAttribute::annotations);
330332
}
331333

332-
private void generateCatchTargets(Consumer<CodeElement> consumer) {
334+
private void generateCatchTargets(Consumer<? super CodeElement> consumer) {
333335
// We attach all catch targets to bci zero, because trying to attach them
334336
// to their range could subtly affect the order of exception processing
335337
iterateExceptionHandlers(new ExceptionHandlerAction() {
@@ -343,7 +345,7 @@ public void accept(int s, int e, int h, int c) {
343345
});
344346
}
345347

346-
private void generateDebugElements(Consumer<CodeElement> consumer) {
348+
private void generateDebugElements(Consumer<? super CodeElement> consumer) {
347349
for (Attribute<?> a : attributes()) {
348350
if (a.attributeMapper() == Attributes.characterRangeTable()) {
349351
var attr = (BoundCharacterRangeTableAttribute) a;

src/java.base/share/classes/jdk/internal/classfile/impl/FieldImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@ public void writeTo(DirectClassBuilder builder) {
101101
builder.withField(fieldName(), fieldType(), new Consumer<>() {
102102
@Override
103103
public void accept(FieldBuilder fb) {
104-
FieldImpl.this.forEachElement(fb);
104+
FieldImpl.this.forEach(fb);
105105
}
106106
});
107107
}
108108
}
109109

110110
@Override
111-
public void forEachElement(Consumer<FieldElement> consumer) {
111+
public void forEach(Consumer<? super FieldElement> consumer) {
112112
consumer.accept(flags());
113113
for (Attribute<?> attr : attributes()) {
114114
if (attr instanceof FieldElement e)

src/java.base/share/classes/jdk/internal/classfile/impl/MethodImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public Optional<CodeModel> code() {
122122
}
123123

124124
@Override
125-
public void forEachElement(Consumer<MethodElement> consumer) {
125+
public void forEach(Consumer<? super MethodElement> consumer) {
126126
consumer.accept(flags());
127127
for (Attribute<?> attr : attributes()) {
128128
if (attr instanceof MethodElement e)
@@ -140,7 +140,7 @@ public void writeTo(DirectClassBuilder builder) {
140140
new Consumer<>() {
141141
@Override
142142
public void accept(MethodBuilder mb) {
143-
MethodImpl.this.forEachElement(mb);
143+
MethodImpl.this.forEach(mb);
144144
}
145145
});
146146
}

src/jdk.jartool/share/classes/sun/tools/jar/FingerPrint.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2024, 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
@@ -172,7 +172,7 @@ private static ClassAttributes getClassAttributes(byte[] bytes) {
172172
cm.thisClass().asInternalName(),
173173
cm.superclass().map(ClassEntry::asInternalName).orElse(null),
174174
cm.majorVersion());
175-
cm.forEachElement(attrs);
175+
cm.forEach(attrs);
176176
return attrs;
177177
}
178178

src/jdk.jfr/share/classes/jdk/jfr/internal/EventInstrumentation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ boolean isEnabled() {
222222
// Only supports String, String[] and Boolean values
223223
private static <T> T annotationValue(ClassModel classModel, ClassDesc classDesc, Class<T> type) {
224224
String typeDescriptor = classDesc.descriptorString();
225-
for (ClassElement ce : classModel.elements()) {
225+
for (ClassElement ce : classModel) {
226226
if (ce instanceof RuntimeVisibleAnnotationsAttribute rvaa) {
227227
for (Annotation a : rvaa.annotations()) {
228228
if (a.className().equalsString(typeDescriptor)) {
@@ -260,7 +260,7 @@ private static List<SettingDesc> buildSettingDescs(Class<?> superClass, ClassMod
260260
Set<String> methodSet = new HashSet<>();
261261
List<SettingDesc> settingDescs = new ArrayList<>();
262262
for (MethodModel m : classModel.methods()) {
263-
for (var me : m.elements()) {
263+
for (var me : m) {
264264
if (me instanceof RuntimeVisibleAnnotationsAttribute rvaa) {
265265
for (Annotation a : rvaa.annotations()) {
266266
// We can't really validate the method at this

src/jdk.jlink/share/classes/jdk/tools/jimage/JImageTask.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2024, 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
@@ -39,7 +39,6 @@
3939
import java.util.function.Predicate;
4040
import java.util.stream.Collectors;
4141
import java.util.stream.Stream;
42-
import java.lang.classfile.ClassModel;
4342
import java.lang.classfile.ClassFile;
4443
import java.lang.classfile.CodeModel;
4544
import java.lang.classfile.MethodModel;
@@ -368,9 +367,9 @@ void verify(BasicImageReader reader, String name, ImageLocation location) {
368367
if (name.endsWith(".class") && !name.endsWith("module-info.class")) {
369368
try {
370369
byte[] bytes = reader.getResource(location);
371-
ClassFile.of().parse(bytes).forEachElement(cle -> {
372-
if (cle instanceof MethodModel mm) mm.forEachElement(me -> {
373-
if (me instanceof CodeModel com) com.forEachElement(coe -> {
370+
ClassFile.of().parse(bytes).forEach(cle -> {
371+
if (cle instanceof MethodModel mm) mm.forEach(me -> {
372+
if (me instanceof CodeModel com) com.forEach(coe -> {
374373
//do nothing here, just visit each model element
375374
});
376375
});

test/jdk/java/lang/instrument/asmlib/Instrumentor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void atEnd(MethodBuilder mb) {
133133
}
134134
}));
135135

136-
builder.withMethod(newName, mt, mm.flags().flagsMask(), mm::forEachElement);
136+
builder.withMethod(newName, mt, mm.flags().flagsMask(), mm::forEach);
137137
} else {
138138
builder.accept(element);
139139
}

test/jdk/java/lang/invoke/8022701/MHIllegalAccess.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2024, 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
@@ -63,7 +63,7 @@ public static void main(String[] args) throws Throwable {
6363
*/
6464
ClassTransform changeName = (cb, ce) -> {
6565
if (ce instanceof MethodModel mm && mm.methodName().equalsString("m")) {
66-
cb.withMethod("nemo", mm.methodTypeSymbol(), mm.flags().flagsMask(), mm::forEachElement);
66+
cb.withMethod("nemo", mm.methodTypeSymbol(), mm.flags().flagsMask(), mm::forEach);
6767
} else {
6868
cb.accept(ce);
6969
}

test/jdk/java/lang/invoke/lambda/LambdaAsm.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2024, 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
@@ -100,7 +100,7 @@ static void emitCode() throws IOException {
100100

101101
static void checkMethod(String cname, String mname, ConstantPool cp,
102102
CodeAttribute code) throws IllegalArgumentException {
103-
for (var inst : code.elements()) {
103+
for (var inst : code) {
104104
if (inst instanceof InvokeInstruction inv && (inv.opcode() == Opcode.INVOKESPECIAL
105105
|| inv.opcode() == Opcode.INVOKEINTERFACE)) {
106106
var ref = inv.method();

test/jdk/jdk/classfile/AdaptCodeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ void testSevenOfThirteenIterator() throws Exception {
114114
void testCopy() throws Exception {
115115
var cc = ClassFile.of();
116116
ClassModel cm = cc.parse(testClassPath);
117-
byte[] newBytes = cc.build(cm.thisClass().asSymbol(), cb -> cm.forEachElement(cb));
117+
byte[] newBytes = cc.build(cm.thisClass().asSymbol(), cm::forEach);
118118
// TestUtil.writeClass(newBytes, "TestClass.class");
119119
String result = (String)
120120
new ByteArrayClassLoader(AdaptCodeTest.class.getClassLoader(), testClassName, newBytes)

0 commit comments

Comments
 (0)