question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Child class overrides lambda in parent class

See original GitHub issue

This was initially reported in the gradle-retrolambda project, but the problem appears to come from this project.

Copying the report here:


public abstract class ParentActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentView());
        findViewById(R.id.button_parent).setOnClickListener(view -> Toast.makeText(this, "Parent onClick", Toast.LENGTH_LONG).show());
    }

    public abstract int getContentView();
}
public class MainActivity extends ParentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        findViewById(R.id.button_child).setOnClickListener(view -> Toast.makeText(this, "Child onClick", Toast.LENGTH_LONG).show());
    }

    @Override
    public int getContentView() {
        return R.layout.activity_main;
    }
}

Clicking on button_parent shows a Toast with “Child onClick”

test app: https://github.com/cpalasanu/RetrolambdaTest


Using the cfr decompiler, we see that the child activity class has a method that overrides the parent activity class:

Parent:

/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  android.content.Context
 *  android.os.Bundle
 *  android.support.annotation.Nullable
 *  android.support.v7.app.AppCompatActivity
 *  android.view.View
 *  android.view.View$OnClickListener
 *  android.widget.Toast
 */
package com.orange.retrolambdatest;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
import com.orange.retrolambdatest.ParentActivity$$Lambda$1;

public abstract class ParentActivity
extends AppCompatActivity {
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(this.getContentView());
        this.findViewById(2131427412).setOnClickListener(ParentActivity$$Lambda$1.lambdaFactory$(this));
    }

    public abstract int getContentView();

    /* synthetic */ void lambda$onCreate$0(View view) {
        Toast.makeText((Context)this, (CharSequence)"Parent onClick", (int)1).show();
    }
}

Child:

/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  android.content.Context
 *  android.os.Bundle
 *  android.view.View
 *  android.view.View$OnClickListener
 *  android.widget.Toast
 */
package com.orange.retrolambdatest;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.orange.retrolambdatest.MainActivity$$Lambda$1;
import com.orange.retrolambdatest.ParentActivity;

public class MainActivity
extends ParentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.findViewById(2131427413).setOnClickListener(MainActivity$$Lambda$1.lambdaFactory$(this));
    }

    @Override
    public int getContentView() {
        return 2130968602;
    }

    @Override
    /* synthetic */ void lambda$onCreate$0(View view) {
        Toast.makeText((Context)this, (CharSequence)"Child onClick", (int)1).show();
    }
}

Both parent and child have the same method lambda$onCreate$0(View view)


I did some tests building the retrolambda project locally. It appears the bug appears in this commit: https://github.com/orfjackal/retrolambda/commit/bf932457e45817cdee07f34c8c2059db44848e03 (between releases 2.2.0 and 2.3.0) The PR for that commit: https://github.com/orfjackal/retrolambda/pull/86

I have not done any investigation to understand what part of that commit introduces the bug.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:16
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
luontolacommented, Jan 15, 2017

I’ve made a fix for this which converts the private instance methods into package-private static methods. Please try it out. I’ll release a new version soon.

0reactions
luontolacommented, Jan 22, 2017

The fix to this has been released in Retrolambda 2.5.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript - how to inherit class and override lambda method
This method is called from the base constructor, and needs access to instance properties, so it needs to be a lambda function, so...
Read more >
Child class overrides lambda in parent class #207 - GitHub
I just cloned this project, built it and ran it. Clicking on the parent button shows "Child onClick". I made one change: to...
Read more >
How to get child property inside parent class, with lambda?
... prop in the base class, make it overridable and override it in each subclass to return the value that makes sense for...
Read more >
Method overriding in Python - The Digital Cat
In Python method overriding occurs simply defining in the child class a method with the same name of a method in the parent...
Read more >
How to call Base class's overridden method from Derived ...
In this article we will discuss how to call parent class's overridden method from a derived class method in Java. Suppose we have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found