Search in sources :

Example 1 with ClassInfo

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

the class AuthKeyValueParser method parseAndClose.

public <T> T parseAndClose(Reader reader, Class<T> dataClass) throws IOException {
    try {
        ClassInfo classInfo = ClassInfo.of(dataClass);
        T newInstance = Types.newInstance(dataClass);
        BufferedReader breader = new BufferedReader(reader);
        while (true) {
            String line = breader.readLine();
            if (line == null) {
                break;
            }
            int equals = line.indexOf('=');
            String key = line.substring(0, equals);
            String value = line.substring(equals + 1);
            // get the field from the type information
            Field field = classInfo.getField(key);
            if (field != null) {
                Class<?> fieldClass = field.getType();
                Object fieldValue;
                if (fieldClass == boolean.class || fieldClass == Boolean.class) {
                    fieldValue = Boolean.valueOf(value);
                } else {
                    fieldValue = value;
                }
                FieldInfo.setFieldValue(field, newInstance, fieldValue);
            } else if (GenericData.class.isAssignableFrom(dataClass)) {
                GenericData data = (GenericData) newInstance;
                data.set(key, value);
            } else if (Map.class.isAssignableFrom(dataClass)) {
                @SuppressWarnings("unchecked") Map<Object, Object> map = (Map<Object, Object>) newInstance;
                map.put(key, value);
            }
        }
        return newInstance;
    } finally {
        reader.close();
    }
}
Also used : GenericData(com.google.api.client.util.GenericData) Field(java.lang.reflect.Field) BufferedReader(java.io.BufferedReader) Map(java.util.Map) ClassInfo(com.google.api.client.util.ClassInfo)

Example 2 with ClassInfo

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

the class AuthKeyValueParser method parse.

public <T> T parse(InputStream content, Class<T> dataClass) throws IOException {
    ClassInfo classInfo = ClassInfo.of(dataClass);
    T newInstance = Types.newInstance(dataClass);
    BufferedReader reader = new BufferedReader(new InputStreamReader(content));
    while (true) {
        String line = reader.readLine();
        if (line == null) {
            break;
        }
        int equals = line.indexOf('=');
        String key = line.substring(0, equals);
        String value = line.substring(equals + 1);
        // get the field from the type information
        Field field = classInfo.getField(key);
        if (field != null) {
            Class<?> fieldClass = field.getType();
            Object fieldValue;
            if (fieldClass == boolean.class || fieldClass == Boolean.class) {
                fieldValue = Boolean.valueOf(value);
            } else {
                fieldValue = value;
            }
            FieldInfo.setFieldValue(field, newInstance, fieldValue);
        } else if (GenericData.class.isAssignableFrom(dataClass)) {
            GenericData data = (GenericData) newInstance;
            data.set(key, value);
        } else if (Map.class.isAssignableFrom(dataClass)) {
            @SuppressWarnings("unchecked") Map<Object, Object> map = (Map<Object, Object>) newInstance;
            map.put(key, value);
        }
    }
    return newInstance;
}
Also used : InputStreamReader(java.io.InputStreamReader) GenericData(com.google.api.client.util.GenericData) Field(java.lang.reflect.Field) BufferedReader(java.io.BufferedReader) Map(java.util.Map) ClassInfo(com.google.api.client.util.ClassInfo)

Example 3 with ClassInfo

use of com.google.api.client.util.ClassInfo 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)

Example 4 with ClassInfo

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

the class MultiKindFeedParser method setEntryClasses.

/**
 * Sets the entry classes to use when parsing.
 */
public void setEntryClasses(Class<?>... entryClasses) {
    int numEntries = entryClasses.length;
    HashMap<String, Class<?>> kindToEntryClassMap = this.kindToEntryClassMap;
    for (int i = 0; i < numEntries; i++) {
        Class<?> entryClass = entryClasses[i];
        ClassInfo typeInfo = ClassInfo.of(entryClass);
        Field field = typeInfo.getField("@gd:kind");
        if (field == null) {
            throw new IllegalArgumentException("missing @gd:kind field for " + entryClass.getName());
        }
        Object entry = Types.newInstance(entryClass);
        String kind = (String) FieldInfo.getFieldValue(field, entry);
        if (kind == null) {
            throw new IllegalArgumentException("missing value for @gd:kind field in " + entryClass.getName());
        }
        kindToEntryClassMap.put(kind, entryClass);
    }
}
Also used : Field(java.lang.reflect.Field) ClassInfo(com.google.api.client.util.ClassInfo)

Aggregations

ClassInfo (com.google.api.client.util.ClassInfo)4 Field (java.lang.reflect.Field)3 Map (java.util.Map)3 GenericData (com.google.api.client.util.GenericData)2 BufferedReader (java.io.BufferedReader)2 ArrayMap (com.google.api.client.util.ArrayMap)1 FieldInfo (com.google.api.client.util.FieldInfo)1 InputStreamReader (java.io.InputStreamReader)1 Collection (java.util.Collection)1 TreeSet (java.util.TreeSet)1