Can we "not override a specific method ..." compile a temporary error in the implementation of the interfaces?

Can we "not redefine a specific method ..." compile a temporary error when implementing interfaces?

An example is clearer:

I am creating a structure containing interfaces. To use infrastructure developers, some interfaces must be implemented. But if they do not override the methods equals(Object object)and hashCode() Object, the internal logic of the API will be violated!

All that is mentioned in javadoc, but I want to have a compile-time error or possibly a runtime exception when the interface is implemented without overriding some concrete methods .

+3
source share
4 answers

Java comes with annotation processor capabilities starting in Java 6: Source Code Analysis . (Technically, this is part of Java 5, but Java 6 integrated it into the compiler phase, and not into a special tool). Sun provides this Getting Started Guide .

The comment handler is processed by the compiler when creating the project and may throw errors in the same way as the compiler does. This is a neat way to provide more rules than Java indicates.

Here is an example of a Processor that would perform the required check:

@SupportedSourceVersion(SourceVersion.RELEASE_6)
@SupportedAnnotationTypes("*")
public class CheckMethodOverride extends AbstractProcessor {
    // returns true if the class has a method with the specified method name
    // and specified number of parameters
    private static boolean hasMethod(TypeElement clazz, String methodName, int arity) {
        for (ExecutableElement method : 
                 ElementFilter.methodsIn(clazz.getEnclosedElements())) {
            if (method.getSimpleName().equals(methodName)
                    && method.getParameters().size() == arity)
                return true;
        }
        return false;
    }

    // the interface whose subclasses must override hashCode and equals
    TypeMirror interfaceToCheck;

    @Override
    public void init(ProcessingEnvironment env) {
        interfaceToCheck = env.getElementUtils().getTypeElement("com.notnoop.myinterface").asType();
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations,
            RoundEnvironment roundEnvironment) {
        for (TypeElement e :
            ElementFilter.typesIn(roundEnvironment.getRootElements())) {
            if (this.processingEnv.getTypeUtils()
                     .isSubtype(e.asType(), interfaceToCheck)
                && (!hasMethod(e, "equals", 0)
                    || !hasMethod(e, "hashCode", 0))) {
                processingEnv.getMessager().printMessage(Kind.ERROR,
                    "Class " + e + " doesn't override hashCode or equals", e);
            }
        }
        return true;
    }
}

( -processor com.notnoop.CheckMethodOverride ) META-INF/services , (com.notnoop.CheckMethodOverride). META-INF ( maven).

. , IDE.

+7

, . .

public interface Entity {
    //...
}

public abstract class AbstractEntity implements Entity {

    @Override
    public abstract boolean equals(Object other);

    @Override
    public abstract int hashCode();

}

AbstractEntity , Entity.

, , - , - bean Spring.

+2

, . , .

Sun Community Process, . , .

, . , , , .

-, , . CheckStyle , , , , , . , , " hashCode() equals() - .

+1

:

(, , ) (, equals2() hashCode2()). , . , - .

, , "" "" , .

, , - .

+1

Source: https://habr.com/ru/post/1727103/


All Articles