-
Notifications
You must be signed in to change notification settings - Fork 37.7k
/
HandlerTypePredicate.java
212 lines (179 loc) · 6.41 KB
/
HandlerTypePredicate.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.method;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
/**
* A {@code Predicate} to match request handling component types if
* <strong>any</strong> of the following selectors match:
* <ul>
* <li>Base packages -- for selecting handlers by their package.
* <li>Assignable types -- for selecting handlers by supertype.
* <li>Annotations -- for selecting handlers annotated in a specific way.
* </ul>
* <p>Composability methods on {@link Predicate} can be used :
* <pre class="code">
* Predicate<Class<?>> predicate =
* HandlerTypePredicate.forAnnotation(RestController.class)
* .and(HandlerTypePredicate.forBasePackage("org.example"));
* </pre>
*
* @author Rossen Stoyanchev
* @since 5.1
*/
public final class HandlerTypePredicate implements Predicate<Class<?>> {
private final Set<String> basePackages;
private final List<Class<?>> assignableTypes;
private final List<Class<? extends Annotation>> annotations;
/**
* Private constructor. See static factory methods.
*/
private HandlerTypePredicate(Set<String> basePackages, List<Class<?>> assignableTypes,
List<Class<? extends Annotation>> annotations) {
this.basePackages = Collections.unmodifiableSet(basePackages);
this.assignableTypes = Collections.unmodifiableList(assignableTypes);
this.annotations = Collections.unmodifiableList(annotations);
}
@Override
public boolean test(@Nullable Class<?> controllerType) {
if (!hasSelectors()) {
return true;
}
else if (controllerType != null) {
for (String basePackage : this.basePackages) {
if (controllerType.getName().startsWith(basePackage)) {
return true;
}
}
for (Class<?> clazz : this.assignableTypes) {
if (ClassUtils.isAssignable(clazz, controllerType)) {
return true;
}
}
for (Class<? extends Annotation> annotationClass : this.annotations) {
if (AnnotationUtils.findAnnotation(controllerType, annotationClass) != null) {
return true;
}
}
}
return false;
}
private boolean hasSelectors() {
return (!this.basePackages.isEmpty() || !this.assignableTypes.isEmpty() || !this.annotations.isEmpty());
}
// Static factory methods
/**
* {@code Predicate} that applies to any handlers.
*/
public static HandlerTypePredicate forAnyHandlerType() {
return new HandlerTypePredicate(
Collections.emptySet(), Collections.emptyList(), Collections.emptyList());
}
/**
* Match handlers declared under a base package, e.g. "org.example".
* @param packages one or more base package names
*/
public static HandlerTypePredicate forBasePackage(String... packages) {
return new Builder().basePackage(packages).build();
}
/**
* Type-safe alternative to {@link #forBasePackage(String...)} to specify a
* base package through a class.
* @param packageClasses one or more base package classes
*/
public static HandlerTypePredicate forBasePackageClass(Class<?>... packageClasses) {
return new Builder().basePackageClass(packageClasses).build();
}
/**
* Match handlers that are assignable to a given type.
* @param types one or more handler supertypes
*/
public static HandlerTypePredicate forAssignableType(Class<?>... types) {
return new Builder().assignableType(types).build();
}
/**
* Match handlers annotated with a specific annotation.
* @param annotations one or more annotations to check for
*/
@SafeVarargs
public static HandlerTypePredicate forAnnotation(Class<? extends Annotation>... annotations) {
return new Builder().annotation(annotations).build();
}
/**
* Return a builder for a {@code HandlerTypePredicate}.
*/
public static Builder builder() {
return new Builder();
}
/**
* A {@link HandlerTypePredicate} builder.
*/
public static class Builder {
private final Set<String> basePackages = new LinkedHashSet<>();
private final List<Class<?>> assignableTypes = new ArrayList<>();
private final List<Class<? extends Annotation>> annotations = new ArrayList<>();
/**
* Match handlers declared under a base package, e.g. "org.example".
* @param packages one or more base package classes
*/
public Builder basePackage(String... packages) {
Arrays.stream(packages).filter(StringUtils::hasText).forEach(this::addBasePackage);
return this;
}
/**
* Type-safe alternative to {@link #forBasePackage(String...)} to specify a
* base package through a class.
* @param packageClasses one or more base package names
*/
public Builder basePackageClass(Class<?>... packageClasses) {
Arrays.stream(packageClasses).forEach(clazz -> addBasePackage(ClassUtils.getPackageName(clazz)));
return this;
}
private void addBasePackage(String basePackage) {
this.basePackages.add(basePackage.endsWith(".") ? basePackage : basePackage + ".");
}
/**
* Match handlers that are assignable to a given type.
* @param types one or more handler supertypes
*/
public Builder assignableType(Class<?>... types) {
this.assignableTypes.addAll(Arrays.asList(types));
return this;
}
/**
* Match types that are annotated with one of the given annotations.
* @param annotations one or more annotations to check for
*/
@SuppressWarnings("unchecked")
public final Builder annotation(Class<? extends Annotation>... annotations) {
this.annotations.addAll(Arrays.asList(annotations));
return this;
}
public HandlerTypePredicate build() {
return new HandlerTypePredicate(this.basePackages, this.assignableTypes, this.annotations);
}
}
}