[英]How to modify third party code
A supposedly simple architectural/design question: 一个简单的建筑/设计问题:
Our Java application is depending on a third party code. 我们的Java应用程序取决于第三方代码。 However, this third party code does a little thing that I don't like (set's the timeZone to GMT; I'd like to set the timezone differently based on TimeZoneID of the application).
但是,此第三方代码做了一些我不喜欢的事情(将timeZone设置为GMT;我想根据应用程序的TimeZoneID设置不同的时区)。 What are my options?
我有什么选择?
1- I can't simply extend the third party classes and override the undesired behavior, since there are callers to the 3rd party classes from litterally everywhere in our application (this is the way that we inherited the application to begin with). 1-我不能简单地扩展第三方类并覆盖不希望的行为,因为在我们的应用程序中的任何地方都有从第三位调用第三方类的调用者(这是我们继承应用程序的方式)。
2- Maybe the too invasive solution is to write our own equivalent of the third party code and depend on that instead. 2-也许过于侵入性的解决方案是编写我们自己等效的第三方代码,然后依靠它。 But, that's too much and too invasive probably.
但是,这可能太多而且太侵入。
3- I looked at Spring AOP (and aspectJ) stuff a little bit to do load time weaving. 3-我稍微看了一下Spring AOP(和AspectJ)的东西,以进行加载时间编织。 I didn't dive in it too much, but it seemed to me that since the third party code calls java.util.Timezone.setDefault(GMTTimeZone);
我并没有投入太多,但是在我看来,由于第三方代码调用了java.util.Timezone.setDefault(GMTTimeZone); (as opposed to calling a class that we wrote/manage ourselves) It can't be done that easily.
(与调用我们自己编写/管理的类相反),这不容易做到。 I could be wrong here, and maybe I can still weave around java.util.Timezone.setDefault().
我在这里可能是错误的,也许我仍然可以围绕java.util.Timezone.setDefault()进行编织。 Please let me know if this is ture, and it is pretty much my (only) solution.
请告诉我这是否正确,这几乎是我的(唯一的)解决方案。
4- Am I missing something, or that's pretty much all available options? 4-我是否缺少某些东西,或者几乎所有可用的选项?
First, you could contact the third party and ask them to change the code. 首先,您可以联系第三方并要求他们更改代码。 It's not as long of a shot as you might think.
它不像您想象的那么长。 It annoys you, and probably their other customers.
它使您,可能还有其他客户感到烦恼。
Also, you could use a subclass as a proxy into your code only. 另外,您可以将子类仅用作代码中的代理。 Others on your project don't have to use your subclass, necessarily.
项目中的其他人不一定必须使用您的子类。 You could clean up the undesired behavior and abstract the change out of your code.
您可以清理不良行为,并从代码中抽象出更改。 You could also send this code to the vendor when asking them to change the behavior!
您也可以在要求他们更改行为时将此代码发送给供应商!
Lastly, the easiest solution would be to just take the return value and fix it in-line. 最后,最简单的解决方案是只获取返回值并内联修复。 It's the fastest way to get it done now, but it's not a good pattern to follow, as you're introducing code that's only used to fix other people's problems.
这是现在完成它的最快方法,但是这不是遵循的好模式,因为您要引入的代码仅用于解决其他人的问题。
You're probably pretty much limited to what you've posted. 您可能只限于已发布的内容。 As smcg said, it might be best to fork the code (if possible) and simple change the code then recompile it.
就像smcg所说,最好最好是分叉代码(如果可能的话),然后简单地更改代码,然后重新编译。 On the AOP, if you could monkey patch the code then you could replace the method that calls the setDefault() on the timezone.
在AOP上,如果可以猴子修补代码,则可以替换在时区调用setDefault()的方法。
extend that class from your class and override that method (fill code according to your requirement) in your subclass, but that class and method should not be declared as final in that third party code. 从您的类扩展该类并在子类中重写该方法(根据您的要求填充代码),但是该类和方法不应在该第三方代码中声明为final。
ThirdPartyClassName obj=new YourSubClassName();
obj.yourOverridenMethod()
This will call your overridden method of your sub class and all other calls will call the original method of third party class 这将调用子类的重写方法,所有其他调用将调用第三方类的原始方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.