Skip to content

Instantly share code, notes, and snippets.

@hugithordarson
Created January 24, 2018 09:39
Show Gist options
  • Save hugithordarson/4815a261109608ae25b7e64e52c4e1ef to your computer and use it in GitHub Desktop.
Save hugithordarson/4815a261109608ae25b7e64e52c4e1ef to your computer and use it in GitHub Desktop.
package com.webobjects.foundation;
import java.lang.reflect.Method;
import java.util.Optional;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.dynamic.loading.ClassReloadingStrategy;
import net.bytebuddy.matcher.ElementMatchers;
public class BuddyTesting {
public static void main( String[] args ) {
ByteBuddyAgent.install();
new ByteBuddy()
.redefine( NSKeyValueCoding.DefaultImplementation.class )
.visit( Advice.to( KVCAdvisor.class )
.on( ElementMatchers.named( "valueForKey" ) ) )
.make()
.load(
NSKeyValueCoding.DefaultImplementation.class.getClassLoader(),
ClassReloadingStrategy.fromInstalledAgent() );
}
public static class KVCAdvisor {
@Advice.OnMethodExit
static void exit( @Advice.Return( readOnly = false ) Object returnValue, @Advice.Origin Method originalMethod, @Advice.Argument( 0 ) Object param0, @Advice.Argument( 1 ) String param1 ) throws Exception {
Object originalValue = originalMethod.invoke( param0, new Object[] { param0, param1 } );
returnValue = doMyOwnStuffTo( originalValue );
}
}
private static Object doMyOwnStuffTo( Object originalValue ) {
// TODO: Do stuff to original value
return originalValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment