1

How to configure Assert/Check methods in Android Studio?

I use Mosby MVP framework, and in presenter I often use this pattern:

if (isViewAttached()) {
    getView().someViewMethod();
}

getView() marked as @Nullable, so Android Studio shows me warning method invocation 'someViewMethod' could produce NullPointerException. It doesn't understand thad I've checked it before.

I found brilliant answer about configuration Assert/Check methods: https://stackoverflow.com/a/19319326/1263771

But can not do it in the fresh Android Studio because it has different settings interface. So, how to do it in the last Studio?

Community
  • 1
  • 1
tse
  • 5,769
  • 6
  • 38
  • 58
  • https://developer.android.com/studio/write/lint.html – Selvin Oct 03 '16 at 12:17
  • have you tried @SuppressWarnings("ConstantConditions") ? – Pier Giorgio Misley Oct 03 '16 at 12:17
  • if(getView()!=null) is something that works for me. – 476rick Oct 03 '16 at 12:18
  • *It doesn't understand thad I've checked it before.* ... well no ... `final View view = getView(); if(view != null && isViewAttached()) view.someViewMethod();` <= should work – Selvin Oct 03 '16 at 12:19
  • @Selvin I've found 'Constant conditions & expressions' section, but it has only 'Configure annotations' option, and has not 'Configure Assert/Check Methods`. Is there any idea where to find it? https://www.dropbox.com/s/bbplo658bm7p6wd/Screenshot%202016-10-03%2016.29.28.png?dl=0 – tse Oct 03 '16 at 13:33
  • @PierGiorgioMisley it works, but it bad solution: it adds additional line to every statement where I use getView(). it looks like garbage in the code. – tse Oct 03 '16 at 13:36
  • you have to use it not before the line but before the method/class.. anyway if you look at the yellow alert on the code, there is the option to remove it all over the application. "looks like garbage" is probably because you don't really know how to use it – Pier Giorgio Misley Oct 03 '16 at 13:38
  • @Selvin it adds lots of unnecessary code. every such peace takes time. so it takes my money. no, the better way is to tune the robots. – tse Oct 03 '16 at 13:39
  • @PierGiorgioMisley could you describe? I do not want disable this inspection at all, so I leave it in settings, and do not want to add this annotation to the whole method. how to remove warning only for `getView()` and only if it really rounded by `if (isViewAttached())`? – tse Oct 03 '16 at 13:42
  • @tse post the code where you check the view != null (the lines where you check everything and where you call the getview with the error. in code you have only the isattached()) – Pier Giorgio Misley Oct 03 '16 at 13:54
  • 1
    @PierGiorgioMisley Yes, it's true, I have only isViewAttached(), and I know that if isViewAttached() is true, than getView() always returns not null. I search the way to configure this rule for lint. Look at the link from questions, it contains howto, but it doesn't work with last Android Studio. – tse Oct 03 '16 at 14:06
  • I don't get why don't use the annotation or @Selvin 's solution.. the logic "takes time so it takes my money has no sense since you wase..lets say.. 10 seconds? to write a `View view = getView(); if(view != null && isViewAttached()) { //dostuffs}` . It took 10 seconds to me without any compiler, so lets say you will waste 5 sec... you wasted 2hours on SO to dodge this solution.. you have to write this code 2h/5sec = 1440 times to make it more expensive than those 2 hours of researches.. are you sure about your sentence? – Pier Giorgio Misley Oct 03 '16 at 14:21

2 Answers2

0

For now the easiest way is to use

V view = getView();
if (view != null){ // instead of isViewAttached()
   ...
}

It's very likely that the @Nullable annotation will be removed in next major release of Mosby 3.0

sockeqwe
  • 15,574
  • 24
  • 88
  • 144
0

Totally agree with you @tse. That was the brilliant answer but out of date.

But I found a useful solution for me. You have to make isViewAttached() method as static and send view as a parameter.

protected static boolean isViewAttached(final View view) {
    return view != null && view.isAttached();
}

if (isViewAttached(getView())) {
   getView().someViewMethod();
}
VKostenc
  • 1,140
  • 14
  • 19