com.google.api.client.util.ClassInfo

Here are the examples of the java api class com.google.api.client.util.ClassInfo taken from open source projects.

1. JsonParser#parse()

Project: google-http-java-client
File: JsonParser.java
/**
   * Parses the next field from the given JSON parser into the given destination object.
   *
   * @param context destination context stack (possibly empty)
   * @param destination destination object instance or {@code null} for none (for example empty
   *        context stack)
   * @param customizeParser optional parser customizer or {@code null} for none
   */
private void parse(ArrayList<Type> context, Object destination, CustomizeJsonParser customizeParser) throws IOException {
    if (destination instanceof GenericJson) {
        ((GenericJson) destination).setFactory(getFactory());
    }
    JsonToken curToken = startParsingObjectOrArray();
    Class<?> destinationClass = destination.getClass();
    ClassInfo classInfo = ClassInfo.of(destinationClass);
    boolean isGenericData = GenericData.class.isAssignableFrom(destinationClass);
    if (!isGenericData && Map.class.isAssignableFrom(destinationClass)) {
        // The destination class is not a sub-class of GenericData but is of Map, so parse data
        // using parseMap.
        @SuppressWarnings("unchecked") Map<String, Object> destinationMap = (Map<String, Object>) destination;
        parseMap(null, destinationMap, Types.getMapValueParameter(destinationClass), context, customizeParser);
        return;
    }
    while (curToken == JsonToken.FIELD_NAME) {
        String key = getText();
        nextToken();
        // stop at items for feeds
        if (customizeParser != null && customizeParser.stopAt(destination, key)) {
            return;
        }
        // get the field from the type information
        FieldInfo fieldInfo = classInfo.getFieldInfo(key);
        if (fieldInfo != null) {
            // skip final fields
            if (fieldInfo.isFinal() && !fieldInfo.isPrimitive()) {
                throw new IllegalArgumentException("final array/object fields are not supported");
            }
            Field field = fieldInfo.getField();
            int contextSize = context.size();
            context.add(field.getGenericType());
            Object fieldValue = parseValue(field, fieldInfo.getGenericType(), context, destination, customizeParser, true);
            context.remove(contextSize);
            fieldInfo.setValue(destination, fieldValue);
        } else if (isGenericData) {
            // store unknown field in generic JSON
            GenericData object = (GenericData) destination;
            object.set(key, parseValue(null, null, context, destination, customizeParser, true));
        } else {
            // unrecognized field, skip value.
            if (customizeParser != null) {
                customizeParser.handleUnrecognizedKey(destination, key);
            }
            skipChildren();
        }
        curToken = nextToken();
    }
}

2. UrlEncodedParser#parse()

Project: google-http-java-client
File: UrlEncodedParser.java
/**
   * Parses the given URL-encoded content into the given data object of data key name/value pairs,
   * including support for repeating data key names.
   *
   * <p>
   * Declared fields of a "primitive" type (as defined by {@link Data#isPrimitive(Type)} are parsed
   * using {@link Data#parsePrimitiveValue(Type, String)} where the {@link Class} parameter is the
   * declared field class. Declared fields of type {@link Collection} are used to support repeating
   * data key names, so each member of the collection is an additional data key value. They are
   * parsed the same as "primitive" fields, except that the generic type parameter of the collection
   * is used as the {@link Class} parameter.
   * </p>
   *
   * <p>
   * If there is no declared field for an input parameter name, it will be ignored unless the input
   * {@code data} parameter is a {@link Map}. If it is a map, the parameter value will be stored
   * either as a string, or as a {@link ArrayList}<String> in the case of repeated parameters.
   * </p>
   *
   * @param reader URL-encoded reader
   * @param data data key name/value pairs
   *
   * @since 1.14
   */
public static void parse(Reader reader, Object data) throws IOException {
    Class<?> clazz = data.getClass();
    ClassInfo classInfo = ClassInfo.of(clazz);
    List<Type> context = Arrays.<Type>asList(clazz);
    GenericData genericData = GenericData.class.isAssignableFrom(clazz) ? (GenericData) data : null;
    @SuppressWarnings("unchecked") Map<Object, Object> map = Map.class.isAssignableFrom(clazz) ? (Map<Object, Object>) data : null;
    ArrayValueMap arrayValueMap = new ArrayValueMap(data);
    StringWriter nameWriter = new StringWriter();
    StringWriter valueWriter = new StringWriter();
    boolean readingName = true;
    mainLoop: while (true) {
        int read = reader.read();
        switch(read) {
            case -1:
            // falls through
            case '&':
                // parse name/value pair
                String name = CharEscapers.decodeUri(nameWriter.toString());
                if (name.length() != 0) {
                    String stringValue = CharEscapers.decodeUri(valueWriter.toString());
                    // get the field from the type information
                    FieldInfo fieldInfo = classInfo.getFieldInfo(name);
                    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, stringValue));
                        } 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(data);
                            if (collection == null) {
                                collection = Data.newCollectionInstance(type);
                                fieldInfo.setValue(data, collection);
                            }
                            Type subFieldType = type == Object.class ? null : Types.getIterableParameter(type);
                            collection.add(parseValue(subFieldType, context, stringValue));
                        } else {
                            // parse into a field that assumes it is a single value
                            fieldInfo.setValue(data, parseValue(type, context, stringValue));
                        }
                    } else if (map != null) {
                        // parse into a map: store as an ArrayList of values
                        @SuppressWarnings("unchecked") ArrayList<String> listValue = (ArrayList<String>) map.get(name);
                        if (listValue == null) {
                            listValue = new ArrayList<String>();
                            if (genericData != null) {
                                genericData.set(name, listValue);
                            } else {
                                map.put(name, listValue);
                            }
                        }
                        listValue.add(stringValue);
                    }
                }
                // ready to read next name/value pair
                readingName = true;
                nameWriter = new StringWriter();
                valueWriter = new StringWriter();
                if (read == -1) {
                    break mainLoop;
                }
                break;
            case '=':
                // finished with name, now read value
                readingName = false;
                break;
            default:
                // read one more character
                if (readingName) {
                    nameWriter.write(read);
                } else {
                    valueWriter.write(read);
                }
        }
    }
    arrayValueMap.setValues();
}

3. HttpHeaders#parseHeader()

Project: google-http-java-client
File: HttpHeaders.java
/** 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);
    }
}

4. GoogleAtom#appendFieldsFor()

Project: google-api-java-client
File: GoogleAtom.java
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]);
            }
        }
    }
}

5. AuthKeyValueParser#parse()

Project: google-api-java-client
File: AuthKeyValueParser.java
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;
}

6. LinkHeaderParser#parse()

Project: android-oauth-client
File: LinkHeaderParser.java
public static void parse(Reader reader, Object data) throws IOException {
    Class<?> clazz = data.getClass();
    ClassInfo classInfo = ClassInfo.of(clazz);
    List<Type> context = Arrays.<Type>asList(clazz);
    GenericData genericData = GenericData.class.isAssignableFrom(clazz) ? (GenericData) data : null;
    @SuppressWarnings("unchecked") Map<Object, Object> map = Map.class.isAssignableFrom(clazz) ? (Map<Object, Object>) data : null;
    ArrayValueMap arrayValueMap = new ArrayValueMap(data);
    StringWriter nameWriter = new StringWriter();
    StringWriter valueWriter = new StringWriter();
    boolean readingValue = true;
    boolean readingName = false;
    mainLoop: while (true) {
        int read = reader.read();
        switch(read) {
            case -1:
            // falls through
            case ',':
                // parse name/value pair
                String name = CharEscapers.decodeUri(nameWriter.toString());
                if (name.length() != 0) {
                    String stringValue = CharEscapers.decodeUri(valueWriter.toString());
                    // get the field from the type information
                    FieldInfo fieldInfo = classInfo.getFieldInfo(name);
                    if (fieldInfo != null) {
                        Type type = Data.resolveWildcardTypeOrTypeVariable(context, fieldInfo.getGenericType());
                        // 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, stringValue));
                        } 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(data);
                            if (collection == null) {
                                collection = Data.newCollectionInstance(type);
                                fieldInfo.setValue(data, collection);
                            }
                            Type subFieldType = type == Object.class ? null : Types.getIterableParameter(type);
                            collection.add(parseValue(subFieldType, context, stringValue));
                        } else {
                            // parse into a field that assumes it is a
                            // single value
                            fieldInfo.setValue(data, parseValue(type, context, stringValue));
                        }
                    } else if (map != null) {
                        // parse into a map: store as an ArrayList of values
                        @SuppressWarnings("unchecked") ArrayList<String> listValue = (ArrayList<String>) map.get(name);
                        if (listValue == null) {
                            listValue = Lists.<String>newArrayList();
                            if (genericData != null) {
                                genericData.set(name, listValue);
                            } else {
                                map.put(name, listValue);
                            }
                        }
                        listValue.add(stringValue);
                    }
                }
                // ready to read next name/value pair
                readingValue = true;
                readingName = false;
                nameWriter = new StringWriter();
                valueWriter = new StringWriter();
                if (read == -1) {
                    break mainLoop;
                }
                break;
            case ';':
                // finished with value, now read name
                readingValue = false;
                break;
            case '<':
            case '>':
            case ' ':
            case '"':
                // skip
                break;
            case '=':
                if (!readingValue) {
                    readingName = true;
                    break;
                }
            default:
                // read one more character
                if (readingValue) {
                    valueWriter.write(read);
                } else {
                    if (readingName) {
                        nameWriter.write(read);
                    }
                }
        }
    }
    arrayValueMap.setValues();
}