如何在Java中更新函数而不编辑/继承文件

huangapple go评论74阅读模式
英文:

How to update functions in java without editing/inheriting the file

问题

我有一个更新Java Tomcat库的用例,用于我的项目。我们通过访问每个类中的各个函数并编辑它们来完成这个任务。这变得很痛苦,因为我们必须在每个Tomcat版本中执行相同的操作。是否有一种方法可以在不直接编辑或继承它们的情况下更新类的函数?

示例:
库 -> 类A -> 函数f1

向类A的函数f1添加行为更改(无需扩展)。
现在,当调用函数f1时,它的定义就像是一个新函数一样。

我不确定如何搜索这个问题的答案。

编辑1:添加更多细节

  1. 更改是针对项目特定的,向Tomcat本身提交补丁行不通。
  2. 更改是针对几乎 6-7 个生成的 JAR 文件进行的(catalina、coyote、servlet 等)。
  3. 我可以考虑通过脚本编辑这些文件的粗糙方法,但我想首先询问是否有更好的替代方法。
英文:

I have a use-case to update java tomcat library for my project. We do this by going to individual functions in each of the classes and editing them. This becomes a pain because we have to perform same activity with every tomcat release. Is there a way to update a function of a class without editing them directly or inheriting them?

Example ->
library -> class A -> function f1

Add a behavior change to function f1 of class A (without extending).
Now when function f1 is called, it's definition is that of a new one.

I am not sure how to search for answer to this question.

Edit 1: Adding more details

  1. Changes are specific to project, submitting a patch to tomcat itself won't work.
  2. Changes are made to almost 6-7 generated jars (catalina, coyote, servlet etc)
  3. I could think of crude approach to edit those files via a script, but I thought I would ask for a better alternatives first.

答案1

得分: 2

你不应该修改Tomcat提供的文件。目前,您正在感受应用错误流程的痛苦。

相反,创建自己的库,实现与Tomcat库相同的接口。在内部,运行您自己的代码或只是调用Tomcat原始库提供的函数。

最后,配置Tomcat,使其使用您的库而不是内置的库。这应该通过server.xml实现,但您没有透露要替换哪个库以及为什么需要替换。因此,您的好朋友应该是Tomcat配置参考文档

英文:

You should not modify the Tomcat-delivered files. Currently you are perceiving the pain of applying the wrong process.

Instead, create your own library that implements the same interface as Tomcat's library. Internally, run your own code or just call the functions that are supplied by the original one from Tomcat.

Finally configure Tomcat such that it would make use of your library instead of the builtin one. This should be possible via server.xml, but you did not disclose exactly which library and why you need to replace. Hence your good friend should be the Tomcat configuration reference documentation.

答案2

得分: 1

在理论上,您可以通过一些"字节码工程"来重写或替换特定方法的字节码,以在构建或运行时进行操作。

实际上,更简单的方法是在源代码级别进行更改,例如使用补丁脚本,或使用GIT合并或类似的方法。

无论哪种情况,这个过程都会比较脆弱;即未来对Tomcat代码库的更改可能会导致您的补丁程序停止工作。

我不会评论您正在进行的操作是否是一个好主意。这将取决于您的更改内容以及是否存在可行的替代方案。

英文:

In theory you could do this with some "bytecode engineering" that rewrites or replaces bytecodes for specific methods at build or run time.

In practice, it would be simpler to make the changes at the source code level; e.g. by using a patch script, or by using GIT merging or similar.

In either case, the process is going to be fragile; i.e. future changes to the Tomcat codebase may mean your procedure for patching may stop working.


<sup>I won't comment on whether what you are doing is a good idea. That will depend on what your changes are doing and whether there are viable alternatives.</sup>

huangapple
  • 本文由 发表于 2023年7月10日 23:18:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定