I want to overwrite the return value of a static method, SysMLUtilityInternal.getFlowPropertyDirection()
, deeply buried in a foreign JAR. The code below is part of a MagicDraw plugin. It runs as expected if the plugin is started from IntelliJ.
If, however, the plugin is run in a standalone mode (that is properly installed under MagicDraw), the code is either a) ignored, b) it crashes MagicDraw, or c) it fails with an unrelated NoClassDefFoundError
depending on the class loader used (commented out in the code below).
How can I ensure a consistent behavior?
private static class MyAdvice {
@Advice.OnMethodExit
public static void exit(@Advice.Return(readOnly = false) String returned) {
returned = "";
}
}
public static void applyPatch() {
ByteBuddyAgent.install();
Class<?> clazz = SysMLUtilityInternal.class;
ClassLoader loader = new MultipleParentClassLoader.Builder().appendMostSpecific(clazz).build();
//ClassLoader loader = clazz.getClassLoader();
//ClassLoader loader = ClassLoader.getPlatformClassLoader();
//ClassLoader loader = ClassLoader.getSystemClassLoader();
new ByteBuddy()
.redefine(clazz)
.visit(Advice.to(MyAdvice.class).on(ElementMatchers.named("getFlowPropertyDirection")))
.make()
.load(loader, ClassReloadingStrategy.fromInstalledAgent());
}