So I'm using java agent with javassist for purpose of injecting some small monitoring related code to different methods in different classes.
My java agent code:
public class ConverterAgent implements ClassFileTransformer {
public static void premain(String args, Instrumentation instrumentation){
System.out.println(">>>>>>>>>> Intializing Java agent <<<<<<<<<<");
ConverterAgent transformer = new ConverterAgent();
instrumentation.addTransformer(transformer);
}
public static void agentmain(String args, Instrumentation instrumentation){
System.out.println(">>>>>>>>>> Intializing Java agent <<<<<<<<<<");
ConverterAgent transformer=new ConverterAgent();
instrumentation.addTransformer(transformer);
}
@Override
public byte[] transform(final ClassLoader loader,
String className,
Class<?> classBeingRedefined,
ProtectionDomain protectionDoman,
byte[] classFileBuffer)
throws IllegalClassFormatException {
//javassist code goes here
return classFileBuffer;
}
}
And my javassist injections look like this:
if ("className1".equals(className)){
//code
}
if ("className2".equals(className)){
//same code as in first class
}
if ("className3".equals(className)){
//same code as in first and second class
}
So I'm injecting the same exact code multiple times, I want to optimize my process and call a method for every injection so I don't have to copy same code all over and over again. But here's where I hit my problem, what Method Type am I supposed to use and what arguments does it need besides Class and Method names.