The mechanisms for doing this vary by language.
In java you can implement your own ClassLoader, and use the reflection
api to discover things about the classes that are being loaded, and
add those things into the existing call chain as desired.
In ruby you can just require gems that re-open classes and exploit the
dynamic nature of ruby to replace or hook into an existing method
call.
You can create APIs that you expose to runtimes (python, lua, &c.) and
then embed a scripting language into your existing project.
You can also be language agnostic and use a messaging queue or similar
mechanism to publish data and intents, and have listeners (in any
language) on those topics that pick up the data and perform the
actions. Then you only have to swap out particular processes
listening on specific topics (like "compute-gcd") to change your
behavior for that particular intent.
-todd[1]