From 208eafba3245da6e3eed1b22269e13c6f9920544 Mon Sep 17 00:00:00 2001 From: Alexander Kriegisch Date: Sat, 13 Aug 2022 22:46:45 +0200 Subject: [PATCH] Improve condy (constant dynamic) support Relates to #170. Fixes the "Unknown constant type 17" problem. Signed-off-by: Alexander Kriegisch --- .../org/aspectj/apache/bcel/Constants.java | 2 +- .../apache/bcel/classfile/ConstantPool.java | 22 ++++++++++++++++++- .../bcel/verifier/DescendingVisitor.java | 10 ++++++--- .../bcel/verifier/EmptyClassVisitor.java | 8 ++++--- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/bcel-builder/src/main/java/org/aspectj/apache/bcel/Constants.java b/bcel-builder/src/main/java/org/aspectj/apache/bcel/Constants.java index 3c1c12daf..6c2240b9d 100644 --- a/bcel-builder/src/main/java/org/aspectj/apache/bcel/Constants.java +++ b/bcel-builder/src/main/java/org/aspectj/apache/bcel/Constants.java @@ -185,7 +185,7 @@ public interface Constants { String[] CONSTANT_NAMES = { "", "CONSTANT_Utf8", "", "CONSTANT_Integer", "CONSTANT_Float", "CONSTANT_Long", "CONSTANT_Double", "CONSTANT_Class", "CONSTANT_String", "CONSTANT_Fieldref", "CONSTANT_Methodref", - "CONSTANT_InterfaceMethodref", "CONSTANT_NameAndType","","","CONSTANT_MethodHandle","CONSTANT_MethodType","","CONSTANT_InvokeDynamic", + "CONSTANT_InterfaceMethodref", "CONSTANT_NameAndType","","","CONSTANT_MethodHandle","CONSTANT_MethodType","CONSTANT_Dynamic","CONSTANT_InvokeDynamic", // J9: "CONSTANT_Module", "CONSTANT_Package"}; diff --git a/bcel-builder/src/main/java/org/aspectj/apache/bcel/classfile/ConstantPool.java b/bcel-builder/src/main/java/org/aspectj/apache/bcel/classfile/ConstantPool.java index 2066b4569..f8894def4 100644 --- a/bcel-builder/src/main/java/org/aspectj/apache/bcel/classfile/ConstantPool.java +++ b/bcel-builder/src/main/java/org/aspectj/apache/bcel/classfile/ConstantPool.java @@ -212,6 +212,10 @@ public class ConstantPool implements Node { ConstantInvokeDynamic cID = ((ConstantInvokeDynamic)c); return "#"+cID.getBootstrapMethodAttrIndex()+"."+constantToString(cID.getNameAndTypeIndex(), Constants.CONSTANT_NameAndType); + case Constants.CONSTANT_Dynamic: + ConstantDynamic cD = ((ConstantDynamic)c); + return "#"+cD.getBootstrapMethodAttrIndex()+"."+constantToString(cD.getNameAndTypeIndex(), Constants.CONSTANT_NameAndType); + case Constants.CONSTANT_MethodHandle: ConstantMethodHandle cMH = ((ConstantMethodHandle)c); return cMH.getReferenceKind()+":"+constantToString(cMH.getReferenceIndex(),Constants.CONSTANT_Methodref); @@ -326,7 +330,7 @@ public class ConstantPool implements Node { StringBuilder buf = new StringBuilder(); for (int i = 1; i < poolSize; i++) - buf.append(i + ")" + pool[i] + "\n"); + buf.append(i).append(") ").append(pool[i]).append("\n"); return buf.toString(); } @@ -632,6 +636,16 @@ public class ConstantPool implements Node { return addInvokeDynamic(index1,index2); } + case Constants.CONSTANT_Dynamic: { + ConstantDynamic cd = (ConstantDynamic)c; + int index1 = cd.getBootstrapMethodAttrIndex(); + ConstantNameAndType cnat = (ConstantNameAndType)constants[cd.getNameAndTypeIndex()]; + ConstantUtf8 name = (ConstantUtf8) constants[cnat.getNameIndex()]; + ConstantUtf8 signature = (ConstantUtf8) constants[cnat.getSignatureIndex()]; + int index2 = addNameAndType(name.getValue(), signature.getValue()); + return addConstantDynamic(index1,index2); + } + case Constants.CONSTANT_MethodHandle: ConstantMethodHandle cmh = (ConstantMethodHandle)c; return addMethodHandle(cmh.getReferenceKind(),addConstant(constants[cmh.getReferenceIndex()],cp)); @@ -725,6 +739,12 @@ public class ConstantPool implements Node { pool[poolSize++] = new ConstantInvokeDynamic(bootstrapMethodIndex, constantNameAndTypeIndex); return ret; } + public int addConstantDynamic(int bootstrapMethodIndex, int constantNameAndTypeIndex) { + adjustSize(); + int ret = poolSize; + pool[poolSize++] = new ConstantDynamic(bootstrapMethodIndex, constantNameAndTypeIndex); + return ret; + } public int addInterfaceMethodref(String class_name, String method_name, String signature) { int ret = lookupInterfaceMethodref(class_name, method_name, signature); diff --git a/bcel-builder/verifier-src/org/aspectj/apache/bcel/verifier/DescendingVisitor.java b/bcel-builder/verifier-src/org/aspectj/apache/bcel/verifier/DescendingVisitor.java index 3335f1ea8..531af4d05 100644 --- a/bcel-builder/verifier-src/org/aspectj/apache/bcel/verifier/DescendingVisitor.java +++ b/bcel-builder/verifier-src/org/aspectj/apache/bcel/verifier/DescendingVisitor.java @@ -381,13 +381,17 @@ public class DescendingVisitor implements ClassVisitor { } @Override - public void visitConstantInvokeDynamic(ConstantInvokeDynamic obj) { - throw new IllegalStateException("nyi"); + public void visitConstantInvokeDynamic(ConstantInvokeDynamic constant) { + stack.push(constant); + constant.accept(visitor); + stack.pop(); } @Override public void visitConstantDynamic(ConstantDynamic obj) { - throw new IllegalStateException("nyi"); + stack.push(obj); + obj.accept(visitor); + stack.pop(); } @Override diff --git a/bcel-builder/verifier-src/org/aspectj/apache/bcel/verifier/EmptyClassVisitor.java b/bcel-builder/verifier-src/org/aspectj/apache/bcel/verifier/EmptyClassVisitor.java index 1ca901650..0873dee5c 100644 --- a/bcel-builder/verifier-src/org/aspectj/apache/bcel/verifier/EmptyClassVisitor.java +++ b/bcel-builder/verifier-src/org/aspectj/apache/bcel/verifier/EmptyClassVisitor.java @@ -117,7 +117,7 @@ import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisTypeAnnos; * * @see DescendingVisitor * @version $Id: EmptyClassVisitor.java,v 1.3 2009/09/15 19:40:22 aclement Exp $ - * + * */ public class EmptyClassVisitor implements ClassVisitor { protected EmptyClassVisitor() { } @@ -149,6 +149,8 @@ public void visitConstantMethodType(ConstantMethodType obj) {} @Override public void visitConstantInvokeDynamic(ConstantInvokeDynamic obj) {} @Override +public void visitConstantDynamic(ConstantDynamic obj) {} + @Override public void visitConstantNameAndType(ConstantNameAndType obj) {} @Override public void visitConstantPool(ConstantPool obj) {} @@ -214,7 +216,7 @@ public void visitRuntimeInvisibleParameterAnnotations(RuntimeInvisParamAnnos att public void visitAnnotationDefault(AnnotationDefault attribute) {} @Override public void visitLocalVariableTypeTable(LocalVariableTypeTable obj) {} - + // J8: @Override public void visitRuntimeVisibleTypeAnnotations(RuntimeVisTypeAnnos attribute) {} @@ -235,5 +237,5 @@ public void visitModuleMainClass(ModuleMainClass attribute) {} @Override public void visitConstantDynamic(ConstantDynamic attribute) {} @Override public void visitNestHost(NestHost attribute) { } @Override public void visitNestMembers(NestMembers attribute) { } - + } -- 2.39.5