0
# Advanced Features
1
2
AOP interceptors, method matching, private modules, and other advanced dependency injection patterns.
3
4
## Capabilities
5
6
### Method Matching
7
8
Classes for matching methods and types for AOP interception.
9
10
```java { .api }
11
/**
12
* Returns true or false for a given input. Used for method and type matching.
13
* @param <T> Type being matched
14
*/
15
public interface Matcher<T> {
16
/**
17
* Returns true if this matcher matches the given input.
18
* @param t Input to match
19
* @return true if matches
20
*/
21
boolean matches(T t);
22
23
/**
24
* Returns a matcher that matches if both this and other match.
25
* @param other Other matcher
26
* @return Combined matcher using AND logic
27
*/
28
default Matcher<T> and(Matcher<? super T> other);
29
30
/**
31
* Returns a matcher that matches if either this or other matches.
32
* @param other Other matcher
33
* @return Combined matcher using OR logic
34
*/
35
default Matcher<T> or(Matcher<? super T> other);
36
}
37
38
/**
39
* Factory for common matcher implementations.
40
*/
41
public final class Matchers {
42
/**
43
* Matches any input.
44
* @return Matcher that matches everything
45
*/
46
public static Matcher<Object> any();
47
48
/**
49
* Matches subclasses of the given type.
50
* @param superclass Superclass to match
51
* @return Matcher for subclasses
52
*/
53
public static Matcher<Class> subclassesOf(Class<?> superclass);
54
55
/**
56
* Matches elements annotated with the given annotation.
57
* @param annotationType Annotation type
58
* @return Matcher for annotated elements
59
*/
60
public static Matcher<AnnotatedElement> annotatedWith(Class<? extends Annotation> annotationType);
61
62
/**
63
* Matches classes in the given package.
64
* @param targetPackage Package to match
65
* @return Matcher for classes in package
66
*/
67
public static Matcher<Class> inPackage(Package targetPackage);
68
}
69
```
70
71
### Private Modules
72
73
Modules that create private child injectors with selective exposure.
74
75
```java { .api }
76
/**
77
* A private module can expose a subset of its bindings. The bindings not exposed
78
* are not available to the outside world, but the private module can still
79
* inject its non-exposed bindings internally.
80
*/
81
public abstract class PrivateModule implements Module {
82
/**
83
* Creates bindings and other configuration for this module.
84
*/
85
protected abstract void configure();
86
87
/**
88
* Exposes the binding for the given type.
89
* @param type Type to expose
90
*/
91
protected final void expose(Class<?> type);
92
93
/**
94
* Exposes the binding for the given key.
95
* @param key Key to expose
96
*/
97
protected final void expose(Key<?> key);
98
99
/**
100
* Returns the private binder for this module.
101
* @return Private binder instance
102
*/
103
protected final PrivateBinder binder();
104
}
105
```
106
107
**Usage Examples:**
108
109
```java
110
// AOP interception
111
public class LoggingModule extends AbstractModule {
112
@Override
113
protected void configure() {
114
// Intercept all methods in Service classes annotated with @Timed
115
bindInterceptor(
116
Matchers.subclassesOf(Service.class),
117
Matchers.annotatedWith(Timed.class),
118
new TimingInterceptor()
119
);
120
}
121
}
122
123
// Private module
124
public class DatabasePrivateModule extends PrivateModule {
125
@Override
126
protected void configure() {
127
// Internal bindings - not visible outside
128
bind(ConnectionPool.class).to(HikariConnectionPool.class);
129
bind(QueryExecutor.class).to(PreparedStatementExecutor.class);
130
131
// Public interface - exposed to outside world
132
bind(DatabaseService.class).to(DatabaseServiceImpl.class);
133
expose(DatabaseService.class);
134
}
135
}
136
```