Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

allow injection of custom RuntimeExceptionDao#1252

Merged
WonderCsabo merged 2 commits into
androidannotations:developandroidannotations/androidannotations:developfrom
dodgex:use_helper.getRuntimeDao_instead_of__RuntimeExceptionDao.createDaoCopy head branch name to clipboard
Dec 9, 2014
Merged

allow injection of custom RuntimeExceptionDao#1252
WonderCsabo merged 2 commits into
androidannotations:developandroidannotations/androidannotations:developfrom
dodgex:use_helper.getRuntimeDao_instead_of__RuntimeExceptionDao.createDaoCopy head branch name to clipboard

Conversation

@dodgex

@dodgex dodgex commented Nov 19, 2014

Copy link
Copy Markdown
Member

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
Copy Markdown
Member

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

@dodgex

dodgex commented Nov 19, 2014

Copy link
Copy Markdown
Member Author

then it still uses the helper.getDao().

@WonderCsabo

Copy link
Copy Markdown
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

dodgex commented Nov 19, 2014

Copy link
Copy Markdown
Member Author

done

@WonderCsabo

Copy link
Copy Markdown
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

dodgex commented Nov 20, 2014

Copy link
Copy Markdown
Member Author

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

@dodgex

dodgex commented Nov 21, 2014

Copy link
Copy Markdown
Member Author

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
Copy Markdown
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

dodgex commented Nov 21, 2014

Copy link
Copy Markdown
Member Author

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

dodgex commented Nov 21, 2014

Copy link
Copy Markdown
Member Author

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

dodgex commented Nov 21, 2014

Copy link
Copy Markdown
Member Author

and yes, the cast is required.

@WonderCsabo

Copy link
Copy Markdown
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

dodgex commented Nov 21, 2014

Copy link
Copy Markdown
Member Author

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
Copy Markdown
Member

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

@dodgex

dodgex commented Nov 21, 2014

Copy link
Copy Markdown
Member Author

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
Copy Markdown
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

dodgex commented Nov 21, 2014

Copy link
Copy Markdown
Member Author

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

dodgex commented Nov 21, 2014

Copy link
Copy Markdown
Member Author

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

@WonderCsabo

Copy link
Copy Markdown
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
Copy Markdown
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

dodgex commented Nov 22, 2014

Copy link
Copy Markdown
Member Author

@yDelouis

Copy link
Copy Markdown
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

dodgex commented Dec 2, 2014

Copy link
Copy Markdown
Member Author

@WonderCsabo

Copy link
Copy Markdown
Member

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

@dodgex

dodgex commented Dec 2, 2014

Copy link
Copy Markdown
Member Author

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

@WonderCsabo

Copy link
Copy Markdown
Member

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

@dodgex

dodgex commented Dec 2, 2014

Copy link
Copy Markdown
Member Author

ah ok.

@dodgex

dodgex commented Dec 2, 2014

Copy link
Copy Markdown
Member Author

added validation for constructor of custom subtypes of RuntimeExceptionDao.

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

dodgex commented Dec 7, 2014

Copy link
Copy Markdown
Member Author
  • 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
Copy Markdown
Member

Thanks. Can you update the wiki?

@dodgex

dodgex commented Dec 9, 2014

Copy link
Copy Markdown
Member Author

@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
Copy Markdown
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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.