Skip to content

Commit

Permalink
8335905: CompoundElement API cleanup
Browse files Browse the repository at this point in the history
Reviewed-by: asotona
  • Loading branch information
liach authored and pull[bot] committed Aug 20, 2024
1 parent 3de0801 commit 8340183
Show file tree
Hide file tree
Showing 34 changed files with 73 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -89,7 +89,7 @@ default void transform(CompoundElement<E> model, ClassFileTransform<?, E, B> tra
B builder = (B) this;
var resolved = transform.resolve(builder);
resolved.startHandler().run();
model.forEachElement(resolved.consumer());
model.forEach(resolved.consumer());
resolved.endHandler().run();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -37,7 +37,7 @@

/**
* Models a classfile. The contents of the classfile can be traversed via
* a streaming view (e.g., {@link #elements()}), or via random access (e.g.,
* a streaming view, or via random access (e.g.,
* {@link #flags()}), or by freely mixing the two.
*
* @since 22
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -36,8 +36,7 @@

/**
* Models the body of a method (the {@code Code} attribute). The instructions
* of the method body are accessed via a streaming view (e.g., {@link
* #elements()}).
* of the method body are accessed via a streaming view.
*
* @since 22
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -40,7 +40,7 @@
* class. When encountering a {@linkplain CompoundElement}, clients have the
* option to treat the element as a single entity (e.g., an entire method)
* or to traverse the contents of that element with the methods in this class
* (e.g., {@link #elements()}, {@link #forEachElement(Consumer)}, etc.)
* (e.g., {@link #forEach(Consumer)}, etc.)
* @param <E> the element type
*
* @sealedGraph
Expand All @@ -55,23 +55,16 @@ public sealed interface CompoundElement<E extends ClassFileElement>
* compound element
* @param consumer the handler
*/
void forEachElement(Consumer<E> consumer);

/**
* {@return an {@link Iterable} describing all the elements contained in this
* compound element}
*/
default Iterable<E> elements() {
return elementList();
}
@Override
void forEach(Consumer<? super E> consumer);

/**
* {@return an {@link Iterator} describing all the elements contained in this
* compound element}
*/
@Override
default Iterator<E> iterator() {
return elements().iterator();
return elementList().iterator();
}

/**
Expand All @@ -91,7 +84,7 @@ default Stream<E> elementStream() {
*/
default List<E> elementList() {
List<E> list = new ArrayList<>();
forEachElement(new Consumer<>() {
forEach(new Consumer<>() {
@Override
public void accept(E e) {
list.add(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -35,7 +35,7 @@

/**
* Models a field. The contents of the field can be traversed via
* a streaming view (e.g., {@link #elements()}), or via random access (e.g.,
* a streaming view, or via random access (e.g.,
* {@link #flags()}), or by freely mixing the two.
*
* @since 22
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -35,7 +35,7 @@

/**
* Models a method. The contents of the method can be traversed via
* a streaming view (e.g., {@link #elements()}), or via random access (e.g.,
* a streaming view, or via random access (e.g.,
* {@link #flags()}), or by freely mixing the two.
*
* @since 22
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -45,7 +45,7 @@ public AbstractUnboundModel(List<E> elements) {
}

@Override
public void forEachElement(Consumer<E> consumer) {
public void forEach(Consumer<? super E> consumer) {
elements.forEach(consumer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public void writeTo(DirectMethodBuilder builder) {
builder.withCode(new Consumer<>() {
@Override
public void accept(CodeBuilder cb) {
forEachElement(cb);
forEach(cb);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -200,7 +200,7 @@ public void writeTo(DirectClassBuilder builder) {
builder.withMethod(methodName(), methodType(), methodFlags(), new Consumer<>() {
@Override
public void accept(MethodBuilder mb) {
forEachElement(mb);
forEach(mb);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public List<Attribute<?>> attributes() {
// ClassModel

@Override
public void forEachElement(Consumer<ClassElement> consumer) {
public void forEach(Consumer<? super ClassElement> consumer) {
consumer.accept(flags());
consumer.accept(ClassFileVersion.of(majorVersion(), minorVersion()));
superclass().ifPresent(new Consumer<ClassEntry>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,10 @@ public void accept(ClassBuilder clb, ClassElement cle) {
switch (cle) {
case FieldModel fm ->
clb.withField(fm.fieldName().stringValue(), map(
fm.fieldTypeSymbol()), fb ->
fm.forEachElement(asFieldTransform().resolve(fb).consumer()));
fm.fieldTypeSymbol()), fb -> fb.transform(fm, asFieldTransform()));
case MethodModel mm ->
clb.withMethod(mm.methodName().stringValue(), mapMethodDesc(
mm.methodTypeSymbol()), mm.flags().flagsMask(), mb ->
mm.forEachElement(asMethodTransform().resolve(mb).consumer()));
mm.methodTypeSymbol()), mm.flags().flagsMask(), mb -> mb.transform(mm, asMethodTransform()));
case Superclass sc ->
clb.withSuperclass(map(sc.superclassEntry().asSymbol()));
case Interfaces ins ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;

Expand Down Expand Up @@ -149,7 +150,7 @@ public void writeTo(BufWriter buf) {
new Consumer<CodeBuilder>() {
@Override
public void accept(CodeBuilder cb) {
forEachElement(cb);
forEach(cb);
}
},
(SplitConstantPool)buf.constantPool(),
Expand All @@ -166,7 +167,8 @@ public Optional<MethodModel> parent() {
}

@Override
public void forEachElement(Consumer<CodeElement> consumer) {
public void forEach(Consumer<? super CodeElement> consumer) {
Objects.requireNonNull(consumer);
inflateMetadata();
boolean doLineNumbers = (lineNumbers != null);
generateCatchTargets(consumer);
Expand Down Expand Up @@ -329,7 +331,7 @@ private void inflateTypeAnnotations() {
findAttribute(Attributes.runtimeInvisibleTypeAnnotations()).ifPresent(RuntimeInvisibleTypeAnnotationsAttribute::annotations);
}

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

private void generateDebugElements(Consumer<CodeElement> consumer) {
private void generateDebugElements(Consumer<? super CodeElement> consumer) {
for (Attribute<?> a : attributes()) {
if (a.attributeMapper() == Attributes.characterRangeTable()) {
var attr = (BoundCharacterRangeTableAttribute) a;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ public void writeTo(DirectClassBuilder builder) {
builder.withField(fieldName(), fieldType(), new Consumer<>() {
@Override
public void accept(FieldBuilder fb) {
FieldImpl.this.forEachElement(fb);
FieldImpl.this.forEach(fb);
}
});
}
}

@Override
public void forEachElement(Consumer<FieldElement> consumer) {
public void forEach(Consumer<? super FieldElement> consumer) {
consumer.accept(flags());
for (Attribute<?> attr : attributes()) {
if (attr instanceof FieldElement e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public Optional<CodeModel> code() {
}

@Override
public void forEachElement(Consumer<MethodElement> consumer) {
public void forEach(Consumer<? super MethodElement> consumer) {
consumer.accept(flags());
for (Attribute<?> attr : attributes()) {
if (attr instanceof MethodElement e)
Expand All @@ -140,7 +140,7 @@ public void writeTo(DirectClassBuilder builder) {
new Consumer<>() {
@Override
public void accept(MethodBuilder mb) {
MethodImpl.this.forEachElement(mb);
MethodImpl.this.forEach(mb);
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/jdk.jartool/share/classes/sun/tools/jar/FingerPrint.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -172,7 +172,7 @@ private static ClassAttributes getClassAttributes(byte[] bytes) {
cm.thisClass().asInternalName(),
cm.superclass().map(ClassEntry::asInternalName).orElse(null),
cm.majorVersion());
cm.forEachElement(attrs);
cm.forEach(attrs);
return attrs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ boolean isEnabled() {
// Only supports String, String[] and Boolean values
private static <T> T annotationValue(ClassModel classModel, ClassDesc classDesc, Class<T> type) {
String typeDescriptor = classDesc.descriptorString();
for (ClassElement ce : classModel.elements()) {
for (ClassElement ce : classModel) {
if (ce instanceof RuntimeVisibleAnnotationsAttribute rvaa) {
for (Annotation a : rvaa.annotations()) {
if (a.className().equalsString(typeDescriptor)) {
Expand Down Expand Up @@ -260,7 +260,7 @@ private static List<SettingDesc> buildSettingDescs(Class<?> superClass, ClassMod
Set<String> methodSet = new HashSet<>();
List<SettingDesc> settingDescs = new ArrayList<>();
for (MethodModel m : classModel.methods()) {
for (var me : m.elements()) {
for (var me : m) {
if (me instanceof RuntimeVisibleAnnotationsAttribute rvaa) {
for (Annotation a : rvaa.annotations()) {
// We can't really validate the method at this
Expand Down
9 changes: 4 additions & 5 deletions src/jdk.jlink/share/classes/jdk/tools/jimage/JImageTask.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -39,7 +39,6 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.lang.classfile.ClassModel;
import java.lang.classfile.ClassFile;
import java.lang.classfile.CodeModel;
import java.lang.classfile.MethodModel;
Expand Down Expand Up @@ -368,9 +367,9 @@ void verify(BasicImageReader reader, String name, ImageLocation location) {
if (name.endsWith(".class") && !name.endsWith("module-info.class")) {
try {
byte[] bytes = reader.getResource(location);
ClassFile.of().parse(bytes).forEachElement(cle -> {
if (cle instanceof MethodModel mm) mm.forEachElement(me -> {
if (me instanceof CodeModel com) com.forEachElement(coe -> {
ClassFile.of().parse(bytes).forEach(cle -> {
if (cle instanceof MethodModel mm) mm.forEach(me -> {
if (me instanceof CodeModel com) com.forEach(coe -> {
//do nothing here, just visit each model element
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/jdk/java/lang/instrument/asmlib/Instrumentor.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void atEnd(MethodBuilder mb) {
}
}));

builder.withMethod(newName, mt, mm.flags().flagsMask(), mm::forEachElement);
builder.withMethod(newName, mt, mm.flags().flagsMask(), mm::forEach);
} else {
builder.accept(element);
}
Expand Down
4 changes: 2 additions & 2 deletions test/jdk/java/lang/invoke/8022701/MHIllegalAccess.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -63,7 +63,7 @@ public static void main(String[] args) throws Throwable {
*/
ClassTransform changeName = (cb, ce) -> {
if (ce instanceof MethodModel mm && mm.methodName().equalsString("m")) {
cb.withMethod("nemo", mm.methodTypeSymbol(), mm.flags().flagsMask(), mm::forEachElement);
cb.withMethod("nemo", mm.methodTypeSymbol(), mm.flags().flagsMask(), mm::forEach);
} else {
cb.accept(ce);
}
Expand Down
4 changes: 2 additions & 2 deletions test/jdk/java/lang/invoke/lambda/LambdaAsm.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -100,7 +100,7 @@ static void emitCode() throws IOException {

static void checkMethod(String cname, String mname, ConstantPool cp,
CodeAttribute code) throws IllegalArgumentException {
for (var inst : code.elements()) {
for (var inst : code) {
if (inst instanceof InvokeInstruction inv && (inv.opcode() == Opcode.INVOKESPECIAL
|| inv.opcode() == Opcode.INVOKEINTERFACE)) {
var ref = inv.method();
Expand Down
2 changes: 1 addition & 1 deletion test/jdk/jdk/classfile/AdaptCodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void testSevenOfThirteenIterator() throws Exception {
void testCopy() throws Exception {
var cc = ClassFile.of();
ClassModel cm = cc.parse(testClassPath);
byte[] newBytes = cc.build(cm.thisClass().asSymbol(), cb -> cm.forEachElement(cb));
byte[] newBytes = cc.build(cm.thisClass().asSymbol(), cm::forEach);
// TestUtil.writeClass(newBytes, "TestClass.class");
String result = (String)
new ByteArrayClassLoader(AdaptCodeTest.class.getClassLoader(), testClassName, newBytes)
Expand Down
Loading

0 comments on commit 8340183

Please sign in to comment.