Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify override method in Java byte code?

I'm now focusing on a project requiring insight of Java byte code.

With the help of bcel, I can now complete most of the work. One point that I'm now not clear is how to identify a sub-class method override its base code? Is there any attribute recorded in the .class file associated with a method indicating this overriding relationship or should I go backwards to its base class can compare method signatures?

Any hints will be highly appreciated.

like image 762
Summer_More_More_Tea Avatar asked Dec 01 '11 15:12

Summer_More_More_Tea


People also ask

How do we identify if a method is an overridden method?

When a method in a subclass has the same name, same parameters or signature, and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class. Method overriding is one of the way by which java achieve Run Time Polymorphism.

How do you know if Java is overriding?

In Java, method overriding occurs when a subclass (child class) has the same method as the parent class. In other words, method overriding occurs when a subclass provides a particular implementation of a method declared by one of its parent classes.

Which method overrides a method?

Instance Methods The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method that it overrides.

What methods can you override in Java?

Instance methods can be overridden only if they are inherited by the subclass. A method declared final cannot be overridden. A method declared static cannot be overridden but can be re-declared. If a method cannot be inherited, then it cannot be overridden.


2 Answers

You need to look up the hierarchy chain--there's nothing in the byte code that indicates it's an overridden method, because there doesn't need to be.

like image 141
Dave Newton Avatar answered Sep 30 '22 21:09

Dave Newton


If you can't rely on the @Override attribute then it seems that according to the spec there is no other way to know by looking just at the class. I think you need to look at the superclasses.

like image 36
opqdonut Avatar answered Sep 30 '22 21:09

opqdonut