How to define an override method in Java bytecode?

Now I'm focusing on a project that requires an understanding of Java byte code.

With bcel, I can do most of the work. One point that I am currently unclear is how to define a subclass method, redefine its base code? Is there any attribute recorded in the .class file associated with the method indicating this overriding relation, or do I need to go back to its base class, can the method signatures be compared?

Any hints would be greatly appreciated.

+6
source share
5 answers

You need to find a chain of hierarchy - there is nothing in the byte code that indicates an overridden method, because it is not necessary.

+5
source

If you cannot rely on the @Override attribute, then it seems that according to the specification there is no other way to find out by looking only at the class. I think you need to look at superclasses.

+2
source

Unfortunately, you cannot say this from bytecode. @Override annotations are only advisory - this is not necessary.

The JVM defines 5 ways to invoke a method. They are invokevirtual, invokeinterface, invokespecial, invokestatic and new invokedynamic.

The focus on invokevirtual is the most common form of submission and is used for the case you are talking about.

The way invokevirtual works is that at runtime it looks at the class of the object you are sending. If he finds an implementation of the method that we need, he calls it. If not, then he looks at the superclass of the feature class and tries again, etc.

Thus, there is no way from the bytecode to reliably determine whether a given method is overridden without looking at the bytecode for the parent class.

+2
source

After compilation, bytecode is created. Thus, he suggests that the method should be called based on the reference variable, since the object has not yet been created.

0
source

You can decompile it and load the code as a project into the IDE of your choice. You can usually easily jump to overridden methods from the inheritance class.

-1
source

Source: https://habr.com/ru/post/902849/


All Articles