java.lang.reflect.Type[] params = ptype.getActualTypeArguments();
for (int i = 0; i < params.length; i++)
System.out.println("param ="+params[i]);
if (GenericArray.class.isAssignableFrom(raw)) { // array
if (params.length != 1)
throw new AvroTypeException("No array type specified.");
return Schema.createArray(createSchema(params[0], names));
} else if (Map.class.isAssignableFrom(raw)) { // map
java.lang.reflect.Type key = params[0];
java.lang.reflect.Type value = params[1];
if (!(key == Utf8.class))
throw new AvroTypeException("Map key class not Utf8: "+key);
return Schema.createMap(createSchema(value, names));
}
} else if (type instanceof Class) {
Class c = (Class)type;
String name = c.getSimpleName();
String space = c.getPackage().getName();
Schema schema = names.get(name);
if (schema == null) {
if (c.isEnum()) { // enum
List<String> symbols = new ArrayList<String>();
Enum[] constants = (Enum[])c.getEnumConstants();
for (int i = 0; i < constants.length; i++)
symbols.add(constants[i].name());
schema = Schema.createEnum(name, space, symbols);
names.put(name, schema);
return schema;
}
// fixed
if (GenericFixed.class.isAssignableFrom(c)) {
int size = ((FixedSize)c.getAnnotation(FixedSize.class)).value();
schema = Schema.createFixed(name, space, size);
names.put(name, schema);
return schema;
}
// record
LinkedHashMap<String,Schema.Field> fields =
new LinkedHashMap<String,Schema.Field>();
schema = Schema.createRecord(name, space,
Throwable.class.isAssignableFrom(c));
if (!names.containsKey(name))
names.put(name, schema);
for (Field field : c.getDeclaredFields())
if ((field.getModifiers()&(Modifier.TRANSIENT|Modifier.STATIC))==0) {
Schema fieldSchema = createFieldSchema(field, names);
fields.put(field.getName(), new Schema.Field(fieldSchema, null));
}
schema.setFields(fields);
}
return schema;
}
throw new AvroTypeException("Unknown type: "+type);
}