-
Notifications
You must be signed in to change notification settings - Fork 2.3k
allow injection of custom RuntimeExceptionDao #1252
allow injection of custom RuntimeExceptionDao #1252
Conversation
|
What about if one wants to inject a simple |
|
then it still uses the |
|
Yeah, i see. Can you add a fixture to the test project which fails with the old behavior but works with this PR? |
|
done |
|
Something is not okay, this code generated: try {
userDao = databaseHelper_.getDao(User.class);
} catch (SQLException e) {
Log.e("OrmLiteActivity_", "Could not create DAO userDao", e);
}
databaseHelper_.getRuntimeExceptionDao(Car.class);
databaseHelper_.getRuntimeExceptionDao(User.class);
try {
carDao = databaseHelper_.getDao(Car.class);
} catch (SQLException e) {
Log.e("OrmLiteActivity_", "Could not create DAO carDao", e);
}
ormLiteBean = OrmLiteBean_.getInstance_(this);You forgot to assign the value in case of |
|
wtf. i'll fix that later or tomorrow depending on when i get the time. |
|
wow this is such an awesome issue... no matter what you do. java wont let you cast these fckn RuntimeExceptionDao as it is class instead of interface. but i think i have another solution. |
|
I do not really understand. As you said the return type is generic BTW there is a note in the javadoc:
|
|
not sure how to explain that. it compiles due to the generic returnt type. but it actualy ireturns only D castDao = (D) new RuntimeExceptionDao(dao);
return castDao;so you have to cast a concrete class to a "higher level" class and that is impossible. |
|
i now changed the code to do try {
runtimeExceptionDao = new RuntimeExceptionDao<Car, Long>((Dao<Car, Long>) databaseHelper_.getDao(Car.class));
} catch (SQLException e) {
Log.e("OrmLiteActivity_", "Could not create DAO runtimeExceptionDao", e);
}
try {
userRuntimeExceptionDao = new UserRuntimeExceptionDao((Dao<User, Long>) databaseHelper_.getDao(User.class));
} catch (SQLException e) {
Log.e("OrmLiteActivity_", "Could not create DAO userRuntimeExceptionDao", e);
}this works, but yields unchecked cast warnings as |
|
and yes, the cast is required. |
|
BTW are you sure this is needed? I just checked the source code of |
|
with this PR, AA could support custom |
|
I think you should ask about this in upstream first, and please reference the thread here. |
|
i think that having a |
|
Yes, you have to pass the custom class or declare it on the top of the entity class. Either way, reflection is required, but that is not a big deal since BTW, how did you used your custom |
|
i actually did what i have now in AA :D public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
private AlarmInfoDao alarmInfoDao;
//....
public AlarmInfoDao getAlarmInfoDao() {
if (alarmInfoDao == null) {
try{
alarmInfoDao = new AlarmInfoDao(getDao(AlarmInfo.class));
} catch(SQLException e) {
Log.e(DatabaseHelper.class.getName(), "can't get dao", e);
}
}
return alarmInfoDao;
}
//....
} |
|
updated the pr and rebased. but also changed my code to use |
|
I see. I just wanted to say you could just use @yDelouis WDYT? |
|
BTW @dodgex can you please ask whether our generated code is a good practice on the ORMLite mailing list or some other channel related to ORMLite? |
|
The generated code seems good to me. |
|
@yDelouis yeah. you are right... https://github.com/excilys/androidannotations/blob/develop/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java#L534 any one got an idea why there is that null check? |
|
@dodgex if OrmLite classes are not on the classpath that reference will be null. |
|
ah okay. so it would make sense to do this null check and error if this check is true (missing classes). |
|
Here is the null check if is enough. indicating that the classes are missing is a responsibility of another method: |
|
ah ok. |
|
added validation for constructor of custom subtypes of RuntimeExceptionDao. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really do not like tiding the helper with the handler class. Figure out sg. else, eg. passing the references which are queried from the handler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could create a class OrmLiteHelper in which you put the methods to get the entity and id class. And this class would be used by both ValidatorHelper and OrmLiteHandler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yDelouis that sounds good. i'll update the pr when possible.
|
Can you reword the first commit (as it now does other things as well). Also try to fit into the 50 char length summary, and add more detail in the commit body if needed (max 72 char length), as it is written in the contribution wiki page. |
|
…_of__RuntimeExceptionDao.createDao Allow injection of custom RuntimeExceptionDao
|
Thanks. Can you update the wiki? |
|
Wiki merged, thanks. |
allow injection of custom RuntimeExceptionDao
when using@OrmLiteDaoon a field that is a class extending fromRuntimeExceptionDaothere is a compile error as RuntimeExceptionDao<T,ID> cannot be assigned to the extending class when usingRuntimExceptionDao.createDao()with this change AA generates a statement that usesdatabaseHelper_.getRuntimeExceptionDao()which returnsD extends RuntimeExceptionDao<T, ?>and therefore can be assigned to the extending class.