-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathExtendedUrlClassLoader.java
More file actions
103 lines (85 loc) · 3.16 KB
/
ExtendedUrlClassLoader.java
File metadata and controls
103 lines (85 loc) · 3.16 KB
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
// This file is part of SoSy-Lab Common,
// a library of useful utilities:
// https://github.com/sosy-lab/java-common-lib
//
// SPDX-FileCopyrightText: 2007-2020 Dirk Beyer <https://www.sosy-lab.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.sosy_lab.common;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.Var;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Predicate;
/**
* Extension of {@link URLClassLoader} with an optional child-first strategy and optional overriding
* of the search path for native libraries.
*/
@SuppressWarnings("BanClassLoader")
final class ExtendedUrlClassLoader extends URLClassLoader {
private final ExtendedUrlClassLoaderConfiguration config;
private ExtendedUrlClassLoader(ExtendedUrlClassLoaderConfiguration pConfig) {
super(
pConfig.urls().toArray(new URL[0]),
pConfig.parent().orElseGet(ClassLoader::getSystemClassLoader));
config = pConfig;
}
@AutoValue
@SuppressWarnings("NoFunctionalReturnType")
abstract static sealed class ExtendedUrlClassLoaderConfiguration
permits AutoValue_ExtendedUrlClassLoader_ExtendedUrlClassLoaderConfiguration {
abstract Optional<ClassLoader> parent();
abstract ImmutableList<URL> urls();
abstract Predicate<String> directLoadClasses();
abstract Predicate<String> customLookupNativeLibraries();
static AutoBuilder builder() {
return new AutoValue_ExtendedUrlClassLoader_ExtendedUrlClassLoaderConfiguration.Builder();
}
@AutoValue.Builder
abstract static sealed class AutoBuilder extends Classes.ClassLoaderBuilder<AutoBuilder>
permits AutoValue_ExtendedUrlClassLoader_ExtendedUrlClassLoaderConfiguration.Builder {
AutoBuilder() {}
@Override
public final URLClassLoader build() {
return new ExtendedUrlClassLoader(autoBuild());
}
}
}
@Override
protected Class<?> loadClass(String name, boolean pResolve) throws ClassNotFoundException {
if (!config.directLoadClasses().test(name)) {
return super.loadClass(name, pResolve);
}
// This is the same code as in {@link URLClassLoader#loadClass(String, boolean)
// except that it never asks the parent class loader
synchronized (getClassLoadingLock(name)) {
// First, check if the class has already been loaded
@Var Class<?> c = findLoadedClass(name);
if (c == null) {
// If still not found, then invoke findClass in order
// to find the class.
c = findClass(name);
}
if (pResolve) {
resolveClass(c);
}
return c;
}
}
@Override
protected String findLibrary(String libname) {
checkNotNull(libname);
if (config.customLookupNativeLibraries().test(libname)) {
@SuppressWarnings("deprecation")
Optional<Path> path = NativeLibraries.findPathForLibrary(libname);
if (path.isPresent()) {
return path.orElseThrow().toAbsolutePath().toString();
}
}
return super.findLibrary(libname);
}
}