There are times when you are not very satisfied with third parties getting the classes in the jar package, and want to do some expansion based on the actual situation. If a third-party jar package already provides some extensible classes, such as Interceptor,filter or other classes, then use native comparison insurance. But if it is not provided, it will take some unusual road. Here I have summed up some feasible methods, some elegant, some are the last resort.
First, inherit the class you want to extend, overriding the method you want to extend
This approach is better and conforms to the idea of object-oriented. The other action is smaller, the risk is not very big. However, this method has the premise that the extended class can be replaced with this subclass in the extension point. For example, in the process of bean configuration, it can be injected with a subclass where the class is needed, rather than injecting the extended class. This means that the classes that originally wanted to extend are all directly new, and our newly written subclasses simply cannot be used where they need to be expanded.
Duplicate a class that you want to extend, create a class with the same name as the package in your project
This approach is what I see on the web, taking advantage of the class loading priority, which is that the class loading priority in the Web-inf directory is higher than the priority of the Jar package class in lib.
Third, using ASM and other bytecode modification tool, directly modify the byte code of this class
This is the ultimate approach, just modify the bytecode of this class to look like you want to extend it. The idea is quite straightforward, and the downside is that you are familiar with bytecode and are familiar with ASM tools. Second, the risk of this approach is relatively large, if you put forward this idea, it is likely to be pass off.
In fact, these are the AOP to do, AOP I touch less, configuration up a little trouble, and sometimes not necessarily give you configuration.
Summarize:
How do I extend the classes in a third-party jar package In a Java project?