Is it possible to monkey patch in Java?

后端 未结 9 430
有刺的猬
有刺的猬 2020-12-09 16:40

I don\'t want to discuss the merits of this approach, just if it is possible. I believe the answer to be \"no\". But maybe someone will surprise me!

Imagine you have

相关标签:
9条回答
  • 2020-12-09 16:49

    Perhaps you could use Aspect Oriented Programming to trap calls to that function and return your own version instead?

    Spring offers some AOP functionality but there are other libraries that do it as well.

    0 讨论(0)
  • 2020-12-09 16:51

    Only suggestions I can think of:

    1. Dig through the library API to see if there's some way of overriding the defaults and sizing. Sizing can be confusing in swing (at least to me) , setMinimum, setMaximum, setdefault, setDefaultOnThursday, ... . It's possible there's a way. If you can contact the library designer(s) you might find an answer that will alleviate the need for unpleasant hacking.

    2. Perhaps extend the factory only overriding some default sizing parameter? depends on the factory but it might be possible.

    Creating a class with the same name might be the only other option, as others have pointed out it's ugly and you're liable to forget it and break stuff when you update the api library or deploy in a different environment and forget why you had the classpath set up that way.

    0 讨论(0)
  • 2020-12-09 16:53

    Just my concept idea,

    It is possible that use AOP, with bytecode engineering way, to inject a aspect to the calculateHeight method.

    Then, you may enable you patch by ThreadLocal or else variable.

    0 讨论(0)
  • 2020-12-09 16:53

    The object-oriented way of doing this would be to create a wrapper implementing IWidget, delegating all calls to the actual widget, except calculateHeight, something like:

    class MyWidget implements IWidget {
        private IWidget delegate;
        public MyWidget(IWidget d) {
            this.delegate = d;
        }
        public int calculateHeight() {
            // my implementation of calculate height
        }
        // for all other methods: {
        public Object foo(Object bar) {
            return delegate.foo(bar);
        }
    }
    

    For this to work, you need to intercept all creations of the widget you want to replace, which probably means creating a similar wrapper for the WidgetFactory. And you must be able to configure which WidgetFactory to use.

    It also depends on no client trying to cast the IWidget back to DefaultWidget...

    0 讨论(0)
  • 2020-12-09 16:56

    cglib is a Java library that can do some things similar to monkey patching - it can manipulate bytecode at runtime to change certain behaviours. I'm not sure if it can do exactly what you need, but it's worth a look...

    0 讨论(0)
  • 2020-12-09 16:59

    It is totally possible to monkeypatch in Java, using Unsafe.putObject and a class finder. Wrote a blog post here:

    https://tersesystems.com/blog/2014/03/02/monkeypatching-java-classes/

    0 讨论(0)
提交回复
热议问题