MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / resolveJavaImportedReference

Function resolveJavaImportedReference

src/resolution/import-resolver.ts:4316–4375  ·  view source on GitHub ↗

* Resolve a Java/Kotlin reference through an import of a project declaration: * `Foo.bar(...)` after `import com.example.Foo`, the class itself (`new Foo()` * emits `Foo` as a `references`/`instantiates` ref), and the member an * `import static com.example.Foo.bar` names. The import is read by qu

(
  ref: UnresolvedRef,
  imports: ImportMapping[],
  context: ResolutionContext
)

Source from the content-addressed store, hash-verified

4314 * found only by its file path (`com/example/Foo.java` or `.kt`) still counts.
4315 */
4316function resolveJavaImportedReference(
4317 ref: UnresolvedRef,
4318 imports: ImportMapping[],
4319 context: ResolutionContext
4320): ResolvedRef | null {
4321 if (imports.length === 0) return null;
4322
4323 for (const imp of importsReadBy(imports, ref.referenceName)) {
4324 const matchesBare = imp.localName === ref.referenceName;
4325 const matchesQualified = ref.referenceName.startsWith(imp.localName + '.');
4326 if (!matchesBare && !matchesQualified) continue;
4327 const memberPath = matchesBare ? [] : ref.referenceName.substring(imp.localName.length + 1).split('.');
4328
4329 // An imported type, and what its name reaches: its members and nested
4330 // types. An imported function or property (`import p.helper`, an
4331 // extension among them) is left to the name strategies, which know a
4332 // Kotlin call's receiver: an untyped chain's last call is no call of it.
4333 const named = jvmImportedDeclarations(imp.source, context);
4334 const declarations = named.filter((n) => JVM_IMPORT_TYPE_KINDS.has(n.kind));
4335 // A Kotlin name can be a function and a type at once (Compose's
4336 // `fun AppBar(…)` beside `sealed interface AppBar`): a call of the name
4337 // alone is the function's, which the file's own path still finds.
4338 const callOfFunction = matchesBare && ref.referenceKind === 'calls' &&
4339 named.some((n) => n.kind === 'function' || n.kind === 'method');
4340 if (declarations.length > 0 && !callOfFunction) {
4341 const owner = declarations.length === 1 ? declarations[0]! : pickJvmDeclaration(declarations, ref);
4342 const target = jvmImportedMember(owner, memberPath, ref, context);
4343 // Of a member's overloads, the one the call's argument count fits:
4344 // `ItemStack.Hash.of(stack, registries)`, not `of(stack)`.
4345 if (target) return retargetSelfOverload({ original: ref, targetNodeId: target.id, confidence: 0.9, resolvedBy: 'import' }, ref, context);
4346 continue;
4347 }
4348
4349 // `UtilsKt.helper()` from Java: a top-level Kotlin function.
4350 if (ref.language === 'java' && memberPath.length === 1) {
4351 const facadeMembers = kotlinFileFacadeMembers(imp.source, memberPath[0]!, context);
4352 if (facadeMembers.length > 0) {
4353 const target = facadeMembers.length === 1 ? facadeMembers[0]! : pickClosestJvmCandidate(facadeMembers, ref.filePath);
4354 return retargetSelfOverload({ original: ref, targetNodeId: target.id, confidence: 0.9, resolvedBy: 'import' }, ref, context);
4355 }
4356 }
4357
4358 // A declaration the index holds under another name: found by the
4359 // import's path (`com/example/Foo.java` or `.kt`, whichever the
4360 // declaring file is), under any source root. Its member, by name.
4361 const memberName = matchesBare ? imp.localName : memberPath.join('.');
4362 const fqnPath = imp.source.replace(/\./g, '/');
4363 const ownerPath = imp.source.includes('.') ? imp.source.substring(0, imp.source.lastIndexOf('.')).replace(/\./g, '/') : '';
4364 const inFile = (node: Node, file: string): boolean => {
4365 const fp = node.filePath.replace(/\\/g, '/');
4366 return ['.java', '.kt'].some((ext) => fp.endsWith(file + ext) && (fp.length === file.length + ext.length || fp[fp.length - file.length - ext.length - 1] === '/'));
4367 };
4368 const candidates = context.getNodesByName(memberName).filter((n) => JVM_IMPORT_LANGUAGES.has(n.language));
4369 const byPath = candidates.find((n) => inFile(n, fqnPath)) ??
4370 // `import static com.example.Foo.bar;`: the member's owner names the file.
4371 (matchesBare && ownerPath ? candidates.find((n) => inFile(n, ownerPath)) : undefined);
4372 if (byPath) return retargetSelfOverload({ original: ref, targetNodeId: byPath.id, confidence: 0.9, resolvedBy: 'import' }, ref, context);
4373 }

Callers 1

resolveViaImportFunction · 0.85

Calls 10

importsReadByFunction · 0.90
jvmImportedDeclarationsFunction · 0.85
pickJvmDeclarationFunction · 0.85
jvmImportedMemberFunction · 0.85
kotlinFileFacadeMembersFunction · 0.85
pickClosestJvmCandidateFunction · 0.85
hasMethod · 0.80
inFileFunction · 0.70
getNodesByNameMethod · 0.65
joinMethod · 0.45

Tested by

no test coverage detected