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

internalFindViewById raises "unchecked cast" warning #2090

Description

@ened

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.

Activity

  1. dodgex commented on Dec 1, 2017

    @dodgex
    Member

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

  2. WonderCsabo commented on Dec 1, 2017

    @WonderCsabo
    Member
  3. dodgex commented on Dec 1, 2017

    @dodgex
    Member

    okay, have fun. :)

  4. dodgex commented on Dec 1, 2017

    @dodgex
    Member

    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.

  5. WonderCsabo commented on Dec 1, 2017

    @WonderCsabo
    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 join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions