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

internalFindViewById raises "unchecked cast" warning #2090

Open
ened opened this issue Nov 30, 2017 · 5 comments
Open

internalFindViewById raises "unchecked cast" warning #2090

ened opened this issue Nov 30, 2017 · 5 comments

Comments

@ened
Copy link
Contributor

ened commented Nov 30, 2017

Starting from recent appcompat library versions (I'm not sure which one exactly), the need to cast the result of findViewById seem to have gone away. In fact, if the casting in current form stays, then compiling with "-Xlint:unchecked" will raise warnings like this:

  required: T
  found:    View
  where T is a type-variable:
    T extends View declared in method <T>internalFindViewById(int)
/git/project/app/src/main/java/SomeActivity_.java:40: warning: [unchecked] unchecked cast
        return ((T) this.findViewById(id));

AndroidAnnotations version: 4.4.0
Android compile SDK version: 26

Annotated code:

@EActivity
public class SomeActivity extends AppCompatActivity {
}

Expected generated code:

@Override
public<T extends View> T internalFindViewById(int id) {
    return this.findViewById(id);
}

Actual generated code:

@Override
public<T extends View> T internalFindViewById(int id) {
    return ((T) this.findViewById(id));
}

This would probably require different style of code generation depending on the activities parent class (AppCompatActivity, Activity) and if the findViewById call supports the casting already.

@dodgex
Copy link
Member

dodgex commented Dec 1, 2017

@WonderCsabo think we should simply put a @SuppressWarning on the generated method. What do you think?

@WonderCsabo
Copy link
Member

WonderCsabo commented Dec 1, 2017 via email

@dodgex
Copy link
Member

dodgex commented Dec 1, 2017

okay, have fun. :)

@dodgex
Copy link
Member

dodgex commented Dec 1, 2017

Not sure, but maybe we could just remove the cast for appcompat. iirc the cast is only there as appcompat required it when we implemented the internalFindViewById stuff.

@WonderCsabo
Copy link
Member

This is a little bit more complex. Actually, this was introduced by Android Oreo. Since that version, the actual android.app.Activity has generic return type for findViewById(). AppCompatActivity also declares this method, to make this available on Android < Oreo . However if the user does not use appcompat and the compileSdkVersion is < Oreo, he only sees the old findViewById(). We declare findViewById() in HasViews, and simply it was implemented by Activity, Fragment, ViewGroup etc. But now this is not the case, because they changed the return type. This is why we introduced a new method and always implement it. See #2001 #2008.

One fix would be check if the parent class has generic findViewById(), and if so, do not add the cast. I would like to implement this first.

I am still thinking about if this possible to get rid of the generated internalFindViewById() method completely, because it is totally unnecessary with appcompat or compileSdkVersion >= Oreo.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants