-
Notifications
You must be signed in to change notification settings - Fork 40.2k
/
FilteredClassLoader.java
227 lines (192 loc) · 6.42 KB
/
FilteredClassLoader.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Copyright 2012-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.boot.test.context;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.ProtectionDomain;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.function.Predicate;
import org.springframework.core.SmartClassLoader;
import org.springframework.core.io.ClassPathResource;
/**
* Test {@link URLClassLoader} that can filter the classes and resources it can load.
*
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Phillip Webb
* @author Roy Jacobs
* @since 2.0.0
*/
public class FilteredClassLoader extends URLClassLoader implements SmartClassLoader {
private final Collection<Predicate<String>> classesFilters;
private final Collection<Predicate<String>> resourcesFilters;
/**
* Create a {@link FilteredClassLoader} that hides the given classes.
* @param hiddenClasses the classes to hide
*/
public FilteredClassLoader(Class<?>... hiddenClasses) {
this(Collections.singleton(ClassFilter.of(hiddenClasses)), Collections.emptyList());
}
/**
* Create a {@link FilteredClassLoader} that hides classes from the given packages.
* @param hiddenPackages the packages to hide
*/
public FilteredClassLoader(String... hiddenPackages) {
this(Collections.singleton(PackageFilter.of(hiddenPackages)), Collections.emptyList());
}
/**
* Create a {@link FilteredClassLoader} that hides resources from the given
* {@link ClassPathResource classpath resources}.
* @param hiddenResources the resources to hide
* @since 2.1.0
*/
public FilteredClassLoader(ClassPathResource... hiddenResources) {
this(Collections.emptyList(), Collections.singleton(ClassPathResourceFilter.of(hiddenResources)));
}
/**
* Create a {@link FilteredClassLoader} that filters based on the given predicate.
* @param filters a set of filters to determine when a class name or resource should
* be hidden. A {@link Predicate#test(Object) result} of {@code true} indicates a
* filtered class or resource. The input of the predicate can either be the binary
* name of a class or a resource name.
*/
@SafeVarargs
@SuppressWarnings("varargs")
public FilteredClassLoader(Predicate<String>... filters) {
this(Arrays.asList(filters), Arrays.asList(filters));
}
private FilteredClassLoader(Collection<Predicate<String>> classesFilters,
Collection<Predicate<String>> resourcesFilters) {
super(new URL[0], FilteredClassLoader.class.getClassLoader());
this.classesFilters = classesFilters;
this.resourcesFilters = resourcesFilters;
}
@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
for (Predicate<String> filter : this.classesFilters) {
if (filter.test(name)) {
throw new ClassNotFoundException();
}
}
return super.loadClass(name, resolve);
}
@Override
public URL getResource(String name) {
for (Predicate<String> filter : this.resourcesFilters) {
if (filter.test(name)) {
return null;
}
}
return super.getResource(name);
}
@Override
public Enumeration<URL> getResources(String name) throws IOException {
for (Predicate<String> filter : this.resourcesFilters) {
if (filter.test(name)) {
return Collections.emptyEnumeration();
}
}
return super.getResources(name);
}
@Override
public InputStream getResourceAsStream(String name) {
for (Predicate<String> filter : this.resourcesFilters) {
if (filter.test(name)) {
return null;
}
}
return super.getResourceAsStream(name);
}
@Override
public Class<?> publicDefineClass(String name, byte[] b, ProtectionDomain protectionDomain) {
for (Predicate<String> filter : this.classesFilters) {
if (filter.test(name)) {
throw new IllegalArgumentException(String.format("Defining class with name %s is not supported", name));
}
}
return defineClass(name, b, 0, b.length, protectionDomain);
}
/**
* Filter to restrict the classes that can be loaded.
*/
public static final class ClassFilter implements Predicate<String> {
private Class<?>[] hiddenClasses;
private ClassFilter(Class<?>[] hiddenClasses) {
this.hiddenClasses = hiddenClasses;
}
@Override
public boolean test(String className) {
for (Class<?> hiddenClass : this.hiddenClasses) {
if (className.equals(hiddenClass.getName())) {
return true;
}
}
return false;
}
public static ClassFilter of(Class<?>... hiddenClasses) {
return new ClassFilter(hiddenClasses);
}
}
/**
* Filter to restrict the packages that can be loaded.
*/
public static final class PackageFilter implements Predicate<String> {
private final String[] hiddenPackages;
private PackageFilter(String[] hiddenPackages) {
this.hiddenPackages = hiddenPackages;
}
@Override
public boolean test(String className) {
for (String hiddenPackage : this.hiddenPackages) {
if (className.startsWith(hiddenPackage)) {
return true;
}
}
return false;
}
public static PackageFilter of(String... hiddenPackages) {
return new PackageFilter(hiddenPackages);
}
}
/**
* Filter to restrict the resources that can be loaded.
*
* @since 2.1.0
*/
public static final class ClassPathResourceFilter implements Predicate<String> {
private final ClassPathResource[] hiddenResources;
private ClassPathResourceFilter(ClassPathResource[] hiddenResources) {
this.hiddenResources = hiddenResources;
}
@Override
public boolean test(String resourceName) {
for (ClassPathResource hiddenResource : this.hiddenResources) {
if (hiddenResource.getFilename() != null && resourceName.equals(hiddenResource.getPath())) {
return true;
}
}
return false;
}
public static ClassPathResourceFilter of(ClassPathResource... hiddenResources) {
return new ClassPathResourceFilter(hiddenResources);
}
}
}