equals メソッドと hashCode メソッドが override されたかを調べる。

あるクラスが、java.lang.Objectequals メソッドと hashCode メソッドを
override して定義しているかを調べる。

equals を override しているかを調べるメソッド

public static boolean isEqualsOverride(Class<?> cls){
   try{
      Method equalsMethod = cls.getMethod("equals", Object.class);
      Class<?> declaringClass = equalsMethod.getDeclaringClass();
      if (declaringClass.equals(Object.class)){
         return false;
      }
      try{
         declaringClass.getSuperclass().getMethod("equals", Object.class);
         return true;
      }catch(NoSuchMethodException e){
         for(Class<?> iface : declaringClass.getInterfaces()){
            try{
               iface.getMethod("equals", Object.class);
               return true;
            }catch(NoSuchMethodException e2){
            }
         }
         return false;
      }
   }catch(NoSuchMethodException | SecurityException e){
      throw new RuntimeException(e);
   }
}

hashCode を override しているかを調べるメソッド

public static boolean isHashCodeOverride(Class<?> cls){
   try{
      Method hashCodeMethod = cls.getMethod("hashCode");
      Class<?> declaringClass = hashCodeMethod.getDeclaringClass();
      if (declaringClass.equals(Object.class)){
         return false;
      }
      try{
         declaringClass.getSuperclass().getMethod("hashCode");
         return true;
      }catch(NoSuchMethodException e){
         for(Class<?> iface : declaringClass.getInterfaces()){
            try{
               iface.getMethod("hashCode");
               return true;
            }catch(NoSuchMethodException e2){
            }
         }
         return false;
      }
   }catch(NoSuchMethodException | SecurityException e){
      throw new RuntimeException(e);
   }
}

それぞれ、override していれば、true を返す。
さらに、、
equals と hashCode 両方ともに override しているかを調べるメソッド

public static boolean isEqualsHashCodeOverride(Class<?> cls){
   try{
      Method equalsMethod = cls.getMethod("equals", Object.class);
      Class<?> declarEqualsClass = equalsMethod.getDeclaringClass();
      if (declarEqualsClass.equals(Object.class)){
         return false;
      }
      Method hashCodeMethod = cls.getMethod("hashCode");
      Class<?> declarHashCodeClass = hashCodeMethod.getDeclaringClass();
      if (declarHashCodeClass.equals(Object.class)){
         return false;
      }
      try{
         declarEqualsClass.getSuperclass().getMethod("equals", Object.class);
         declarHashCodeClass.getSuperclass().getMethod("hashCode");
         return true;
      }catch(NoSuchMethodException e){
         boolean b = false;
         for(Class<?> iface : declarEqualsClass.getInterfaces()){
            try{
               iface.getMethod("equals", Object.class);
               b = true;
            }catch(NoSuchMethodException e2){
            }
            if (b) break;
         }
         if (b){
            for(Class<?> iface : declarHashCodeClass.getInterfaces()){
               try{
                  iface.getMethod("hashCode");
                  return true;
               }catch(NoSuchMethodException e2){
               }
            }
         }
         return false;
      }
   }catch(NoSuchMethodException | SecurityException e){
      throw new RuntimeException(e);
   }
}

注意:int.class など、Primitive のクラスでは、使えない。当然、NoSuchMethodException になる。