-
Notifications
You must be signed in to change notification settings - Fork 66
/
ClassLoaderTools.java
124 lines (111 loc) · 3.8 KB
/
ClassLoaderTools.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
/*******************************************************************************
* Copyright (c) 2018, 2022 Red Hat Inc. and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Lucas Bullen (Red Hat Inc.) - initial API and implementation
*******************************************************************************/
package org.eclipse.test;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import org.eclipse.core.runtime.Platform;
import org.osgi.framework.Bundle;
import org.osgi.framework.wiring.BundleWiring;
class ClassLoaderTools {
public static ClassLoader getPluginClassLoader(Bundle bundle, ClassLoader fallback) {
// The fallback classloader is used in order to make ServiceLoader capable of
// loading
// services defined in JUnit bundles but not visible in the test bundle
// classloader
// (which will then be used by SuiteLauncher to lookup other JUnit engines the
// test
// is not aware of).
return new ClassLoader(bundle.adapt(BundleWiring.class).getClassLoader()) {
@Override
public Enumeration<URL> getResources(String name) throws IOException {
Enumeration<URL> supr = super.getResources(name);
return supr != null && supr.hasMoreElements() ? supr
: fallback.getResources(name);
}
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
try {
return super.loadClass(name);
} catch (ClassNotFoundException ex) {
return fallback.loadClass(name);
}
}
};
}
static Bundle getTestBundle(String pluginName, String className) {
if (pluginName != null) {
Bundle bundle = Platform.getBundle(pluginName);
if (bundle != null) {
return bundle;
}
} else if (className != null) {
for (int index = className.lastIndexOf('.'); index != -1; index = className.lastIndexOf('.', index - 1)) {
String symbolicName = className.substring(0, index);
Bundle bundle = Platform.getBundle(symbolicName);
if (bundle != null) {
return bundle;
}
}
}
throw new IllegalArgumentException("Bundle \"" + pluginName
+ "\" not found. Possible causes include missing dependencies, too restrictive version ranges, or a non-matching required execution environment."); //$NON-NLS-1$
}
static ClassLoader getJUnit5Classloader(List<Bundle> platformEngineBundles) {
return new MultiBundleClassLoader(platformEngineBundles);
}
private static class MultiBundleClassLoader extends ClassLoader {
private final List<Bundle> bundleList;
public MultiBundleClassLoader(List<Bundle> platformEngineBundles) {
this.bundleList = platformEngineBundles;
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
for (Bundle bundle : bundleList) {
try {
Class<?> c = bundle.loadClass(name);
if (c != null) {
return c;
}
} catch (ClassNotFoundException e) {
}
}
return null;
}
@Override
protected URL findResource(String name) {
for (Bundle bundle : bundleList) {
URL url = bundle.getResource(name);
if (url != null) {
return url;
}
}
return null;
}
@Override
protected Enumeration<URL> findResources(String name) throws IOException {
List<URL> resources = new ArrayList<>();
for (Bundle bundle : bundleList) {
Enumeration<URL> bundleResources = bundle.getResources(name);
while (bundleResources != null && bundleResources.hasMoreElements()) {
resources.add(bundleResources.nextElement());
}
}
return Collections.enumeration(resources);
}
}
}