@SuppressWarnings("rawtypes")
  public static void assertClassesAreBuilderStyle(Class... classes) {
    for (Class clazz : classes) {
      System.out.println("Checking " + clazz);
      Method[] methods = clazz.getDeclaredMethods();
      Map<String, Set<Method>> methodsBySignature = new HashMap<>();
      for (Method method : methods) {
        if (!Modifier.isPublic(method.getModifiers())) {
          continue; // only public classes
        }
        Class<?> ret = method.getReturnType();
        if (method.getName().startsWith("set") || method.getName().startsWith("add")) {
          System.out.println(
            "  " + clazz.getSimpleName() + "." + method.getName() + "() : " + ret.getSimpleName());

          // because of subclass / super class method overrides, we group the methods fitting the
          // same signatures because we get two method definitions from java reflection:
          // Mutation.setDurability() : Mutation
          // Delete.setDurability() : Mutation
          // Delete.setDurability() : Delete
          String sig = method.getName();
          for (Class<?> param : method.getParameterTypes()) {
            sig += param.getName();
          }
          Set<Method> sigMethods = methodsBySignature.get(sig);
          if (sigMethods == null) {
            sigMethods = new HashSet<>();
            methodsBySignature.put(sig, sigMethods);
          }
          sigMethods.add(method);
        }
      }
      // now iterate over the methods by signatures
      for (Map.Entry<String, Set<Method>> e : methodsBySignature.entrySet()) {
        // at least one of method sigs should return the declaring class
        boolean found = false;
        for (Method m : e.getValue()) {
          found = clazz.isAssignableFrom(m.getReturnType());
          if (found) {
            break;
          }
        }
        String errorMsg = "All setXXX()|addXX() methods in " + clazz.getSimpleName()
          + " should return a " + clazz.getSimpleName() + " object in builder style. "
          + "Offending method:" + e.getValue().iterator().next().getName();
        assertTrue(errorMsg, found);
      }
    }
  }
Utility class to check whether a given class conforms to builder-style: Foo foo = new Foo()
.setBar(bar) .setBaz(baz)
hbase-client/src/test/java/org/apache/hadoop/hbase/util/BuilderStyleTest.java