Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds premain method such that Mockito can be used as an agent and ena…
…ble instrumentation, considering upcoming restrictions on dynamic attach.
- Loading branch information
Showing
4 changed files
with
47 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,35 @@ | ||
/* | ||
* Copyright (c) 2007 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockito.internal; | ||
|
||
import java.lang.instrument.Instrumentation; | ||
|
||
public class MockitoAgent { | ||
|
||
private static Instrumentation instrumentation; | ||
|
||
public static void premain(String arg, Instrumentation instrumentation) { | ||
MockitoAgent.instrumentation = instrumentation; | ||
} | ||
|
||
public static Instrumentation getInstrumentation() { | ||
Class<?> caller = | ||
StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE).getCallerClass(); | ||
try { | ||
if (caller | ||
!= Class.forName( | ||
"org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker") | ||
&& caller | ||
!= Class.forName( | ||
"org.mockito.internal.util.reflection.InstrumentationMemberAccessor")) { | ||
throw new RuntimeException( | ||
"Cannot access Mockito agent from unknown class: " + caller); | ||
} | ||
} catch (ClassNotFoundException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
return instrumentation; | ||
} | ||
} |
This file contains 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 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