Skip to content
This repository was archived by the owner on Nov 1, 2022. It is now read-only.
This repository was archived by the owner on Nov 1, 2022. It is now read-only.

TrackingProtectionIconView#onCreateDrawableState getting called before class properties get initialized #4058

@Amejia481

Description

@Amejia481

Related issue mozilla-mobile/fenix#4604.

We are seeing in some situations that siteTrackingProtection: SiteTrackingProtection is getting set to null even though it is a non-null value type and it's has a default value.

This is happening because override fun onCreateDrawableState in some situations is getting called from the super() constructor on the View class, way before the child class (TrackingProtectionIconView) get an opportunity to initialize its values (siteTrackingProtection) as a result we get a null pointer exception.

The below snipped can help to exemplify the issue:

class Parent{
  
  Parent(){
        overrideme();
    }

    public void overrideme() { }
}

class  Child extends Parent{
    String value ="value";

    Child(){
        System.out.println(value);
    }
    
    @Override
    public void overrideme() {
        System.out.println(value);
    }
}

class HelloWorld{
    public static void main(String[] args) {
        Parent p = new Child();
    }
}

Output:

null
value
  1. The first print statement is null because we are trying to use the value property from the super() constructor before is initialized ( In every constructor, the first line to be called is super()).

  2. Now we have the expected because after the super() constructor runs the properties in the child class get initialized.

More info related https://stackoverflow.com/questions/3404301/whats-wrong-with-overridable-method-calls-in-constructors and https://medium.com/keepsafe-engineering/an-in-depth-look-at-kotlins-initializers-a0420fcbf546

┆Issue is synchronized with this Jira Task

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions