code: protected static Method getWriteReplace(Class<?> cl)
      {
          for (; cl != null; cl = cl.getSuperclass()) {
              Method[] methods = cl.getDeclaredMethods();
  
              for (int i = 0; i < methods.length; i++) {
                  Method method = methods[i];
  
                  if (method.getName().equals("writeReplace")
                      && method.getParameterTypes().length == 0)
                      return method;
              }
          }
  
          return null;
      }
docs: Returns the writeReplace method