@@ -34,7 +34,7 @@ object ContextOps:
3434 if (elem.name == name) return elem.sym.denot // return self
3535 }
3636 val pre = ctx.owner.thisType
37- if ( ctx.isJava) then javaFindMember(name, pre, required, excluded)
37+ if ctx.isJava then javaFindMember(name, pre, required, excluded)
3838 else pre.findMember(name, pre, required, excluded)
3939 }
4040 else // we are in the outermost context belonging to a class; self is invisible here. See inClassContext.
@@ -43,34 +43,40 @@ object ContextOps:
4343 ctx.scope.denotsNamed(name).filterWithFlags(required, excluded).toDenot(NoPrefix )
4444 }
4545
46- // Only invoke this when ctx.isJava == true
4746 final def javaFindMember (name : Name , pre : Type , required : FlagSet = EmptyFlags , excluded : FlagSet = EmptyFlags ): Denotation =
47+ assert(ctx.isJava)
4848 inContext(ctx) {
49+
4950 val preSym = pre.typeSymbol
50- val denot = pre.findMember(name, pre, required, excluded)
51- if (denot.exists || preSym.isPackageObject || ! preSym.isClass) denot
52- else {
53- // In Java code, static innner classes, which we model as members of the companion object,
54- // can be referenced from an ident in a subclass or by a selection prefixed by the subclass.
55- val toSearch = if (preSym.is(Flags .Module )) then
51+
52+ // 1. Try to search in current type and parents
53+ val directSearch = pre.findMember(name, pre, required, excluded)
54+
55+ // 2. Try to search in companion class if current is an object
56+ def searchCompanionClass = if preSym.is(Flags .Module ) then
57+ preSym.companionClass.thisType.findMember(name, pre, required, excluded)
58+ else NoDenotation
59+
60+ // 3. Try to search in companion objects of super classes.
61+ // In Java code, static inner classes, which we model as members of the companion object,
62+ // can be referenced from an ident in a subclass or by a selection prefixed by the subclass.
63+ def searchSuperCompanionObjects =
64+ val toSearch = if preSym.is(Flags .Module ) then
5665 if preSym.companionClass.exists then
5766 preSym.companionClass.asClass.baseClasses
5867 else Nil
5968 else
6069 preSym.asClass.baseClasses
6170
6271 toSearch.iterator.map { bc =>
63- val pre1 = bc.thisType
64- val found = pre1.findMember(name,pre,required,excluded | Flags .TypeParam )
65- found match {
66- case NoDenotation =>
67- val companionModule = pre1.typeSymbol.companionClass
68- val pre2 = companionModule.thisType
69- pre2.findMember(name, pre2, required, excluded)
70- case denot => denot
71- }
72+ val pre1 = bc.thisType.typeSymbol.companionClass.thisType
73+ pre1.findMember(name, pre1, required, excluded)
7274 }.find(_.exists).getOrElse(NoDenotation )
73- }
75+
76+ if preSym.isClass then
77+ directSearch orElse searchCompanionClass orElse searchSuperCompanionObjects
78+ else
79+ directSearch
7480 }
7581
7682 /** A fresh local context with given tree and owner.
0 commit comments