in schema-converter/avro-schema-converter/src/main/java/org/apache/rocketmq/schema/avro/util/AvroSchemaUtils.java [85:135]
public static Schema getSchema(Object object, boolean useReflection,
boolean reflectionAllowNull, boolean removeJavaProperties) {
if (object == null) {
return PRIMITIVE_SCHEMAS.get("Null");
} else if (object instanceof Boolean) {
return PRIMITIVE_SCHEMAS.get("Boolean");
} else if (object instanceof Integer) {
return PRIMITIVE_SCHEMAS.get("Integer");
} else if (object instanceof Long) {
return PRIMITIVE_SCHEMAS.get("Long");
} else if (object instanceof Float) {
return PRIMITIVE_SCHEMAS.get("Float");
} else if (object instanceof Double) {
return PRIMITIVE_SCHEMAS.get("Double");
} else if (object instanceof CharSequence) {
return PRIMITIVE_SCHEMAS.get("String");
} else if (object instanceof byte[] || object instanceof ByteBuffer) {
return PRIMITIVE_SCHEMAS.get("Bytes");
} else if (useReflection) {
Schema schema = reflectionAllowNull ? ReflectData.AllowNull.get().getSchema(object.getClass())
: ReflectData.get().getSchema(object.getClass());
if (schema == null) {
throw new SerializationException("Schema is null for object of class " + object.getClass()
.getCanonicalName());
} else {
return schema;
}
} else if (object instanceof GenericContainer) {
Schema schema = ((GenericContainer) object).getSchema();
if (removeJavaProperties) {
schema = removeJavaProperties(schema);
}
return schema;
} else if (object instanceof Map) {
// This case is unusual -- the schema isn't available directly anywhere, instead we have to
// take get the value schema out of one of the entries and then construct the full schema.
Map mapValue = (Map) object;
if (mapValue.isEmpty()) {
// In this case the value schema doesn't matter since there is no content anyway. This
// only works because we know in this case that we are only using this for conversion and
// no data will be added to the map.
return Schema.createMap(PRIMITIVE_SCHEMAS.get("Null"));
}
Schema valueSchema = getSchema(mapValue.values().iterator().next());
return Schema.createMap(valueSchema);
} else {
throw new IllegalArgumentException(
"Unsupported Avro type. Supported types are null, Boolean, Integer, Long, "
+ "Float, Double, String, byte[] and IndexedRecord");
}
}