From eb490104252d7c1492a871cb125321cffdc5fc39 Mon Sep 17 00:00:00 2001 From: Ross Bamford Date: Fri, 16 Sep 2022 17:44:38 +0100 Subject: [PATCH] Fix literal names in const args --- src/main/kotlin/com/roscopeco/jasm/JasmAssemblingVisitor.kt | 6 +++--- src/test/java/com/roscopeco/jasm/e2e/AssemblerE2ETests.java | 5 ++++- .../java/com/roscopeco/jasm/e2e/DisassemblerE2ETests.java | 1 - .../kotlin/com/roscopeco/jasm/model/LdcAconstAreturn.kt | 1 + .../resources/jasm/com/roscopeco/jasm/LdcAconstAreturn.jasm | 5 +++++ 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/com/roscopeco/jasm/JasmAssemblingVisitor.kt b/src/main/kotlin/com/roscopeco/jasm/JasmAssemblingVisitor.kt index 80c9647..e30720c 100644 --- a/src/main/kotlin/com/roscopeco/jasm/JasmAssemblingVisitor.kt +++ b/src/main/kotlin/com/roscopeco/jasm/JasmAssemblingVisitor.kt @@ -277,7 +277,7 @@ class JasmAssemblingVisitor( } override fun visitLabel(ctx: JasmParser.LabelContext) { - val label = declareLabel(ctx.LABEL()?.text ?: ctx.LITERAL_NAME().text) + val label = declareLabel(ctx.LABEL()?.text ?: LiteralNames.unescape(ctx.LITERAL_NAME().text)) methodVisitor.visitLabel(label.label) } @@ -454,7 +454,7 @@ class JasmAssemblingVisitor( } override fun visitInsn_goto(ctx: JasmParser.Insn_gotoContext) - = methodVisitor.visitJumpInsn(Opcodes.GOTO, getLabel(ctx.NAME()?.text ?: ctx.LITERAL_NAME().text).label) + = methodVisitor.visitJumpInsn(Opcodes.GOTO, getLabel(ctx.NAME()?.text ?: LiteralNames.unescape(ctx.LITERAL_NAME().text)).label) override fun visitInsn_i2b(ctx: JasmParser.Insn_i2bContext) = methodVisitor.visitInsn(Opcodes.I2B) @@ -899,7 +899,7 @@ class JasmAssemblingVisitor( ctx.string_atom() != null -> unescapeConstantString(ctx.string_atom().text) ctx.bool_atom() != null -> if (java.lang.Boolean.parseBoolean(ctx.bool_atom().text)) 1 else 0 ctx.NAME() != null -> Type.getType("L" + ctx.NAME().text + ";") - ctx.LITERAL_NAME() != null -> Type.getType("L" + ctx.LITERAL_NAME().text + ";") + ctx.LITERAL_NAME() != null -> Type.getType("L" + LiteralNames.unescape(ctx.LITERAL_NAME().text) + ";") ctx.QNAME() != null -> Type.getType("L" + ctx.QNAME().text + ";") ctx.method_handle() != null -> buildBootstrapHandle(ctx.method_handle()) ctx.method_descriptor() != null -> Type.getMethodType(typeVisitor.visitMethod_descriptor(ctx.method_descriptor())) diff --git a/src/test/java/com/roscopeco/jasm/e2e/AssemblerE2ETests.java b/src/test/java/com/roscopeco/jasm/e2e/AssemblerE2ETests.java index 084c269..6c9b396 100644 --- a/src/test/java/com/roscopeco/jasm/e2e/AssemblerE2ETests.java +++ b/src/test/java/com/roscopeco/jasm/e2e/AssemblerE2ETests.java @@ -158,7 +158,7 @@ void shouldAssembleLdcAconstNullAreturnToValidJavaClass() { assertThat(clz.getDeclaredFields()).isEmpty(); assertThat(clz.getDeclaredConstructors()).hasSize(1); - assertThat(clz.getDeclaredMethods()).hasSize(11); + assertThat(clz.getDeclaredMethods()).hasSize(12); final var obj = instantiate(clz, LdcAconstAreturn.class); @@ -186,6 +186,9 @@ void shouldAssembleLdcAconstNullAreturnToValidJavaClass() { // Tests LDC(class), ARETURN assertThat(obj.testLdcClass()).isEqualTo(List.class); + // Tests LDC(class), ARETURN + assertThat(obj.testLdcClassWithLiteralName()).isEqualTo(List.class); + // Tests LDC(methodtype), ARETURN assertThat(obj.testLdcMethodType().returnType()).isEqualTo(int.class); assertThat(obj.testLdcMethodType().parameterList()).containsExactly(List.class); diff --git a/src/test/java/com/roscopeco/jasm/e2e/DisassemblerE2ETests.java b/src/test/java/com/roscopeco/jasm/e2e/DisassemblerE2ETests.java index a00037a..6ba2bc3 100644 --- a/src/test/java/com/roscopeco/jasm/e2e/DisassemblerE2ETests.java +++ b/src/test/java/com/roscopeco/jasm/e2e/DisassemblerE2ETests.java @@ -194,7 +194,6 @@ void shouldDisassembleClassWithFields() { @Test void shouldDisassembleInvokeDynamic() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { - System.out.println(disassemble("InvokeDynamicTest")); final var source = disassemble("InvokeDynamicTest"); final var test = doParseString(source); diff --git a/src/test/kotlin/com/roscopeco/jasm/model/LdcAconstAreturn.kt b/src/test/kotlin/com/roscopeco/jasm/model/LdcAconstAreturn.kt index a95bd27..07565e5 100644 --- a/src/test/kotlin/com/roscopeco/jasm/model/LdcAconstAreturn.kt +++ b/src/test/kotlin/com/roscopeco/jasm/model/LdcAconstAreturn.kt @@ -12,6 +12,7 @@ interface LdcAconstAreturn { fun testLdcDouble(): Double fun testLdcBool(): Boolean fun testLdcClass(): Class<*> + fun testLdcClassWithLiteralName(): Class<*> fun testLdcMethodType(): MethodType fun testLdcMethodHandle(): MethodHandle fun testLdcDynamicConst(): String diff --git a/src/test/resources/jasm/com/roscopeco/jasm/LdcAconstAreturn.jasm b/src/test/resources/jasm/com/roscopeco/jasm/LdcAconstAreturn.jasm index 1a91583..3ddaf63 100644 --- a/src/test/resources/jasm/com/roscopeco/jasm/LdcAconstAreturn.jasm +++ b/src/test/resources/jasm/com/roscopeco/jasm/LdcAconstAreturn.jasm @@ -45,6 +45,11 @@ class com/roscopeco/jasm/LdcAconstAreturn implements com/roscopeco/jasm/model/Ld areturn } + public testLdcClassWithLiteralName()java/lang/Class { + ldc `java/util/List` + areturn + } + public testLdcMethodType()java/lang/invoke/MethodType { ldc (java/util/List)I areturn