0
# Assembly Reflection
1
2
jsii-reflect is a strongly-typed reflection library that provides programmatic access to jsii assemblies. It enables deep inspection of type definitions, method signatures, and assembly metadata for tooling and analysis workflows.
3
4
## Capabilities
5
6
### TypeSystem
7
8
The main entry point for loading and managing jsii assemblies with dependency resolution.
9
10
```typescript { .api }
11
/**
12
* Main entry point for loading and managing assemblies
13
*/
14
class TypeSystem {
15
/**
16
* The root assemblies (explicitly loaded)
17
*/
18
readonly roots: readonly Assembly[];
19
20
/**
21
* Whether the TypeSystem is locked from further changes
22
*/
23
readonly isLocked: boolean;
24
25
/**
26
* All assemblies in this type system
27
*/
28
get assemblies(): readonly Assembly[];
29
30
/**
31
* Create new TypeSystem instance
32
*/
33
constructor();
34
35
/**
36
* Lock the TypeSystem from further changes for optimization
37
*/
38
lock(): void;
39
40
/**
41
* Load JSII dependencies of NPM package directory
42
* @param packageRoot Path to package directory
43
* @param options Loading options
44
*/
45
async loadNpmDependencies(
46
packageRoot: string,
47
options?: { validate?: boolean }
48
): Promise<void>;
49
50
/**
51
* Load jsii module or .jsii file with dependencies
52
* @param fileOrDirectory Path to .jsii file or module directory
53
* @param options Loading options
54
*/
55
async load(
56
fileOrDirectory: string,
57
options?: { validate?: boolean }
58
): Promise<Assembly>;
59
60
/**
61
* Load jsii module by directory
62
* @param dir Module directory path
63
* @param options Loading options
64
*/
65
async loadModule(
66
dir: string,
67
options?: { validate?: boolean }
68
): Promise<Assembly>;
69
70
/**
71
* Load assembly file directly
72
* @param file Path to .jsii file
73
* @param options Loading options
74
*/
75
async loadFile(
76
file: string,
77
options?: { isRoot?: boolean; validate?: boolean }
78
): Promise<Assembly>;
79
80
/**
81
* Find assembly by name (throws if not found)
82
* @param name Assembly name to find
83
* @returns Assembly instance
84
*/
85
findAssembly(name: string): Assembly;
86
87
/**
88
* Try to find assembly by name
89
* @param name Assembly name to find
90
* @returns Assembly instance or undefined
91
*/
92
tryFindAssembly(name: string): Assembly | undefined;
93
94
/**
95
* Find type by fully qualified name (throws if not found)
96
* @param fqn Fully qualified type name
97
* @returns Type instance
98
*/
99
findFqn(fqn: string): Type;
100
101
/**
102
* Try to find type by fully qualified name
103
* @param fqn Fully qualified type name
104
* @returns Type instance or undefined
105
*/
106
tryFindFqn(fqn: string): Type | undefined;
107
108
/**
109
* Find class by fully qualified name (throws if not found)
110
* @param fqn Fully qualified class name
111
* @returns Class instance
112
*/
113
findClass(fqn: string): Class;
114
115
/**
116
* Check if assembly is included
117
* @param name Assembly name
118
* @returns True if assembly is loaded
119
*/
120
includesAssembly(name: string): boolean;
121
}
122
```
123
124
### Assembly
125
126
Reflection interface for a loaded jsii assembly with access to all contained types.
127
128
```typescript { .api }
129
/**
130
* Represents a loaded jsii assembly
131
*/
132
class Assembly extends ModuleLike {
133
/**
134
* Assembly name
135
*/
136
readonly name: string;
137
138
/**
139
* Assembly version
140
*/
141
readonly version: string;
142
143
/**
144
* Assembly description
145
*/
146
readonly description?: string;
147
148
/**
149
* Assembly homepage URL
150
*/
151
readonly homepage?: string;
152
153
/**
154
* Assembly repository information
155
*/
156
readonly repository?: {
157
type?: string;
158
url?: string;
159
directory?: string;
160
};
161
162
/**
163
* Assembly author
164
*/
165
readonly author?: {
166
name?: string;
167
email?: string;
168
url?: string;
169
};
170
171
/**
172
* Assembly license
173
*/
174
readonly license?: string;
175
176
/**
177
* All classes in this assembly
178
*/
179
readonly classes: readonly Class[];
180
181
/**
182
* All interfaces in this assembly
183
*/
184
readonly interfaces: readonly Interface[];
185
186
/**
187
* All enums in this assembly
188
*/
189
readonly enums: readonly Enum[];
190
191
/**
192
* Assembly dependencies
193
*/
194
readonly dependencies: readonly Dependency[];
195
196
/**
197
* Assembly submodules
198
*/
199
readonly submodules: readonly Submodule[];
200
201
/**
202
* Assembly targets configuration
203
*/
204
readonly targets?: { [language: string]: any };
205
206
/**
207
* Assembly bundled dependencies
208
*/
209
readonly bundled?: { [name: string]: string };
210
211
/**
212
* Assembly metadata
213
*/
214
readonly metadata?: { [key: string]: any };
215
216
/**
217
* Assembly fingerprint
218
*/
219
readonly fingerprint?: string;
220
221
/**
222
* Get assembly specification object
223
*/
224
get spec(): import('@jsii/spec').Assembly;
225
226
/**
227
* Find class by name within this assembly
228
* @param name Class name (can be FQN or simple name)
229
*/
230
findClass(name: string): Class | undefined;
231
232
/**
233
* Find interface by name within this assembly
234
* @param name Interface name (can be FQN or simple name)
235
*/
236
findInterface(name: string): Interface | undefined;
237
238
/**
239
* Find enum by name within this assembly
240
* @param name Enum name (can be FQN or simple name)
241
*/
242
findEnum(name: string): Enum | undefined;
243
244
/**
245
* Find type by name within this assembly
246
* @param name Type name (can be FQN or simple name)
247
*/
248
findType(name: string): Type | undefined;
249
}
250
```
251
252
### Class Reflection
253
254
Detailed reflection interface for class types with methods, properties, and inheritance information.
255
256
```typescript { .api }
257
/**
258
* Represents a class type in an assembly
259
*/
260
class Class extends ReferenceType {
261
/**
262
* Class constructor/initializer
263
*/
264
readonly initializer?: Initializer;
265
266
/**
267
* Whether the class is abstract
268
*/
269
readonly abstract: boolean;
270
271
/**
272
* Base class (superclass)
273
*/
274
readonly base?: Class;
275
276
/**
277
* All implemented interfaces
278
*/
279
readonly interfaces: readonly Interface[];
280
281
/**
282
* Properties defined directly on this class (not inherited)
283
*/
284
readonly ownProperties: readonly Property[];
285
286
/**
287
* Methods defined directly on this class (not inherited)
288
*/
289
readonly ownMethods: readonly Method[];
290
291
/**
292
* All properties including inherited ones
293
*/
294
readonly allProperties: readonly Property[];
295
296
/**
297
* All methods including inherited ones
298
*/
299
readonly allMethods: readonly Method[];
300
301
/**
302
* Whether this class can be instantiated
303
*/
304
get isInstantiable(): boolean;
305
306
/**
307
* Get all superclasses in inheritance chain
308
*/
309
get ancestors(): readonly Class[];
310
311
/**
312
* Check if this class extends another class
313
* @param other Class to check inheritance from
314
*/
315
extends(other: Class): boolean;
316
317
/**
318
* Check if this class implements an interface
319
* @param iface Interface to check implementation of
320
*/
321
implements(iface: Interface): boolean;
322
323
/**
324
* Find property by name (including inherited)
325
* @param name Property name
326
*/
327
findProperty(name: string): Property | undefined;
328
329
/**
330
* Find method by name (including inherited)
331
* @param name Method name
332
*/
333
findMethod(name: string): Method | undefined;
334
}
335
```
336
337
### Interface Reflection
338
339
Reflection interface for interface types with method and property definitions.
340
341
```typescript { .api }
342
/**
343
* Represents an interface type in an assembly
344
*/
345
class Interface extends ReferenceType {
346
/**
347
* Whether this is a data-only interface (struct)
348
*/
349
readonly datatype: boolean;
350
351
/**
352
* Extended interfaces
353
*/
354
readonly interfaces: readonly Interface[];
355
356
/**
357
* Properties defined on this interface
358
*/
359
readonly ownProperties: readonly Property[];
360
361
/**
362
* Methods defined on this interface
363
*/
364
readonly ownMethods: readonly Method[];
365
366
/**
367
* All properties including inherited ones
368
*/
369
readonly allProperties: readonly Property[];
370
371
/**
372
* All methods including inherited ones
373
*/
374
readonly allMethods: readonly Method[];
375
376
/**
377
* Get all extended interfaces in inheritance chain
378
*/
379
get extends(): readonly Interface[];
380
381
/**
382
* Check if this interface extends another interface
383
* @param other Interface to check inheritance from
384
*/
385
extendsInterface(other: Interface): boolean;
386
387
/**
388
* Find property by name (including inherited)
389
* @param name Property name
390
*/
391
findProperty(name: string): Property | undefined;
392
393
/**
394
* Find method by name (including inherited)
395
* @param name Method name
396
*/
397
findMethod(name: string): Method | undefined;
398
}
399
```
400
401
### Method Reflection
402
403
Detailed information about methods including parameters, return types, and modifiers.
404
405
```typescript { .api }
406
/**
407
* Represents a method on a class or interface
408
*/
409
class Method extends Callable {
410
/**
411
* Method name
412
*/
413
readonly name: string;
414
415
/**
416
* Return type reference (undefined for void methods)
417
*/
418
readonly returns?: TypeRef;
419
420
/**
421
* Whether method is static
422
*/
423
readonly static: boolean;
424
425
/**
426
* Whether method is abstract
427
*/
428
readonly abstract: boolean;
429
430
/**
431
* Whether method is protected
432
*/
433
readonly protected: boolean;
434
435
/**
436
* Whether method is async
437
*/
438
readonly async: boolean;
439
440
/**
441
* Method signature as string
442
*/
443
get signature(): string;
444
445
/**
446
* Whether method returns a value
447
*/
448
get returnsValue(): boolean;
449
450
/**
451
* Whether method can be overridden
452
*/
453
get canOverride(): boolean;
454
}
455
456
/**
457
* Base class for callable members (methods and constructors)
458
*/
459
class Callable extends TypeMember {
460
/**
461
* Method parameters
462
*/
463
readonly parameters: readonly Parameter[];
464
465
/**
466
* Whether callable is variadic (has ...args)
467
*/
468
readonly variadic: boolean;
469
470
/**
471
* Parameter signature string
472
*/
473
get parameterSignature(): string;
474
475
/**
476
* Find parameter by name
477
* @param name Parameter name
478
*/
479
findParameter(name: string): Parameter | undefined;
480
}
481
482
/**
483
* Represents a constructor/initializer
484
*/
485
class Initializer extends Callable {
486
/**
487
* Whether constructor is protected
488
*/
489
readonly protected: boolean;
490
}
491
```
492
493
### Property Reflection
494
495
Property information including types, access modifiers, and optional status.
496
497
```typescript { .api }
498
/**
499
* Represents a property on a class or interface
500
*/
501
class Property extends TypeMember {
502
/**
503
* Property name
504
*/
505
readonly name: string;
506
507
/**
508
* Property type reference
509
*/
510
readonly type: TypeRef;
511
512
/**
513
* Whether property is static
514
*/
515
readonly static: boolean;
516
517
/**
518
* Whether property is immutable (readonly)
519
*/
520
readonly immutable: boolean;
521
522
/**
523
* Whether property is abstract
524
*/
525
readonly abstract: boolean;
526
527
/**
528
* Whether property is protected
529
*/
530
readonly protected: boolean;
531
532
/**
533
* Whether property is optional
534
*/
535
readonly optional: boolean;
536
537
/**
538
* Whether property is const
539
*/
540
readonly const: boolean;
541
542
/**
543
* Property type as string
544
*/
545
get typeSignature(): string;
546
547
/**
548
* Whether property has a getter
549
*/
550
get hasGetter(): boolean;
551
552
/**
553
* Whether property has a setter
554
*/
555
get hasSetter(): boolean;
556
557
/**
558
* Whether property can be overridden
559
*/
560
get canOverride(): boolean;
561
}
562
563
/**
564
* Represents a method parameter
565
*/
566
class Parameter extends OptionalValue {
567
/**
568
* Parameter name
569
*/
570
readonly name: string;
571
572
/**
573
* Parameter type reference
574
*/
575
readonly type: TypeRef;
576
577
/**
578
* Whether parameter is variadic (...args)
579
*/
580
readonly variadic: boolean;
581
582
/**
583
* Parameter signature string
584
*/
585
get signature(): string;
586
}
587
```
588
589
### Enum Reflection
590
591
Enumeration type reflection with member values and names.
592
593
```typescript { .api }
594
/**
595
* Represents an enumeration type
596
*/
597
class Enum extends Type {
598
/**
599
* Enumeration members
600
*/
601
readonly members: readonly EnumMember[];
602
603
/**
604
* Find member by name
605
* @param name Member name
606
*/
607
findMember(name: string): EnumMember | undefined;
608
609
/**
610
* Find member by value
611
* @param value Member value
612
*/
613
findMemberByValue(value: string | number): EnumMember | undefined;
614
}
615
616
/**
617
* Represents an enumeration member
618
*/
619
class EnumMember {
620
/**
621
* Member name
622
*/
623
readonly name: string;
624
625
/**
626
* Member value
627
*/
628
readonly value?: string | number;
629
630
/**
631
* Parent enum
632
*/
633
readonly enum: Enum;
634
635
/**
636
* Member documentation
637
*/
638
readonly docs: Docs;
639
640
/**
641
* Whether member is deprecated
642
*/
643
get isDeprecated(): boolean;
644
}
645
```
646
647
### Type References and Resolution
648
649
System for resolving and working with type references throughout assemblies.
650
651
```typescript { .api }
652
/**
653
* Type reference representation
654
*/
655
class TypeRef {
656
/**
657
* Create type reference from spec
658
* @param system Type system for resolution
659
* @param spec Type reference specification
660
*/
661
constructor(system: TypeSystem, spec: import('@jsii/spec').TypeReference);
662
663
/**
664
* Whether this is a primitive type
665
*/
666
get isPrimitive(): boolean;
667
668
/**
669
* Whether this is a collection type
670
*/
671
get isCollection(): boolean;
672
673
/**
674
* Whether this is a union type
675
*/
676
get isUnion(): boolean;
677
678
/**
679
* Whether this is an intersection type
680
*/
681
get isIntersection(): boolean;
682
683
/**
684
* Primitive type (if isPrimitive)
685
*/
686
get primitive(): import('@jsii/spec').PrimitiveType | undefined;
687
688
/**
689
* Array element type (if isCollection && kind=Array)
690
*/
691
get arrayOfType(): TypeRef | undefined;
692
693
/**
694
* Map value type (if isCollection && kind=Map)
695
*/
696
get mapOfType(): TypeRef | undefined;
697
698
/**
699
* Union member types (if isUnion)
700
*/
701
get unionOfTypes(): readonly TypeRef[] | undefined;
702
703
/**
704
* Intersection member types (if isIntersection)
705
*/
706
get intersectionOfTypes(): readonly TypeRef[] | undefined;
707
708
/**
709
* Referenced type (if type reference)
710
*/
711
get type(): Type | undefined;
712
713
/**
714
* String representation of this type
715
*/
716
toString(): string;
717
718
/**
719
* Type specification object
720
*/
721
get spec(): import('@jsii/spec').TypeReference;
722
}
723
724
/**
725
* Base class for reference types (classes and interfaces)
726
*/
727
class ReferenceType extends Type {
728
/**
729
* All members (properties and methods) on this type
730
*/
731
readonly allMembers: readonly TypeMember[];
732
733
/**
734
* Find member by name
735
* @param name Member name
736
*/
737
findMember(name: string): TypeMember | undefined;
738
}
739
```
740
741
### Documentation and Metadata
742
743
Access to documentation and metadata attached to types and members.
744
745
```typescript { .api }
746
/**
747
* Documentation information
748
*/
749
class Docs {
750
/**
751
* Create docs from specification
752
* @param spec Documentation specification
753
*/
754
constructor(spec?: import('@jsii/spec').Docs);
755
756
/**
757
* Brief summary
758
*/
759
readonly summary?: string;
760
761
/**
762
* Extended remarks
763
*/
764
readonly remarks?: string;
765
766
/**
767
* Return value description
768
*/
769
readonly returns?: string;
770
771
/**
772
* Default value description
773
*/
774
readonly default?: string;
775
776
/**
777
* Deprecation notice
778
*/
779
readonly deprecated?: string;
780
781
/**
782
* Usage example
783
*/
784
readonly example?: string;
785
786
/**
787
* See-also references
788
*/
789
readonly see?: string;
790
791
/**
792
* Since version
793
*/
794
readonly since?: string;
795
796
/**
797
* Whether type is subclassable
798
*/
799
readonly subclassable?: boolean;
800
801
/**
802
* Custom documentation tags
803
*/
804
readonly customTags: { [key: string]: string };
805
806
/**
807
* Whether documentation exists
808
*/
809
get exists(): boolean;
810
811
/**
812
* Full documentation as string
813
*/
814
toString(): string;
815
}
816
817
/**
818
* Source location information
819
*/
820
class Source {
821
/**
822
* Source file name
823
*/
824
readonly filename: string;
825
826
/**
827
* Line number (1-based)
828
*/
829
readonly line: number;
830
831
/**
832
* Column number (1-based)
833
*/
834
readonly column: number;
835
836
/**
837
* Source location as string
838
*/
839
toString(): string;
840
}
841
842
/**
843
* Assembly dependency information
844
*/
845
class Dependency {
846
/**
847
* Dependency name
848
*/
849
readonly name: string;
850
851
/**
852
* Dependency version
853
*/
854
readonly version: string;
855
856
/**
857
* Loaded assembly for this dependency
858
*/
859
readonly assembly?: Assembly;
860
861
/**
862
* Dependency submodules
863
*/
864
readonly submodules?: readonly string[];
865
}
866
```
867
868
### Base Classes and Utilities
869
870
Base classes and utility functions used throughout the reflection system.
871
872
```typescript { .api }
873
/**
874
* Base class for all types
875
*/
876
class Type {
877
/**
878
* Type system this type belongs to
879
*/
880
readonly system: TypeSystem;
881
882
/**
883
* Parent assembly
884
*/
885
readonly assembly: Assembly;
886
887
/**
888
* Fully qualified name
889
*/
890
readonly fqn: string;
891
892
/**
893
* Simple type name
894
*/
895
readonly name: string;
896
897
/**
898
* Type namespace
899
*/
900
readonly namespace?: string;
901
902
/**
903
* Type documentation
904
*/
905
readonly docs: Docs;
906
907
/**
908
* Source location
909
*/
910
readonly source?: Source;
911
912
/**
913
* Whether type is deprecated
914
*/
915
get isDeprecated(): boolean;
916
917
/**
918
* API stability level
919
*/
920
readonly stability?: import('@jsii/spec').Stability;
921
922
/**
923
* Type specification
924
*/
925
get spec(): import('@jsii/spec').Type;
926
927
/**
928
* String representation
929
*/
930
toString(): string;
931
}
932
933
/**
934
* Base class for type members
935
*/
936
class TypeMember {
937
/**
938
* Member name
939
*/
940
readonly name: string;
941
942
/**
943
* Parent type
944
*/
945
readonly parentType: ReferenceType;
946
947
/**
948
* Member documentation
949
*/
950
readonly docs: Docs;
951
952
/**
953
* Source location
954
*/
955
readonly source?: Source;
956
957
/**
958
* Whether member is deprecated
959
*/
960
get isDeprecated(): boolean;
961
962
/**
963
* Whether member can be overridden
964
*/
965
readonly override: boolean;
966
967
/**
968
* String representation
969
*/
970
toString(): string;
971
}
972
973
/**
974
* Base for optional values
975
*/
976
class OptionalValue {
977
/**
978
* Whether value is optional
979
*/
980
readonly optional: boolean;
981
}
982
983
/**
984
* Base for module-like entities
985
*/
986
class ModuleLike {
987
/**
988
* Submodules
989
*/
990
readonly submodules: readonly Submodule[];
991
992
/**
993
* Find submodule by name
994
* @param name Submodule name
995
*/
996
findSubmodule(name: string): Submodule | undefined;
997
}
998
999
/**
1000
* Represents a submodule
1001
*/
1002
class Submodule extends ModuleLike {
1003
/**
1004
* Submodule name
1005
*/
1006
readonly name: string;
1007
1008
/**
1009
* Parent assembly
1010
*/
1011
readonly assembly: Assembly;
1012
1013
/**
1014
* Submodule documentation
1015
*/
1016
readonly docs: Docs;
1017
1018
/**
1019
* Submodule README
1020
*/
1021
readonly readme?: string;
1022
1023
/**
1024
* Target configurations
1025
*/
1026
readonly targets?: { [language: string]: any };
1027
}
1028
```
1029
1030
### Tree Visualization
1031
1032
ASCII tree visualization utilities for assemblies and types.
1033
1034
```typescript { .api }
1035
/**
1036
* Tree representation for assemblies
1037
*/
1038
class Tree {
1039
/**
1040
* Create tree from assembly
1041
* @param assembly Assembly to visualize
1042
* @param options Tree generation options
1043
*/
1044
static fromAssembly(assembly: Assembly, options?: {
1045
types?: boolean;
1046
members?: boolean;
1047
inherited?: boolean;
1048
}): Tree;
1049
1050
/**
1051
* Render tree as ASCII art
1052
* @returns ASCII tree string
1053
*/
1054
toString(): string;
1055
1056
/**
1057
* Print tree to console
1058
*/
1059
print(): void;
1060
}
1061
```
1062
1063
## Types
1064
1065
```typescript { .api }
1066
/**
1067
* Assembly loading options
1068
*/
1069
interface LoadOptions {
1070
/** Whether to validate assembly */
1071
validate?: boolean;
1072
/** Whether to load dependencies */
1073
loadDependencies?: boolean;
1074
/** Dependency search paths */
1075
searchPaths?: string[];
1076
}
1077
1078
/**
1079
* Type visitor interface
1080
*/
1081
interface TypeVisitor {
1082
/** Visit a class */
1083
visitClass?(cls: Class): void;
1084
/** Visit an interface */
1085
visitInterface?(iface: Interface): void;
1086
/** Visit an enum */
1087
visitEnum?(enumType: Enum): void;
1088
/** Visit a method */
1089
visitMethod?(method: Method): void;
1090
/** Visit a property */
1091
visitProperty?(property: Property): void;
1092
}
1093
1094
/**
1095
* Assembly analysis result
1096
*/
1097
interface AnalysisResult {
1098
/** Total types */
1099
typeCount: number;
1100
/** Class count */
1101
classCount: number;
1102
/** Interface count */
1103
interfaceCount: number;
1104
/** Enum count */
1105
enumCount: number;
1106
/** Method count */
1107
methodCount: number;
1108
/** Property count */
1109
propertyCount: number;
1110
/** Dependencies */
1111
dependencies: string[];
1112
}
1113
```
1114
1115
**Usage Examples:**
1116
1117
```typescript
1118
// Load and analyze an assembly
1119
import { TypeSystem } from 'jsii-reflect';
1120
1121
const typeSystem = new TypeSystem();
1122
const assembly = typeSystem.loadAssembly('./my-lib/.jsii');
1123
1124
// Explore classes
1125
for (const cls of assembly.classes) {
1126
console.log(`Class: ${cls.name}`);
1127
1128
if (cls.base) {
1129
console.log(` Extends: ${cls.base.name}`);
1130
}
1131
1132
for (const iface of cls.interfaces) {
1133
console.log(` Implements: ${iface.name}`);
1134
}
1135
1136
// Examine methods
1137
for (const method of cls.ownMethods) {
1138
console.log(` Method: ${method.signature}`);
1139
if (method.docs.summary) {
1140
console.log(` ${method.docs.summary}`);
1141
}
1142
}
1143
}
1144
1145
// Find specific types
1146
const myClass = assembly.findClass('MyClass');
1147
if (myClass) {
1148
const myMethod = myClass.findMethod('doSomething');
1149
if (myMethod) {
1150
console.log(`Found method: ${myMethod.signature}`);
1151
for (const param of myMethod.parameters) {
1152
console.log(` Parameter: ${param.name}: ${param.type}`);
1153
}
1154
}
1155
}
1156
1157
// Visualize assembly structure
1158
import { Tree } from 'jsii-reflect';
1159
1160
const tree = Tree.fromAssembly(assembly, {
1161
types: true,
1162
members: true,
1163
inherited: false
1164
});
1165
1166
tree.print();
1167
```