ParameterizedType ptype = (ParameterizedType)type;
Class raw = (Class)ptype.getRawType();
java.lang.reflect.Type[] params = ptype.getActualTypeArguments();
if (Collection.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 instanceof Class
&& CharSequence.class.isAssignableFrom((Class)key)))
throw new AvroTypeException("Map key class not CharSequence: "+key);
return Schema.createMap(createSchema(value, names));
} else {
return createSchema(raw, names);
}
} else if (type instanceof Class) { // class
Class c = (Class)type;
String fullName = c.getName();
Schema schema = names.get(fullName);
if (schema == null)
try {
schema = (Schema)(c.getDeclaredField("SCHEMA$").get(null));
if (!fullName.equals(getClassName(schema)))
// HACK: schema mismatches class. maven shade plugin? try replacing.
schema = Schema.parse
(schema.toString().replace(schema.getNamespace(),
c.getPackage().getName()));
} catch (NoSuchFieldException e) {
throw new AvroRuntimeException("Not a Specific class: "+c);
} catch (IllegalAccessException e) {
throw new AvroRuntimeException(e);
}
names.put(fullName, schema);
return schema;
}
throw new AvroTypeException("Unknown type: "+type);
}