Java Code Examples for com.google.api.client.util.FieldInfo#getValue()

The following examples show how to use com.google.api.client.util.FieldInfo#getValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseApiService.java    From connector-sdk with Apache License 2.0 6 votes vote down vote up
private static Object setDefaultValuesForPrimitiveTypes(Object object) {
  if (!(object instanceof GenericJson)) {
    return object;
  }
  GenericJson genericJson = (GenericJson) object;
  for (FieldInfo f : genericJson.getClassInfo().getFieldInfos()) {
    if (Boolean.class.equals(f.getType())) {
      f.setValue(genericJson, Optional.ofNullable(f.getValue(genericJson)).orElse(false));
    } else if (Integer.class.equals(f.getType())) {
      f.setValue(genericJson, Optional.ofNullable(f.getValue(genericJson)).orElse(0));
    } else if (GenericJson.class.isAssignableFrom(f.getType())) {
      setDefaultValuesForPrimitiveTypes(f.getValue(genericJson));
    } else if (Collection.class.isAssignableFrom(f.getType())) {
      Object collection = f.getValue(genericJson);
      if (collection == null) {
        f.setValue(genericJson, Collections.emptyList());
      } else {
        Collection<?> values = (Collection<?>) collection;
        for (Object v : values) {
          setDefaultValuesForPrimitiveTypes(v);
        }
      }
    }
  }
  return object;
}
 
Example 2
Source File: HttpHeaders.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
/** Parses the specified case-insensitive header pair into this HttpHeaders instance. */
void parseHeader(String headerName, String headerValue, ParseHeaderState state) {
  List<Type> context = state.context;
  ClassInfo classInfo = state.classInfo;
  ArrayValueMap arrayValueMap = state.arrayValueMap;
  StringBuilder logger = state.logger;

  if (logger != null) {
    logger.append(headerName + ": " + headerValue).append(StringUtils.LINE_SEPARATOR);
  }
  // use field information if available
  FieldInfo fieldInfo = classInfo.getFieldInfo(headerName);
  if (fieldInfo != null) {
    Type type = Data.resolveWildcardTypeOrTypeVariable(context, fieldInfo.getGenericType());
    // type is now class, parameterized type, or generic array type
    if (Types.isArray(type)) {
      // array that can handle repeating values
      Class<?> rawArrayComponentType =
          Types.getRawArrayComponentType(context, Types.getArrayComponentType(type));
      arrayValueMap.put(
          fieldInfo.getField(),
          rawArrayComponentType,
          parseValue(rawArrayComponentType, context, headerValue));
    } else if (Types.isAssignableToOrFrom(
        Types.getRawArrayComponentType(context, type), Iterable.class)) {
      // iterable that can handle repeating values
      @SuppressWarnings("unchecked")
      Collection<Object> collection = (Collection<Object>) fieldInfo.getValue(this);
      if (collection == null) {
        collection = Data.newCollectionInstance(type);
        fieldInfo.setValue(this, collection);
      }
      Type subFieldType = type == Object.class ? null : Types.getIterableParameter(type);
      collection.add(parseValue(subFieldType, context, headerValue));
    } else {
      // parse value based on field type
      fieldInfo.setValue(this, parseValue(type, context, headerValue));
    }
  } else {
    // store header values in an array list
    @SuppressWarnings("unchecked")
    ArrayList<String> listValue = (ArrayList<String>) this.get(headerName);
    if (listValue == null) {
      listValue = new ArrayList<String>();
      this.set(headerName, listValue);
    }
    listValue.add(headerValue);
  }
}