circle-ir 4.19.0 → 4.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (262) hide show
  1. package/dist/analysis/advisories.d.ts +21 -3
  2. package/dist/analysis/advisories.d.ts.map +1 -1
  3. package/dist/analysis/advisories.js +26 -8
  4. package/dist/analysis/advisories.js.map +1 -1
  5. package/dist/analysis/arg-executing-programs.d.ts +16 -0
  6. package/dist/analysis/arg-executing-programs.d.ts.map +1 -0
  7. package/dist/analysis/arg-executing-programs.js +24 -0
  8. package/dist/analysis/arg-executing-programs.js.map +1 -0
  9. package/dist/analysis/config-loader.d.ts.map +1 -1
  10. package/dist/analysis/config-loader.js +27 -3
  11. package/dist/analysis/config-loader.js.map +1 -1
  12. package/dist/analysis/constant-caller-gate.d.ts +3 -1
  13. package/dist/analysis/constant-caller-gate.d.ts.map +1 -1
  14. package/dist/analysis/constant-caller-gate.js +108 -1
  15. package/dist/analysis/constant-caller-gate.js.map +1 -1
  16. package/dist/analysis/constant-propagation/index.d.ts.map +1 -1
  17. package/dist/analysis/constant-propagation/index.js +1 -1
  18. package/dist/analysis/constant-propagation/index.js.map +1 -1
  19. package/dist/analysis/constant-propagation/patterns.d.ts.map +1 -1
  20. package/dist/analysis/constant-propagation/patterns.js +8 -0
  21. package/dist/analysis/constant-propagation/patterns.js.map +1 -1
  22. package/dist/analysis/constant-propagation/propagator.d.ts +47 -2
  23. package/dist/analysis/constant-propagation/propagator.d.ts.map +1 -1
  24. package/dist/analysis/constant-propagation/propagator.js +422 -57
  25. package/dist/analysis/constant-propagation/propagator.js.map +1 -1
  26. package/dist/analysis/constant-propagation/types.d.ts +2 -0
  27. package/dist/analysis/constant-propagation/types.d.ts.map +1 -1
  28. package/dist/analysis/dependency-versions.d.ts +33 -0
  29. package/dist/analysis/dependency-versions.d.ts.map +1 -1
  30. package/dist/analysis/dependency-versions.js +70 -0
  31. package/dist/analysis/dependency-versions.js.map +1 -1
  32. package/dist/analysis/findings.d.ts.map +1 -1
  33. package/dist/analysis/findings.js +7 -5
  34. package/dist/analysis/findings.js.map +1 -1
  35. package/dist/analysis/interprocedural.d.ts +16 -0
  36. package/dist/analysis/interprocedural.d.ts.map +1 -1
  37. package/dist/analysis/interprocedural.js +545 -197
  38. package/dist/analysis/interprocedural.js.map +1 -1
  39. package/dist/analysis/passes/constant-propagation-pass.d.ts.map +1 -1
  40. package/dist/analysis/passes/constant-propagation-pass.js +1 -0
  41. package/dist/analysis/passes/constant-propagation-pass.js.map +1 -1
  42. package/dist/analysis/passes/cross-file-pass.d.ts.map +1 -1
  43. package/dist/analysis/passes/cross-file-pass.js +61 -0
  44. package/dist/analysis/passes/cross-file-pass.js.map +1 -1
  45. package/dist/analysis/passes/csharp-sanitizer-credit.d.ts.map +1 -1
  46. package/dist/analysis/passes/csharp-sanitizer-credit.js +60 -3
  47. package/dist/analysis/passes/csharp-sanitizer-credit.js.map +1 -1
  48. package/dist/analysis/passes/csrf-samesite-none-session-pass.d.ts +58 -0
  49. package/dist/analysis/passes/csrf-samesite-none-session-pass.d.ts.map +1 -0
  50. package/dist/analysis/passes/csrf-samesite-none-session-pass.js +159 -0
  51. package/dist/analysis/passes/csrf-samesite-none-session-pass.js.map +1 -0
  52. package/dist/analysis/passes/deserialization-safety-gate-pass.d.ts +15 -0
  53. package/dist/analysis/passes/deserialization-safety-gate-pass.d.ts.map +1 -1
  54. package/dist/analysis/passes/deserialization-safety-gate-pass.js +60 -1
  55. package/dist/analysis/passes/deserialization-safety-gate-pass.js.map +1 -1
  56. package/dist/analysis/passes/interprocedural-pass.d.ts +23 -1
  57. package/dist/analysis/passes/interprocedural-pass.d.ts.map +1 -1
  58. package/dist/analysis/passes/interprocedural-pass.js +220 -7
  59. package/dist/analysis/passes/interprocedural-pass.js.map +1 -1
  60. package/dist/analysis/passes/language-sources-pass.d.ts +0 -18
  61. package/dist/analysis/passes/language-sources-pass.d.ts.map +1 -1
  62. package/dist/analysis/passes/language-sources-pass.js +549 -22
  63. package/dist/analysis/passes/language-sources-pass.js.map +1 -1
  64. package/dist/analysis/passes/param-source-flow-gate-pass.d.ts.map +1 -1
  65. package/dist/analysis/passes/param-source-flow-gate-pass.js +71 -1
  66. package/dist/analysis/passes/param-source-flow-gate-pass.js.map +1 -1
  67. package/dist/analysis/passes/prompt-injection-safety-gate-pass.js +1 -1
  68. package/dist/analysis/passes/prompt-injection-safety-gate-pass.js.map +1 -1
  69. package/dist/analysis/passes/resource-leak-pass.d.ts.map +1 -1
  70. package/dist/analysis/passes/resource-leak-pass.js +15 -3
  71. package/dist/analysis/passes/resource-leak-pass.js.map +1 -1
  72. package/dist/analysis/passes/scan-secrets-pass.js +2 -2
  73. package/dist/analysis/passes/scan-secrets-pass.js.map +1 -1
  74. package/dist/analysis/passes/sensitive-data-in-log-pass.d.ts +66 -0
  75. package/dist/analysis/passes/sensitive-data-in-log-pass.d.ts.map +1 -0
  76. package/dist/analysis/passes/sensitive-data-in-log-pass.js +462 -0
  77. package/dist/analysis/passes/sensitive-data-in-log-pass.js.map +1 -0
  78. package/dist/analysis/passes/sink-filter-pass.d.ts +7 -1
  79. package/dist/analysis/passes/sink-filter-pass.d.ts.map +1 -1
  80. package/dist/analysis/passes/sink-filter-pass.js +564 -22
  81. package/dist/analysis/passes/sink-filter-pass.js.map +1 -1
  82. package/dist/analysis/passes/taint-propagation-pass.d.ts +7 -3
  83. package/dist/analysis/passes/taint-propagation-pass.d.ts.map +1 -1
  84. package/dist/analysis/passes/taint-propagation-pass.js +464 -27
  85. package/dist/analysis/passes/taint-propagation-pass.js.map +1 -1
  86. package/dist/analysis/passes/xml-entity-expansion-pass.d.ts.map +1 -1
  87. package/dist/analysis/passes/xml-entity-expansion-pass.js +50 -6
  88. package/dist/analysis/passes/xml-entity-expansion-pass.js.map +1 -1
  89. package/dist/analysis/pep440-version.d.ts +40 -0
  90. package/dist/analysis/pep440-version.d.ts.map +1 -0
  91. package/dist/analysis/pep440-version.js +149 -0
  92. package/dist/analysis/pep440-version.js.map +1 -0
  93. package/dist/analysis/project-callee-escape-gate.js +1 -1
  94. package/dist/analysis/project-callee-escape-gate.js.map +1 -1
  95. package/dist/analysis/rules.d.ts.map +1 -1
  96. package/dist/analysis/rules.js +15 -0
  97. package/dist/analysis/rules.js.map +1 -1
  98. package/dist/analysis/sbom.d.ts +92 -4
  99. package/dist/analysis/sbom.d.ts.map +1 -1
  100. package/dist/analysis/sbom.js +390 -33
  101. package/dist/analysis/sbom.js.map +1 -1
  102. package/dist/analysis/taint-matcher.d.ts.map +1 -1
  103. package/dist/analysis/taint-matcher.js +124 -19
  104. package/dist/analysis/taint-matcher.js.map +1 -1
  105. package/dist/analysis/taint-propagation.d.ts +1 -0
  106. package/dist/analysis/taint-propagation.d.ts.map +1 -1
  107. package/dist/analysis/taint-propagation.js +32 -0
  108. package/dist/analysis/taint-propagation.js.map +1 -1
  109. package/dist/analysis/test-to-production-taint-gate.d.ts +13 -0
  110. package/dist/analysis/test-to-production-taint-gate.d.ts.map +1 -0
  111. package/dist/analysis/test-to-production-taint-gate.js +30 -0
  112. package/dist/analysis/test-to-production-taint-gate.js.map +1 -0
  113. package/dist/analyzer.d.ts +21 -3
  114. package/dist/analyzer.d.ts.map +1 -1
  115. package/dist/analyzer.js +83 -3
  116. package/dist/analyzer.js.map +1 -1
  117. package/dist/browser/circle-ir.js +8118 -2526
  118. package/dist/core/circle-ir-core.cjs +3098 -2280
  119. package/dist/core/circle-ir-core.js +3036 -2214
  120. package/dist/core/extractors/calls.d.ts +10 -1
  121. package/dist/core/extractors/calls.d.ts.map +1 -1
  122. package/dist/core/extractors/calls.js +148 -6
  123. package/dist/core/extractors/calls.js.map +1 -1
  124. package/dist/core/extractors/cfg.d.ts.map +1 -1
  125. package/dist/core/extractors/cfg.js +12 -0
  126. package/dist/core/extractors/cfg.js.map +1 -1
  127. package/dist/core/extractors/dfg.d.ts.map +1 -1
  128. package/dist/core/extractors/dfg.js +151 -18
  129. package/dist/core/extractors/dfg.js.map +1 -1
  130. package/dist/core/extractors/ts-declaration-detail.d.ts +8 -0
  131. package/dist/core/extractors/ts-declaration-detail.d.ts.map +1 -1
  132. package/dist/core/extractors/ts-declaration-detail.js +22 -0
  133. package/dist/core/extractors/ts-declaration-detail.js.map +1 -1
  134. package/dist/core/extractors/types.d.ts +5 -0
  135. package/dist/core/extractors/types.d.ts.map +1 -1
  136. package/dist/core/extractors/types.js +149 -4
  137. package/dist/core/extractors/types.js.map +1 -1
  138. package/dist/core/parser.d.ts.map +1 -1
  139. package/dist/core/parser.js +3 -1
  140. package/dist/core/parser.js.map +1 -1
  141. package/dist/core/ts-grammar-workarounds.d.ts +18 -0
  142. package/dist/core/ts-grammar-workarounds.d.ts.map +1 -0
  143. package/dist/core/ts-grammar-workarounds.js +40 -0
  144. package/dist/core/ts-grammar-workarounds.js.map +1 -0
  145. package/dist/facts/arrow.d.ts +106 -0
  146. package/dist/facts/arrow.d.ts.map +1 -0
  147. package/dist/facts/arrow.js +196 -0
  148. package/dist/facts/arrow.js.map +1 -0
  149. package/dist/facts/facts2.d.ts +68 -0
  150. package/dist/facts/facts2.d.ts.map +1 -0
  151. package/dist/facts/facts2.js +379 -0
  152. package/dist/facts/facts2.js.map +1 -0
  153. package/dist/facts/index.d.ts +22 -0
  154. package/dist/facts/index.d.ts.map +1 -0
  155. package/dist/facts/index.js +15 -0
  156. package/dist/facts/index.js.map +1 -0
  157. package/dist/facts/java-facts.d.ts +56 -0
  158. package/dist/facts/java-facts.d.ts.map +1 -0
  159. package/dist/facts/java-facts.js +250 -0
  160. package/dist/facts/java-facts.js.map +1 -0
  161. package/dist/facts/order.d.ts +39 -0
  162. package/dist/facts/order.d.ts.map +1 -0
  163. package/dist/facts/order.js +120 -0
  164. package/dist/facts/order.js.map +1 -0
  165. package/dist/facts/schema.d.ts +60 -0
  166. package/dist/facts/schema.d.ts.map +1 -0
  167. package/dist/facts/schema.js +126 -0
  168. package/dist/facts/schema.js.map +1 -0
  169. package/dist/facts/schema2.d.ts +47 -0
  170. package/dist/facts/schema2.d.ts.map +1 -0
  171. package/dist/facts/schema2.js +194 -0
  172. package/dist/facts/schema2.js.map +1 -0
  173. package/dist/facts/t6.d.ts +84 -0
  174. package/dist/facts/t6.d.ts.map +1 -0
  175. package/dist/facts/t6.js +226 -0
  176. package/dist/facts/t6.js.map +1 -0
  177. package/dist/facts/table.d.ts +80 -0
  178. package/dist/facts/table.d.ts.map +1 -0
  179. package/dist/facts/table.js +548 -0
  180. package/dist/facts/table.js.map +1 -0
  181. package/dist/graph-facts/dead-code.d.ts +2 -0
  182. package/dist/graph-facts/dead-code.d.ts.map +1 -1
  183. package/dist/graph-facts/dead-code.js +4 -3
  184. package/dist/graph-facts/dead-code.js.map +1 -1
  185. package/dist/graph-facts/index.d.ts +1 -1
  186. package/dist/graph-facts/index.d.ts.map +1 -1
  187. package/dist/graph-facts/index.js +1 -1
  188. package/dist/graph-facts/index.js.map +1 -1
  189. package/dist/graph-facts/reach.d.ts +53 -8
  190. package/dist/graph-facts/reach.d.ts.map +1 -1
  191. package/dist/graph-facts/reach.js +172 -16
  192. package/dist/graph-facts/reach.js.map +1 -1
  193. package/dist/graph-facts/removal-reach.d.ts +2 -0
  194. package/dist/graph-facts/removal-reach.d.ts.map +1 -1
  195. package/dist/graph-facts/removal-reach.js +1 -1
  196. package/dist/graph-facts/removal-reach.js.map +1 -1
  197. package/dist/index.d.ts +15 -7
  198. package/dist/index.d.ts.map +1 -1
  199. package/dist/index.js +19 -4
  200. package/dist/index.js.map +1 -1
  201. package/dist/navigation/index.d.ts +1 -0
  202. package/dist/navigation/index.d.ts.map +1 -1
  203. package/dist/navigation/index.js +1 -0
  204. package/dist/navigation/index.js.map +1 -1
  205. package/dist/navigation/java-graph.d.ts +47 -2
  206. package/dist/navigation/java-graph.d.ts.map +1 -1
  207. package/dist/navigation/java-graph.js +395 -18
  208. package/dist/navigation/java-graph.js.map +1 -1
  209. package/dist/navigation/java-registrations.d.ts +93 -0
  210. package/dist/navigation/java-registrations.d.ts.map +1 -0
  211. package/dist/navigation/java-registrations.js +254 -0
  212. package/dist/navigation/java-registrations.js.map +1 -0
  213. package/dist/navigation/navigation-index.d.ts +74 -3
  214. package/dist/navigation/navigation-index.d.ts.map +1 -1
  215. package/dist/navigation/navigation-index.js +206 -11
  216. package/dist/navigation/navigation-index.js.map +1 -1
  217. package/dist/navigation/ts-call-edges.d.ts +15 -0
  218. package/dist/navigation/ts-call-edges.d.ts.map +1 -1
  219. package/dist/navigation/ts-call-edges.js.map +1 -1
  220. package/dist/navigation/ts-entry-tables.d.ts +1 -1
  221. package/dist/navigation/ts-entry-tables.d.ts.map +1 -1
  222. package/dist/navigation/ts-entry-tables.js.map +1 -1
  223. package/dist/navigation/xml-scan.d.ts +40 -0
  224. package/dist/navigation/xml-scan.d.ts.map +1 -0
  225. package/dist/navigation/xml-scan.js +269 -0
  226. package/dist/navigation/xml-scan.js.map +1 -0
  227. package/dist/resolution/cross-file.d.ts +44 -0
  228. package/dist/resolution/cross-file.d.ts.map +1 -1
  229. package/dist/resolution/cross-file.js +259 -13
  230. package/dist/resolution/cross-file.js.map +1 -1
  231. package/dist/stable-ids/files.d.ts +22 -0
  232. package/dist/stable-ids/files.d.ts.map +1 -0
  233. package/dist/stable-ids/files.js +28 -0
  234. package/dist/stable-ids/files.js.map +1 -0
  235. package/dist/stable-ids/hash.d.ts +16 -4
  236. package/dist/stable-ids/hash.d.ts.map +1 -1
  237. package/dist/stable-ids/hash.js +266 -80
  238. package/dist/stable-ids/hash.js.map +1 -1
  239. package/dist/stable-ids/index.d.ts +6 -5
  240. package/dist/stable-ids/index.d.ts.map +1 -1
  241. package/dist/stable-ids/index.js +19 -6
  242. package/dist/stable-ids/index.js.map +1 -1
  243. package/dist/stable-ids/nodes.d.ts +61 -7
  244. package/dist/stable-ids/nodes.d.ts.map +1 -1
  245. package/dist/stable-ids/nodes.js +134 -21
  246. package/dist/stable-ids/nodes.js.map +1 -1
  247. package/dist/stable-ids/ts-parse-errors.d.ts +5 -1
  248. package/dist/stable-ids/ts-parse-errors.d.ts.map +1 -1
  249. package/dist/stable-ids/ts-parse-errors.js +55 -3
  250. package/dist/stable-ids/ts-parse-errors.js.map +1 -1
  251. package/dist/stable-ids/typescript.d.ts +5 -0
  252. package/dist/stable-ids/typescript.d.ts.map +1 -1
  253. package/dist/stable-ids/typescript.js +41 -8
  254. package/dist/stable-ids/typescript.js.map +1 -1
  255. package/dist/types/index.d.ts +21 -3
  256. package/dist/types/index.d.ts.map +1 -1
  257. package/dist/types/index.js +1 -0
  258. package/dist/types/index.js.map +1 -1
  259. package/docs/SPEC.md +9 -0
  260. package/package.json +9 -5
  261. package/schemas/facts@1.json +134 -0
  262. package/schemas/facts@2.json +216 -0
@@ -12,11 +12,15 @@
12
12
  * - **Entries** (`entry-index@0.1`): Spring and JAX-RS routes with class
13
13
  * prefixes, servlets and filters, Spring beans, lifecycle, scheduled and
14
14
  * listener methods, `main`, JUnit/TestNG tests and test files,
15
- * `Class.forName` literals and `META-INF/services` files.
15
+ * `Class.forName` literals and `META-INF/services` files; and the classes
16
+ * and methods registering files name (OSGi DS component descriptions,
17
+ * `MANIFEST.MF`, Eclipse `plugin.xml`, servlet `web.xml`; see
18
+ * `java-registrations.ts`).
16
19
  * - **typeOnly**: interfaces, annotation types and body-less methods.
17
20
  *
18
21
  * Library code: no file access; the caller passes the sources (and may pass
19
- * each file's IR, which must come from `analyze` with `navigationTypes`).
22
+ * each file's IR, which must come from `analyze` with `navigationTypes`) and
23
+ * any registering files as text.
20
24
  */
21
25
  import { analyze } from '../analyzer.js';
22
26
  import { disposeTree, parse } from '../core/parser.js';
@@ -25,8 +29,26 @@ import { declLocator } from '../stable-ids/nodes.js';
25
29
  import { namedChildren } from '../stable-ids/normalize.js';
26
30
  import { buildNavigationIndex } from './navigation-index.js';
27
31
  import { DEFAULT_JAVA_ENTRY_TABLES } from './java-entry-tables.js';
32
+ import { scanRegistrations } from './java-registrations.js';
28
33
  import { joinRoutePath } from './ts-call-edges.js';
34
+ import { facts2ToArrow, factsToArrow } from '../facts/arrow.js';
35
+ import { javaGraphFactSet } from '../facts/java-facts.js';
36
+ import { javaGraphFact2Set, markDeclaringEdge } from '../facts/facts2.js';
29
37
  export const JAVA_GRAPH_SCHEME = 'java-graph@0.1';
38
+ /** Every `import` of a file, as the `imports` relation records it (static imports included). */
39
+ function importRows(root) {
40
+ const out = [];
41
+ for (const c of namedChildren(root)) {
42
+ if (c.type !== 'import_declaration')
43
+ continue;
44
+ const text = c.text.replace(/^import\s+/, '').replace(/;\s*$/, '').replace(/^static\s+/, '').replace(/\s+/g, '');
45
+ if (text.endsWith('.*'))
46
+ out.push({ target: text.slice(0, -2), wildcard: true });
47
+ else
48
+ out.push({ target: text, wildcard: false });
49
+ }
50
+ return out;
51
+ }
30
52
  function importsOf(root) {
31
53
  const out = { pkg: '', explicit: new Map(), wildcards: [] };
32
54
  for (const c of namedChildren(root)) {
@@ -118,9 +140,18 @@ class JavaGraphBuilder {
118
140
  references = [];
119
141
  unresolved = [];
120
142
  unresolvedRegistrations = [];
143
+ /** Registrations with a `bound`, filled with `boundTypes` once the type hierarchy is known (#1092). */
144
+ pendingBounds = [];
121
145
  typeOnly = new Set();
122
146
  typesBySimple = new Map();
123
147
  typesByQname = new Map();
148
+ /** A class's superclass as written, with the file that wrote it (registered members may be inherited). */
149
+ superclassOf = new Map();
150
+ /** Public top-level types by package (`Export-Package`). */
151
+ publicTypesByPkg = new Map();
152
+ /** Declared constructors with a `public` modifier. */
153
+ publicCtors = new Set();
154
+ childrenByParent = null;
124
155
  constructor(files, tables) {
125
156
  this.files = files;
126
157
  this.tables = tables;
@@ -167,6 +198,18 @@ class JavaGraphBuilder {
167
198
  return;
168
199
  if (node.type === 'interface_declaration' || node.type === 'annotation_type_declaration')
169
200
  this.typeOnly.add(type.id);
201
+ const superclass = node.childForFieldName('superclass');
202
+ if (superclass) {
203
+ const written = superclass.text.replace(/^extends\s+/, '').replace(/<.*$/s, '').replace(/\s+/g, '');
204
+ if (written)
205
+ this.superclassOf.set(type.id, { written, ctx });
206
+ }
207
+ if (node.parent?.type === 'program' && namedChildren(node).find(c => c.type === 'modifiers')?.children.some(c => c.type === 'public')) {
208
+ const pkg = ctx.imports.pkg;
209
+ const list = this.publicTypesByPkg.get(pkg) ?? [];
210
+ list.push(type);
211
+ this.publicTypesByPkg.set(pkg, list);
212
+ }
170
213
  const classAnns = annotationsOf(node, ctx.imports);
171
214
  // Class-level entries (beans).
172
215
  for (const row of this.tables.annotations) {
@@ -196,6 +239,15 @@ class JavaGraphBuilder {
196
239
  return pkg === null ? ctx.imports.wildcards.some(w => row.packages.includes(w)) : row.packages.includes(pkg);
197
240
  }));
198
241
  const body = node.childForFieldName('body');
242
+ for (const c of body ? namedChildren(body) : []) {
243
+ if (c.type !== 'constructor_declaration')
244
+ continue;
245
+ if (!namedChildren(c).find(x => x.type === 'modifiers')?.children.some(x => x.type === 'public'))
246
+ continue;
247
+ const d = declAt(ctx, c, 'method');
248
+ if (d)
249
+ this.publicCtors.add(d.id);
250
+ }
199
251
  const methodNodes = body ? namedChildren(body).filter(m => m.type === 'method_declaration' || m.type === 'annotation_type_element_declaration') : [];
200
252
  for (const row of servlets) {
201
253
  const ann = classAnns.find(a => a.name === row.pathAnnotation);
@@ -293,10 +345,14 @@ class JavaGraphBuilder {
293
345
  const target = parts[0];
294
346
  const last = parts[parts.length - 1];
295
347
  const isNew = node.text.trimEnd().endsWith('::new');
348
+ // #1093: `T[]::new` creates an array; it calls no declared constructor.
349
+ if (isNew && target?.type === 'array_type')
350
+ return;
296
351
  const name = isNew ? 'constructor' : last?.text ?? '';
297
352
  const site = { file: ctx.path, line: node.startPosition.row + 1, col: node.startPosition.column };
298
353
  const from = (ctx.locate(site.line, site.col) ?? ctx.ids.decls[0]).id;
299
354
  let types = [];
355
+ let typeName = null;
300
356
  if (target?.type === 'this' || target?.type === 'super') {
301
357
  // The enclosing class's own methods (an inherited one is reached through the call graph's types).
302
358
  let enclosing = ctx.locate(site.line, site.col);
@@ -305,9 +361,13 @@ class JavaGraphBuilder {
305
361
  if (enclosing)
306
362
  types = [enclosing];
307
363
  }
308
- else if (target && (target.type === 'identifier' || target.type === 'type_identifier' || target.type === 'scoped_type_identifier' || target.type === 'generic_type')) {
309
- const simple = target.text.replace(/<.*$/s, '').split('.').pop() ?? '';
364
+ else if (target && (target.type === 'identifier' || target.type === 'type_identifier' || target.type === 'scoped_type_identifier' || target.type === 'generic_type'
365
+ // #1093: a qualified type before `::new` (`java.util.LinkedList::new`) parses as an expression
366
+ || (isNew && (target.type === 'field_access' || target.type === 'scoped_identifier')))) {
367
+ const written = target.text.replace(/<.*$/s, '');
368
+ const simple = written.split('.').pop() ?? '';
310
369
  const fqnPkg = ctx.imports.explicit.get(simple);
370
+ typeName = written.includes('.') ? written : fqnPkg ? `${fqnPkg}.${simple}` : simple;
311
371
  const all = this.typesBySimple.get(simple) ?? [];
312
372
  const exact = fqnPkg ? all.filter(t => t.qualified_name === `${fqnPkg}:${simple}`) : all.filter(t => t.qualified_name.startsWith(`${ctx.imports.pkg || '<default>'}:`));
313
373
  types = exact.length > 0 ? exact : all;
@@ -316,6 +376,12 @@ class JavaGraphBuilder {
316
376
  if (isNew && targets.length === 0)
317
377
  targets.push(...types);
318
378
  if (targets.length === 0) {
379
+ // #1093: `Ext::new` on a type that is not declared in the tree is a call into a
380
+ // library, like `new Ext()`: external, naming its receiver, not every constructor.
381
+ if (isNew && types.length === 0 && typeName) {
382
+ this.unresolved.push({ from, methodName: name, site, reason: 'external', evidence: 'constructor reference to a type outside the tree', receiver: typeName });
383
+ return;
384
+ }
319
385
  this.unresolved.push({ from, methodName: name, site, reason: 'dynamic', evidence: 'method reference not tied to a declaration' });
320
386
  return;
321
387
  }
@@ -329,23 +395,162 @@ class JavaGraphBuilder {
329
395
  const args = node.childForFieldName('arguments');
330
396
  const first = args ? namedChildren(args)[0] : undefined;
331
397
  const site = { file: ctx.path, line: node.startPosition.row + 1, col: node.startPosition.column };
332
- if (first?.type !== 'string_literal') {
333
- this.unresolvedRegistrations.push({ site, reason: 'handler-unresolved', evidence: 'Class.forName with a computed name' });
398
+ if (first?.type === 'string_literal') {
399
+ this.reflect(stringsOf(first)[0] ?? '', ctx.path, 'Class.forName');
334
400
  return;
335
401
  }
336
- this.reflect(stringsOf(first)[0] ?? '', ctx.path, 'Class.forName');
402
+ // #1092: a local (or static final field) whose every assignment is a string literal
403
+ // is the literal case: each name is a reflection entry, a name outside the tree nothing.
404
+ const method = enclosingBody(node);
405
+ if (first?.type === 'identifier' && method) {
406
+ const names = constantStrings(method, first.text, node);
407
+ if (names) {
408
+ for (const n of names)
409
+ this.reflect(n, ctx.path, 'Class.forName (local constant)');
410
+ return;
411
+ }
412
+ }
413
+ const from = (ctx.locate(site.line, site.col) ?? ctx.ids.decls[0]).id;
414
+ const bound = method ? boundTypeNames(method.text) : [];
415
+ const reg = { site, reason: 'handler-unresolved', evidence: 'Class.forName with a computed name', from };
416
+ if (bound.length > 0) {
417
+ reg.bound = bound.map(b => resolveTypeName(b, ctx));
418
+ this.pendingBounds.push(reg);
419
+ }
420
+ this.unresolvedRegistrations.push(reg);
337
421
  }
338
- reflect(fqn, file, evidence) {
422
+ /** `instantiated`: the loader also constructs the class (`ServiceLoader`), not only loads it (`Class.forName`). */
423
+ reflect(fqn, file, evidence, instantiated = false) {
424
+ const t = this.typeByName(fqn);
425
+ if (!t)
426
+ return;
427
+ const constructors = instantiated ? this.instantiationConstructors(t) : [];
428
+ this.entry(t, 'reflection', 'declared', file, evidence, constructors.length > 0 ? { constructors } : {});
429
+ }
430
+ /**
431
+ * The constructors a runtime that instantiates the class by reflection runs:
432
+ * its public no-arg constructor; when it declares none, its public
433
+ * constructors (constructor injection). Empty when it declares no
434
+ * constructor at all (the implicit one has no declaration).
435
+ */
436
+ instantiationConstructors(type) {
437
+ const pub = this.childrenOf(type.id).filter(d => d.kind === 'method' && d.name === 'constructor' && this.publicCtors.has(d.id));
438
+ const noArg = pub.filter(d => d.signature === '()');
439
+ return (noArg.length > 0 ? noArg : pub).map(d => d.id);
440
+ }
441
+ /** The tree's type for a binary (`a.B$C`) or canonical (`a.B.C`) name. */
442
+ typeByName(fqn) {
339
443
  const clean = fqn.trim().replace(/\$/g, '.');
340
444
  // `a.b.Outer.Inner`: try each split point between package and type path.
341
445
  const segs = clean.split('.');
342
446
  for (let i = segs.length - 1; i >= 0; i--) {
343
447
  const t = this.typesByQname.get(`${segs.slice(0, i).join('.') || '<default>'}:${segs.slice(i).join('.')}`);
344
- if (t) {
345
- this.entry(t, 'reflection', 'declared', file, evidence);
346
- return;
448
+ if (t)
449
+ return t;
450
+ }
451
+ return undefined;
452
+ }
453
+ childrenOf(id) {
454
+ if (!this.childrenByParent) {
455
+ this.childrenByParent = new Map();
456
+ for (const f of this.files) {
457
+ for (const d of f.ids.decls) {
458
+ if (d.parent === null)
459
+ continue;
460
+ const list = this.childrenByParent.get(d.parent) ?? [];
461
+ list.push(d);
462
+ this.childrenByParent.set(d.parent, list);
463
+ }
347
464
  }
348
465
  }
466
+ return this.childrenByParent.get(id) ?? [];
467
+ }
468
+ /** The in-tree superclass of a class, from its `extends` clause, the file's imports and its package. */
469
+ superclass(type) {
470
+ const s = this.superclassOf.get(type.id);
471
+ if (!s)
472
+ return undefined;
473
+ const { written, ctx } = s;
474
+ const pkg = ctx.imports.pkg || '<default>';
475
+ const first = written.split('.')[0];
476
+ const imported = ctx.imports.explicit.get(first);
477
+ // `import p.Outer;` + `extends Outer.Inner`, or `import p.Outer.Inner;` + `extends Inner`.
478
+ if (imported)
479
+ return this.typeByName(`${imported}.${written}`);
480
+ // A type nested in an enclosing type or the package, nearest first.
481
+ const path = type.qualified_name.slice(type.qualified_name.indexOf(':') + 1).split('.');
482
+ for (let i = path.length - 1; i >= 0; i--) {
483
+ const t = this.typesByQname.get(`${pkg}:${[...path.slice(0, i), written].join('.')}`);
484
+ if (t)
485
+ return t;
486
+ }
487
+ if (written.includes('.')) {
488
+ const t = this.typeByName(written);
489
+ if (t)
490
+ return t;
491
+ }
492
+ for (const w of ctx.imports.wildcards) {
493
+ const t = this.typesByQname.get(`${w}:${written}`);
494
+ if (t)
495
+ return t;
496
+ }
497
+ return undefined;
498
+ }
499
+ /** The methods named `name` the type declares, else those of its nearest in-tree superclass that does. */
500
+ methodsNamed(type, name) {
501
+ const seen = new Set();
502
+ for (let t = type; t && !seen.has(t.id); t = this.superclass(t)) {
503
+ seen.add(t.id);
504
+ const ms = this.childrenOf(t.id).filter(d => d.kind === 'method' && d.name === name);
505
+ if (ms.length > 0)
506
+ return ms;
507
+ }
508
+ return [];
509
+ }
510
+ /**
511
+ * Entries for what registering files name: a class's type, a member's
512
+ * method. A name the tree does not declare is a `manifest-unresolved`
513
+ * registration (an optional member, the DS default lifecycle names, is
514
+ * nothing). Exported packages' public types only with `exportedPackages`.
515
+ */
516
+ register(scan, exportedPackages) {
517
+ for (const r of scan.registrations) {
518
+ const site = { file: r.file, line: r.line, col: r.col };
519
+ const type = this.typeByName(r.className);
520
+ const targets = !type ? [] : r.member ? this.methodsNamed(type, r.member) : [type];
521
+ if (targets.length === 0) {
522
+ if (!r.optional) {
523
+ const what = type ? `${r.className}#${r.member ?? ''} is not a method of the class or an in-tree superclass` : `${r.className} is not a type in the tree`;
524
+ this.unresolvedRegistrations.push({ site, reason: 'manifest-unresolved', evidence: `${r.source}: ${what}` });
525
+ }
526
+ continue;
527
+ }
528
+ const extra = { framework: r.framework };
529
+ if (r.method)
530
+ extra.method = r.method;
531
+ if (r.path !== undefined)
532
+ extra.path = r.path;
533
+ if (r.surface)
534
+ extra.surface = r.surface;
535
+ // A class a registering file names is instantiated by its runtime: the entry carries the constructors that runs.
536
+ if (!r.member && type) {
537
+ const constructors = this.instantiationConstructors(type);
538
+ if (constructors.length > 0)
539
+ extra.constructors = constructors;
540
+ }
541
+ for (const d of targets)
542
+ this.entries.push({ id: d.id, kind: r.kind, certainty: r.certainty, file: r.file, line: r.line, evidence: r.source, ...extra });
543
+ }
544
+ if (!exportedPackages)
545
+ return;
546
+ for (const p of scan.exportedPackages) {
547
+ const types = this.publicTypesByPkg.get(p.name) ?? [];
548
+ if (types.length === 0) {
549
+ this.unresolvedRegistrations.push({ site: { file: p.file, line: p.line, col: 0 }, reason: 'manifest-unresolved', evidence: `Export-Package: ${p.name} has no public type in the tree` });
550
+ }
551
+ for (const t of types)
552
+ this.entries.push({ id: t.id, kind: 'export', certainty: 'declared', file: p.file, line: p.line, evidence: 'Export-Package', framework: 'osgi' });
553
+ }
349
554
  }
350
555
  }
351
556
  /** Java graph facts for a set of files. Deterministic: the same files give the same graph. */
@@ -376,7 +581,7 @@ export async function buildJavaGraph(files, options = {}) {
376
581
  for (const line of s.text.split('\n')) {
377
582
  const fqn = line.replace(/#.*$/, '').trim();
378
583
  if (fqn)
379
- b.reflect(fqn, s.path, 'META-INF/services');
584
+ b.reflect(fqn, s.path, 'META-INF/services', true);
380
585
  }
381
586
  }
382
587
  for (const m of options.mainFiles ?? []) {
@@ -384,8 +589,39 @@ export async function buildJavaGraph(files, options = {}) {
384
589
  if (ctx)
385
590
  b.entries.push({ id: ctx.ids.decls[0].id, kind: 'main', certainty: 'declared', file: ctx.path, line: 1, evidence: 'program file given by the caller' });
386
591
  }
592
+ if (options.registrationFiles && options.registrationFiles.length > 0) {
593
+ const regs = [...options.registrationFiles].sort((x, y) => (x.path < y.path ? -1 : x.path > y.path ? 1 : 0));
594
+ b.register(scanRegistrations(regs), options.exportedPackages === true);
595
+ }
387
596
  // Call edges from the navigation index, keyed to declaration ids.
388
597
  const index = buildNavigationIndex(irs.map(f => ({ path: f.path, language: 'java', ir: f.ir, source: f.code })));
598
+ // #1092: a bound names the in-tree types that are it or extend / implement it.
599
+ if (b.pendingBounds.length > 0) {
600
+ const typeIdByFqn = new Map();
601
+ for (const c of ctxs)
602
+ for (const d of c.ids.decls) {
603
+ if (d.kind !== 'type')
604
+ continue;
605
+ const [pkg, path] = d.qualified_name.split(':');
606
+ typeIdByFqn.set(pkg && pkg !== '<default>' ? `${pkg}.${path}` : path, d.id);
607
+ }
608
+ const pkgByFile = new Map(ctxs.map(c => [c.path, c.imports.pkg]));
609
+ for (const reg of b.pendingBounds) {
610
+ const ids = new Set();
611
+ for (const name of reg.bound ?? []) {
612
+ const pkg = pkgByFile.get(reg.site.file) ?? '';
613
+ const cands = name.includes('.') ? [name] : [pkg ? `${pkg}.${name}` : name, `java.lang.${name}`, name];
614
+ for (const c of cands) {
615
+ for (const fqn of [c, ...index.subtypesOf(c)]) {
616
+ const id = typeIdByFqn.get(fqn) ?? typeIdByFqn.get(fqn.replace(/\$/g, '.'));
617
+ if (id)
618
+ ids.add(id);
619
+ }
620
+ }
621
+ }
622
+ reg.boundTypes = [...ids].sort();
623
+ }
624
+ }
389
625
  const byPath = new Map(ctxs.map(c => [c.path, c]));
390
626
  const keyOf = (t) => {
391
627
  const ctx = byPath.get(t.file);
@@ -417,8 +653,12 @@ export async function buildJavaGraph(files, options = {}) {
417
653
  }
418
654
  for (const t of targets) {
419
655
  const e = { from, to: t.id, tier: s.tier, methodName: s.methodName, site, evidence: 'navigation index' };
420
- if (s.tier === 'polymorphic' && declaring && t.id !== declaring.id)
421
- e.via = declaring.id;
656
+ if (s.tier === 'polymorphic' && declaring) {
657
+ if (t.id !== declaring.id)
658
+ e.via = declaring.id;
659
+ else
660
+ markDeclaringEdge(e);
661
+ }
422
662
  edges.push(e);
423
663
  }
424
664
  }
@@ -432,17 +672,22 @@ export async function buildJavaGraph(files, options = {}) {
432
672
  }
433
673
  const cmp = (a, c) => (a < c ? -1 : a > c ? 1 : 0);
434
674
  const bySite = (x, y) => cmp(x.site.file, y.site.file) || x.site.line - y.site.line || x.site.col - y.site.col;
435
- const seen = new Set();
675
+ const seen = new Map();
436
676
  const entries = [...b.entries]
437
677
  .sort((x, y) => cmp(x.file, y.file) || x.line - y.line || cmp(x.id, y.id) || cmp(x.kind, y.kind) || cmp(x.path ?? '', y.path ?? '') || cmp(x.method ?? '', y.method ?? ''))
438
678
  .filter(e => {
439
679
  const k = `${e.id}|${e.kind}|${e.method ?? ''}|${e.path ?? ''}`;
440
- if (seen.has(k))
680
+ const kept = seen.get(k);
681
+ if (kept) {
682
+ // A duplicate that instantiates the class (a service file) keeps its constructors on the entry kept.
683
+ if (e.constructors && !kept.constructors)
684
+ kept.constructors = e.constructors;
441
685
  return false;
442
- seen.add(k);
686
+ }
687
+ seen.set(k, e);
443
688
  return true;
444
689
  });
445
- return {
690
+ const graph = {
446
691
  scheme: JAVA_GRAPH_SCHEME,
447
692
  ids: 'ids@0.1',
448
693
  files: ctxs.map(c => c.ids),
@@ -457,10 +702,142 @@ export async function buildJavaGraph(files, options = {}) {
457
702
  typeOnly: [...b.typeOnly].sort(),
458
703
  sites: sites.filter(s => byPath.has(s.site.file)).length,
459
704
  };
705
+ if (options.emitFacts === 'arrow') {
706
+ const inputs = sorted.map((f, i) => ({ path: f.path, code: f.code, imports: importRows(ctxs[i].tree.rootNode), dfg: irs[i].ir.dfg }));
707
+ const scheme = options.factsScheme ?? 'facts@2';
708
+ const factsOptions = { source: JAVA_GRAPH_SCHEME, ...options.facts };
709
+ if (scheme === 'facts@2')
710
+ graph.facts = await facts2ToArrow(javaGraphFact2Set(graph, inputs), factsOptions);
711
+ else if (scheme === 'facts@1')
712
+ graph.facts = await factsToArrow(javaGraphFactSet(graph, inputs), factsOptions);
713
+ else
714
+ throw new Error(`unknown fact scheme ${JSON.stringify(scheme)} (expected facts@1 or facts@2)`);
715
+ }
716
+ return graph;
460
717
  }
461
718
  finally {
462
719
  for (const t of trees)
463
720
  disposeTree(t);
464
721
  }
465
722
  }
723
+ /** The method, constructor, initializer or lambda body enclosing `node` (#1092). */
724
+ function enclosingBody(node) {
725
+ let n = node.parent;
726
+ while (n && !['method_declaration', 'constructor_declaration', 'static_initializer', 'lambda_expression', 'class_body'].includes(n.type))
727
+ n = n.parent;
728
+ return n && n.type !== 'class_body' ? n : null;
729
+ }
730
+ /** Whether `n` directly declares the local `name` (a `local_variable_declaration` child). */
731
+ function declaresLocal(n, name) {
732
+ return namedChildren(n).some(c => c.type === 'local_variable_declaration'
733
+ && namedChildren(c).some(d => d.type === 'variable_declarator' && d.childForFieldName('name')?.text === name));
734
+ }
735
+ /** The innermost node between `at` and `body` that declares the local `name`, if any. */
736
+ function declaringBlock(at, body, name) {
737
+ for (let n = at.parent; n && n !== body; n = n.parent)
738
+ if (declaresLocal(n, name))
739
+ return n;
740
+ return null;
741
+ }
742
+ /**
743
+ * The string literals `name` can hold at a `Class.forName(name)` in `body` (#1092): every
744
+ * local declaration and assignment of it in the body is a string literal, or it is a
745
+ * `static final String` field of the enclosing class with a literal value. A `null`
746
+ * value names no class (`Class.forName(null)` throws) and adds nothing. Null when any
747
+ * assignment is not a literal, or nothing assigns it. Only the local in scope at `at` is
748
+ * read: a same-named local declared in a sibling block is another variable.
749
+ */
750
+ function constantStrings(body, name, at) {
751
+ const values = [];
752
+ const scope = declaringBlock(at, body, name) ?? body;
753
+ let other = false;
754
+ const visit = (n) => {
755
+ // another block declaring `name` holds a different variable (a sibling scope)
756
+ if (n !== scope && declaresLocal(n, name))
757
+ return;
758
+ if (n.type === 'variable_declarator' && n.childForFieldName('name')?.text === name) {
759
+ const v = n.childForFieldName('value');
760
+ if (v?.type === 'string_literal')
761
+ values.push(...stringsOf(v));
762
+ else if (v && v.type !== 'null_literal')
763
+ other = true;
764
+ }
765
+ else if (n.type === 'assignment_expression' && n.childForFieldName('left')?.text === name) {
766
+ const v = n.childForFieldName('right');
767
+ if (v?.type === 'string_literal')
768
+ values.push(...stringsOf(v));
769
+ else if (v?.type !== 'null_literal')
770
+ other = true;
771
+ }
772
+ else if ((n.type === 'formal_parameter' || n.type === 'catch_formal_parameter') && n.childForFieldName('name')?.text === name) {
773
+ other = true;
774
+ }
775
+ for (const c of namedChildren(n))
776
+ visit(c);
777
+ };
778
+ visit(scope);
779
+ if (other)
780
+ return null;
781
+ if (values.length > 0)
782
+ return values;
783
+ // a static final String field of the enclosing class
784
+ let cls = body.parent;
785
+ while (cls && cls.type !== 'class_body')
786
+ cls = cls.parent;
787
+ for (const f of cls ? namedChildren(cls).filter(m => m.type === 'field_declaration') : []) {
788
+ const mods = f.children.find(ch => ch.type === 'modifiers')?.text ?? '';
789
+ if (!/\bstatic\b/.test(mods) || !/\bfinal\b/.test(mods))
790
+ continue;
791
+ for (const d of namedChildren(f).filter(x => x.type === 'variable_declarator')) {
792
+ if (d.childForFieldName('name')?.text !== name)
793
+ continue;
794
+ const v = d.childForFieldName('value');
795
+ return v?.type === 'string_literal' ? stringsOf(v) : null;
796
+ }
797
+ }
798
+ return null;
799
+ }
800
+ /** A type as written: a capitalised simple name, optionally package-qualified (`java.security.Provider`). */
801
+ const TYPE = String.raw `(?:[a-z_$][\w$]*\.)*[A-Z][\w$.]*`;
802
+ const CAST_NEW_INSTANCE = new RegExp(String.raw `\(\s*(${TYPE}(?:<[^()]*>)?)\s*\)\s*[\w$.\s]*?(?:\.\s*getDeclaredConstructor\s*\([^)]*\)|\.\s*getConstructor\s*\([^)]*\))?\s*\.\s*newInstance\s*\(`, 'g');
803
+ const CAST_FORNAME_NEW_INSTANCE = new RegExp(String.raw `\(\s*(${TYPE}(?:<[^()]*>)?)\s*\)\s*Class\s*\.\s*forName\s*\([^)]*\)\s*\.\s*(?:getDeclaredConstructor|getConstructor)\s*\([^)]*\)\s*\.\s*newInstance\s*\(`, 'g');
804
+ const INSTANCEOF = new RegExp(String.raw `\binstanceof\s+(${TYPE})`, 'g');
805
+ /**
806
+ * The type a reflectively loaded class must be, from how the method uses it (#1092): a cast
807
+ * of `newInstance()` (directly or through `getDeclaredConstructor` / `getConstructor`),
808
+ * `asSubclass(T.class)`, `instanceof T` (also with a pattern variable), `T.class.isAssignableFrom(…)`,
809
+ * or a `getSuperclass()` walk compared with `== T.class`, directly or through a variable the
810
+ * walk assigns (`s = c.getSuperclass(); … if (s == T.class)`). Names as written, simple or package-qualified,
811
+ * generics stripped.
812
+ */
813
+ function boundTypeNames(text) {
814
+ const out = new Set();
815
+ const add = (t) => { const c = (t ?? '').replace(/<.*$/s, '').trim(); if (/^[A-Za-z_$][\w$.]*$/.test(c) && c !== 'Object' && !c.endsWith('.class'))
816
+ out.add(c); };
817
+ for (const m of text.matchAll(CAST_NEW_INSTANCE))
818
+ add(m[1]);
819
+ for (const m of text.matchAll(CAST_FORNAME_NEW_INSTANCE))
820
+ add(m[1]);
821
+ for (const m of text.matchAll(/\.\s*asSubclass\s*\(\s*([\w$.]+)\s*\.\s*class\s*\)/g))
822
+ add(m[1]);
823
+ for (const m of text.matchAll(INSTANCEOF))
824
+ add(m[1]);
825
+ for (const m of text.matchAll(/\b([\w$.]+)\s*\.\s*class\s*\.\s*isAssignableFrom\s*\(/g))
826
+ add(m[1]);
827
+ for (const m of text.matchAll(/getSuperclass\s*\(\s*\)\s*==\s*([\w$.]+)\s*\.\s*class/g))
828
+ add(m[1]);
829
+ // a walk through a variable: `s = c.getSuperclass(); while (s != null) { if (s == T.class) … }`
830
+ for (const w of text.matchAll(/\b([A-Za-z_$][\w$]*)\s*=\s*[\w$.]+\s*\.\s*getSuperclass\s*\(\s*\)/g)) {
831
+ const v = w[1].replace(/[$]/g, '\\$&');
832
+ for (const m of text.matchAll(new RegExp(String.raw `\b${v}\s*==\s*([\w$.]+)\s*\.\s*class\b`, 'g')))
833
+ add(m[1]);
834
+ }
835
+ return [...out].sort();
836
+ }
837
+ /** A type name as written, qualified through the file's explicit imports when it is simple. */
838
+ function resolveTypeName(name, ctx) {
839
+ if (name.includes('.'))
840
+ return name;
841
+ return ctx.imports.explicit.get(name) ? `${ctx.imports.explicit.get(name)}.${name}` : name;
842
+ }
466
843
  //# sourceMappingURL=java-graph.js.map