-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Allow for installing a Java agent within the Mockito jar, without exposing Byte Buddy's attach mechanism. #3437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
13de203
Allow for installing a Java agent within the Mockito jar, without exp…
raphw e2ad121
Adjust use of Mockito premain class to resolve from the system class …
raphw 67bcb1d
Adjust comment to better fit linebreaks.
raphw c243a19
Add additional documentation on how to add Mockito as an agent.
raphw 8ceb5a3
Adjust javadoc.
raphw fb8b03b
Avoid duplicate closing.
raphw bb8c3dd
Add warning and fix numeration of documentation.
raphw 19b56e1
Update src/main/java/org/mockito/Mockito.java
raphw 7b6b8b3
Update src/main/java/org/mockito/Mockito.java
raphw 87341b1
Update src/main/java/org/mockito/Mockito.java
bric3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * Copyright (c) 2024 Mockito contributors | ||
| * This program is made available under the terms of the MIT License. | ||
| */ | ||
| package org.mockito.internal; | ||
|
|
||
| import java.lang.instrument.Instrumentation; | ||
|
|
||
| /** | ||
| * Allows users to specify Mockito as a Java agent where the {@link Instrumentation} | ||
| * instance is stored for use by the inline mock maker. | ||
| * | ||
| * The <a href="https://openjdk.org/jeps/451">JEP 451</a>, delivered with JDK 21, | ||
| * is the first milestone to disallow dynamic loading of by default which will happen | ||
| * in a future version of the JDK. | ||
| */ | ||
| public class PremainAttach { | ||
|
|
||
| private static volatile Instrumentation instrumentation; | ||
|
|
||
| public static void premain(String arg, Instrumentation instrumentation) { | ||
| if (PremainAttach.instrumentation != null) { | ||
| return; | ||
| } | ||
| PremainAttach.instrumentation = instrumentation; | ||
| } | ||
|
|
||
| public static Instrumentation getInstrumentation() { | ||
| // A Java agent is always added to the system class loader. If Mockito is executed from a | ||
| // different class loader we need to make sure to resolve the instrumentation instance | ||
| // from there, or fail the resolution, if this class does not exist on the system class | ||
| // loader. | ||
| ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); | ||
| if (PremainAttach.class.getClassLoader() == systemClassLoader) { | ||
| return instrumentation; | ||
| } else { | ||
| try { | ||
| return (Instrumentation) | ||
| Class.forName(PremainAttach.class.getName(), true, systemClassLoader) | ||
| .getMethod("getInstrumentation") | ||
| .invoke(null); | ||
| } catch (Exception ignored) { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.