Skip to content

Commit 98b7789

Browse files
authored
Merge pull request scala#10644 from som-snytt/issue/9714-java-names-singleton
Allow module lookup from Java
2 parents fc3c3d5 + 9710b52 commit 98b7789

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
lines changed

src/compiler/scala/tools/nsc/typechecker/Contexts.scala

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1316,8 +1316,20 @@ trait Contexts { self: Analyzer =>
13161316
}
13171317

13181318
final def javaFindMember(pre: Type, name: Name, qualifies: Symbol => Boolean): (Type, Symbol) = {
1319-
val sym = pre.member(name).filter(qualifies)
13201319
val preSym = pre.typeSymbol
1320+
val sym = {
1321+
def asModule =
1322+
if (name.isTypeName && nme.isModuleName(name))
1323+
pre.member(name.dropModule.toTermName) match {
1324+
case nope @ NoSymbol => nope
1325+
case member => member.filter(qualifies).moduleClass
1326+
}
1327+
else NoSymbol
1328+
pre.member(name) match {
1329+
case NoSymbol => asModule
1330+
case member => member.filter(qualifies)
1331+
}
1332+
}
13211333
if (sym.exists || preSym.isPackageClass || !preSym.isClass) (pre, sym)
13221334
else {
13231335
// In Java code, static inner classes, which we model as members of the companion object,

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5356,9 +5356,8 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
53565356
// For Java, instance and static members are in the same scope, but we put the static ones in the companion object
53575357
// so, when we can't find a member in the class scope, check the companion
53585358
def inCompanionForJavaStatic(cls: Symbol, name: Name): Symbol =
5359-
if (!(context.unit.isJava && cls.isClass)) NoSymbol else {
5360-
context.javaFindMember(cls.typeOfThis, name, _.isStaticMember)._2
5361-
}
5359+
if (!(context.unit.isJava && cls.isClass)) NoSymbol
5360+
else context.javaFindMember(cls.typeOfThis, name, s => s.isStaticMember || s.isStaticModule)._2
53625361

53635362
/* Attribute a selection where `tree` is `qual.name`.
53645363
* `qual` is already attributed.
@@ -5379,7 +5378,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
53795378
if (!isPastTyper && isUniversalMember(result.symbol))
53805379
context.warning(tree.pos, s"dubious usage of ${result.symbol} with unit value", WarningCategory.LintUniversalMethods)
53815380

5382-
val sym = tree.symbol orElse member(qual.tpe, name) orElse inCompanionForJavaStatic(qual.tpe.typeSymbol, name)
5381+
val sym = tree.symbol orElse member(qualTp, name) orElse inCompanionForJavaStatic(qualTp.typeSymbol, name)
53835382
if ((sym eq NoSymbol) && name != nme.CONSTRUCTOR && mode.inAny(EXPRmode | PATTERNmode)) {
53845383
// symbol not found? --> try to convert implicitly to a type that does have the required
53855384
// member. Added `| PATTERNmode` to allow enrichment in patterns (so we can add e.g., an

src/reflect/scala/reflect/internal/Names.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ trait Names extends api.Names {
519519

520520
def dropLocal: TermName = toTermName stripSuffix NameTransformer.LOCAL_SUFFIX_STRING
521521
def dropSetter: TermName = toTermName stripSuffix NameTransformer.SETTER_SUFFIX_STRING
522-
def dropModule: ThisNameType = this stripSuffix NameTransformer.MODULE_SUFFIX_STRING
522+
def dropModule: ThisNameType = stripSuffix(NameTransformer.MODULE_SUFFIX_STRING)
523523
def localName: TermName = getterName append NameTransformer.LOCAL_SUFFIX_STRING
524524
def setterName: TermName = getterName append NameTransformer.SETTER_SUFFIX_STRING
525525
def getterName: TermName = dropTraitSetterSeparator.dropSetter.dropLocal

test/files/run/t9714/J.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
package p;
3+
4+
public class J {
5+
public static J j = new J();
6+
public static p.J f() {
7+
return p.J.j;
8+
}
9+
public static p.Module$ module() {
10+
return p.Module$.MODULE$;
11+
}
12+
13+
public String toString() { return "J"; }
14+
}

test/files/run/t9714/Module.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package p {
2+
object Module {
3+
override def toString = "Module"
4+
}
5+
}
6+
7+
object Test extends App {
8+
assert(p.J.f().toString == "J")
9+
assert(p.J.module().toString == "Module")
10+
}

0 commit comments

Comments
 (0)