Bug 562919

Summary: Lambdas to public methods of package-private class with package-private upper-bound return type raise IllegalArgumentException
Product: [Eclipse Project] JDT Reporter: Jack Smith <kzqmn7+f1b6ut8xes25g>
Component: DebugAssignee: JDT-Debug-Inbox <jdt-debug-inbox>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P3 CC: jarthana, sarika.sinha
Version: 4.15   
Target Milestone: ---   
Hardware: PC   
OS: Windows 10   
Whiteboard: stalebug
Attachments:
Description Flags
Minimal example to demonstrate issue. none

Description Jack Smith CLA 2020-05-07 06:29:46 EDT
Created attachment 282736 [details]
Minimal example to demonstrate issue.

The following example raises an IllegalArgumentException when compiled under Eclipse and NonVisibleLambda is run. No IllegalArgumentException is raised when compiled with javac and run.

stack trace:
Exception in thread "main" java.lang.BootstrapMethodError: java.lang.IllegalAccessError: tried to access class some.other.pkg.AbstractFoo$AbstractFooBuilder from class some.pkg.NonVisibleLambda
	at some.pkg.NonVisibleLambda.main(NonVisibleLambda.java:13)
Caused by: java.lang.IllegalAccessError: tried to access class some.other.pkg.AbstractFoo$AbstractFooBuilder from class some.pkg.NonVisibleLambda
	... 1 more

-------------------------------
some/pkg/NonVisibleLambda.java
-------------------------------
package some.pkg;

import java.util.stream.Stream;

import some.other.pkg.ShinyFoo;
import some.other.pkg.ShinyFoo.ShinyFooBuilder;

public class NonVisibleLambda
{
    public static void main( final String[] args )
    {
        final ShinyFooBuilder builder = ShinyFoo.builder();
        Stream.of( "hello", "hola", "hallo", "bonjour" ).forEach( builder::bar );
        builder.baz( "world" ).build().foo();
    }
}
-------------------------------
some/other/pkg/AbstractFoo.java
-------------------------------
package some.other.pkg;

import java.util.ArrayList;
import java.util.List;

abstract class AbstractFoo
{
    protected final List<String> bars;

    protected AbstractFoo( final AbstractFooBuilder<?, ?> builder )
    {
        bars = builder.bars;
    }

    abstract void foo();

    static abstract class AbstractFooBuilder<T extends AbstractFoo, B extends AbstractFooBuilder<T, B>>
    {
        private final List<String> bars = new ArrayList<>();;

        public B bar( final String bar )
        {
            bars.add( bar );
            return getThis();
        }

        protected B getThis()
        {
            return (B)this;
        }

        protected abstract T build();
    }
}
-------------------------------
some/other/pkg/ShinyFoo.java
-------------------------------
package some.other.pkg;

import static java.util.stream.Collectors.joining;

public class ShinyFoo extends AbstractFoo
{
    public static ShinyFooBuilder builder()
    {
        return new ShinyFooBuilder();
    }

    private final String baz;

    private ShinyFoo( final ShinyFooBuilder builder )
    {
        super( builder );
        baz = builder.baz;
    }

    @Override
    public void foo()
    {
        System.out.println( bars.stream().collect( joining( ", ") ) + ", " + baz + "!" );
    }

    public static class ShinyFooBuilder extends AbstractFooBuilder<ShinyFoo, ShinyFooBuilder>
    {
        private String baz;

        public ShinyFooBuilder baz( final String baz )
        {
            this.baz = baz;
            return getThis();
        }

        @Override
        public ShinyFoo build()
        {
            return new ShinyFoo( this );
        }
    }
}

If the method being referenced by method refernce (i.e. some/other/pkg/AbstractFoo.AbstractFooBuilder.bar(...)):

public B bar( final String bar )
{
    bars.add( bar );
    return getThis();
}

is changed to

public void bar( final String bar )
{
    bars.add( bar );
}

i.e. no return type, then no exception is raised.

Changing the method reference to an expression i.e. 
builder::foo to 
s -> builder.foo( s ) 
is the workaround at present.

Version information:

Eclipse IDE for Java Developers
Version: 2020-03 (4.15.0)
Build id: 20200313-1211

Eclipse Java Development Tools
Version: 3.18.300.v20200305-0155
Build id: I20200305-0155

openjdk version "1.8.0_252"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_252-b09)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.252-b09, mixed mode)

javac 1.8.0_252

Windows 10 64-bit
Microsoft Windows
Verion 1909 (OS Build 18363.657)

n.b. this it NOT a duplicate of https://bugs.eclipse.org/bugs/show_bug.cgi?id=480930 but looks very similar. Bug 480930 had no return type on the method reference - this one does.
Comment 1 Eclipse Genie CLA 2022-04-28 18:02:29 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 2 Sarika Sinha CLA 2022-04-29 01:29:05 EDT
I could reproduce the problem with the latest build. 

Note: Can not move to "Core", due to Github migration.
Comment 3 Eclipse Genie CLA 2024-04-19 01:01:25 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.