code: public static String getPackageName(Class<?> clazz) {
      int lengthDiff = clazz.getName().length() - clazz.getSimpleName().length();
      return lengthDiff == 0 ? clazz.getName() : clazz.getName().substring(0, lengthDiff - 1);
    }
docs: Determines the package name without calling {@code Class.getPackage()}
  (which may return null).