List of usage examples for java.lang.instrument Instrumentation addTransformer
void addTransformer(ClassFileTransformer transformer, boolean canRetransform);
From source file:com.xiaoqq.practise.threadmonitor.Agent.java
public static void premain(String arg, Instrumentation instrumentation) { System.out.println("Start to create thread monitor Agent."); Agent agent = new Agent(); instrumentation.addTransformer(agent, true); /*/*www . j a v a2 s .com*/ Class[] allClasses = instrumentation.getAllLoadedClasses(); for (Class c : allClasses) { if (!c.isInterface() && instrumentation.isModifiableClass(c)) { try { instrumentation.retransformClasses(c); } catch (Exception e) { System.err.println("Cannot retransform class. Exception: " + e); e.printStackTrace(); System.exit(1); } } } */ }
From source file:de.codesourcery.asm.rewrite.ProfilingClassTransformer.java
public static void premain(String agentArgs, Instrumentation inst) { // parse options final Map<String, String> options = parseArgs(agentArgs); if (StringUtils.isBlank(options.get(OPTION_PACKAGES))) { throw new RuntimeException( "Agent " + ProfilingClassTransformer.class.getName() + " requires the 'packages=....' option"); }/*from w ww . jav a 2s . c o m*/ final boolean debug = options.containsKey(OPTION_DEBUG); final String[] packages = options.get(OPTION_PACKAGES).split(","); if (ArrayUtils.isEmpty(packages)) { throw new RuntimeException("Agent " + ProfilingClassTransformer.class.getName() + " requires at least one pattern with the 'packages=....' option"); } if (debug) { System.out.println( "ProfilingClassTransformer activated (packages: " + StringUtils.join(packages, " , ") + ")"); } final IJoinpointFilter filter = new IJoinpointFilter() { @Override public boolean matches(String clazz, String methodName) { return true; } @Override public boolean matches(String clazz) { for (String p : packages) { if (clazz.contains(p)) { return true; } } return false; } }; final File debugOutputDir = options.containsKey(OPTION_DEBUG_WRITE_CLASSFILES) ? new File(options.get(OPTION_DEBUG_WRITE_CLASSFILES)) : null; inst.addTransformer(new MyTransformer(filter, debug, debugOutputDir), false); // no re-transformation support }