Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 61e86e9a0d6166171e5363ac0fe69470672c062f (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2013, 2018 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.osgi.tests.classloader.hooks.a;

import org.eclipse.osgi.container.Module;
import org.eclipse.osgi.internal.hookregistry.ClassLoaderHook;
import org.eclipse.osgi.internal.hookregistry.HookConfigurator;
import org.eclipse.osgi.internal.hookregistry.HookRegistry;
import org.eclipse.osgi.internal.loader.classpath.ClasspathEntry;
import org.eclipse.osgi.internal.loader.classpath.ClasspathManager;
import org.eclipse.osgi.storage.bundlefile.BundleEntry;

public class TestHookConfigurator implements HookConfigurator {
	private static final String REJECT_PROP = "classloader.hooks.a.reject";
	private static final String BAD_TRANSFORM_PROP = "classloader.hooks.a.bad.transform";
	private static final String RECURSION_LOAD = "classloader.hooks.a.recursion.load";
	private static final String RECURSION_LOAD_SUPPORTED = "classloader.hooks.a.recursion.load.supported";
	private static final String FILTER_CLASS_PATHS = "classloader.hooks.a.filter.class.paths";
	final ThreadLocal<Boolean> doingRecursionLoad = new ThreadLocal<Boolean>() {
		protected Boolean initialValue() {
			return false;
		}
	};

	public void addHooks(HookRegistry hookRegistry) {
		hookRegistry.addClassLoaderHook(new ClassLoaderHook() {

			@Override
			public boolean rejectTransformation(String name, byte[] transformedBytes, ClasspathEntry classpathEntry, BundleEntry entry, ClasspathManager manager) {
				return Boolean.getBoolean(REJECT_PROP);
			}

			@Override
			public byte[] processClass(String name, byte[] classbytes, ClasspathEntry classpathEntry, BundleEntry entry, ClasspathManager manager) {
				if (Boolean.getBoolean(BAD_TRANSFORM_PROP)) {
					return new byte[] {'b', 'a', 'd', 'b', 'y', 't', 'e', 's'};
				}
				if (Boolean.getBoolean(RECURSION_LOAD)) {
					if (isProcessClassRecursionSupported() && doingRecursionLoad.get()) {
						return null;
					}
					Module m = manager.getGeneration().getBundleInfo().getStorage().getModuleContainer().getModule(1);
					doingRecursionLoad.set(true);
					try {
						m.getCurrentRevision().getWiring().getClassLoader().loadClass("substitutes.x.Ax");
						if (!isProcessClassRecursionSupported()) {
							throw new LinkageError("Recursion is no supported.");
						}
					} catch (ClassNotFoundException e) {
						if (isProcessClassRecursionSupported()) {
							throw new LinkageError("Recursion should be supported.");
						}
						// expected
					} finally {
						doingRecursionLoad.set(false);
					}
				}
				return null;
			}

			@Override
			public boolean isProcessClassRecursionSupported() {
				return Boolean.getBoolean(RECURSION_LOAD_SUPPORTED);
			}

			@Override
			public ClasspathEntry[] getClassPathEntries(String name, ClasspathManager manager) {
				if (Boolean.getBoolean(FILTER_CLASS_PATHS)) {
					return new ClasspathEntry[0];
				}
				return super.getClassPathEntries(name, manager);
			}
		});
	}
}

Back to the top