-
Notifications
You must be signed in to change notification settings - Fork 0
/
Agent.java
40 lines (30 loc) · 1.45 KB
/
Agent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import org.objectweb.asm.*;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.instrument.Instrumentation;
import java.security.ProtectionDomain;
import java.io.*;
//import javassist.*;
public class Agent {
public static void premain(String agentArgs, Instrumentation inst) {
inst.addTransformer(new ClassFileTransformer() {
@Override
public byte[] transform(ClassLoader classLoader, String className, Class<?> aClass, ProtectionDomain protectionDomain, byte[] bytes) throws IllegalClassFormatException {
String normalizedClassName = className.replaceAll("/", ".");
if (!normalizedClassName.equals("Reika.ChromatiCraft.Auxiliary.RecipeManagers.CastingRecipes.Blocks.PortalRecipe")) {
System.out.println("AGENT: not interested in " + normalizedClassName);
return null; // No modifications.
}
System.out.println("AGENT: found class: " + normalizedClassName);
ClassReader cr = new ClassReader(bytes);
ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES);
ClassVisitor cv = new MethodFinder(cw, className);
cr.accept(cv, 0);
System.out.println("AGENT: writing back");
byte[] nC = cw.toByteArray();
System.out.println("AGENT: length + " + nC.length);
return nC;
}
});
}
}