8000 allow injection of custom RuntimeExceptionDao by dodgex · Pull Request #1252 · androidannotations/androidannotations · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Conversation

@dodgex
Copy link
Member
@dodgex dodgex commented Nov 19, 2014

allow injection of custom RuntimeExceptionDao

when using @OrmLiteDao on a field that is a class extending from RuntimeExceptionDao there is a compile error as RuntimeExceptionDao<T,ID> cannot be assigned to the extending class when using RuntimExceptionDao.createDao()

with this change AA generates a statement that uses databaseHelper_.getRuntimeExceptionDao() which returns D extends RuntimeExceptionDao<T, ?> and therefore can be assigned to the extending class.

@WonderCsabo
Copy link
Member

What about if one wants to inject a simple Dao (not a RuntimeExceptionDao)?

@dodgex
Copy link
Member Author
dodgex commented Nov 19, 2014

then it still uses the helper.getDao().

@WonderCsabo
Copy link
Member

Yeah, i see. Can you add a fixture to the test project which fails with the old behavior but works with this PR?

@dodgex
Copy link
Member Author
dodgex commented Nov 19, 2014

done

@WonderCsabo
Copy link
Member

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 RuntimeExceptionDaos.

@dodgex
Copy link
Member Author
dodgex commented Nov 20, 2014

wtf. i'll fix that later or tomorrow depending on when i get the time.

@dodgex
Copy link
Member 8000 Author
dodgex commented Nov 21, 2014

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.

@WonderCsabo
Copy link
Member

I do not really understand. As you said the return type is generic D extends RuntimeExceptionDao<T, ?>, so you do not have to cast at all. What am i missing?

BTW there is a note in the javadoc:

NOTE: This routing does not return RuntimeExceptionDao because of casting issues if we are assigning it to a custom DAO. Grumble.

@dodgex
Copy link
Member Author
dodgex commented Nov 21, 2014

not sure how to explain that. it compiles due to the generic returnt type. but it actualy ireturns only RuntimeExceptionDao

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.

@dodgex
Copy link
Member Author
dodgex commented Nov 21, 2014

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 getDao returns Dao<T, ?>. not sure how to handle this warning.

@dodgex
Copy link
Member Author
dodgex commented Nov 21, 2014

and yes, the cast is required.

@WonderCsabo
Copy link
Member

BTW are you sure this is needed? I just checked the source code of ORMLite, and for me it seems RuntimeExceptionDao.createDao and OrmLiteSqliteOpenHelper.getRuntimeExceptionDao create the normal dao, and they are just wrapping into a RuntimeExceptionDao. So a custom RuntimeExceptionDao class is not really supported.

@dodgex
Copy link
Member Author
dodgex commented Nov 21, 2014

with this PR, AA could support custom RuntimeExceptionDao.
not sure if this is needed. but i used custom ones in my code until now. and i just migrated to @OrmLiteDao instead of my custom code in my DatabaseHelper and got trouble with my RuntimeExceptionDaos. if you decide to not merge this PR as it adds features to AA that are not supported by the upstream project, i'm ok with this and will change to RuntimeExceptionDao<T,ID>-

@WonderCsabo
Copy link
Member

I think you should ask about this in upstream first, and please reference the thread here.

@dodgex
Copy link
Member Author
dodgex commented Nov 21, 2014

i think that having a OrmLiteSqliteOpenHelper.getRuntimeExceptionDao() method that returns an custom class would require to pass this class to the and then use reflection to get the constructor and invoke it. and i'm not sure if this is something that should be desired on android.

@WonderCsabo
Copy link
Member

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 OrmLite already uses reflection to create the normal dao.

BTW, how did you used your custom RuntimeExceptionDao class before AA? 😕

@dodgex
Copy link
Member Author
dodgex commented Nov 21, 2014

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;
    }
//....
}

@dodgex
Copy link
Member Author
dodgex commented Nov 21, 2014

updated the pr and rebased. but also changed my code to use RuntimeExceptionDao.

@WonderCsabo
Copy link
Member

I see. I just wanted to say you could just use getDao and wrap into the custom, but i see you already implemented that. 😏
This PR indeed adds a feature which is missing from the upstream project, but i think we can still add this, since upstream would also provide this use-case but cannot since it does not work at compile-time (at least if i interpret the javadoc correctly).

@yDelouis WDYT?

@WonderCsabo
Copy link
Member

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?

@dodgex
Copy link
Member Author
dodgex commented Nov 22, 2014

@yDelouis
Copy link
Contributor

The generated code seems good to me.
But, we have to validate that, in case of a custom RuntimeExceptionDao, the class has the right constructor.

@dodgex
Copy link
Member Author
dodgex commented Dec 2, 2014

@WonderCsabo
Copy link
Member

@dodgex if OrmLite classes are not on the classpath that reference will be null.

@dodgex
Copy link
Member Author 67E6
dodgex commented Dec 2, 2014

ah okay. so it would make sense to do this null check and error if this check is true (missing classes).

@WonderCsabo
Copy link
Member

Here is the null check if is enough. indicating that the classes are missing is a responsibility of another method: hasOrmLiteJars.

@dodgex
Copy link
Member Author
dodgex commented Dec 2, 2014

ah ok.

@dodgex
Copy link
Member Author

added validation for constructor of custom subtypes of RuntimeExceptionDao.

Copy link
Member

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.

Copy link
Contributor

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.

Copy link
Member Author

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.

@WonderCsabo
Copy link
Member

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.

@dodgex dodgex changed the title use getRuntimeExceptionDao() instead of RuntimeExceptionDao.createDao allow injection of custom RuntimeExceptionDao Dec 7, 2014
@dodgex
Copy link
Member Author
dodgex commented Dec 7, 2014
  • reabased
  • refactored to move code to OrmLiteHelper
  • reworded first commit and PR title

WonderCsabo added a commit that referenced this pull request Dec 9, 2014
…_of__RuntimeExceptionDao.createDao

Allow injection of custom RuntimeExceptionDao
@WonderCsabo WonderCsabo merged commit fe45c9b into androidannotations:develop Dec 9, 2014
@WonderCsabo
Copy link
Member

Thanks. Can you update the wiki?

@dodgex
Copy link
Member Author
dodgex commented Dec 9, 2014

@dodgex dodgex deleted the use_helper.getRuntimeDao_instead_of__RuntimeExceptionDao.createDao branch December 9, 2014 20:45
@yDelouis yDelouis added this to the 3.3 milestone Dec 13, 2014
@WonderCsabo
Copy link
Member

Wiki merged, thanks.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0