From 838527a4925b4e0c44cadd90e0fcbacd913308e8 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Tue, 1 Dec 2026 04:19:48 +0200 Subject: [PATCH 1/2] Rewrite JDK API calls in dependencies of the staged jar (#6363) The bytecode-compliance goal applies the String regex and wrapper rewrites only to the module's own classes; dependency jars were merged into the jar-with-dependencies unchanged. A library calling String.replaceAll thus reached the iOS translator as virtual_java_lang_String_replaceAll, which vm/JavaAPI never defines: a green build and an Xcode compile error. AppBuilder now applies the same rules to every class of the staged jar, including a reused one. The rewrite is idempotent. Co-Authored-By: Claude Opus 5.5 --- .../java/com/codename1/maven/AppBuilder.java | 9 +++ .../codename1/maven/BytecodeCompliance.java | 58 +++++++++++++++++++ .../maven/BytecodeComplianceTest.java | 44 ++++++++++++++ 3 files changed, 111 insertions(+) diff --git a/maven/build-engine/src/main/java/com/codename1/maven/AppBuilder.java b/maven/build-engine/src/main/java/com/codename1/maven/AppBuilder.java index 58617f77660..33a76ef3891 100644 --- a/maven/build-engine/src/main/java/com/codename1/maven/AppBuilder.java +++ b/maven/build-engine/src/main/java/com/codename1/maven/AppBuilder.java @@ -2074,6 +2074,15 @@ private void createAntProject() throws IOException, LibraryPropertiesException, writeStringToFile(stagedInputsFile, stagedInputs); } + // Dependencies are merged as they are; the bytecode-compliance goal + // rewrote only this module's classes. See rewriteInvocationsInJar. Run + // on a reused jar too, which a plugin without this step may have + // staged; on an already rewritten jar it reads and writes nothing. + int rewritten = BytecodeCompliance.rewriteInvocationsInJar(jarWithDependencies); + if (rewritten > 0) { + getLog().info("Rewrote " + rewritten + " JDK API callsite(s) in dependencies of " + jarWithDependencies.getName()); + } + verifyApplicationClassClosure(jarWithDependencies, cpElements); if (stageOnly) { diff --git a/maven/build-engine/src/main/java/com/codename1/maven/BytecodeCompliance.java b/maven/build-engine/src/main/java/com/codename1/maven/BytecodeCompliance.java index a7f694e9938..c4b9378c5b3 100644 --- a/maven/build-engine/src/main/java/com/codename1/maven/BytecodeCompliance.java +++ b/maven/build-engine/src/main/java/com/codename1/maven/BytecodeCompliance.java @@ -1006,6 +1006,64 @@ static byte[] rewriteInvocations(byte[] classBytes) { return rewriteClassInvocations(classBytes, INVOCATION_REWRITE_RULES).bytes; } + /// Applies the invocation rewrites to every class in `jar`, in place, and + /// answers how many callsites were redirected. + /// + /// The goal itself rewrites only the module's own output directory. A + /// dependency jar is merged into the staged jar as it is, so a library + /// calling `String.replaceAll` reached the iOS translator unchanged and + /// became a call to `virtual_java_lang_String_replaceAll...`, a function + /// vm/JavaAPI never defines: a green build and an Xcode compile error + /// (issue #6363). The staged jar is everything that ships, so rewriting it + /// covers dependencies and cn1libs built before a rule existed alike. The + /// rewrite is idempotent; classes the goal already rewrote are unchanged. + public static int rewriteInvocationsInJar(File jar) throws IOException { + File rewritten = new File(jar.getPath() + ".rewrite.tmp"); + int callsites = 0; + java.util.zip.ZipFile in = new java.util.zip.ZipFile(jar); + try { + java.util.zip.ZipOutputStream out = new java.util.zip.ZipOutputStream( + new java.io.BufferedOutputStream(new java.io.FileOutputStream(rewritten))); + try { + java.util.Enumeration entries = in.entries(); + while (entries.hasMoreElements()) { + java.util.zip.ZipEntry entry = entries.nextElement(); + InputStream entryIn = in.getInputStream(entry); + byte[] bytes; + try { + bytes = org.apache.commons.io.IOUtils.toByteArray(entryIn); + } finally { + entryIn.close(); + } + if (!entry.isDirectory() && entry.getName().endsWith(".class") + && !entry.getName().startsWith("META-INF/")) { + InvocationRewriteResult result = rewriteClassInvocations(bytes, INVOCATION_REWRITE_RULES); + if (result.rewrittenCallsites > 0) { + bytes = result.bytes; + callsites += result.rewrittenCallsites; + } + } + java.util.zip.ZipEntry copy = new java.util.zip.ZipEntry(entry.getName()); + copy.setTime(entry.getTime()); + out.putNextEntry(copy); + out.write(bytes); + out.closeEntry(); + } + } finally { + out.close(); + } + } finally { + in.close(); + } + if (callsites == 0) { + FileUtils.deleteQuietly(rewritten); + return 0; + } + FileUtils.deleteQuietly(jar); + FileUtils.moveFile(rewritten, jar); + return callsites; + } + private static InvocationRewriteResult rewriteClassInvocations(byte[] classBytes, final Map rules) { final InvocationRewriteResult result = new InvocationRewriteResult(); diff --git a/maven/build-engine/src/test/java/com/codename1/maven/BytecodeComplianceTest.java b/maven/build-engine/src/test/java/com/codename1/maven/BytecodeComplianceTest.java index a45f1edd32e..0c66533f2f1 100644 --- a/maven/build-engine/src/test/java/com/codename1/maven/BytecodeComplianceTest.java +++ b/maven/build-engine/src/test/java/com/codename1/maven/BytecodeComplianceTest.java @@ -181,6 +181,50 @@ void backendMovesRegularExpressionsOntoTheSharedEngine(@TempDir Path dir) throws org.junit.jupiter.api.Assertions.assertTrue(rewritten.contains(r + "StringRegex")); } + /// Issue #6363: a dependency jar calling String.replaceAll was merged into + /// the staged jar unrewritten and reached the iOS translator as a call to a + /// method vm/JavaAPI does not have. + @Test + public void stagedJarDependencyRegexCallsAreRewritten(@TempDir Path dir) throws Exception { + ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); + writer.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "dep/Squash", null, "java/lang/Object", null); + MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "sq", + "(Ljava/lang/String;)Ljava/lang/String;", null, null); + mv.visitCode(); + mv.visitVarInsn(Opcodes.ALOAD, 0); + mv.visitLdcInsn(" +"); + mv.visitLdcInsn(" "); + mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "replaceAll", + "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", false); + mv.visitInsn(Opcodes.ARETURN); + mv.visitMaxs(0, 0); + mv.visitEnd(); + writer.visitEnd(); + File jar = dir.resolve("staged.jar").toFile(); + JarOutputStream out = new JarOutputStream(Files.newOutputStream(jar.toPath())); + out.putNextEntry(new JarEntry("dep/Squash.class")); + out.write(writer.toByteArray()); + out.closeEntry(); + out.putNextEntry(new JarEntry("res.txt")); + out.write("kept".getBytes("UTF-8")); + out.closeEntry(); + out.close(); + + assertEquals(1, BytecodeCompliance.rewriteInvocationsInJar(jar)); + java.util.zip.ZipFile zip = new java.util.zip.ZipFile(jar); + try { + String cls = new String(org.apache.commons.io.IOUtils.toByteArray( + zip.getInputStream(zip.getEntry("dep/Squash.class"))), "ISO-8859-1"); + assertTrue(cls.contains("com/codename1/util/regex/StringRegex")); + assertEquals("kept", new String(org.apache.commons.io.IOUtils.toByteArray( + zip.getInputStream(zip.getEntry("res.txt"))), "UTF-8")); + } finally { + zip.close(); + } + // Idempotent: a second pass finds nothing to do. + assertEquals(0, BytecodeCompliance.rewriteInvocationsInJar(jar)); + } + /// A class whose methods are native: present for the compliance index, /// nothing to run. A method spec is `[static ]name descriptor`. private void writeStub(Path root, String className, String... methods) throws Exception { From cd0f1afe74298e5138aa6f45c2a0d8eaeec44e64 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Tue, 1 Dec 2026 07:12:07 +0200 Subject: [PATCH 2/2] Keep duplicate entries when rewriting the staged jar Ant's zip task merges the staged jar and keeps duplicate entries (META-INF/MANIFEST.MF, orm-enhanced-dependencies.list), which java.util.zip.ZipOutputStream refuses. Copy every entry in physical order with Ant's ZipOutputStream instead. Co-Authored-By: Claude Opus 5.5 --- .../codename1/maven/BytecodeCompliance.java | 27 +++++++++---------- .../maven/BytecodeComplianceTest.java | 12 +++++++++ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/maven/build-engine/src/main/java/com/codename1/maven/BytecodeCompliance.java b/maven/build-engine/src/main/java/com/codename1/maven/BytecodeCompliance.java index c4b9378c5b3..5d5707523b0 100644 --- a/maven/build-engine/src/main/java/com/codename1/maven/BytecodeCompliance.java +++ b/maven/build-engine/src/main/java/com/codename1/maven/BytecodeCompliance.java @@ -1020,21 +1020,20 @@ static byte[] rewriteInvocations(byte[] classBytes) { public static int rewriteInvocationsInJar(File jar) throws IOException { File rewritten = new File(jar.getPath() + ".rewrite.tmp"); int callsites = 0; - java.util.zip.ZipFile in = new java.util.zip.ZipFile(jar); + // Read in physical order and write with Ant's stream: the staged jar is + // merged by Ant's zip task, which keeps duplicate entries (one per + // input jar that carries, say, META-INF/cn1/orm-enhanced-dependencies.list), + // and java.util.zip.ZipOutputStream refuses a duplicate name. Every + // entry is copied as it is, duplicates included, so the jar the + // builder reads differs only in the rewritten classes. + java.util.zip.ZipInputStream in = new java.util.zip.ZipInputStream( + new java.io.BufferedInputStream(new java.io.FileInputStream(jar))); try { - java.util.zip.ZipOutputStream out = new java.util.zip.ZipOutputStream( - new java.io.BufferedOutputStream(new java.io.FileOutputStream(rewritten))); + org.apache.tools.zip.ZipOutputStream out = new org.apache.tools.zip.ZipOutputStream(rewritten); try { - java.util.Enumeration entries = in.entries(); - while (entries.hasMoreElements()) { - java.util.zip.ZipEntry entry = entries.nextElement(); - InputStream entryIn = in.getInputStream(entry); - byte[] bytes; - try { - bytes = org.apache.commons.io.IOUtils.toByteArray(entryIn); - } finally { - entryIn.close(); - } + java.util.zip.ZipEntry entry; + while ((entry = in.getNextEntry()) != null) { + byte[] bytes = org.apache.commons.io.IOUtils.toByteArray(in); if (!entry.isDirectory() && entry.getName().endsWith(".class") && !entry.getName().startsWith("META-INF/")) { InvocationRewriteResult result = rewriteClassInvocations(bytes, INVOCATION_REWRITE_RULES); @@ -1043,7 +1042,7 @@ public static int rewriteInvocationsInJar(File jar) throws IOException { callsites += result.rewrittenCallsites; } } - java.util.zip.ZipEntry copy = new java.util.zip.ZipEntry(entry.getName()); + org.apache.tools.zip.ZipEntry copy = new org.apache.tools.zip.ZipEntry(entry.getName()); copy.setTime(entry.getTime()); out.putNextEntry(copy); out.write(bytes); diff --git a/maven/build-engine/src/test/java/com/codename1/maven/BytecodeComplianceTest.java b/maven/build-engine/src/test/java/com/codename1/maven/BytecodeComplianceTest.java index 0c66533f2f1..099d7d523b7 100644 --- a/maven/build-engine/src/test/java/com/codename1/maven/BytecodeComplianceTest.java +++ b/maven/build-engine/src/test/java/com/codename1/maven/BytecodeComplianceTest.java @@ -209,6 +209,18 @@ public void stagedJarDependencyRegexCallsAreRewritten(@TempDir Path dir) throws out.write("kept".getBytes("UTF-8")); out.closeEntry(); out.close(); + // The staged jar is merged by Ant, which keeps duplicate entries. + org.apache.tools.zip.ZipOutputStream dup = new org.apache.tools.zip.ZipOutputStream( + dir.resolve("dup.jar").toFile()); + java.util.zip.ZipFile single = new java.util.zip.ZipFile(jar); + for (String name : new String[] {"dep/Squash.class", "res.txt", "res.txt"}) { + dup.putNextEntry(new org.apache.tools.zip.ZipEntry(name)); + dup.write(org.apache.commons.io.IOUtils.toByteArray(single.getInputStream(single.getEntry(name)))); + dup.closeEntry(); + } + single.close(); + dup.close(); + Files.move(dir.resolve("dup.jar"), jar.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); assertEquals(1, BytecodeCompliance.rewriteInvocationsInJar(jar)); java.util.zip.ZipFile zip = new java.util.zip.ZipFile(jar);