Warn developer about calling `super.foo ()` in java

Suppose I have these two classes, one of which extends the rest

public class Bar{ public void foo(){ } } public class FooBar extends Bar { @Override public void foo(){ super.foo(); //<-- Line in question } } 

What I want to do is warn the user about calling the superclass method foo if he does not have an override method, is this possible?

Or is there a way to find out, using reflection, that a method that overrides the method of its superclass calls the original method if I pass the class type to super?

eg:

 public abstract class Bar{ public Bar(Class<? extends Bar> cls){ Object instance = getInstance(); if (!instance.getClass().equals(cls)) { throw new EntityException("The instance given does not match the class given."); } //Find the method here if it has been overriden then throw an exception //If the super method isn't being called in that method } public abstract Object getInstance(); public void foo(){ } } public class FooBar extends Bar { public FooBar(){ super(FooBar.class); } @Override public Object getInstance(){ return this; } @Override public void foo(){ super.foo(); } } 

Maybe even an annotation that I can apply to the super method, so it shows that it needs to be called?


EDIT

Note that this is not a superclass that should call the foo method, it will be someone calling the foo method of the subclass, for example, the database method close

I would even be pleased that the method is "un-overrideable" if it came to it, but still would like to give it a special message.


Edit 2

Here is what I wanted along the way:

enter image description here

But it would be nice to have the above or even give them a custom message to do something else, like Cannot override the final method from Bar, please call it from your implementation of the method instead

+7
java reflection
source share
2 answers

EDIT: answer an edited question that includes:

I would even be pleased that the method is "un-overrideable"

... just make the final method. This will prevent overriding subclasses. From section 8.4.3.3 JLS :

A method can be declared final to prevent overriding or hiding subclasses.

Compilation error to try to override or hide the final method.

To answer the original question, use the template method template instead:

 public abstract class Bar { public foo() { // Do unconditional things... ... // Now subclass-specific things fooImpl(); } protected void fooImpl(); } public class FooBar extends Bar { @Override protected void fooImpl() { // ... } } 

This does not cause the FooBar subclasses FooBar override fooImpl and call super.fooImpl() , of course, but FooBar can do this by applying the same template again - by making its own implementation of fooImpl final, and introducing a new protected abstract method.

+4
source share

what you can do is something like the following

 public class Bar{ public final void foo(){ //do mandatory stuff customizeFoo(); } public void customizeFoo(){ } } public class FooBar extends Bar { @Override public void customizeFoo(){ //do custom suff } } 

The foo method made "final" in the superclass, so subclasses cannot override or do required material

0
source share

All Articles