jtulach@155
|
1 |
package org.apidesign.runtime.check;
|
jtulach@155
|
2 |
|
jtulach@155
|
3 |
import java.io.IOException;
|
jtulach@155
|
4 |
import java.io.InputStream;
|
jtulach@155
|
5 |
import java.util.Map;
|
jtulach@155
|
6 |
|
jtulach@155
|
7 |
final class VersionAwareClassLoader extends ClassLoader
|
jtulach@155
|
8 |
implements RuntimeCheck.AwareLoader {
|
jtulach@155
|
9 |
|
jtulach@155
|
10 |
private final Map<String,String> requestedVersions;
|
jtulach@155
|
11 |
|
jtulach@155
|
12 |
public VersionAwareClassLoader(ClassLoader parent, Map<String,String> requestedVersions) {
|
jtulach@155
|
13 |
super(parent);
|
jtulach@155
|
14 |
this.requestedVersions = requestedVersions;
|
jtulach@155
|
15 |
}
|
jtulach@155
|
16 |
|
jtulach@155
|
17 |
@Override
|
jtulach@155
|
18 |
protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
|
jtulach@155
|
19 |
if (name.endsWith("Test") && name.startsWith("api.")) {
|
jtulach@155
|
20 |
try {
|
jtulach@155
|
21 |
InputStream is = getResourceAsStream(name.replace('.', '/').concat(".class"));
|
jtulach@155
|
22 |
byte[] arr = new byte[is.available()];
|
jtulach@155
|
23 |
int read = is.read(arr);
|
jtulach@155
|
24 |
assert read >= 0;
|
jtulach@155
|
25 |
is.close();
|
jtulach@155
|
26 |
return defineClass(name, arr, 0, read);
|
jtulach@155
|
27 |
} catch (IOException ex) {
|
jtulach@155
|
28 |
throw new ClassNotFoundException("Cannot load " + name, ex);
|
jtulach@155
|
29 |
}
|
jtulach@155
|
30 |
}
|
jtulach@155
|
31 |
|
jtulach@155
|
32 |
return super.loadClass(name, resolve);
|
jtulach@155
|
33 |
}
|
jtulach@155
|
34 |
|
jtulach@155
|
35 |
public String requestedVersion(String apiName) {
|
jtulach@155
|
36 |
return requestedVersions.get(apiName);
|
jtulach@155
|
37 |
}
|
jtulach@155
|
38 |
|
jtulach@155
|
39 |
|
jtulach@155
|
40 |
}
|