From cf715322bee1ef3bf31dff8ddcbf22b72e2f7c9e Mon Sep 17 00:00:00 2001 From: Vasil Vasilev Date: Thu, 10 Mar 2022 23:38:00 +0100 Subject: [PATCH] Filter out static forwarders for Java package private methods Co-authored-by: Guillaume Martres --- .../src/dotty/tools/dotc/core/SymDenotations.scala | 2 +- tests/run/forwarder-java-package-private.check | 1 + tests/run/forwarder-java-package-private/Base_1.java | 11 +++++++++++ .../run/forwarder-java-package-private/Test_2.scala | 12 ++++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tests/run/forwarder-java-package-private.check create mode 100644 tests/run/forwarder-java-package-private/Base_1.java create mode 100644 tests/run/forwarder-java-package-private/Test_2.scala diff --git a/compiler/src/dotty/tools/dotc/core/SymDenotations.scala b/compiler/src/dotty/tools/dotc/core/SymDenotations.scala index 7be005ea4b8a..e1a54325af03 100644 --- a/compiler/src/dotty/tools/dotc/core/SymDenotations.scala +++ b/compiler/src/dotty/tools/dotc/core/SymDenotations.scala @@ -1388,7 +1388,7 @@ object SymDenotations { final def accessBoundary(base: Symbol)(using Context): Symbol = if (this.is(Private)) owner else if (this.isAllOf(StaticProtected)) defn.RootClass - else if (privateWithin.exists && !ctx.phase.erasedTypes) privateWithin + else if (privateWithin.exists && (!ctx.phase.erasedTypes || this.is(JavaDefined))) privateWithin else if (this.is(Protected)) base else defn.RootClass diff --git a/tests/run/forwarder-java-package-private.check b/tests/run/forwarder-java-package-private.check new file mode 100644 index 000000000000..edb5977b4b2d --- /dev/null +++ b/tests/run/forwarder-java-package-private.check @@ -0,0 +1 @@ +public static java.lang.String scalapackage.Foo.publicMethod() diff --git a/tests/run/forwarder-java-package-private/Base_1.java b/tests/run/forwarder-java-package-private/Base_1.java new file mode 100644 index 000000000000..1ee88795019d --- /dev/null +++ b/tests/run/forwarder-java-package-private/Base_1.java @@ -0,0 +1,11 @@ +package javapackage; + +public class Base_1 { + int packagePrivateMethod() { + return 42; + } + + public String publicMethod() { + return "foo"; + } +} diff --git a/tests/run/forwarder-java-package-private/Test_2.scala b/tests/run/forwarder-java-package-private/Test_2.scala new file mode 100644 index 000000000000..546356b07c56 --- /dev/null +++ b/tests/run/forwarder-java-package-private/Test_2.scala @@ -0,0 +1,12 @@ +package scalapackage: + object Foo extends javapackage.Base_1 +end scalapackage + +object Test { + def main(args: Array[String]): Unit = { + println( + Class.forName("scalapackage.Foo").getMethods + .filter(m => (m.getModifiers & java.lang.reflect.Modifier.STATIC) != 0) + .mkString("\n")) + } +}