@SuppressWarnings("unused") public static void premain(String args, Instrumentation inst) { boolean verbose = false; if (args != null) { if (args.contains("verbose")) { verbose = true; } } inst.addTransformer(new FileStreamInstrumentationTransformer(verbose), true); System.out.println("OrcAgent is registered."); if (!inst.isRetransformClassesSupported()) { System.err.println("WARNING: JVM does not support class retransformation."); } else { try { inst.retransformClasses(FileInputStream.class, FileOutputStream.class); } catch (UnmodifiableClassException e) { System.err.println("Unable to modify FileStream classes."); e.printStackTrace(); } } Runtime.getRuntime() .addShutdownHook( new Thread( new Runnable() { public void run() { System.err.println(); System.err.println(FileStreamStatistics.asString()); } })); }
private void instrumentApplication() throws FileNotFoundException, UnmodifiableClassException { if (!instrumentation.isRetransformClassesSupported()) { throw new UnmodifiableClassException(); } instrumentation.addTransformer(this, true); for (Class<?> c : instrumentation.getAllLoadedClasses()) { if (isInstrumentClass(c)) { if (configuration.isAsyncTransformation()) { try { blockingQueue.put(c); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break; } } else { try { instrumentation.retransformClasses(new Class[] {c}); } catch (Throwable e) { LOG.error("Could not transform " + c.getName(), e); } } } } if (configuration.isAsyncTransformation() && !blockingQueue.isEmpty()) { startTransformThread(); } }
@Test public void testRetransformationStrategyIsRetransforming() throws Exception { Instrumentation instrumentation = mock(Instrumentation.class); when(instrumentation.isRetransformClassesSupported()).thenReturn(true); assertThat( AgentBuilder.RedefinitionStrategy.RETRANSFORMATION.isRetransforming(instrumentation), is(true)); }
public static void agentmain(@SuppressWarnings("unused") String args, Instrumentation inst) throws Exception { if (inst.isRedefineClassesSupported() && inst.isRetransformClassesSupported()) { inst.addTransformer(new FooTransformer(), true); Class<?>[] allClasses = inst.getAllLoadedClasses(); for (int i = 0; i < allClasses.length; i++) { Class<?> c = allClasses[i]; if (c == Foo.class) { inst.retransformClasses(new Class<?>[] {c}); } } } }
public static synchronized void premain(String agentArguments, Instrumentation instrumentation) { String[] arguments = agentArguments.split(";"); String[] includes = arguments[0].split(","); String[] excludes = arguments[1].split(","); if (Boolean.getBoolean("junit.code.coverage")) { final CoberturaClassFileTransformer coberturaClassFileTransformer = new CoberturaClassFileTransformer(includes, excludes); instrumentation.addTransformer(coberturaClassFileTransformer); Runtime runtime = Runtime.getRuntime(); runtime.addShutdownHook( new Thread() { @Override public void run() { ProjectDataUtil.runMergeHooks(); } }); } else if (instrumentation.isRedefineClassesSupported() && instrumentation.isRetransformClassesSupported()) { _instrumentation = instrumentation; _includes = includes; _excludes = excludes; // Forcibly clear the data file to make sure that the coverage // assert is based on the current test File dataFile = CoverageDataFileHandler.getDefaultDataFile(); dataFile.delete(); } else { StringBuilder sb = new StringBuilder(); sb.append("Current JVM is not capable for dynamic "); sb.append("instrumententation. Instrumentation "); if (instrumentation.isRetransformClassesSupported()) { sb.append("supports "); } else { sb.append("does not support "); } sb.append("restranforming classes. Instrumentation "); if (instrumentation.isRedefineClassesSupported()) { sb.append("supports "); } else { sb.append("does not support "); } sb.append("redefining classes. Dynamic instrumententation is "); sb.append("disabled."); System.out.println(sb.toString()); } }