|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2009, 2026 Mountainminds GmbH & Co. KG and Contributors |
| 3 | + * This program and the accompanying materials are made available under |
| 4 | + * the terms of the Eclipse Public License 2.0 which is available at |
| 5 | + * https://www.eclipse.org/legal/epl-2.0 |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Evgeny Mandrikov - initial API and implementation |
| 11 | + * |
| 12 | + *******************************************************************************/ |
| 13 | +package org.jacoco.ant; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.lang.instrument.Instrumentation; |
| 17 | +import java.lang.reflect.Method; |
| 18 | +import java.net.URL; |
| 19 | +import java.net.URLClassLoader; |
| 20 | +import java.util.jar.Manifest; |
| 21 | + |
| 22 | +/** |
| 23 | + * Simulates behavior of <a href= |
| 24 | + * "https://github.com/jboss-modules/jboss-modules/blob/2.3.0/src/main/java/org/jboss/modules/Main.java">JBoss |
| 25 | + * Modules</a> that in contrast to {@link java.lang.instrument normal agent |
| 26 | + * execution} loads and executes agents using non |
| 27 | + * {@link ClassLoader#getSystemClassLoader() system class loader}. |
| 28 | + * <p> |
| 29 | + * This class must be declared and executed as both |
| 30 | + * {@link #premain(String, Instrumentation) Premain-Class} and |
| 31 | + * {@link #main(String[]) Main-Class}, so that first execution can capture |
| 32 | + * instance of {@link Instrumentation} used by second. |
| 33 | + * </p> |
| 34 | + */ |
| 35 | +public final class AgentLoader { |
| 36 | + |
| 37 | + private static Instrumentation instrumentation; |
| 38 | + |
| 39 | + /** Captures instance of {@link Instrumentation}. */ |
| 40 | + public static void premain(final String agentArgs, |
| 41 | + final Instrumentation instrumentation) { |
| 42 | + AgentLoader.instrumentation = instrumentation; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Loads and executes {@code Premain-Class} from JAR file passed as first |
| 47 | + * argument using new (<strong>non system</strong>) class loader. |
| 48 | + */ |
| 49 | + public static void main(final String[] args) throws Exception { |
| 50 | + final URLClassLoader classLoader = new URLClassLoader( |
| 51 | + new URL[] { new File(args[0]).toURI().toURL() }); |
| 52 | + final Manifest manifest = new Manifest(); |
| 53 | + manifest.read( |
| 54 | + classLoader.findResource("META-INF/MANIFEST.MF").openStream()); |
| 55 | + final Class<?> agentClass = classLoader.loadClass( |
| 56 | + manifest.getMainAttributes().getValue("Premain-Class")); |
| 57 | + final Method premain = agentClass.getDeclaredMethod("premain", |
| 58 | + String.class, Instrumentation.class); |
| 59 | + premain.invoke(null, "", instrumentation); |
| 60 | + } |
| 61 | + |
| 62 | +} |
0 commit comments