As far as I know, everything is an object in Java.
I am looking through the java JDK and am experimenting with creating a custom JDK that allows me to perform more debugging operations. For example, I can manipulate every object by changing the Object.java class.
My question is: do methods inherit a class from the JRE as well, so that I can modify this parent to eg. make a method save it's most recently passed arguments?
Thanks.
No, not everything in Java is an object. Methods are not object, primitive values are not objects, references are not objects. Only objects are objects in Java (and arrays).
There are objects that represent classes (java.lang.Class), methods (java.lang.reflect.Method), constructors (java.lang.reflect.Constructor) and fields (java.lang.reflect.Field). But "representing" something is not the same thing as "being" something.
So no: methods are not objects, they don't inherit from any base class (or "base method").
If you want to interact with the JVM at a low level, then JVM TI might be the correct API for you.
If you only want to enhance debugging, you can start with looking at what JVMTI has to offer. Even before that, see if JDI, a much higher level (and therefore easier to use) API built on top of JVMTI suits your needs.
And although what you're saying doesn't sound possible in itself, a similar effect can be achieved by instrumentation, using a byte code manipulating library, like BCEL.
As for your specific example, if you want to record method invocations and their parameters, simply setting a breakpoint with JDI and then querying the call stack can do the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With