Regression: accessing a private method through a type variable

Sundararajan Athijegannathan sundararajan.athijegannathan at oracle.com
Mon Oct 16 09:40:00 UTC 2023


fwiw, this compile from at least JDK 11 onwards:

import java.util.*;
import java.util.stream.*;

public class TDotToString {
  class Bar {
    private String asString() {
      return "bar";
    }
  }

  static <T extends Bar> String foo(List<T> list) {
    return list.stream().map(T::asString).collect(Collectors.joining());
  }
}

javac from 1.8.0 does issue error as mentioned by Remi.


-Sundar
________________________________
From: compiler-dev <compiler-dev-retn at openjdk.org> on behalf of Remi Forax <forax at univ-mlv.fr>
Sent: 16 October 2023 13:57
To: compiler-dev <compiler-dev at openjdk.org>
Subject: Regression: accessing a private method through a type variable

Hello,
There is a regression in recent versions of javac.
javac 21 allows to access a private method through a type variable, here T::asString.

public class TDotToString {
  class Bar() {
    private String asString() {
      return "bar";
    }
  }

  static <T extends Bar> String foo(List<T> list) {
    return list.stream().map(T::asString).collect(Collectors.joining());
  }
}

Both IntelliJ and Eclipse emit an error in this case.

And javac 8 emits
  TDotToString.java:12: error: invalid method reference
    return list.stream().map(T::asString).collect(Collectors.joining());
                             ^
  cannot find symbol
    symbol:   method asString()
    location: bound of type variable T
  where T is a type-variable:
    T extends TDotToString.Bar declared in method <T>foo(List<T>)
1 error

regards,
Rémi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20231016/6a38e338/attachment-0001.htm>


More information about the compiler-dev mailing list