How to quickly determine if a method is overridden in Java
I wouldn't do this. It violates encapsulation and changes the contract of what your class is supposed to do without implementers knowing about it.
If you must do it, though, the best way is to invoke
class.getMethod("myMethod").getDeclaringClass();
If the class that's returned is your own, then it's not overridden; if it's something else, that subclass has overridden it. Yes, this is reflection, but it's still pretty cheap.
I do like your protected-method approach, though. That would look something like this:
public class ExpensiveStrategy {
public void expensiveMethod() {
// ...
if (employOptimization()) {
// take a shortcut
}
}
protected boolean employOptimization() {
return false;
}
}
public class TargetedStrategy extends ExpensiveStrategy {
@Override
protected boolean employOptimization() {
return true; // Now we can shortcut ExpensiveStrategy.
}
}
Is there a way to determine if a method has been overridden in a Java class
You can do it with reflection by examining the declaring class of your method:
class Base {
public void foo() {}
public void bar() {}
}
class Derived extends Base {
@Override
public void bar() {}
}
...
Method mfoo = Derived.class.getMethod("foo");
boolean ovrFoo = mfoo.getDeclaringClass() != Base.class;
Method mbar = Derived.class.getMethod("bar");
boolean ovrBar = mbar.getDeclaringClass() != Base.class;
System.out.println("Have override for foo: "+ovrFoo);
System.out.println("Have override for bar: "+ovrBar);
Prints
Have override for foo: false
Have override for bar: true
Demo.
Determining if a method overrides another at runtime
You can simply cross-check method names and signatures.
public static boolean isOverriden(Method parent, Method toCheck) {
if (parent.getDeclaringClass().isAssignableFrom(toCheck.getDeclaringClass())
&& parent.getName().equals(toCheck.getName())) {
Class<?>[] params1 = parent.getParameterTypes();
Class<?>[] params2 = toCheck.getParameterTypes();
if (params1.length == params2.length) {
for (int i = 0; i < params1.length; i++) {
if (!params1[i].equals(params2[i])) {
return false;
}
}
return true;
}
}
return false;
}
However, since your goal is to rename methods, you might instead wish to use a bytecode analysis/manipulation library such as ASM, where you can perform the same tests as well as easily modify the methods' names if the method returns true.
Checking if a method is an overridden method of Interface using reflection
1) If overridden methods are with @Overridden annotation, than you could iterate through these methods, and check their annotation using this API: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/reflect/Method.html#getDeclaredAnnotations%28%29
2) If there are no annotations, I think, the only way is to iterate through parent classes and interfaces and compare method signatures, declared there with signatures in your class.
Related Topics
Java: Import Statement VS Fully Qualified Name
When Does Java's Thread.Sleep Throw Interruptedexception
Sorting 2D Array of Strings in Java
Java Gif Animation Not Repainting Correctly
How to Get a Index Value from Foreach Loop in Jstl
Printing a JPAnel with Scrollable Jtable on It
How to Unit Test Abstract Classes: Extend with Stubs
What's the Difference Between Getrequesturi and Getpathinfo Methods in Httpservletrequest
Get All Variable Names in a Class
Java. Ignore Accents When Comparing Strings
Why Is System.Out.Println So Slow
Java/Jdk for the Apple M1 Chip
Multiple Inheritance for an Anonymous Class
Why Does Java's Java.Time.Format.Datetimeformatter#Format(Localdatetime) Add a Year
Java.Lang.Unsupportedclassversionerror Unsupported Major.Minor Version 51.0