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();
}
}
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;
}
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]);
}
}
}
}
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);
}
}
Aggregations