private static IllegalArgumentException handleExceptionForNewInstance()

in google-http-client/src/main/java/com/google/api/client/util/Types.java [124:166]


  private static IllegalArgumentException handleExceptionForNewInstance(
      Exception e, Class<?> clazz) {
    StringBuilder buf =
        new StringBuilder("unable to create new instance of class ").append(clazz.getName());
    ArrayList<String> reasons = new ArrayList<String>();
    if (clazz.isArray()) {
      reasons.add("because it is an array");
    } else if (clazz.isPrimitive()) {
      reasons.add("because it is primitive");
    } else if (clazz == Void.class) {
      reasons.add("because it is void");
    } else {
      if (Modifier.isInterface(clazz.getModifiers())) {
        reasons.add("because it is an interface");
      } else if (Modifier.isAbstract(clazz.getModifiers())) {
        reasons.add("because it is abstract");
      }
      if (clazz.getEnclosingClass() != null && !Modifier.isStatic(clazz.getModifiers())) {
        reasons.add("because it is not static");
      }
      // we don't know what visibility is necessary, but we can give a hint
      if (!Modifier.isPublic(clazz.getModifiers())) {
        reasons.add("possibly because it is not public");
      } else {
        try {
          clazz.getConstructor();
        } catch (NoSuchMethodException e1) {
          reasons.add("because it has no accessible default constructor");
        }
      }
    }
    // append reasons
    boolean and = false;
    for (String reason : reasons) {
      if (and) {
        buf.append(" and");
      } else {
        and = true;
      }
      buf.append(" ").append(reason);
    }
    return new IllegalArgumentException(buf.toString(), e);
  }