Skip to content

Commit

Permalink
fix agent gradle inject
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed Jun 7, 2024
1 parent 253d81c commit b7a7ba9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
22 changes: 20 additions & 2 deletions expect-platform-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ buildscript {
}
}
dependencies {
classpath "xyz.wagyourtail.unimined.expect-platform:expect-platform:1.0.2"
classpath "xyz.wagyourtail.unimined.expect-platform:expect-platform:1.0.3"
classpath "org.ow2.asm:asm:9.7"
classpath "org.ow2.asm:asm-commons:9.7"
classpath "org.ow2.asm:asm-tree:9.7"
}
}
Expand Down Expand Up @@ -87,7 +89,23 @@ tasks.register('runAgentA', JavaExec) {
mainClass = 'xyz.wagyourtail.ept.Main'
group = 'ept'

expectPlatform.insertAgent(delegate as JavaExecSpec, "a")
expectPlatform.insertAgent(delegate, "a")
}

tasks.register('runAgentB', JavaExec) {
classpath = sourceSets.b.runtimeClasspath + sourceSets.main.runtimeClasspath
mainClass = 'xyz.wagyourtail.ept.Main'
group = 'ept'

expectPlatform.insertAgent(delegate, "b")
}

tasks.register('runAgentC', JavaExec) {
classpath = sourceSets.c.runtimeClasspath + sourceSets.main.runtimeClasspath
mainClass = 'xyz.wagyourtail.ept.Main'
group = 'ept'

expectPlatform.insertAgent(delegate, "c")
}

tasks.register('jarA', ExpectPlatformJar) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ public class ExpectPlatformAgent {


public static void premain(String args, Instrumentation inst) {
System.out.println("[ExpectPlatformAgent] Starting agent");
System.out.println("[ExpectPlatformAgent] Platform: " + platform);
System.out.println("[ExpectPlatformAgent] Remap: " + remap);
if (platform == null) {
throw new IllegalStateException("-D" + EXPECT_PLATFORM + " not set");
}
inst.addTransformer(new ExpectPlatformTransformer());
inst.addTransformer(new ExpectPlatformTransformer(), inst.isRetransformClassesSupported());
}

public static void agentmain(String args, Instrumentation inst) {
Expand All @@ -35,7 +38,7 @@ public byte[] transform(ClassLoader loader, String className, Class<?> classBein
ClassNode classNode = new ClassNode();
reader.accept(classNode, 0);

transformPlatform.transform(classNode);
classNode = transformPlatform.transform(classNode);

ClassWriter writer = new ClassWriter(reader, 0);
classNode.accept(writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package xyz.wagyourtail.unimined.expect
import groovy.lang.Closure
import groovy.lang.DelegatesTo
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.artifacts.Configuration
import org.gradle.api.attributes.Attribute
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.JavaExec
import org.gradle.api.tasks.TaskProvider
import org.gradle.process.JavaExecSpec
import xyz.wagyourtail.unimined.expect.transform.ExpectPlatformParams
import xyz.wagyourtail.unimined.expect.transform.ExpectPlatformTransform
Expand Down Expand Up @@ -64,10 +67,10 @@ abstract class ExpectPlatformExtension(val project: Project) {
}
}

// @JvmOverloads
// fun insertAgent(spec: JavaExecSpec, platformName: String, remap: Map<String, String> = emptyMap()) {
// spec.jvmArgs("-javaagent:${agentJar.absolutePath}", "-Dexpect.platform=${platformName}", "-Dexpect.remap=${TransformPlatform.mapToString(remap)}")
// }
@JvmOverloads
fun insertAgent(spec: JavaExecSpec, platformName: String, remap: Map<String, String> = emptyMap()) {
spec.jvmArgs("-javaagent:${agentJar.absolutePath}", "-Dexpect.platform=${platformName}", "-Dexpect.remap=${TransformPlatform.mapToString(remap)}")
}

val agentJar by lazy {
val config = project.configurations.detachedConfiguration(project.dependencies.create(agentDep))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ private void getCurrentTarget(ClassNode classNode) {
AbstractInsnNode insnNode = iterator.next();
if (insnNode.getOpcode() == Opcodes.INVOKESTATIC) {
MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode;
if (methodInsnNode.owner.equals("xyz/wagyourtail/unimined/expect/Target") && methodInsnNode.name.equals("getCurrentTarget")) {
if (methodInsnNode.owner.equals("xyz/wagyourtail/unimined/expect/Target") &&
methodInsnNode.name.equals("getCurrentTarget")
) {
iterator.set(new LdcInsnNode(platformName));
}
}
Expand All @@ -196,7 +198,9 @@ public static String mapToString(Map<String, String> map) {
for (Map.Entry<String, String> entry : map.entrySet()) {
sb.append(entry.getKey()).append(";=;").append(entry.getValue()).append(";|;");
}
sb.setLength(sb.length() - 3);
if (sb.length() > 0) {
sb.setLength(sb.length() - 3);
}
return sb.toString();
}

Expand Down

0 comments on commit b7a7ba9

Please sign in to comment.