Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: do methods inherit a JRE super class, like classes inherit Object?

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.

like image 776
Tom Avatar asked Apr 08 '26 00:04

Tom


2 Answers

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.

like image 162
Joachim Sauer Avatar answered Apr 10 '26 14:04

Joachim Sauer


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.

like image 21
biziclop Avatar answered Apr 10 '26 14:04

biziclop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!