Can we have a “not override a concrete method …” compile time error when implementing interfaces ?
An example to be more clear :
I build up a framework containing interfaces. To use the framework developers need to implements some interfaces.
But if they don’t override equals(Object object) and hashCode() Object methods the internal API logic will be broken !!
All that is mentioned in the javadoc but I want to have a compile time error or maybe a runtime exception when an interface is implemented without overriding some concretes methods.
Java ships with annotation processor capabilities, starting from Java 6: Source Code Analysis. (Technically it’s part of Java 5, but Java 6 integrated it into the compiler phase rather than a special tool). Sun provides this Getting Started guide.
An annotation processor gets invoked by the compiler while building the project, and may issue errors just like the compiler does. It’s a neat way to enforce more rules than Java specifies.
Here is a sample Processor that would perform the check you desired:
The annotation processors can be specified directly in the compiler options (by adding
-processor com.notnoop.CheckMethodOverrideto the compiler options), or adding aMETA-INF/servicesfile to your classpath which contains the name of the processor (com.notnoop.CheckMethodOverride). The forlderMETA-INFcan live in the root source directory of the project (or resource directory if using maven).This works with any command line compiler. I have no idea how to activate it in the IDEs though.