Search in sources :

Example 1 with FieldInfo

use of com.google.api.client.util.FieldInfo in project google-api-java-client by google.

the class GoogleAtom method appendFieldsFor.

private static void appendFieldsFor(StringBuilder fieldsBuf, Class<?> dataClass, int[] numFields) {
    if (Map.class.isAssignableFrom(dataClass) || Collection.class.isAssignableFrom(dataClass)) {
        throw new IllegalArgumentException("cannot specify field mask for a Map or Collection class: " + dataClass);
    }
    ClassInfo classInfo = ClassInfo.of(dataClass);
    for (String name : new TreeSet<String>(classInfo.getNames())) {
        FieldInfo fieldInfo = classInfo.getFieldInfo(name);
        if (fieldInfo.isFinal()) {
            continue;
        }
        if (++numFields[0] != 1) {
            fieldsBuf.append(',');
        }
        fieldsBuf.append(name);
        // TODO(yanivi): handle Java arrays?
        Class<?> fieldClass = fieldInfo.getType();
        if (Collection.class.isAssignableFrom(fieldClass)) {
            // TODO(yanivi): handle Java collection of Java collection or Java map?
            fieldClass = (Class<?>) Types.getIterableParameter(fieldInfo.getField().getGenericType());
        }
        // TODO(yanivi): implement support for map when server implements support for *:*
        if (fieldClass != null) {
            if (fieldInfo.isPrimitive()) {
                if (name.charAt(0) != '@' && !name.equals("text()")) {
                // TODO(yanivi): wait for bug fix from server to support text() -- already fixed???
                // buf.append("/text()");
                }
            } else if (!Collection.class.isAssignableFrom(fieldClass) && !Map.class.isAssignableFrom(fieldClass)) {
                int[] subNumFields = new int[1];
                int openParenIndex = fieldsBuf.length();
                fieldsBuf.append('(');
                // TODO(yanivi): abort if found cycle to avoid infinite loop
                appendFieldsFor(fieldsBuf, fieldClass, subNumFields);
                updateFieldsBasedOnNumFields(fieldsBuf, openParenIndex, subNumFields[0]);
            }
        }
    }
}
Also used : TreeSet(java.util.TreeSet) Collection(java.util.Collection) Map(java.util.Map) ArrayMap(com.google.api.client.util.ArrayMap) FieldInfo(com.google.api.client.util.FieldInfo) ClassInfo(com.google.api.client.util.ClassInfo)

Aggregations

ArrayMap (com.google.api.client.util.ArrayMap)1 ClassInfo (com.google.api.client.util.ClassInfo)1 FieldInfo (com.google.api.client.util.FieldInfo)1 Collection (java.util.Collection)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1