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.
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.
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.
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.
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.
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.
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.
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