0
# Service Provider Interface (SPI)
1
2
Introspection and tooling APIs for analyzing injector configuration, visiting bindings, and integrating with development tools.
3
4
## Capabilities
5
6
### Elements Class
7
8
Utilities for extracting and rewriting elements from modules.
9
10
```java { .api }
11
/**
12
* Exposes elements of a module so they can be inspected, validated or rewritten.
13
* Elements are extractable from modules and can be used to build new modules.
14
*/
15
public final class Elements {
16
/**
17
* Records the elements executed by modules so they can be analyzed.
18
* @param modules Modules to extract elements from
19
* @return List of elements from the modules
20
*/
21
public static List<Element> getElements(Module... modules);
22
23
/**
24
* Records the elements executed by modules so they can be analyzed.
25
* @param stage Development stage
26
* @param modules Modules to extract elements from
27
* @return List of elements from the modules
28
*/
29
public static List<Element> getElements(Stage stage, Iterable<? extends Module> modules);
30
31
/**
32
* Creates a module from the given elements.
33
* @param elements Elements to build module from
34
* @return Module containing the elements
35
*/
36
public static Module getModule(Iterable<? extends Element> elements);
37
}
38
```
39
40
### Element Interface
41
42
Core component representing configuration elements for introspection.
43
44
```java { .api }
45
/**
46
* A core component of a module or injector. Elements are extractable from modules
47
* and can be inspected, validated or rewritten.
48
*/
49
public interface Element {
50
/**
51
* Returns the source object which was used to create this element.
52
* @return Source object
53
*/
54
Object getSource();
55
56
/**
57
* Accepts a visitor for this element.
58
* @param visitor Element visitor
59
* @return Result from visitor
60
*/
61
<T> T acceptVisitor(ElementVisitor<T> visitor);
62
63
/**
64
* Writes this element to the given binder.
65
* @param binder Binder to write to
66
*/
67
void applyTo(Binder binder);
68
}
69
```
70
71
**Usage Examples:**
72
73
```java
74
// Extract elements from modules for analysis
75
public class ModuleAnalyzer {
76
public void analyzeModule(Module module) {
77
List<Element> elements = Elements.getElements(module);
78
79
for (Element element : elements) {
80
element.acceptVisitor(new DefaultElementVisitor<Void>() {
81
@Override
82
public <T> Void visit(Binding<T> binding) {
83
System.out.println("Found binding: " + binding.getKey());
84
return null;
85
}
86
87
@Override
88
public Void visit(PrivateElements privateElements) {
89
System.out.println("Found private module with " +
90
privateElements.getElements().size() + " elements");
91
return null;
92
}
93
});
94
}
95
}
96
}
97
98
// Rewrite modules
99
public Module rewriteModule(Module originalModule) {
100
List<Element> elements = Elements.getElements(originalModule);
101
102
// Filter or transform elements as needed
103
List<Element> filteredElements = elements.stream()
104
.filter(element -> shouldIncludeElement(element))
105
.collect(Collectors.toList());
106
107
return Elements.getModule(filteredElements);
108
}
109
```