* MyBatis: link a Java mapper interface method to the XML statement that holds * its SQL. The XML extractor (`src/extraction/mybatis-extractor.ts`) qualifies * each ` ` as ` :: ` where * ` ` is the Java FQN of the mapper interface. A J
(queries: QueryBuilder, onYield: MaybeYield)
| 3409 | * mapper interfaces are typically uniquely named within a project. |
| 3410 | */ |
| 3411 | async function mybatisJavaXmlEdges(queries: QueryBuilder, onYield: MaybeYield): Promise<Edge[]> { |
| 3412 | let scanned255 = 0; |
| 3413 | const edges: Edge[] = []; |
| 3414 | const seen = new Set<string>(); |
| 3415 | // Collect the XML side FIRST (mapper `<select id=…>` statements extracted as |
| 3416 | // xml-language method nodes): if the project has none — every Java+XML repo |
| 3417 | // that doesn't use MyBatis — return before paying the full java-method |
| 3418 | // stream below. Same rowid stream order as matching inline, so the edge |
| 3419 | // output is byte-identical when mappers do exist. |
| 3420 | const xmlMethods: Node[] = []; |
| 3421 | for (const m of queries.iterateNodesByKindIn('method', ['xml'])) { |
| 3422 | if ((++scanned255 & 63) === 0) await onYield(); |
| 3423 | xmlMethods.push(m); |
| 3424 | } |
| 3425 | if (xmlMethods.length === 0) return edges; |
| 3426 | |
| 3427 | // Index Java methods by `<ClassName>::<methodName>` for O(1) lookup. |
| 3428 | const javaIndex = new Map<string, Node[]>(); |
| 3429 | for (const m of queries.iterateNodesByKindIn('method', ['java', 'kotlin'])) { |
| 3430 | if ((++scanned255 & 63) === 0) await onYield(); |
| 3431 | const parts = m.qualifiedName.split('::'); |
| 3432 | const last = parts[parts.length - 1]; |
| 3433 | const cls = parts[parts.length - 2]; |
| 3434 | if (!last || !cls) continue; |
| 3435 | const key = `${cls}::${last}`; |
| 3436 | const arr = javaIndex.get(key); |
| 3437 | if (arr) arr.push(m); else javaIndex.set(key, [m]); |
| 3438 | } |
| 3439 | |
| 3440 | for (const xml of xmlMethods) { |
| 3441 | if ((++scanned255 & 63) === 0) await onYield(); |
| 3442 | // Qualified name: `<namespace>::<id>`. Extract the simple class name. |
| 3443 | const colonIdx = xml.qualifiedName.lastIndexOf('::'); |
| 3444 | if (colonIdx < 0) continue; |
| 3445 | const namespace = xml.qualifiedName.slice(0, colonIdx); |
| 3446 | const id = xml.qualifiedName.slice(colonIdx + 2); |
| 3447 | if (!namespace || !id) continue; |
| 3448 | const dotIdx = namespace.lastIndexOf('.'); |
| 3449 | const className = dotIdx >= 0 ? namespace.slice(dotIdx + 1) : namespace; |
| 3450 | const candidates = javaIndex.get(`${className}::${id}`); |
| 3451 | if (!candidates || candidates.length === 0) continue; |
| 3452 | // Drop ambiguous matches (multiple same-name classes); the user can |
| 3453 | // disambiguate by adding the package-suffix match in a future enhancement. |
| 3454 | if (candidates.length > 1) continue; |
| 3455 | const java = candidates[0]!; |
| 3456 | const key = `${java.id}>${xml.id}`; |
| 3457 | if (seen.has(key)) continue; |
| 3458 | seen.add(key); |
| 3459 | edges.push({ |
| 3460 | source: java.id, |
| 3461 | target: xml.id, |
| 3462 | kind: 'calls', |
| 3463 | line: java.startLine, |
| 3464 | provenance: 'heuristic', |
| 3465 | metadata: { |
| 3466 | synthesizedBy: 'mybatis-java-xml', |
| 3467 | via: `${className}.${id}`, |
| 3468 | registeredAt: `${xml.filePath}:${xml.startLine}`, |
no test coverage detected