Detailed code explanation of Java delegation mechanism through reflection
sketch
I've always been resentful about the lack of a ready-made delegation mechanism in Java. Fortunately, I recently wrote a simple delegation module with reflection for reference.
Module API
source code
Module test
Execution results:
StaticCustom!
StaticDelegate!
Custom!
HelloDelegate!
------------------
StaticCustom!
StaticDelegate!
Custom!
Other matters
Some public methods use synchronized to ensure the thread safety of the register variable so that it will not make errors due to multithreading.
For a delegate with a return value, a warning will be reported, but the module still accepts such a delegate, but you will not get the return value when executing the delegate.
The maximum number of delegates added is integer MAX_ VALUE-Integer. MIN_ The fault tolerance processing after value exceeding is not considered (generally, there are not so many functions to delegate).
Delegate execution is out of order, and when performance requirements are required, delegate functions should not have blocking processes as much as possible, otherwise it will affect the execution of other delegate functions.
What other questions can be sent up and discussed together.
summary
The above is all about the detailed code explanation of Java delegation mechanism through reflection. I hope it will be helpful to you. Interested friends can continue to refer to other Java related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!