val output = com.gradle.publish.plugin.dep.org.apache.commons.io.output.ByteArrayOutputStream().use { project.exec { commandLine(javap, "-private", "-cp", buildDir.absolutePath, file.absolutePath) standardOutput = it }.assertNormalExitValue() it.toString() }
val (qualifiedName, methodInfo) = bodyExtractingRegex.find(output)?.destructured ?: return@forEach
val lastDot = qualifiedName.lastIndexOf('.') val packageName = qualifiedName.substring(0, lastDot) val className = qualifiedName.substring(lastDot+1, qualifiedName.length)
val nativeMethods = nativeMethodExtractingRegex.findAll(methodInfo).mapNotNull { it.groups }.flatMap { it.asSequence().mapNotNull { group -> group?.value } }.toList() if (nativeMethods.isEmpty()) return@forEach
val source = buildString { appendLine("package $packageName;") appendLine("public class $className {") for (method in nativeMethods) { if ("()"in method) appendLine(method) else { val updatedMethod = StringBuilder(method).apply { var count = 0 var i = 0 while (i < length) { if (this[i] == ',' || this[i] == ')') insert(i, " arg${count++}".also { i += it.length + 1 }) else i++ } } appendLine(updatedMethod) } } appendLine("}") } val outputFile = tmpDir.resolve(packageName.replace(".", "/")).apply { mkdirs() }.resolve("$className.java").apply { delete() }.apply { createNewFile() } outputFile.writeText(source)