Class<?> eleType = this.field.getType().getComponentType();
if (!Mirror.me(eleType).isPojo()) {
return val;
}
if (eleType.isAssignableFrom(val.getClass())) {
MongoEntity en = Mongos.entity(eleType);
if (en instanceof StaticMongoEntity)
return en.toDBObject(val);
return Mongos.obj2dbo(val);
}
// 否则 ...
if (!check)
return val;
throw Lang.makeThrow( "ArrayAdaptor can only adaptForGet array, but is is '%s'",
val.getClass().getName());
}
// 首先,建立一个新数组
Object[] array = new Object[Array.getLength(val)];
// 将每个项目都转换成 DBObject
for (int i = 0; i < array.length; i++) {
Object ele = Array.get(val, i);
// 如果是POJO或者容器
if (Mirror.me(ele.getClass()).isObj()) {
if (field.isRef()) {
MongoEntity en = Mongos.entity(val.getClass().getComponentType());
DBObject dbo = en.toDBObject(ele);
if (dbo.containsField("_id")) {
array[i] = new DBRef(null, en.getCollectionName(null), dbo.get("_id"));
continue;
} else {
if (log.isWarnEnabled())
log.warn("!!obj without _id but using as ref field value!! fallback to embed doc!!");
}
} else {
MongoEntity en = Mongos.entity(val.getClass().getComponentType());
if (en instanceof StaticMongoEntity)
array[i] = en.toDBObject(ele);
else
array[i] = Mongos.obj2dbo(ele);
}
}
// 否则直接设置