Java源码示例:net.bytebuddy.utility.JavaModule
示例1
@Override
public DynamicType.Builder<?> transform(
DynamicType.Builder<?> builder,
TypeDescription typeDescription,
ClassLoader classLoader,
JavaModule module
) {
Map<String, Boolean> methods = allowances.get(typeDescription.getName());
if (methods == null) {
return builder;
}
AsmVisitorWrapper advice = Advice
.withCustomMapping()
.bind(new AllowedArgument.Factory(methods))
.to(AllowAdvice.class)
.on(method -> methods.containsKey(method.getInternalName()));
return builder.visit(advice);
}
示例2
@Test
public void testReadTypesMultiple() throws Exception {
Map<Class<?>, byte[]> binaryRepresentations = ClassFileLocator.ForClassLoader.read(Object.class, Foo.class);
assertThat(binaryRepresentations.size(), is(2));
JavaModule module = JavaModule.ofType(Object.class);
InputStream objectStream = module == null
? Object.class.getResourceAsStream(Object.class.getSimpleName() + ".class")
: module.getResourceAsStream(Object.class.getName().replace('.', '/') + ".class");
try {
assertThat(binaryRepresentations.get(Object.class), is(StreamDrainer.DEFAULT.drain(objectStream)));
} finally {
objectStream.close();
}
InputStream fooStream = getClass().getClassLoader().getResourceAsStream(Foo.class.getName().replace('.', '/') + ".class");
try {
assertThat(binaryRepresentations.get(Foo.class), is(StreamDrainer.DEFAULT.drain(fooStream)));
} finally {
fooStream.close();
}
}
示例3
@Override
public DynamicType.Builder<?> transform(final DynamicType.Builder<?> builder,
final TypeDescription typeDescription,
final ClassLoader classLoader,
final JavaModule module) {
List<AbstractClassEnhancePluginDefine> pluginDefines = pluginFinder.find(typeDescription);
if (pluginDefines.size() > 0) {
DynamicType.Builder<?> newBuilder = builder;
EnhanceContext context = new EnhanceContext();
for (AbstractClassEnhancePluginDefine define : pluginDefines) {
DynamicType.Builder<?> possibleNewBuilder = define.define(
typeDescription, newBuilder, classLoader, context);
if (possibleNewBuilder != null) {
newBuilder = possibleNewBuilder;
}
}
if (context.isEnhanced()) {
logger.debug("Finish the prepare stage for {}.", typeDescription.getName());
}
return newBuilder;
}
logger.debug("Matched class {}, but ignore by finding mechanism.", typeDescription.getTypeName());
return builder;
}
示例4
@Test
public void testReadEdgeAddingListenerNamedCannotRead() throws Exception {
Instrumentation instrumentation = mock(Instrumentation.class);
JavaModule source = mock(JavaModule.class), target = mock(JavaModule.class);
when(source.isNamed()).thenReturn(true);
when(source.canRead(target)).thenReturn(false);
AgentBuilder.Listener listener = new AgentBuilder.Listener.ModuleReadEdgeCompleting(instrumentation, false, Collections.singleton(target));
listener.onTransformation(mock(TypeDescription.class), mock(ClassLoader.class), source, LOADED, mock(DynamicType.class));
verify(source).isNamed();
verify(source).canRead(target);
verify(source).modify(instrumentation,
Collections.singleton(target),
Collections.<String, Set<JavaModule>>emptyMap(),
Collections.<String, Set<JavaModule>>emptyMap(),
Collections.<Class<?>>emptySet(),
Collections.<Class<?>, List<Class<?>>>emptyMap());
verifyNoMoreInteractions(source);
verifyZeroInteractions(target);
}
示例5
@Test
public void testReadEdgeAddingListenerDuplexCanRead() throws Exception {
Instrumentation instrumentation = mock(Instrumentation.class);
JavaModule source = mock(JavaModule.class), target = mock(JavaModule.class);
TypeDescription typeDescription = mock(TypeDescription.class);
PackageDescription packageDescription = mock(PackageDescription.class);
when(typeDescription.getPackage()).thenReturn(packageDescription);
when(source.isNamed()).thenReturn(true);
when(source.canRead(target)).thenReturn(true);
when(source.isOpened(packageDescription, target)).thenReturn(true);
when(target.canRead(source)).thenReturn(true);
AgentBuilder.Listener listener = new AgentBuilder.Listener.ModuleReadEdgeCompleting(instrumentation, true, Collections.singleton(target));
listener.onTransformation(typeDescription, mock(ClassLoader.class), source, LOADED, mock(DynamicType.class));
verify(source).isNamed();
verify(source).canRead(target);
verify(source).isOpened(packageDescription, target);
verifyNoMoreInteractions(source);
verify(target).canRead(source);
verifyNoMoreInteractions(target);
}
示例6
@Test
public void testMatchesNot() throws Exception {
assertThat(AgentBuilder.RawMatcher.Trivial.NON_MATCHING.matches(mock(TypeDescription.class),
mock(ClassLoader.class),
mock(JavaModule.class),
Void.class,
mock(ProtectionDomain.class)), is(false));
}
示例7
@Test
public void testLoadedOnLoaded() throws Exception {
assertThat(AgentBuilder.RawMatcher.ForLoadState.LOADED.matches(mock(TypeDescription.class),
mock(ClassLoader.class),
mock(JavaModule.class),
Object.class,
mock(ProtectionDomain.class)), is(true));
}
示例8
@Override
public void onTransformation(final TypeDescription typeDescription, final ClassLoader classLoader, final JavaModule module, final boolean loaded, final DynamicType dynamicType) {
if (events[Event.TRANSFORMATION.ordinal()] != null)
log(Level.SEVERE, "Event::onTransformation(" + typeDescription.getName() + ", " + AssembleUtil.getNameId(classLoader) + ", " + module + ", " + loaded + ", " + dynamicType + ")");
// FIXME: Should remove the `pluginManifest != null` condition, because a pluginManifest here should be required!
// FIXME: How to communicate an error here? Cause ByteBuddy swallows all exceptions in this context.
final PluginManifest pluginManifest = this.pluginManifest != null ? this.pluginManifest : AgentRule.getPluginManifest(typeDescription);
if (pluginManifest != null && !SpecialAgent.linkRule(pluginManifest, classLoader))
throw new IncompatiblePluginException(typeDescription.getName());
if (classLoader != null) {
try {
final JavaModule unnamedModule = JavaModule.of(ClassLoader.class.getMethod("getUnnamedModule").invoke(classLoader));
if (!module.canRead(unnamedModule)) {
module.modify(inst, Collections.singleton(unnamedModule), Collections.EMPTY_MAP, Collections.EMPTY_MAP, Collections.EMPTY_SET, Collections.EMPTY_MAP);
if (logger.isLoggable(Level.FINEST))
logger.finest("Added module reads: " + module + " -> " + unnamedModule);
}
}
catch (final NoSuchMethodException e) {
}
catch (final Throwable t) {
logger.log(Level.SEVERE, t.getMessage(), t);
}
}
}
示例9
@Test
public void testRetransformationConsiderationExceptionListenerException() throws Exception {
RuntimeException exception = new RuntimeException();
when(instrumentation.isRetransformClassesSupported()).thenReturn(true);
when(instrumentation.getAllLoadedClasses()).thenReturn(new Class<?>[]{REDEFINED});
when(instrumentation.isModifiableClass(REDEFINED)).thenReturn(true);
when(typeMatcher.matches(TypeDescription.ForLoadedType.of(REDEFINED), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), REDEFINED, REDEFINED.getProtectionDomain()))
.thenThrow(exception);
doThrow(new RuntimeException()).when(listener).onError(REDEFINED.getName(), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), true, exception);
ResettableClassFileTransformer classFileTransformer = new AgentBuilder.Default(byteBuddy)
.with(initializationStrategy)
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
.with(poolStrategy)
.with(typeStrategy)
.with(installationListener)
.with(listener)
.disableNativeMethodPrefix()
.ignore(none())
.type(typeMatcher).transform(transformer)
.installOn(instrumentation);
verify(instrumentation).addTransformer(classFileTransformer, true);
verify(listener).onDiscovery(REDEFINED.getName(), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), true);
verify(listener).onError(REDEFINED.getName(), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), true, exception);
verify(listener).onComplete(REDEFINED.getName(), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), true);
verifyNoMoreInteractions(listener);
verify(installationListener).onBeforeInstall(instrumentation, classFileTransformer);
verify(installationListener).onInstall(instrumentation, classFileTransformer);
verifyNoMoreInteractions(installationListener);
}
示例10
@Override
public void onTransformation(
TypeDescription typeDescription,
ClassLoader classLoader,
JavaModule module,
boolean loaded,
DynamicType dynamicType) {
logger.log(Level.FINE, "{0}", typeDescription);
}
示例11
@Override
public void onError(
String typeName,
ClassLoader classLoader,
JavaModule module,
boolean loaded,
Throwable throwable) {
logger.log(Level.WARNING, "Failed to handle " + typeName, throwable);
}
示例12
@Override
public DynamicType.Builder<?> transform(
DynamicType.Builder<?> builder,
TypeDescription typeDescription,
ClassLoader classLoader,
JavaModule module) {
return builder.visit(Advice.to(GetContent.class).on(named("getContent")));
}
示例13
@Override
public DynamicType.Builder<?> transform(
DynamicType.Builder<?> builder,
TypeDescription typeDescription,
ClassLoader classLoader,
JavaModule module) {
return builder
.visit(Advice.to(Start.class).on(named("start")))
.visit(Advice.to(Run.class).on(named("run")));
}
示例14
@Override
public void onError(String error, ClassLoader classLoader, JavaModule module, boolean loaded, Throwable throwable) {
if(!isScalaCompilerClassLoader(classLoader)) {
val classLoaderLabel = (classLoader == null) ? "Bootstrap ClassLoader" : classLoader.toString();
Logger.error(() -> format("Error => {0} with message {1}. Class loader: {2}", error, throwable.getMessage(), classLoaderLabel), throwable);
}
}
示例15
@Test
public void testResetRetransformationWithError() throws Exception {
ResettableClassFileTransformer classFileTransformer = new AgentBuilder.Default(byteBuddy)
.with(initializationStrategy)
.with(poolStrategy)
.with(typeStrategy)
.with(installationListener)
.with(listener)
.disableNativeMethodPrefix()
.ignore(none())
.type(typeMatcher).transform(transformer)
.installOn(instrumentation);
when(instrumentation.removeTransformer(classFileTransformer)).thenReturn(true);
when(instrumentation.isRetransformClassesSupported()).thenReturn(true);
when(instrumentation.isModifiableClass(REDEFINED)).thenReturn(true);
when(instrumentation.getAllLoadedClasses()).thenReturn(new Class<?>[]{REDEFINED});
Throwable throwable = new RuntimeException();
when(typeMatcher.matches(TypeDescription.ForLoadedType.of(REDEFINED), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), REDEFINED, REDEFINED.getProtectionDomain()))
.thenThrow(throwable);
assertThat(classFileTransformer.reset(instrumentation, AgentBuilder.RedefinitionStrategy.RETRANSFORMATION), is(true));
verifyZeroInteractions(listener);
verify(instrumentation).addTransformer(classFileTransformer, false);
verify(instrumentation).isRetransformClassesSupported();
verify(instrumentation).getAllLoadedClasses();
verify(instrumentation).isModifiableClass(REDEFINED);
verify(instrumentation).removeTransformer(classFileTransformer);
verifyNoMoreInteractions(instrumentation);
verify(typeMatcher).matches(TypeDescription.ForLoadedType.of(REDEFINED), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), REDEFINED, REDEFINED.getProtectionDomain());
verifyNoMoreInteractions(typeMatcher);
}
示例16
@Override
public void onError(String typeName, ClassLoader classLoader, JavaModule module, boolean loaded, Throwable throwable) {
if(!isScalaCompilerClassLoader(classLoader)) {
val typeErrors = errors.computeIfAbsent(typeName, (tn) -> Collections.synchronizedList(new LinkedList<>()));
typeErrors.add(throwable);
}
}
示例17
@Test
public void testSuccessfulWithRedefinitionMatchedAndReset() throws Exception {
when(typeMatcher.matches(TypeDescription.ForLoadedType.of(REDEFINED), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), REDEFINED, REDEFINED.getProtectionDomain()))
.thenReturn(true);
when(instrumentation.isModifiableClass(REDEFINED)).thenReturn(true);
when(instrumentation.isRedefineClassesSupported()).thenReturn(true);
ResettableClassFileTransformer classFileTransformer = new AgentBuilder.Default(byteBuddy)
.with(initializationStrategy)
.with(AgentBuilder.RedefinitionStrategy.REDEFINITION)
.with(poolStrategy)
.with(typeStrategy)
.with(installationListener)
.with(listener)
.disableNativeMethodPrefix()
.ignore(none())
.type(typeMatcher).transform(transformer)
.installOn(instrumentation);
when(instrumentation.removeTransformer(classFileTransformer)).thenReturn(true);
assertThat(classFileTransformer.reset(instrumentation, AgentBuilder.RedefinitionStrategy.REDEFINITION), is(true));
verifyZeroInteractions(listener);
verify(instrumentation).addTransformer(classFileTransformer, false);
verify(instrumentation).removeTransformer(classFileTransformer);
verify(instrumentation, times(2)).getAllLoadedClasses();
verify(instrumentation, times(2)).isModifiableClass(REDEFINED);
verify(instrumentation, times(2)).redefineClasses(any(ClassDefinition.class));
verify(instrumentation, times(2)).isRedefineClassesSupported();
verifyNoMoreInteractions(instrumentation);
verify(typeMatcher, times(2)).matches(TypeDescription.ForLoadedType.of(REDEFINED), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), REDEFINED, REDEFINED.getProtectionDomain());
verifyNoMoreInteractions(typeMatcher);
verifyZeroInteractions(dispatcher);
verify(installationListener).onBeforeInstall(instrumentation, classFileTransformer);
verify(installationListener).onInstall(instrumentation, classFileTransformer);
verify(installationListener).onReset(instrumentation, classFileTransformer);
verifyNoMoreInteractions(installationListener);
}
示例18
@Override
public void onTransformation(final TypeDescription typeDescription,
final ClassLoader classLoader,
final JavaModule module,
final boolean loaded,
final DynamicType dynamicType) {
if (logger.isDebugEnable()) {
logger.debug("On Transformation class {}.", typeDescription.getName());
}
InstrumentDebuggingClass.INSTANCE.log(dynamicType);
}
示例19
@Override
public void onError(final String typeName,
final ClassLoader classLoader,
final JavaModule module,
final boolean loaded,
final Throwable throwable) {
logger.error("Enhance class " + typeName + " error.", throwable);
}
示例20
@Test
public void testSuccessfulWithRedefinitionMatched() throws Exception {
when(typeMatcher.matches(TypeDescription.ForLoadedType.of(REDEFINED), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), REDEFINED, REDEFINED.getProtectionDomain()))
.thenReturn(true);
when(instrumentation.isModifiableClass(REDEFINED)).thenReturn(true);
when(instrumentation.isRedefineClassesSupported()).thenReturn(true);
ResettableClassFileTransformer classFileTransformer = new AgentBuilder.Default(byteBuddy)
.with(initializationStrategy)
.with(AgentBuilder.RedefinitionStrategy.REDEFINITION)
.with(poolStrategy)
.with(typeStrategy)
.with(installationListener)
.with(listener)
.disableNativeMethodPrefix()
.ignore(none())
.type(typeMatcher).transform(transformer)
.installOn(instrumentation);
verifyZeroInteractions(listener);
verify(instrumentation).addTransformer(classFileTransformer, false);
verify(instrumentation).getAllLoadedClasses();
verify(instrumentation).isModifiableClass(REDEFINED);
verify(instrumentation).redefineClasses(any(ClassDefinition.class));
verify(instrumentation).isRedefineClassesSupported();
verifyNoMoreInteractions(instrumentation);
verify(typeMatcher).matches(TypeDescription.ForLoadedType.of(REDEFINED), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), REDEFINED, REDEFINED.getProtectionDomain());
verifyNoMoreInteractions(typeMatcher);
verifyZeroInteractions(dispatcher);
verify(installationListener).onBeforeInstall(instrumentation, classFileTransformer);
verify(installationListener).onInstall(instrumentation, classFileTransformer);
verifyNoMoreInteractions(installationListener);
}
示例21
@Test
@SuppressWarnings("unchecked")
public void testExecutingTransformerDoesNotRecurse() throws Exception {
final AgentBuilder.Default.ExecutingTransformer executingTransformer = new AgentBuilder.Default.ExecutingTransformer(byteBuddy,
listener,
poolStrategy,
typeStrategy,
locationStrategy,
mock(AgentBuilder.Default.NativeMethodStrategy.class),
initializationStrategy,
mock(AgentBuilder.InjectionStrategy.class),
AgentBuilder.LambdaInstrumentationStrategy.DISABLED,
AgentBuilder.DescriptionStrategy.Default.HYBRID,
mock(AgentBuilder.FallbackStrategy.class),
mock(AgentBuilder.ClassFileBufferStrategy.class),
mock(AgentBuilder.InstallationListener.class),
mock(AgentBuilder.RawMatcher.class),
Collections.<AgentBuilder.Default.Transformation>emptyList(),
new AgentBuilder.Default.CircularityLock.Default());
final ClassLoader classLoader = mock(ClassLoader.class);
final ProtectionDomain protectionDomain = mock(ProtectionDomain.class);
doAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) throws Throwable {
assertThat(executingTransformer.transform(classLoader,
FOO,
Object.class,
protectionDomain,
new byte[0]), nullValue(byte[].class));
return null;
}
}).when(listener).onComplete(FOO, classLoader, JavaModule.UNSUPPORTED, true);
assertThat(executingTransformer.transform(classLoader,
FOO,
Object.class,
protectionDomain,
new byte[0]), nullValue(byte[].class));
verify(listener).onComplete(FOO, classLoader, JavaModule.UNSUPPORTED, true);
}
示例22
public void onTransformation(
final TypeDescription typeDescription,
final ClassLoader classLoader,
final JavaModule module,
final boolean loaded,
final DynamicType dynamicType)
{
}
示例23
public void onIgnored(
final TypeDescription typeDescription,
final ClassLoader classLoader,
final JavaModule module,
final boolean loaded)
{
}
示例24
public void onError(
final String typeName,
final ClassLoader classLoader,
final JavaModule module,
final boolean loaded,
final Throwable throwable)
{
System.err.println("ERROR " + typeName);
throwable.printStackTrace(System.err);
}
示例25
public void onComplete(
final String typeName,
final ClassLoader classLoader,
final JavaModule module,
final boolean loaded)
{
}
示例26
public void onDiscovery(
final String typeName,
final ClassLoader classLoader,
final JavaModule module,
final boolean loaded)
{
}
示例27
@Test
public void testSuccessfulWithRetransformationMatchedFallbackAndReset() throws Exception {
when(typeMatcher.matches(TypeDescription.ForLoadedType.of(REDEFINED), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), REDEFINED, REDEFINED.getProtectionDomain()))
.thenThrow(new RuntimeException());
when(typeMatcher.matches(TypeDescription.ForLoadedType.of(REDEFINED), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), null, REDEFINED.getProtectionDomain()))
.thenReturn(true);
when(resolution.resolve()).thenReturn(TypeDescription.ForLoadedType.of(REDEFINED));
when(instrumentation.isModifiableClass(REDEFINED)).thenReturn(true);
when(instrumentation.isRetransformClassesSupported()).thenReturn(true);
ResettableClassFileTransformer classFileTransformer = new AgentBuilder.Default(byteBuddy)
.with(initializationStrategy)
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
.with(poolStrategy)
.with(typeStrategy)
.with(installationListener)
.with(AgentBuilder.FallbackStrategy.Simple.ENABLED)
.with(listener)
.disableNativeMethodPrefix()
.ignore(none())
.type(typeMatcher).transform(transformer)
.installOn(instrumentation);
when(instrumentation.removeTransformer(classFileTransformer)).thenReturn(true);
assertThat(classFileTransformer.reset(instrumentation, AgentBuilder.RedefinitionStrategy.RETRANSFORMATION), is(true));
verifyZeroInteractions(listener);
verify(instrumentation).addTransformer(classFileTransformer, true);
verify(instrumentation).removeTransformer(classFileTransformer);
verify(instrumentation, times(2)).getAllLoadedClasses();
verify(instrumentation, times(2)).isModifiableClass(REDEFINED);
verify(instrumentation, times(2)).retransformClasses(REDEFINED);
verify(instrumentation, times(2)).isRetransformClassesSupported();
verifyNoMoreInteractions(instrumentation);
verify(typeMatcher, times(2)).matches(TypeDescription.ForLoadedType.of(REDEFINED), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), REDEFINED, REDEFINED.getProtectionDomain());
verify(typeMatcher, times(2)).matches(TypeDescription.ForLoadedType.of(REDEFINED), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), null, REDEFINED.getProtectionDomain());
verifyNoMoreInteractions(typeMatcher);
verifyZeroInteractions(initializationStrategy);
verify(installationListener).onBeforeInstall(instrumentation, classFileTransformer);
verify(installationListener).onInstall(instrumentation, classFileTransformer);
verify(installationListener).onReset(instrumentation, classFileTransformer);
verifyNoMoreInteractions(installationListener);
}
示例28
@Test
public void testSkipRedefinitionWithNonMatched() throws Exception {
when(dynamicType.getBytes()).thenReturn(BAZ);
when(resolution.resolve()).thenReturn(TypeDescription.ForLoadedType.of(REDEFINED));
when(typeMatcher.matches(TypeDescription.ForLoadedType.of(REDEFINED), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), REDEFINED, REDEFINED.getProtectionDomain()))
.thenReturn(false);
when(instrumentation.isModifiableClass(REDEFINED)).thenReturn(true);
when(instrumentation.isRedefineClassesSupported()).thenReturn(true);
ResettableClassFileTransformer classFileTransformer = new AgentBuilder.Default(byteBuddy)
.with(initializationStrategy)
.with(AgentBuilder.RedefinitionStrategy.REDEFINITION)
.with(poolStrategy)
.with(typeStrategy)
.with(installationListener)
.with(listener)
.disableNativeMethodPrefix()
.ignore(none())
.type(typeMatcher).transform(transformer)
.installOn(instrumentation);
verify(listener).onDiscovery(REDEFINED.getName(), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), true);
verify(listener).onIgnored(TypeDescription.ForLoadedType.of(REDEFINED), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), true);
verify(listener).onComplete(REDEFINED.getName(), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), true);
verifyNoMoreInteractions(listener);
verify(instrumentation).addTransformer(classFileTransformer, false);
verify(instrumentation).isModifiableClass(REDEFINED);
verify(instrumentation).getAllLoadedClasses();
verify(instrumentation).isRedefineClassesSupported();
verifyNoMoreInteractions(instrumentation);
verify(typeMatcher).matches(TypeDescription.ForLoadedType.of(REDEFINED), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), REDEFINED, REDEFINED.getProtectionDomain());
verifyNoMoreInteractions(typeMatcher);
verifyZeroInteractions(initializationStrategy);
verify(installationListener).onBeforeInstall(instrumentation, classFileTransformer);
verify(installationListener).onInstall(instrumentation, classFileTransformer);
verifyNoMoreInteractions(installationListener);
}
示例29
@Test
public void testRetransformationConsiderationException() throws Exception {
RuntimeException exception = new RuntimeException();
when(instrumentation.isRetransformClassesSupported()).thenReturn(true);
when(instrumentation.getAllLoadedClasses()).thenReturn(new Class<?>[]{REDEFINED});
when(instrumentation.isModifiableClass(REDEFINED)).thenReturn(true);
when(typeMatcher.matches(TypeDescription.ForLoadedType.of(REDEFINED), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), REDEFINED, REDEFINED.getProtectionDomain()))
.thenThrow(exception);
ResettableClassFileTransformer classFileTransformer = new AgentBuilder.Default(byteBuddy)
.with(initializationStrategy)
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
.with(poolStrategy)
.with(typeStrategy)
.with(installationListener)
.with(listener)
.disableNativeMethodPrefix()
.ignore(none())
.type(typeMatcher).transform(transformer)
.installOn(instrumentation);
verify(instrumentation).addTransformer(classFileTransformer, true);
verify(listener).onDiscovery(REDEFINED.getName(), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), true);
verify(listener).onError(REDEFINED.getName(), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), true, exception);
verify(listener).onComplete(REDEFINED.getName(), REDEFINED.getClassLoader(), JavaModule.ofType(REDEFINED), true);
verifyNoMoreInteractions(listener);
verify(installationListener).onBeforeInstall(instrumentation, classFileTransformer);
verify(installationListener).onInstall(instrumentation, classFileTransformer);
verifyNoMoreInteractions(installationListener);
}
示例30
@Test
@JavaVersionRule.Enforce(9)
public void testExecutingTransformerDoesNotRecurseWithModules() throws Exception {
final AgentBuilder.Default.ExecutingTransformer executingTransformer = new AgentBuilder.Default.ExecutingTransformer(byteBuddy,
listener,
poolStrategy,
typeStrategy,
locationStrategy,
mock(AgentBuilder.Default.NativeMethodStrategy.class),
initializationStrategy,
mock(AgentBuilder.InjectionStrategy.class),
AgentBuilder.LambdaInstrumentationStrategy.DISABLED,
AgentBuilder.DescriptionStrategy.Default.HYBRID,
mock(AgentBuilder.FallbackStrategy.class),
mock(AgentBuilder.ClassFileBufferStrategy.class),
mock(AgentBuilder.InstallationListener.class),
mock(AgentBuilder.RawMatcher.class),
Collections.<AgentBuilder.Default.Transformation>emptyList(),
new AgentBuilder.CircularityLock.Default());
final ClassLoader classLoader = mock(ClassLoader.class);
final ProtectionDomain protectionDomain = mock(ProtectionDomain.class);
doAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) throws Throwable {
assertThat(executingTransformer.transform(JavaModule.ofType(Object.class).unwrap(),
classLoader,
FOO,
Object.class,
protectionDomain,
new byte[0]), nullValue(byte[].class));
return null;
}
}).when(listener).onComplete(FOO, classLoader, JavaModule.ofType(Object.class), true);
assertThat(executingTransformer.transform(JavaModule.ofType(Object.class).unwrap(),
classLoader,
FOO,
Object.class,
protectionDomain,
new byte[0]), nullValue(byte[].class));
verify(listener).onComplete(FOO, classLoader, JavaModule.ofType(Object.class), true);
}