When creating a base super DO class that extends from Model class with some common properties/methods, then create some DOs extending from that base DO.
It turns the extended DOs to miss their properties.
Look into more detail, the problem is from the TableInfo.java
public TableInfo(Class<? extends Model> type) {
mType = type;
final Table tableAnnotation = type.getAnnotation(Table.class);
if (tableAnnotation != null) {
mTableName = tableAnnotation.name();
}
else {
mTableName = type.getSimpleName();
}
List<Field> fields = new ArrayList<Field>(Arrays.asList(type.getDeclaredFields()));
fields.add(getIdField(type));
for (Field field : fields) {
if (field.isAnnotationPresent(Column.class)) {
final Column columnAnnotation = field.getAnnotation(Column.class);
mColumnNames.put(field, columnAnnotation.name());
}
}
}
private Field getIdField(Class<?> type) {
if (type.equals(Model.class)) {
try {
return type.getDeclaredField("mId");
}
catch (NoSuchFieldException e) {
Log.e("Impossible!", e);
}
}
else if (type.getSuperclass() != null) {
return getIdField(type.getSuperclass());
}
return null;
}
The reason is the above red codes just get its properties but without including its super classes' properties.
So I created a new method getDeclaredFields to replace the getIdField as below:
/**Get declared fields from the given class & it's ancestors
* @param type class
* @return
*/
private List<Field> getDeclaredFields(Class<?> type) {
List<Field> fields = new ArrayList<Field>();
if (type.getSuperclass() != null && type.getSuperclass() != Object.class) {
fields.addAll(getDeclaredFields(type.getSuperclass()));
}
fields.addAll(Arrays.asList(type.getDeclaredFields()));
return fields;
}
And replace red lines with:
List<Field> fields = getDeclaredFields(type);
Hi, can you please provide an example of how the inheritance worked.Because when I extend the DO class,no new table is created?
ReplyDeleteSorry was using an old version of active android.. with the new version it is added.
Delete