|
| 1 | +/* |
| 2 | + * Copyright (c) Forge Development LLC |
| 3 | + * SPDX-License-Identifier: LGPL-2.1-only |
| 4 | + */ |
| 5 | +package buildsrc; |
| 6 | + |
| 7 | +import org.gradle.api.DefaultTask; |
| 8 | +import org.gradle.api.file.RegularFileProperty; |
| 9 | +import org.gradle.api.tasks.OutputFile; |
| 10 | +import org.gradle.api.tasks.TaskAction; |
| 11 | + |
| 12 | +import org.objectweb.asm.ClassVisitor; |
| 13 | +import org.objectweb.asm.ClassWriter; |
| 14 | +import org.objectweb.asm.MethodVisitor; |
| 15 | +import static org.objectweb.asm.Opcodes.*; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.nio.file.Files; |
| 19 | +import java.nio.file.Path; |
| 20 | +import java.nio.file.StandardOpenOption; |
| 21 | + |
| 22 | +abstract class ClassGeneratorTask extends DefaultTask { |
| 23 | + @OutputFile |
| 24 | + abstract RegularFileProperty getOutputFile(); |
| 25 | + |
| 26 | + public ClassGeneratorTask() { |
| 27 | + // I don't think this is necessary, as the build cache should use the hash of this compiled class as the cache key |
| 28 | + // But just in case, this is how to make it always run. |
| 29 | + this.getOutputs().upToDateWhen(t -> false); |
| 30 | + this.getOutputFile().convention(this.getProject() |
| 31 | + .getLayout() |
| 32 | + .getBuildDirectory() |
| 33 | + .file(this.getName() + "/JavaProbe.class") |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + @TaskAction |
| 38 | + public void run() throws IOException { |
| 39 | + Path output = getOutputFile().getAsFile().get().toPath(); |
| 40 | + byte[] data = buildClass(); |
| 41 | + Files.write(output, data, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); |
| 42 | + } |
| 43 | + |
| 44 | + private byte[] buildClass() { |
| 45 | + ClassWriter writer = new ClassWriter(0); |
| 46 | + writer.visit(V1_1, ACC_PUBLIC | ACC_SUPER, "JavaProbe", null, "java/lang/Object", null); |
| 47 | + |
| 48 | + constructor(writer); |
| 49 | + main(writer); |
| 50 | + |
| 51 | + writer.visitEnd(); |
| 52 | + return writer.toByteArray(); |
| 53 | + } |
| 54 | + |
| 55 | + private void constructor(ClassVisitor writer) { |
| 56 | + MethodVisitor mtd = writer.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); |
| 57 | + mtd.visitCode(); |
| 58 | + mtd.visitVarInsn(ALOAD, 0); |
| 59 | + mtd.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); |
| 60 | + mtd.visitInsn(RETURN); |
| 61 | + mtd.visitMaxs(1, 1); |
| 62 | + mtd.visitEnd(); |
| 63 | + } |
| 64 | + |
| 65 | + private void main(ClassVisitor classWriter) { |
| 66 | + MethodVisitor mtd = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null); |
| 67 | + mtd.visitCode(); |
| 68 | + |
| 69 | + for (String prop : new String[] { |
| 70 | + "java.home", |
| 71 | + "java.version", |
| 72 | + "java.vendor", |
| 73 | + "java.runtime.name", |
| 74 | + "java.runtime.version", |
| 75 | + "java.vm.name", |
| 76 | + "java.vm.version", |
| 77 | + "java.vm.vendor", |
| 78 | + "os.arch" |
| 79 | + }) { |
| 80 | + String prefix = "JAVA_PROBE: " + prop + " "; |
| 81 | + // System.out.print(prefix); |
| 82 | + mtd.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); |
| 83 | + mtd.visitLdcInsn(prefix); |
| 84 | + mtd.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false); |
| 85 | + |
| 86 | + // System.out.println(System.getProperty(prop, "unset")); |
| 87 | + mtd.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); |
| 88 | + mtd.visitLdcInsn(prop); |
| 89 | + mtd.visitLdcInsn("unset"); |
| 90 | + mtd.visitMethodInsn(INVOKESTATIC, "java/lang/System", "getProperty", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", false); |
| 91 | + mtd.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); |
| 92 | + } |
| 93 | + |
| 94 | + mtd.visitInsn(RETURN); |
| 95 | + |
| 96 | + mtd.visitMaxs(3, 2); |
| 97 | + |
| 98 | + mtd.visitEnd(); |
| 99 | + } |
| 100 | +} |
0 commit comments