From 68d1f5c33bf3f64f44f8a10c2f9e4007cfd07d2b Mon Sep 17 00:00:00 2001 From: Chen Liang Date: Tue, 20 Aug 2024 05:43:04 +0000 Subject: [PATCH] 8338543: ClassBuilder withMethod builders should cache the method type symbol Reviewed-by: asotona --- .../classes/java/lang/classfile/ClassBuilder.java | 5 +---- .../internal/classfile/impl/ChainedClassBuilder.java | 10 ++++++++++ .../internal/classfile/impl/DirectClassBuilder.java | 8 ++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/java.base/share/classes/java/lang/classfile/ClassBuilder.java b/src/java.base/share/classes/java/lang/classfile/ClassBuilder.java index 7ad6e94df3f..98371c9e15a 100644 --- a/src/java.base/share/classes/java/lang/classfile/ClassBuilder.java +++ b/src/java.base/share/classes/java/lang/classfile/ClassBuilder.java @@ -276,10 +276,7 @@ public sealed interface ClassBuilder MethodTypeDesc descriptor, int methodFlags, Consumer handler) { - return withMethodBody(constantPool().utf8Entry(name), - constantPool().utf8Entry(descriptor), - methodFlags, - handler); + return withMethod(name, descriptor, methodFlags, mb -> mb.withCode(handler)); } /** diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/ChainedClassBuilder.java b/src/java.base/share/classes/jdk/internal/classfile/impl/ChainedClassBuilder.java index d31debf6f35..50c1590e8a2 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/ChainedClassBuilder.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/ChainedClassBuilder.java @@ -24,6 +24,7 @@ */ package jdk.internal.classfile.impl; +import java.lang.constant.MethodTypeDesc; import java.util.function.Consumer; import java.lang.classfile.*; @@ -78,6 +79,15 @@ public final class ChainedClassBuilder return this; } + @Override + public ClassBuilder withMethod(String name, MethodTypeDesc descriptor, int flags, Consumer handler) { + var mb = new BufferedMethodBuilder(terminal.constantPool, terminal.context, + constantPool().utf8Entry(name), constantPool().utf8Entry(descriptor), flags, null); + mb.mDesc = descriptor; + consumer.accept(mb.run(handler).toModel()); + return this; + } + @Override public ClassBuilder transformMethod(MethodModel method, MethodTransform transform) { BufferedMethodBuilder builder = new BufferedMethodBuilder(terminal.constantPool, terminal.context, diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/DirectClassBuilder.java b/src/java.base/share/classes/jdk/internal/classfile/impl/DirectClassBuilder.java index 21fde0f6002..0d61895fe9f 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/DirectClassBuilder.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/DirectClassBuilder.java @@ -26,6 +26,7 @@ package jdk.internal.classfile.impl; import java.lang.constant.ConstantDescs; +import java.lang.constant.MethodTypeDesc; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -106,6 +107,13 @@ public final class DirectClassBuilder .run(handler)); } + @Override + public ClassBuilder withMethod(String name, MethodTypeDesc descriptor, int flags, Consumer handler) { + var method = new DirectMethodBuilder(constantPool, context, constantPool.utf8Entry(name), constantPool.utf8Entry(descriptor), flags, null); + method.mDesc = descriptor; + return withMethod(method.run(handler)); + } + @Override public ClassBuilder transformMethod(MethodModel method, MethodTransform transform) { DirectMethodBuilder builder = new DirectMethodBuilder(constantPool, context, method.methodName(),