8335939: Hide element writing across the ClassFile API

Reviewed-by: asotona
This commit is contained in:
Chen Liang 2024-07-23 12:11:47 +00:00
parent e83b4b236e
commit a2a236f904
50 changed files with 263 additions and 360 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
@ -51,7 +51,6 @@ import jdk.internal.javac.PreviewFeature;
*/
@PreviewFeature(feature = PreviewFeature.Feature.CLASSFILE_API)
public sealed interface Annotation
extends WritableElement<Annotation>
permits TypeAnnotation, AnnotationImpl {
/**

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
@ -41,7 +41,6 @@ import jdk.internal.javac.PreviewFeature;
*/
@PreviewFeature(feature = PreviewFeature.Feature.CLASSFILE_API)
public sealed interface AnnotationElement
extends WritableElement<AnnotationElement>
permits AnnotationImpl.AnnotationElementImpl {
/**

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
@ -49,7 +49,7 @@ import jdk.internal.javac.PreviewFeature;
* @since 22
*/
@PreviewFeature(feature = PreviewFeature.Feature.CLASSFILE_API)
public sealed interface AnnotationValue extends WritableElement<AnnotationValue>
public sealed interface AnnotationValue
permits AnnotationValue.OfAnnotation, AnnotationValue.OfArray,
AnnotationValue.OfConstant, AnnotationValue.OfClass,
AnnotationValue.OfEnum {

View file

@ -80,7 +80,7 @@ import jdk.internal.javac.PreviewFeature;
*/
@PreviewFeature(feature = PreviewFeature.Feature.CLASSFILE_API)
public sealed interface Attribute<A extends Attribute<A>>
extends WritableElement<A>
extends ClassFileElement
permits AnnotationDefaultAttribute, BootstrapMethodsAttribute,
CharacterRangeTableAttribute, CodeAttribute, CompilationIDAttribute,
ConstantValueAttribute, DeprecatedAttribute, EnclosingMethodAttribute,

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
@ -43,7 +43,6 @@ import jdk.internal.javac.PreviewFeature;
*/
@PreviewFeature(feature = PreviewFeature.Feature.CLASSFILE_API)
public sealed interface BootstrapMethodEntry
extends WritableElement<BootstrapMethodEntry>
permits BootstrapMethodEntryImpl {
/**

View file

@ -24,8 +24,6 @@
*/
package java.lang.classfile;
import java.util.List;
import java.lang.classfile.constantpool.ConstantPool;
import java.lang.classfile.constantpool.ConstantPoolBuilder;
import java.lang.classfile.constantpool.PoolEntry;
@ -110,13 +108,6 @@ public sealed interface BufWriter
*/
void writeBytes(byte[] arr);
/**
* Write the contents of another {@link BufWriter} to the buffer
*
* @param other the other {@linkplain BufWriter}
*/
void writeBytes(BufWriter other);
/**
* Write a range of a byte array to the buffer
*
@ -166,38 +157,8 @@ public sealed interface BufWriter
*/
void writeIndexOrZero(PoolEntry entry);
/**
* Write a list of entities to the buffer. The length of the list is
* written as a {@code u2}, followed by the bytes corresponding to each
* element in the list. Writing of the entities is delegated to the entry.
*
* @param list the entities
* @param <T> the type of entity
*/
<T extends WritableElement<?>> void writeList(List<T> list);
/**
* Write a list of constant pool entry indexes to the buffer. The length
* of the list is written as a {@code u2}, followed by a {@code u2} for each
* entry in the list.
*
* @param list the list of entries
* @throws IllegalArgumentException if any entry has invalid index
*/
void writeListIndices(List<? extends PoolEntry> list);
/**
* {@return the number of bytes that have been written to the buffer}
*/
int size();
/**
* Copy the contents of the buffer into a byte array.
*
* @param array the byte array
* @param bufferOffset the offset into the array at which to write the
* contents of the buffer
* @throws IndexOutOfBoundsException if copying outside of the array bounds
*/
void copyTo(byte[] array, int bufferOffset);
}

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
@ -39,6 +39,6 @@ import jdk.internal.javac.PreviewFeature;
*/
@PreviewFeature(feature = PreviewFeature.Feature.CLASSFILE_API)
public sealed interface ClassFileElement
permits AttributedElement, CompoundElement, WritableElement,
permits AttributedElement, CompoundElement, Attribute,
ClassElement, CodeElement, FieldElement, MethodElement {
}

View file

@ -189,19 +189,4 @@ public sealed interface ClassReader extends ConstantPool
* @param len the length of the range
*/
void copyBytesTo(BufWriter buf, int offset, int len);
/**
* Compare a range of bytes from the classfile to a range of bytes within
* a {@link BufWriter}.
*
* @param bufWriter the {@linkplain BufWriter}
* @param bufWriterOffset the offset within the {@linkplain BufWriter}
* @param classReaderOffset the offset within the classfile
* @param length the length of the range
* @return whether the two ranges were identical
*/
boolean compare(BufWriter bufWriter,
int bufWriterOffset,
int classReaderOffset,
int length);
}

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
@ -59,12 +59,6 @@ public abstract non-sealed class CustomAttribute<T extends CustomAttribute<T>>
return mapper.name();
}
@Override
@SuppressWarnings("unchecked")
public final void writeTo(BufWriter buf) {
mapper.writeAttribute(buf, (T) this);
}
@Override
public String toString() {
return String.format("CustomAttribute[name=%s]", mapper.name());

View file

@ -42,7 +42,7 @@ import jdk.internal.javac.PreviewFeature;
*/
@PreviewFeature(feature = PreviewFeature.Feature.CLASSFILE_API)
public sealed interface FieldModel
extends WritableElement<FieldModel>, CompoundElement<FieldElement>, AttributedElement, ClassElement
extends CompoundElement<FieldElement>, AttributedElement, ClassElement
permits BufferedFieldBuilder.Model, FieldImpl {
/** {@return the access flags} */

View file

@ -42,7 +42,7 @@ import jdk.internal.javac.PreviewFeature;
*/
@PreviewFeature(feature = PreviewFeature.Feature.CLASSFILE_API)
public sealed interface MethodModel
extends WritableElement<MethodModel>, CompoundElement<MethodElement>, AttributedElement, ClassElement
extends CompoundElement<MethodElement>, AttributedElement, ClassElement
permits BufferedMethodBuilder.Model, MethodImpl {
/** {@return the access flags} */

View file

@ -1,53 +0,0 @@
/*
* Copyright (c) 2022, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.lang.classfile;
import java.lang.classfile.constantpool.ConstantPoolBuilder;
import java.lang.classfile.constantpool.PoolEntry;
import jdk.internal.classfile.impl.DirectFieldBuilder;
import jdk.internal.classfile.impl.DirectMethodBuilder;
import jdk.internal.javac.PreviewFeature;
/**
* A classfile element that can encode itself as a stream of bytes in the
* encoding expected by the classfile format.
*
* @param <T> the type of the entity
*
* @sealedGraph
* @since 22
*/
@PreviewFeature(feature = PreviewFeature.Feature.CLASSFILE_API)
public sealed interface WritableElement<T> extends ClassFileElement
permits Annotation, AnnotationElement, AnnotationValue, Attribute,
PoolEntry, BootstrapMethodEntry, FieldModel, MethodModel,
ConstantPoolBuilder, DirectFieldBuilder, DirectMethodBuilder {
/**
* Writes the element to the specified writer
*
* @param buf the writer
*/
void writeTo(BufWriter buf);
}

View file

@ -33,13 +33,11 @@ import java.lang.constant.MethodTypeDesc;
import java.util.List;
import java.lang.classfile.BootstrapMethodEntry;
import java.lang.classfile.BufWriter;
import java.lang.classfile.ClassBuilder;
import java.lang.classfile.ClassModel;
import jdk.internal.classfile.impl.ClassReaderImpl;
import java.lang.constant.ModuleDesc;
import java.lang.constant.PackageDesc;
import java.lang.classfile.WritableElement;
import jdk.internal.classfile.impl.AbstractPoolEntry.ClassEntryImpl;
import jdk.internal.classfile.impl.AbstractPoolEntry.NameAndTypeEntryImpl;
import jdk.internal.classfile.impl.SplitConstantPool;
@ -61,7 +59,7 @@ import static java.util.Objects.requireNonNull;
*/
@PreviewFeature(feature = PreviewFeature.Feature.CLASSFILE_API)
public sealed interface ConstantPoolBuilder
extends ConstantPool, WritableElement<ConstantPool>
extends ConstantPool
permits SplitConstantPool, TemporaryConstantPool {
/**

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
@ -24,7 +24,6 @@
*/
package java.lang.classfile.constantpool;
import java.lang.classfile.WritableElement;
import jdk.internal.javac.PreviewFeature;
/**
@ -34,7 +33,7 @@ import jdk.internal.javac.PreviewFeature;
* @since 22
*/
@PreviewFeature(feature = PreviewFeature.Feature.CLASSFILE_API)
public sealed interface PoolEntry extends WritableElement<PoolEntry>
public sealed interface PoolEntry
permits AnnotationConstantValueEntry, DynamicConstantPoolEntry,
LoadableConstantEntry, MemberRefEntry, ModuleEntry, NameAndTypeEntry,
PackageEntry {

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
@ -24,9 +24,6 @@
*/
package java.lang.classfile.instruction;
import java.lang.constant.ClassDesc;
import java.lang.classfile.BufWriter;
import java.lang.classfile.ClassFile;
import java.lang.classfile.CodeElement;
import java.lang.classfile.CodeModel;
@ -34,6 +31,8 @@ import java.lang.classfile.Label;
import java.lang.classfile.PseudoInstruction;
import java.lang.classfile.attribute.LocalVariableTableAttribute;
import java.lang.classfile.constantpool.Utf8Entry;
import java.lang.constant.ClassDesc;
import jdk.internal.classfile.impl.AbstractPseudoInstruction;
import jdk.internal.classfile.impl.BoundLocalVariable;
import jdk.internal.classfile.impl.TemporaryConstantPool;
@ -84,14 +83,6 @@ public sealed interface LocalVariable extends PseudoInstruction
*/
Label endScope();
/**
* Writes the local variable to the specified writer
*
* @param buf the writer
* @return true if the variable has been written
*/
boolean writeTo(BufWriter buf);
/**
* {@return a local variable pseudo-instruction}
*

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
@ -24,7 +24,6 @@
*/
package java.lang.classfile.instruction;
import java.lang.classfile.BufWriter;
import java.lang.classfile.ClassFile;
import java.lang.classfile.CodeElement;
import java.lang.classfile.CodeModel;
@ -33,6 +32,7 @@ import java.lang.classfile.PseudoInstruction;
import java.lang.classfile.Signature;
import java.lang.classfile.attribute.LocalVariableTypeTableAttribute;
import java.lang.classfile.constantpool.Utf8Entry;
import jdk.internal.classfile.impl.AbstractPseudoInstruction;
import jdk.internal.classfile.impl.BoundLocalVariableType;
import jdk.internal.classfile.impl.TemporaryConstantPool;
@ -81,14 +81,6 @@ public sealed interface LocalVariableType extends PseudoInstruction
*/
Label endScope();
/**
* Writes the local variable to the specified writer
*
* @param buf the writer
* @return true if the variable has been written
*/
boolean writeTo(BufWriter buf);
/**
* {@return a local variable type pseudo-instruction}
*