MethodReturnTransformer implements ClassFileTransformer { @Override public byte[] transform(ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { if (filter.allow(classLoader, className)) { logger.info("Transform => " + className); try { ClassNodeWrapper classNodeWrapper = new ClassNodeWrapper(); ClassReader reader = new ClassReaderWrapper(classfileBuffer); reader.accept(new ClassVisitor(Opcodes.ASM5, classNodeWrapper) { @Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { MethodVisitor methodVisitor = cv.visitMethod(access, name, desc, signature, exceptions); if (name.contains("<")) { return methodVisitor; } return new MethodReturnVisitor(access, desc, methodVisitor, ASMUtil.isStatic(access)); } }, ClassReader.EXPAND_FRAMES); if (classNodeWrapper.isInterface()) { return classfileBuffer; } return ASMUtil.toBytes(classNodeWrapper); } catch (Exception e) { e.printStackTrace(); } } return classfileBuffer; } MethodReturnTransformer(TraceRegisterBinder traceRegisterBinder, TraceContext traceContext); @Override byte[] transform(ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer);  }
@Test public void testTransform() throws Exception { String className = TestMethods.class.getName(); ClassFileTransformer transformer = new MethodReturnTransformer(new DefaultTraceRegisterBinder(), new AgentTraceContext(ProfilerConfig.load(getClass().getClassLoader(), "light.conf"))); byte[] original = TestUtil.load(className); byte[] transform = transformer.transform(getClass().getClassLoader(), className, null, null, original); assertThat(original, not(transform)); TestClassLoader classLoader = new TestClassLoader(getClass().getClassLoader()); Class<?> clazz = classLoader.findClass(className, transform); assertThat(clazz, notNullValue()); assertThat(clazz.getName(), is(className)); Method method = clazz.getMethod("doWithBoolean", boolean.class); Object instance = clazz.newInstance(); assertThat((Boolean) method.invoke(instance, true), is(true)); assertThat((Boolean) method.invoke(instance, false), is(false)); method = clazz.getMethod("doWithInt", int.class); assertThat((Integer) method.invoke(instance, 13), is(13)); }