8335642: Hide Transform implementation for Class-File API

Reviewed-by: asotona
This commit is contained in:
Chen Liang 2024-07-15 12:11:53 +00:00
parent 2b0adfc2de
commit a253e0ff4b
9 changed files with 66 additions and 136 deletions

View file

@ -27,8 +27,9 @@ package java.lang.classfile;
import java.lang.constant.ClassDesc;
import java.util.function.Consumer;
import java.lang.classfile.constantpool.ConstantPool;
import java.lang.classfile.constantpool.ConstantPoolBuilder;
import jdk.internal.classfile.impl.TransformImpl;
import jdk.internal.javac.PreviewFeature;
/**
@ -71,25 +72,19 @@ public sealed interface ClassFileBuilder<E extends ClassFileElement, B extends C
*/
ConstantPoolBuilder constantPool();
/**
* {@return whether the provided constant pool is compatible with this builder}
* @param source the constant pool to test compatibility with
*/
default boolean canWriteDirect(ConstantPool source) {
return constantPool().canWriteDirect(source);
}
/**
* Apply a transform to a model, directing results to this builder.
* @param model the model to transform
* @param transform the transform to apply
* @return this builder
*/
default void transform(CompoundElement<E> model, ClassFileTransform<?, E, B> transform) {
default B transform(CompoundElement<E> model, ClassFileTransform<?, E, B> transform) {
@SuppressWarnings("unchecked")
B builder = (B) this;
var resolved = transform.resolve(builder);
var resolved = TransformImpl.resolve(transform, builder);
resolved.startHandler().run();
model.forEach(resolved.consumer());
resolved.endHandler().run();
return builder;
}
}

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;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.lang.classfile.attribute.RuntimeVisibleAnnotationsAttribute;
@ -124,46 +123,4 @@ public sealed interface ClassFileTransform<
* @return the chained transform
*/
C andThen(C next);
/**
* The result of binding a transform to a builder. Used primarily within
* the implementation to perform transformation.
*
* @param <E> the element type
*
* @since 22
*/
@PreviewFeature(feature = PreviewFeature.Feature.CLASSFILE_API)
interface ResolvedTransform<E extends ClassFileElement> {
/**
* {@return a {@link Consumer} to receive elements}
*/
Consumer<E> consumer();
/**
* {@return an action to call at the end of transformation}
*/
Runnable endHandler();
/**
* {@return an action to call at the start of transformation}
*/
Runnable startHandler();
}
/**
* Bind a transform to a builder. If the transform is chained, intermediate
* builders are created for each chain link. If the transform is stateful
* (see, e.g., {@link ClassTransform#ofStateful(Supplier)}), the supplier is
* invoked to get a fresh transform object.
*
* <p>This method is a low-level method that should rarely be used by
* user code; most of the time, user code should prefer
* {@link ClassFileBuilder#transform(CompoundElement, ClassFileTransform)},
* which resolves the transform and executes it on the current builder.
*
* @param builder the builder to bind to
* @return the bound result
*/
ResolvedTransform<E> resolve(B builder);
}

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
@ -171,15 +171,4 @@ public non-sealed interface ClassTransform
default ClassTransform andThen(ClassTransform t) {
return new TransformImpl.ChainedClassTransform(this, t);
}
/**
* @implSpec The default implementation returns a resolved transform bound
* to the given class builder.
*/
@Override
default ResolvedTransform<ClassElement> resolve(ClassBuilder builder) {
return new TransformImpl.ResolvedTransformImpl<>(e -> accept(builder, e),
() -> atEnd(builder),
() -> atStart(builder));
}
}

View file

@ -87,6 +87,7 @@ import java.lang.classfile.instruction.TypeCheckInstruction;
import static java.util.Objects.requireNonNull;
import static jdk.internal.classfile.impl.BytecodeHelpers.handleDescToHandleInfo;
import jdk.internal.classfile.impl.TransformImpl;
import jdk.internal.javac.PreviewFeature;
/**
@ -190,7 +191,7 @@ public sealed interface CodeBuilder
* @return this builder
*/
default CodeBuilder transforming(CodeTransform transform, Consumer<CodeBuilder> handler) {
var resolved = transform.resolve(this);
var resolved = TransformImpl.resolve(transform, this);
resolved.startHandler().run();
handler.accept(new ChainedCodeBuilder(this, 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
@ -96,15 +96,4 @@ public non-sealed interface CodeTransform
default CodeTransform andThen(CodeTransform t) {
return new TransformImpl.ChainedCodeTransform(this, t);
}
/**
* @implSpec The default implementation returns a resolved transform bound
* to the given code builder.
*/
@Override
default ResolvedTransform<CodeElement> resolve(CodeBuilder builder) {
return new TransformImpl.ResolvedTransformImpl<>(e -> accept(builder, e),
() -> atEnd(builder),
() -> atStart(builder));
}
}

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
@ -111,15 +111,4 @@ public non-sealed interface FieldTransform
default FieldTransform andThen(FieldTransform t) {
return new TransformImpl.ChainedFieldTransform(this, t);
}
/**
* @implSpec The default implementation returns a resolved transform bound
* to the given field builder.
*/
@Override
default ResolvedTransform<FieldElement> resolve(FieldBuilder builder) {
return new TransformImpl.ResolvedTransformImpl<>(e -> accept(builder, e),
() -> atEnd(builder),
() -> atStart(builder));
}
}

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
@ -111,17 +111,6 @@ public non-sealed interface MethodTransform
return new TransformImpl.MethodCodeTransform(xform);
}
/**
* @implSpec The default implementation returns a resolved transform bound
* to the given method builder.
*/
@Override
default ResolvedTransform<MethodElement> resolve(MethodBuilder builder) {
return new TransformImpl.ResolvedTransformImpl<>(e -> accept(builder, e),
() -> atEnd(builder),
() -> atStart(builder));
}
/**
* @implSpec
* The default implementation returns this method transform chained with another