How to Quickly Determine If a Method Is Overridden in Java

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



Leave a reply



Submit