Yes, it is doable, and this is called weaving at boot time. Essentially, you will need to define your own ClassLoader, which will decide for each class if it is necessary to modify the class file that needs to be loaded; this means that you will need to check the loadable binary class, perhaps change it and then pass it to the JVM for definition / resolution. This is a bit cumbersome, complex, and prone to ClassCastExceptions (the same class defined in two different classloaders will produce 2 different classes that are not compatible with the destination).
Please note that weaving allows you to do much more: you can add new methods, interfaces, fields, change the code of existing classes, etc.
There are already tools that can help you - see, for example, AspectJ as a complete modification of the language or something like BCEL or javassist, which allows you to write such tools for weaving.
Tassos bassassos
source share