是否有可能在Java中修补补丁? [英] Is it possible to monkey patch in Java?

查看:176
本文介绍了是否有可能在Java中修补补丁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想讨论这种方法的优点,只要有可能。我相信答案是不。但也许有人会让我感到惊讶!

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!

想象一下你有一个核心小部件类。它有一个方法 calculateHeight(),它返回一个高度。高度太大 - 这导致按钮(比如说)太大了。您可以扩展DefaultWidget来创建自己的NiceWidget,并实现自己的 calculateHeight()以返回更好的大小。

Imagine you have a core widget class. It has a method calculateHeight(), that returns a height. The height is too big - this result in buttons (say) that are too big. You can extend DefaultWidget to create your own NiceWidget, and implement your own calculateHeight() to return a nicer size.

现在一个库类WindowDisplayFactory,在一个相当复杂的方法中实例化DefaultWidget。您希望它使用您的NiceWidget。工厂类的方法如下所示:

Now a library class WindowDisplayFactory, instantiates DefaultWidget in a fairly complex method. You would like it to use your NiceWidget. The factory class's method looks something like this:

public IWidget createView(Component parent) {
    DefaultWidget widget = new DefaultWidget(CONSTS.BLUE, CONSTS.SIZE_STUPIDLY);

    // bunch of ifs ...
    SomeOtherWidget bla = new SomeOtherWidget(widget);
    SomeResultWidget result = new SomeResultWidget(parent);
    SomeListener listener = new SomeListener(parent, widget, flags);

    // more widget creation and voodoo here

    return result;
}

这就是交易。结果使DefaultWidget深入其他对象的层次结构中。问题 - 如何让这个工厂方法使用我自己的NiceWidget?或者至少在那里得到我自己的 calculateHeight()。理想情况下,我希望能够修补DefaultWidget以使其calculateHeight做正确的事情......

That's the deal. The result has the DefaultWidget deep within a hierarchy of other objects. The question - how to get this factory method to use my own NiceWidget? Or at least get my own calculateHeight() in there. Ideally, I'd like to be able to monkey patch the DefaultWidget so that its calculateHeight did the right thing...

public class MyWindowDisplayFactory {
    public IWidget createView(Component parent) {
        DefaultWidget.class.setMethod("calculateHeight", myCalculateHeight);
        return super.createView(parent);
    }
}

我可以用Python做什么,Ruby,我发明了名字 setMethod()。对我开放的其他选项是:

Which is what I could do in Python, Ruby, etc. I've invented the name setMethod() though. The other options open to me are:


  • 复制并粘贴 createView()继承自工厂类的我自己的类的方法

  • 生活中太小的小部件

  • Copying and pasting the code of the createView() method into my own class that inherits from the factory class
  • Living with widgets that are too big

工厂类无法更改 - 它是核心平台API的一部分。我尝试对返回的结果进行反射以获得(最终)添加的小部件,但它是几个小部件层向下并且在某处它用于初始化其他东西,导致奇怪的副作用。

The factory class can't be changed - it is part of a core platform API. I tried reflection on the returned result to get to the widget that (eventually) got added, but it is several widget-layers down and somewhere it gets used to initialize other stuff, causing odd side effects.

任何想法?到目前为止,我的解决方案是复制粘贴工作,但这是一个需要在升级到更新版本的平台时跟踪父工厂类中的更改的警察,我有兴趣听到其他选项。

Any ideas? My solution so far is the copy-paste job, but that's a cop out that requires tracking the changes in the parent factory class when upgrading to newer versions of the platform, and I'd be interested to hear other options.

推荐答案

也许您可以使用面向方面编程来捕获对该函数的调用并返回您自己的版本?

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

Spring提供了一些AOP功能,但也有其他库可以做到这一点。

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

这篇关于是否有可能在Java中修补补丁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆