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

Function extract

src/resolution/frameworks/java.ts:216–332  ·  view source on GitHub ↗
(filePath, content)

Source from the content-addressed store, hash-verified

214 },
215
216 extract(filePath, content) {
217 // Spring config files (application.yml / application.properties /
218 // bootstrap.yml + per-profile variants) are extracted on the framework
219 // path, not in the language extractor, so the keys become first-class
220 // nodes a `@Value("${k}")` reference can resolve to.
221 if (isSpringConfigFile(filePath)) {
222 return extractSpringConfig(filePath, content);
223 }
224 // Spring Boot is used from both Java and Kotlin (identical @GetMapping etc.
225 // annotations); the difference is method syntax — Kotlin `fun name(...)` vs
226 // Java `public X name(...)` — handled in the method regex below.
227 if (!filePath.endsWith('.java') && !filePath.endsWith('.kt')) return { nodes: [], references: [] };
228 const nodes: Node[] = [];
229 const references: UnresolvedRef[] = [];
230 const now = Date.now();
231 const lang: 'java' | 'kotlin' = filePath.endsWith('.kt') ? 'kotlin' : 'java';
232 const safe = stripCommentsForRegex(content, 'java');
233 const consts = new Map<string, string>();
234 for (const m of safe.matchAll(/\bstatic\s+final\s+String\s+(\w+)\s*=\s*"([^"]*)"\s*;/g)) {
235 consts.set(m[1]!, m[2]!);
236 }
237
238 // Class-level @RequestMapping prefix (an @RequestMapping whose tail leads to a
239 // `class`). Joined onto each method's path — and, crucially, NOT treated as a
240 // route itself (the old regex did, creating one bogus class route and missing
241 // every BARE method mapping like `@PostMapping` with the path on the class).
242 const cls = /@RequestMapping\s*\(([^)]*)\)\s*(?:@[\w.]+(?:\([^)]*\))?\s*)*(?:public\s+|final\s+|abstract\s+|open\s+|data\s+|sealed\s+)*class\b/.exec(safe);
243 const classPrefixes = cls ? parseMappingPaths(cls[1]!, consts) : [''];
244
245 const VERB: Record<string, string> = {
246 GetMapping: 'GET', PostMapping: 'POST', PutMapping: 'PUT', PatchMapping: 'PATCH', DeleteMapping: 'DELETE',
247 };
248 // Verb-specific method mappings — always method-level, BARE or with a path.
249 const mappingRegex = /@(GetMapping|PostMapping|PutMapping|PatchMapping|DeleteMapping)\b/g;
250 let match: RegExpExecArray | null;
251 while ((match = mappingRegex.exec(safe)) !== null) {
252 const annotation = readAnnotation(safe, match.index, lang);
253 if (!annotation) continue;
254 const method = VERB[match[1]!]!;
255 const paths = parseMappingPaths(annotation.args, consts);
256 const line = lineAt(safe, match.index);
257 // The method it decorates: the declaration it is written on (#851).
258 const handler = annotatedMethodName(safe, match.index, lang);
259 for (const routePath of classPrefixes.flatMap(prefix => paths.map(sub => joinPath(prefix, sub)))) {
260 const routeNode: Node = {
261 id: `route:${filePath}:${line}:${method}:${routePath}`,
262 kind: 'route',
263 name: `${method} ${routePath}`,
264 qualifiedName: `${filePath}::route:${routePath}`,
265 filePath,
266 startLine: line,
267 endLine: line,
268 startColumn: 0,
269 endColumn: annotation.end - match.index,
270 language: lang,
271 updatedAt: now,
272 };
273 nodes.push(routeNode);

Callers

nothing calls this directly

Calls 13

stripCommentsForRegexFunction · 0.90
lineAtFunction · 0.90
isSpringConfigFileFunction · 0.85
extractSpringConfigFunction · 0.85
parseMappingPathsFunction · 0.85
readAnnotationFunction · 0.85
annotatedMethodNameFunction · 0.85
mapMethod · 0.80
pushMethod · 0.80
joinPathFunction · 0.70
execMethod · 0.65

Tested by

no test coverage detected