java 如何覆盖 jar 文件中的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6694114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to override a method in a jar file?
提问by indira
I have a method in a class and a jar file is created using this class. The jar file is included in the project. How can I override this method in the application? Please help
我在一个类中有一个方法,并使用这个类创建了一个 jar 文件。jar 文件包含在项目中。如何在应用程序中覆盖此方法?请帮忙
采纳答案by AlexR
@Pablo Santa Cruz is right (+1): you cannot change existing code. You have to inherit existing class and override what you want in subclass.
@Pablo Santa Cruz 是对的 (+1):您不能更改现有代码。您必须继承现有类并覆盖子类中所需的内容。
BUT if you really want to change something in existing compiled module you can still do it using byte-code modification techniques. There are a lot of packages that can do this. A popular higher level package that implements aspect-oriented paradigm for Java AspectJ can also help.
但是,如果您真的想更改现有编译模块中的某些内容,您仍然可以使用字节码修改技术来进行。有很多包可以做到这一点。为 Java AspectJ 实现面向方面范式的流行的高级包也可以提供帮助。
回答by Pablo Santa Cruz
You need to subclass the class inside your JAR and then override the method. There is no way to "change" an existing method on an existing class in Java unless you change the source code and recompile it.
您需要对 JAR 中的类进行子类化,然后覆盖该方法。除非您更改源代码并重新编译它,否则无法在 Java 中“更改”现有类上的现有方法。
回答by dotrc
If you mean to kind of 'patch' the current behavior, then you'll need to copy the existing java file, update the method you want to override and place its .class ahead in the CLASSPATH.
如果您想对当前行为进行“修补”,那么您需要复制现有的 java 文件,更新要覆盖的方法并将其 .class 放在 CLASSPATH 的前面。
回答by Michael Berry
If you're talking about overriding the method in terms of inheriting from the class and then overriding it (i.e. from a polymorphic viewpoint) then unless the class is final you can extend from it as you would any other.
如果您正在讨论从类继承然后覆盖它的方法(即从多态的角度来看),那么除非该类是最终的,否则您可以像其他任何类一样对其进行扩展。
If you're talking about changing the method behaviour inside the jar file itself, you'll need to get the source, change the method yourself, then recompile it and repackage it in another jar file. Note however I really wouldn't recommend that approach especially if the jar is a common library jar - someone maintaining your code later on will be really confused / hacked off if the behaviour of a library class has been altered (bug fixes aside)!
如果您正在谈论更改 jar 文件本身内部的方法行为,则需要获取源代码,自己更改方法,然后重新编译它并将其重新打包到另一个 jar 文件中。但是请注意,我真的不推荐这种方法,特别是如果 jar 是一个常见的库 jar - 如果库类的行为已经改变(除了错误修复),稍后维护您的代码的人会真的很困惑/被黑客攻击!
If you haven't got the source then yes, you can hack the bytecode and do things that way. But I'm pretty confident that isn't what's needed here :-)
如果您还没有获得源代码,那么是的,您可以破解字节码并以这种方式进行操作。但我非常有信心这不是这里需要的:-)
回答by Bjarni S?varsson
If you are thinking about overriding a method in an already loaded class then look at this Java reflection: How do I override or generate methods at runtime?
如果您正在考虑覆盖已加载类中的方法,请查看此Java 反射:如何在运行时覆盖或生成方法?