8335905: CompoundElement API cleanup

Reviewed-by: asotona
This commit is contained in:
Chen Liang 2024-07-14 15:01:51 +00:00
parent 6f325db493
commit a9f5e76a65
34 changed files with 73 additions and 82 deletions

View file

@ -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
@ -89,7 +89,7 @@ public sealed interface ClassFileBuilder<E extends ClassFileElement, B extends C
B builder = (B) this;
var resolved = transform.resolve(builder);
resolved.startHandler().run();
model.forEachElement(resolved.consumer());
model.forEach(resolved.consumer());
resolved.endHandler().run();
}
}

View file

@ -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
@ -37,7 +37,7 @@ import jdk.internal.javac.PreviewFeature;
/**
* 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

View file

@ -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
@ -36,8 +36,7 @@ import jdk.internal.javac.PreviewFeature;
/**
* 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
*/

View file

@ -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
@ -40,7 +40,7 @@ import jdk.internal.javac.PreviewFeature;
* 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
@ -55,15 +55,8 @@ 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
@ -71,7 +64,7 @@ public sealed interface CompoundElement<E extends ClassFileElement>
*/
@Override
default Iterator<E> iterator() {
return elements().iterator();
return elementList().iterator();
}
/**
@ -91,7 +84,7 @@ public sealed interface CompoundElement<E extends ClassFileElement>
*/
default List<E> elementList() {
List<E> list = new ArrayList<>();
forEachElement(new Consumer<>() {
forEach(new Consumer<>() {
@Override
public void accept(E e) {
list.add(e);

View file

@ -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
@ -35,7 +35,7 @@ import jdk.internal.javac.PreviewFeature;
/**
* 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

View file

@ -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
@ -35,7 +35,7 @@ import jdk.internal.javac.PreviewFeature;
/**
* 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

View file

@ -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
@ -45,7 +45,7 @@ public abstract sealed class AbstractUnboundModel<E extends ClassFileElement>
}
@Override
public void forEachElement(Consumer<E> consumer) {
public void forEach(Consumer<? super E> consumer) {
elements.forEach(consumer);
}

View file

@ -201,7 +201,7 @@ public final class BufferedCodeBuilder
builder.withCode(new Consumer<>() {
@Override
public void accept(CodeBuilder cb) {
forEachElement(cb);
forEach(cb);
}
});
}

View file

@ -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
@ -200,7 +200,7 @@ public final class BufferedMethodBuilder
builder.withMethod(methodName(), methodType(), methodFlags(), new Consumer<>() {
@Override
public void accept(MethodBuilder mb) {
forEachElement(mb);
forEach(mb);
}
});
}

View file

@ -156,7 +156,7 @@ public final class ClassImpl
// 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>() {

View file

@ -97,12 +97,10 @@ public record ClassRemapperImpl(Function<ClassDesc, ClassDesc> mapFunction) impl
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 ->

View file

@ -27,6 +27,7 @@ package jdk.internal.classfile.impl;
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;
@ -149,7 +150,7 @@ public final class CodeImpl
new Consumer<CodeBuilder>() {
@Override
public void accept(CodeBuilder cb) {
forEachElement(cb);
forEach(cb);
}
},
(SplitConstantPool)buf.constantPool(),
@ -166,7 +167,8 @@ public final class CodeImpl
}
@Override
public void forEachElement(Consumer<CodeElement> consumer) {
public void forEach(Consumer<? super CodeElement> consumer) {
Objects.requireNonNull(consumer);
inflateMetadata();
boolean doLineNumbers = (lineNumbers != null);
generateCatchTargets(consumer);
@ -329,7 +331,7 @@ public final class CodeImpl
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() {
@ -343,7 +345,7 @@ public final class CodeImpl
});
}
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;

View file

@ -101,14 +101,14 @@ public final class FieldImpl
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)

View file

@ -122,7 +122,7 @@ public final class MethodImpl
}
@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)
@ -140,7 +140,7 @@ public final class MethodImpl
new Consumer<>() {
@Override
public void accept(MethodBuilder mb) {
MethodImpl.this.forEachElement(mb);
MethodImpl.this.forEach(mb);
}
});
}