Skip to content

Commit

Permalink
fix asm remapper to actually work properly, add to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed Jun 7, 2024
1 parent b7a7ba9 commit 53119d1
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 9 deletions.
56 changes: 51 additions & 5 deletions expect-platform-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,41 @@ dependencies {
tasks.register("aExpectPlatform", ExpectPlatformFiles) {
platformName = "a"
inputCollection = sourceSets.main.output

remap = [
"xyz/wagyourtail/unimined/expect/annotation/Environment": "xyz/wagyourtail/ept/a/Env",
"xyz/wagyourtail/unimined/expect/annotation/Environment\$EnvType": "xyz/wagyourtail/ept/a/Env\$EnvType",
"xyz/wagyourtail/unimined/expect/annotation/Environment\$EnvType.COMBINED": "JOINED",
]
}

tasks.register("bExpectPlatform", ExpectPlatformFiles) {
platformName = "b"
inputCollection = sourceSets.main.output

remap = [
"xyz/wagyourtail/unimined/expect/annotation/Environment": "xyz/wagyourtail/ept/b/OnlyIn",
"xyz/wagyourtail/unimined/expect/annotation/Environment.value": "env",
"xyz/wagyourtail/unimined/expect/annotation/Environment\$EnvType": "xyz/wagyourtail/ept/b/OnlyIn\$Type",
]
}

tasks.register("cExpectPlatform", ExpectPlatformFiles) {
platformName = "c"
inputCollection = sourceSets.main.output

remap = [
"xyz/wagyourtail/unimined/expect/annotation/Environment": "xyz/wagyourtail/ept/c/Environment",
"xyz/wagyourtail/unimined/expect/annotation/Environment.value": "type",
"xyz/wagyourtail/unimined/expect/annotation/Environment\$EnvType": "xyz/wagyourtail/ept/c/Environment\$EnvType",
]
}

tasks.register('runA', JavaExec) {
dependsOn(tasks.aExpectPlatform)
classpath = sourceSets.a.runtimeClasspath + tasks.aExpectPlatform.outputCollection
mainClass = 'xyz.wagyourtail.ept.Main'
group = 'ept'

System.out.println("classpath: " + classpath.files)
}

tasks.register('runB', JavaExec) {
Expand All @@ -89,44 +105,74 @@ tasks.register('runAgentA', JavaExec) {
mainClass = 'xyz.wagyourtail.ept.Main'
group = 'ept'

expectPlatform.insertAgent(delegate, "a")
expectPlatform.insertAgent(delegate, "a", [
"xyz/wagyourtail/unimined/expect/annotation/Environment": "xyz/wagyourtail/ept/a/Env",
"xyz/wagyourtail/unimined/expect/annotation/Environment\$EnvType": "xyz/wagyourtail/ept/a/Env\$EnvType",
"xyz/wagyourtail/unimined/expect/annotation/Environment\$EnvType.COMBINED": "JOINED",
])
}

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

expectPlatform.insertAgent(delegate, "b")
expectPlatform.insertAgent(delegate, "b", [
"xyz/wagyourtail/unimined/expect/annotation/Environment": "xyz/wagyourtail/ept/b/OnlyIn",
"xyz/wagyourtail/unimined/expect/annotation/Environment.value": "env",
"xyz/wagyourtail/unimined/expect/annotation/Environment\$EnvType": "xyz/wagyourtail/ept/b/OnlyIn\$Type",
])
}

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

expectPlatform.insertAgent(delegate, "c")
expectPlatform.insertAgent(delegate, "c", [
"xyz/wagyourtail/unimined/expect/annotation/Environment": "xyz/wagyourtail/ept/c/Environment",
"xyz/wagyourtail/expect/unimined/annotation/Environment.value": "type",
"xyz/wagyourtail/expect/unimined/annotation/Environment\$EnvType": "xyz/wagyourtail/ept/c/Environment\$EnvType",
])
}

tasks.register('jarA', ExpectPlatformJar) {
platformName = "a"
inputFiles = sourceSets.main.output
from sourceSets.a.output
archiveFileName = "a.jar"

remap = [
"xyz/wagyourtail/unimined/expect/annotation/Environment": "xyz/wagyourtail/ept/a/Env",
"xyz/wagyourtail/unimined/expect/annotation/Environment\$EnvType": "xyz/wagyourtail/ept/a/Env\$EnvType",
"xyz/wagyourtail/unimined/expect/annotation/Environment\$EnvType.COMBINED": "JOINED",
]
}

tasks.register('jarB', ExpectPlatformJar) {
platformName = "b"
inputFiles = sourceSets.main.output
from sourceSets.b.output
archiveFileName = "b.jar"

remap = [
"xyz/wagyourtail/unimined/expect/annotation/Environment": "xyz/wagyourtail/ept/b/OnlyIn",
"xyz/wagyourtail/unimined/expect/annotation/Environment.value": "env",
"xyz/wagyourtail/unimined/expect/annotation/Environment\$EnvType": "xyz/wagyourtail/ept/b/OnlyIn\$Type",
]
}

tasks.register('jarC', ExpectPlatformJar) {
platformName = "c"
inputFiles = sourceSets.main.output
from sourceSets.c.output
archiveFileName = "c.jar"

remap = [
"xyz/wagyourtail/expect/unimined/annotation/Environment": "xyz/wagyourtail/ept/c/Environment",
"xyz/wagyourtail/expect/unimined/annotation/Environment.value": "type",
"xyz/wagyourtail/expect/unimined/annotation/Environment\$EnvType": "xyz/wagyourtail/ept/c/Environment\$EnvType",
]
}

assemble.dependsOn(tasks.jarA)
Expand Down
19 changes: 19 additions & 0 deletions expect-platform-test/src/a/java/xyz/wagyourtail/ept/a/Env.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package xyz.wagyourtail.ept.a;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
public @interface Env {

EnvType value();

enum EnvType {
CLIENT,
SERVER,
JOINED
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@ public static String platformTest2(String name) {
return "Goodbye a! " + name;
}

public static void environmentCheck(Env env) {
if (env.value() == Env.EnvType.CLIENT) {
System.out.println("Client only");
} else if (env.value() == Env.EnvType.SERVER) {
System.out.println("Server only");
} else {
System.out.println("Joined");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,14 @@ public static String platformTest(String name) {
public static String platformTest2(String name) {
return "Goodbye b! " + name;
}

public static void environmentCheck(OnlyIn onlyIn) {
if (onlyIn.env() == OnlyIn.Type.CLIENT) {
System.out.println("Client only");
} else if (onlyIn.env() == OnlyIn.Type.SERVER) {
System.out.println("Server only");
} else {
System.out.println("Combined");
}
}
}
20 changes: 20 additions & 0 deletions expect-platform-test/src/b/java/xyz/wagyourtail/ept/b/OnlyIn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package xyz.wagyourtail.ept.b;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
public @interface OnlyIn {

Type env();

enum Type {
CLIENT,
SERVER,
COMBINED
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package xyz.wagyourtail.ept.c;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
public @interface Environment {

EnvType type();

enum EnvType {
CLIENT,
SERVER,
COMBINED
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,14 @@ public static String platformTest2(String name) {
return "Goodbye c! " + name;
}

public static void environmentCheck(Environment env) {
if (env.type() == Environment.EnvType.CLIENT) {
System.out.println("Client only");
} else if (env.type() == Environment.EnvType.SERVER) {
System.out.println("Server only");
} else {
System.out.println("Combined");
}
}

}
12 changes: 12 additions & 0 deletions expect-platform-test/src/main/java/xyz/wagyourtail/ept/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package xyz.wagyourtail.ept;

import xyz.wagyourtail.unimined.expect.annotation.Environment;
import xyz.wagyourtail.unimined.expect.annotation.ExpectPlatform;
import xyz.wagyourtail.unimined.expect.annotation.PlatformOnly;
import xyz.wagyourtail.unimined.expect.Target;
Expand All @@ -18,6 +19,8 @@ public static void main(String[] args) {
try {
Main.class.getDeclaredMethod("platformOnlyTest");
System.out.println("platformOnlyTest exists");

environmentCheck(Main.class.getDeclaredMethod("clientTest").getAnnotation(Environment.class));
} catch (NoSuchMethodException e) {
System.out.println("platformOnlyTest does not exist");
}
Expand All @@ -42,4 +45,13 @@ public static String platformTest2(String name) {
@PlatformOnly({"a", "b"})
public static void platformOnlyTest() {
}

@Environment(Environment.EnvType.COMBINED)
public static void clientTest() {
}

@ExpectPlatform
public static void environmentCheck(Environment env) {
throw new AssertionError();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public class ExpectPlatformAgent {
private static final String platform = System.getProperty(EXPECT_PLATFORM);
private static final String remap = System.getProperty(REMAP);

private static final TransformPlatform transformPlatform = new TransformPlatform(platform, remap);

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);
System.out.println("[ExpectPlatformAgent] Remap: " + transformPlatform.getRemap());
if (platform == null) {
throw new IllegalStateException("-D" + EXPECT_PLATFORM + " not set");
}
Expand All @@ -30,7 +30,6 @@ public static void agentmain(String args, Instrumentation inst) {
}

public static class ExpectPlatformTransformer implements ClassFileTransformer {
TransformPlatform transformPlatform = new TransformPlatform(platform, remap);

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package xyz.wagyourtail.unimined.expect;

import org.objectweb.asm.*;
import org.objectweb.asm.commons.*;

public class BetterClassRemapper extends ClassRemapper {

public BetterClassRemapper(ClassVisitor classVisitor, Remapper remapper) {
super(classVisitor, remapper);
}

@Override
protected AnnotationVisitor createAnnotationRemapper(String descriptor, AnnotationVisitor annotationVisitor) {
return new AnnotationRemapper(api, Type.getType(descriptor).getInternalName(), annotationVisitor, remapper) {
@Override
public void visitEnum(String name, String descriptor, String value) {
super.visitEnum(name, descriptor, remapper.mapFieldName(Type.getType(descriptor).getInternalName(), value, descriptor));
}
};
}

@Override
protected MethodVisitor createMethodRemapper(MethodVisitor methodVisitor) {
return new MethodRemapper(api, methodVisitor, remapper) {

@Override
protected AnnotationVisitor createAnnotationRemapper(String descriptor, AnnotationVisitor annotationVisitor) {
return BetterClassRemapper.this.createAnnotationRemapper(descriptor, annotationVisitor);
}

};
}

@Override
protected FieldVisitor createFieldRemapper(FieldVisitor fieldVisitor) {
return new FieldRemapper(api, fieldVisitor, remapper) {
@Override
public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
return BetterClassRemapper.this.createAnnotationRemapper(descriptor, super.visitAnnotation(descriptor, visible));
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public TransformPlatform(String platformName, Map<String, String> map) {
remap.putAll(map);
}

public Map<String, String> getRemap() {
return remap;
}

public void transform(Path inputRoot, Path outputRoot) throws IOException {
try (Stream<Path> files = Files.walk(inputRoot)) {
files.parallel().forEach(path -> {
Expand Down Expand Up @@ -60,7 +64,7 @@ public ClassNode transform(ClassNode classNode) {
Map<MethodNode, AnnotationNode> platformOnly = new HashMap<>();

ClassNode target = new ClassNode();
ClassRemapper remapper = new ClassRemapper(target, new SimpleRemapper(remap));
ClassRemapper remapper = new BetterClassRemapper(target, new SimpleRemapper(remap));
classNode.accept(remapper);

classNode = target;
Expand Down

0 comments on commit 53119d1

Please sign in to comment.