From c08c21ebd2b636d6bc9e3543aaf980f9e4733a04 Mon Sep 17 00:00:00 2001 From: Rich Bolen Date: Tue, 20 Aug 2019 10:52:01 -0400 Subject: Add support for the new Dynamic constant (17) created in java 11 --- src/main/javassist/bytecode/Bytecode.java | 19 ++ src/main/javassist/bytecode/ClassFileWriter.java | 20 ++- src/main/javassist/bytecode/ConstPool.java | 217 ++++++++++++++++++----- 3 files changed, 208 insertions(+), 48 deletions(-) (limited to 'src/main/javassist/bytecode') diff --git a/src/main/javassist/bytecode/Bytecode.java b/src/main/javassist/bytecode/Bytecode.java index 37a6d127..193e3f8a 100644 --- a/src/main/javassist/bytecode/Bytecode.java +++ b/src/main/javassist/bytecode/Bytecode.java @@ -1240,6 +1240,25 @@ public class Bytecode extends ByteVector implements Cloneable, Opcode { growStack(Descriptor.dataSize(desc)); // assume ConstPool#REF_invokeStatic } + /** + * Appends DYNAMIC. + * + * @param bootstrap an index into the bootstrap_methods array + * of the bootstrap method table. + * @param name the method name. + * @param desc the method descriptor. + * @see Descriptor#ofMethod(CtClass,CtClass[]) + * @since 3.17 + */ + public void addDynamic(int bootstrap, String name, String desc) { + int nt = constPool.addNameAndTypeInfo(name, desc); + int dyn = constPool.addDynamicInfo(bootstrap, nt); + add(INVOKEDYNAMIC); + addIndex(dyn); + add(0, 0); + growStack(Descriptor.dataSize(desc)); // assume ConstPool#REF_invokeStatic + } + /** * Appends LDC or LDC_W. The pushed item is a String * object. diff --git a/src/main/javassist/bytecode/ClassFileWriter.java b/src/main/javassist/bytecode/ClassFileWriter.java index 931ffcb4..03650b75 100644 --- a/src/main/javassist/bytecode/ClassFileWriter.java +++ b/src/main/javassist/bytecode/ClassFileWriter.java @@ -693,13 +693,31 @@ public class ClassFileWriter { * @since 3.17.1 */ public int addInvokeDynamicInfo(int bootstrap, - int nameAndTypeInfo) { + int nameAndTypeInfo) { output.write(InvokeDynamicInfo.tag); output.writeShort(bootstrap); output.writeShort(nameAndTypeInfo); return num++; } + /** + * Adds a new CONSTANT_InvokeDynamic_info + * structure. + * + * @param bootstrap bootstrap_method_attr_index. + * @param nameAndTypeInfo name_and_type_index. + * @return the index of the added entry. + * + * @since 3.17.1 + */ + public int addDynamicInfo(int bootstrap, + int nameAndTypeInfo) { + output.write(DynamicInfo.tag); + output.writeShort(bootstrap); + output.writeShort(nameAndTypeInfo); + return num++; + } + /** * Adds a new CONSTANT_String_info * structure. diff --git a/src/main/javassist/bytecode/ConstPool.java b/src/main/javassist/bytecode/ConstPool.java index 0447ece7..08985089 100644 --- a/src/main/javassist/bytecode/ConstPool.java +++ b/src/main/javassist/bytecode/ConstPool.java @@ -716,53 +716,99 @@ public final class ConstPool return mtinfo.descriptor; } - /** - * Reads the bootstrap_method_attr_index field of the - * CONSTANT_InvokeDynamic_info structure - * at the given index. - * - * @since 3.17 - */ - public int getInvokeDynamicBootstrap(int index) - { - InvokeDynamicInfo iv = (InvokeDynamicInfo)getItem(index); - return iv.bootstrap; - } - - /** - * Reads the name_and_type_index field of the - * CONSTANT_InvokeDynamic_info structure - * at the given index. - * - * @since 3.17 - */ - public int getInvokeDynamicNameAndType(int index) - { - InvokeDynamicInfo iv = (InvokeDynamicInfo)getItem(index); - return iv.nameAndType; - } - - /** - * Reads the descriptor_index field of the - * CONSTANT_NameAndType_info structure - * indirectly specified by the given index. - * - * @param index an index to a CONSTANT_InvokeDynamic_info. - * @return the descriptor of the method. - * @since 3.17 - */ - public String getInvokeDynamicType(int index) - { - InvokeDynamicInfo iv = (InvokeDynamicInfo)getItem(index); - if (iv == null) - return null; - NameAndTypeInfo n = (NameAndTypeInfo)getItem(iv.nameAndType); - if(n == null) - return null; - return getUtf8Info(n.typeDescriptor); - } - - /** + /** + * Reads the bootstrap_method_attr_index field of the + * CONSTANT_InvokeDynamic_info structure + * at the given index. + * + * @since 3.17 + */ + public int getInvokeDynamicBootstrap(int index) + { + InvokeDynamicInfo iv = (InvokeDynamicInfo)getItem(index); + return iv.bootstrap; + } + + /** + * Reads the name_and_type_index field of the + * CONSTANT_InvokeDynamic_info structure + * at the given index. + * + * @since 3.17 + */ + public int getInvokeDynamicNameAndType(int index) + { + InvokeDynamicInfo iv = (InvokeDynamicInfo)getItem(index); + return iv.nameAndType; + } + + /** + * Reads the descriptor_index field of the + * CONSTANT_NameAndType_info structure + * indirectly specified by the given index. + * + * @param index an index to a CONSTANT_InvokeDynamic_info. + * @return the descriptor of the method. + * @since 3.17 + */ + public String getInvokeDynamicType(int index) + { + InvokeDynamicInfo iv = (InvokeDynamicInfo)getItem(index); + if (iv == null) + return null; + NameAndTypeInfo n = (NameAndTypeInfo)getItem(iv.nameAndType); + if(n == null) + return null; + return getUtf8Info(n.typeDescriptor); + } + + /** + * Reads the bootstrap_method_attr_index field of the + * CONSTANT_Dynamic_info structure + * at the given index. + * + * @since 3.17 + */ + public int getDynamicBootstrap(int index) + { + DynamicInfo iv = (DynamicInfo)getItem(index); + return iv.bootstrap; + } + + /** + * Reads the name_and_type_index field of the + * CONSTANT_Dynamic_info structure + * at the given index. + * + * @since 3.17 + */ + public int getDynamicNameAndType(int index) + { + DynamicInfo iv = (DynamicInfo)getItem(index); + return iv.nameAndType; + } + + /** + * Reads the descriptor_index field of the + * CONSTANT_NameAndType_info structure + * indirectly specified by the given index. + * + * @param index an index to a CONSTANT_Dynamic_info. + * @return the descriptor of the method. + * @since 3.17 + */ + public String getDynamicType(int index) + { + DynamicInfo iv = (DynamicInfo)getItem(index); + if (iv == null) + return null; + NameAndTypeInfo n = (NameAndTypeInfo)getItem(iv.nameAndType); + if(n == null) + return null; + return getUtf8Info(n.typeDescriptor); + } + + /** * Reads the name_index field of the * CONSTANT_Module_info structure at the given index. * @@ -1196,6 +1242,18 @@ public final class ConstPool } /** + * Adds a new CONSTANT_InvokeDynamic_info structure. + * + * @param bootstrap bootstrap_method_attr_index. + * @param nameAndType name_and_type_index. + * @return the index of the added entry. + * @since 3.17 + */ + public int addDynamicInfo(int bootstrap, int nameAndType) { + return addItem(new DynamicInfo(bootstrap, nameAndType, numOfItems)); + } + + /** * Adds a new CONSTANT_Module_info * @param nameIndex the index of the Utf8 entry. * @return the index of the added entry. @@ -1342,6 +1400,9 @@ public final class ConstPool case MethodTypeInfo.tag : // 16 info = new MethodTypeInfo(in, numOfItems); break; + case DynamicInfo.tag : // 17 + info = new DynamicInfo(in, numOfItems); + break; case InvokeDynamicInfo.tag : // 18 info = new InvokeDynamicInfo(in, numOfItems); break; @@ -2322,6 +2383,68 @@ class InvokeDynamicInfo extends ConstInfo } } +class DynamicInfo extends ConstInfo { + + static final int tag = 17; + int bootstrap, nameAndType; + + public DynamicInfo(int bootstrapMethod, + int ntIndex, int index) { + super(index); + bootstrap = bootstrapMethod; + nameAndType = ntIndex; + } + + public DynamicInfo(DataInputStream in, int index) + throws IOException { + super(index); + bootstrap = in.readUnsignedShort(); + nameAndType = in.readUnsignedShort(); + } + + @Override + public int hashCode() { + return (bootstrap << 16) ^ nameAndType; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof DynamicInfo) { + DynamicInfo iv = (DynamicInfo) obj; + return iv.bootstrap == bootstrap + && iv.nameAndType == nameAndType; + } + return false; + } + + @Override + public int getTag() { + return tag; + } + + @Override + public int copy(ConstPool src, ConstPool dest, + Map map) { + return dest.addDynamicInfo(bootstrap, + src.getItem(nameAndType).copy(src, dest, map)); + } + + @Override + public void write(DataOutputStream out) throws IOException { + out.writeByte(tag); + out.writeShort(bootstrap); + out.writeShort(nameAndType); + } + + @Override + public void print(PrintWriter out) { + out.print("Dynamic #"); + out.print(bootstrap); + out.print(", name&type #"); + out.println(nameAndType); + } +} + class ModuleInfo extends ConstInfo { static final int tag = 19; -- cgit v1.2.3