package alt.jiapi.agent;
import java.util.Properties;
import alt.jiapi.InstrumentationContext;
import alt.jiapi.InstrumentationDescriptor;
import alt.jiapi.reflect.JiapiClass;
import alt.jiapi.util.HotSpotAdvice;
import alt.jiapi.util.HotSpotAdvisor;
/**
* Transforms classes. Currently, only method invocations are
* subject to instrumentation. Following is a list of properties,
* that can be given as an argument to <i>-javaagent</i> command
* line switch
* <ul>
* <li><b>advice=fq_name_of_advice_class</b>: Names a class that
* extends <i>alt.jiapi.util.HotSpotAdvice</i>
* <li><b>inclusion-rule=rule_string</b>: A regular expression indicating
* which classes are to be instrumented.
* <li><b>ir=rule_string</b>: same as above
* <li><b>exclusion-rule=rule_string</b>: A regular expression indicating
* which classes are not to be instrumented.
* <li><b>er=rule_string</b>: same as above
* <li><b>resolution=rule_string</b>: A regular expression indicating
* which method invocations should be the subject of instrumentation.
* <li><b>dump</b>: If given, dumps instrumented class into
* working directory
* </ul>
*
* @see alt.jiapi.util.HotSpotAdvice
*/
public class HotSpotTransformer extends Transformer {
private InstrumentationContext context;
private Properties properties;
public HotSpotTransformer() {
}
public void init(Properties p) {
this.properties = p;
String aName = p.getProperty("advice","alt.jiapi.agent.DefaultAdvice");
String iRule = p.getProperty("inclusion-rule");
String eRule = p.getProperty("exclusion-rule");
String resolution = p.getProperty("resolution", "*");
if (iRule == null) {
iRule = p.getProperty("ir");
}
if (eRule == null) {
eRule = p.getProperty("er");
}
if (iRule != null) {
try {
Class c = Class.forName(aName);
Object o = c.newInstance();
HotSpotAdvice hsa = (HotSpotAdvice)o;
InstrumentationContext ctx = new InstrumentationContext();
InstrumentationDescriptor id = new InstrumentationDescriptor();
id.addInclusionRule(iRule);
if (eRule != null) {
id.addExclusionRule(eRule);
}
ctx.addInstrumentationDescriptor(id);
HotSpotAdvisor hsi =
new HotSpotAdvisor(id, hsa, HotSpotAdvisor.INVOCATIONS,
resolution);
this.context = ctx;
}
catch(Exception e) {
e.printStackTrace();
}
}
else {
System.out.println("Warning: No inclusion rule given. This Transformer will do nothing.");
}
}
/**
* Instrument given JiapiClass.
*/
public boolean transform(JiapiClass clazz) {
if (context != null) {
context.instrument(clazz);
if (properties.getProperty("dump") != null) {
try {
java.io.FileOutputStream fos =
new java.io.FileOutputStream(clazz.getName() + ".clazz");
clazz.dump(fos);
}
catch(Exception e) {
System.out.println("Failed to dump " + clazz.getName() +e);
}
}
return true;
}
return false;
}
// restransforming is not supported
// public boolean retransform(JiapiClass clazz) {
// }
}