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

ぃ、小莉子 提交于 2019-12-14 03:55:56

问题


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.


回答1:


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.




回答2:


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.




回答3:


Fields, methods, parameters, local variables are not objects in Java. They have Object which can represent them using reflections, but they are not Objects.

If you want to change the behaviour of a method to trace arguments passed to it you can use code injection. e.g. AspectJ does this and has examples on how to do it.



来源:https://stackoverflow.com/questions/4945358/java-do-methods-inherit-a-jre-super-class-like-classes-inherit-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!