Skip to content

Commit

Permalink
8335642: Hide Transform implementation for Class-File API
Browse files Browse the repository at this point in the history
Reviewed-by: asotona
  • Loading branch information
liach committed Jul 15, 2024
1 parent 2b0adfc commit a253e0f
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
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;

/**
Expand Down Expand Up @@ -71,25 +72,19 @@ default void accept(E e) {
*/
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;
}
}
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 All @@ -24,7 +24,6 @@
*/
package java.lang.classfile;

import java.util.function.Consumer;
import java.util.function.Supplier;

import java.lang.classfile.attribute.RuntimeVisibleAnnotationsAttribute;
Expand Down Expand Up @@ -124,46 +123,4 @@ default void atStart(B builder) {
* @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);
}
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 @@ -171,15 +171,4 @@ static ClassTransform transformingFields(FieldTransform xform) {
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
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;

/**
Expand Down Expand Up @@ -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();
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 @@ -96,15 +96,4 @@ public void atEnd(CodeBuilder builder) {
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));
}
}
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 @@ -111,15 +111,4 @@ static FieldTransform dropping(Predicate<FieldElement> filter) {
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));
}
}
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 @@ -111,17 +111,6 @@ static MethodTransform transformingCode(CodeTransform xform) {
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
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 All @@ -24,6 +24,7 @@
*/
package jdk.internal.classfile.impl;

import java.lang.classfile.constantpool.ConstantPool;
import java.util.Optional;

import java.lang.classfile.Attribute;
Expand All @@ -43,6 +44,10 @@ public SplitConstantPool constantPool() {
return constantPool;
}

public boolean canWriteDirect(ConstantPool source) {
return constantPool().canWriteDirect(source);
}

public Optional<M> original() {
return Optional.ofNullable(original);
}
Expand Down
Loading

1 comment on commit a253e0f

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.