net.bytebuddy.agent.ByteBuddyAgent Java Examples
The following examples show how to use
net.bytebuddy.agent.ByteBuddyAgent.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MapRedInputFormatTracerTest.java From garmadon with Apache License 2.0 | 6 votes |
@Test public void InputFormatTracer_should_let_getRecordReader_return_its_original_value() throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException { assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class)); ClassFileTransformer classFileTransformer = new MapReduceTracer.DeprecatedInputFormatTracer().installOnByteBuddyAgent(); try { //Prepare JobConf when(jobConf.get("mapreduce.input.fileinputformat.inputdir")).thenReturn("/some/path"); Class<?> type = classLoader.loadClass(MapRedInputFormatTestClasses.OneLevelHierarchy.class.getName()); Object recordReader = invokeRecordReader(type); assertThat(recordReader, equalTo(getRecordReaderMockReflection(type))); } finally { ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer); } }
Example #2
Source File: ElasticAttachmentProvider.java From apm-agent-java with Apache License 2.0 | 6 votes |
/** * Initializes attachment provider, this method can only be called once as it loads native code. * * @param useEmulatedAttach {@literal true} to enable emulated attach, {@literal false} to disable */ public synchronized static void init(boolean useEmulatedAttach) { if (provider != null) { throw new IllegalStateException("ElasticAttachmentProvider.init() should only be called once"); } ArrayList<ByteBuddyAgent.AttachmentProvider> providers = new ArrayList<>(); if (useEmulatedAttach) { providers.add(ByteBuddyAgent.AttachmentProvider.ForEmulatedAttachment.INSTANCE); } providers.addAll(Arrays.asList(ByteBuddyAgent.AttachmentProvider.ForModularizedVm.INSTANCE, ByteBuddyAgent.AttachmentProvider.ForJ9Vm.INSTANCE, new CachedAttachmentProvider(ByteBuddyAgent.AttachmentProvider.ForStandardToolsJarVm.JVM_ROOT), new CachedAttachmentProvider(ByteBuddyAgent.AttachmentProvider.ForStandardToolsJarVm.JDK_ROOT), new CachedAttachmentProvider(ByteBuddyAgent.AttachmentProvider.ForStandardToolsJarVm.MACINTOSH), new CachedAttachmentProvider(ByteBuddyAgent.AttachmentProvider.ForUserDefinedToolsJar.INSTANCE))); provider = new ByteBuddyAgent.AttachmentProvider.Compound(providers); }
Example #3
Source File: ClassReloadingStrategyTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @AgentAttachmentRule.Enforce(retransformsClasses = true) @JavaVersionRule.Enforce(atMost = 10) // Wait for mechanism in sun.misc.Unsafe to define class. public void testFromAgentClassWithAuxiliaryReloadingStrategy() throws Exception { assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class)); Foo foo = new Foo(); assertThat(foo.foo(), is(FOO)); ClassReloadingStrategy classReloadingStrategy = ClassReloadingStrategy.fromInstalledAgent(); String randomName = FOO + RandomString.make(); new ByteBuddy() .redefine(Foo.class) .method(named(FOO)) .intercept(FixedValue.value(BAR)) .make() .include(new ByteBuddy().subclass(Object.class).name(randomName).make()) .load(Foo.class.getClassLoader(), classReloadingStrategy); try { assertThat(foo.foo(), is(BAR)); } finally { classReloadingStrategy.reset(Foo.class); assertThat(foo.foo(), is(FOO)); } assertThat(Class.forName(randomName), notNullValue(Class.class)); }
Example #4
Source File: AgentAttachmentRule.java From byte-buddy with Apache License 2.0 | 6 votes |
public Statement apply(Statement base, FrameworkMethod method, Object target) { Enforce enforce = method.getAnnotation(Enforce.class); if (enforce != null) { if (!available) { return new NoOpStatement("The executing JVM does not support runtime attachment"); } Instrumentation instrumentation = ByteBuddyAgent.install(ByteBuddyAgent.AttachmentProvider.DEFAULT); if (enforce.redefinesClasses() && !instrumentation.isRedefineClassesSupported()) { return new NoOpStatement("The executing JVM does not support class redefinition"); } else if (enforce.retransformsClasses() && !instrumentation.isRetransformClassesSupported()) { return new NoOpStatement("The executing JVM does not support class retransformation"); } else if (enforce.nativeMethodPrefix() && !instrumentation.isNativeMethodPrefixSupported()) { return new NoOpStatement("The executing JVM does not support class native method prefixes"); } } return base; }
Example #5
Source File: ByteBuddyTutorialExamplesTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @AgentAttachmentRule.Enforce(redefinesClasses = true) public void testTutorialGettingStartedClassReloading() throws Exception { ByteBuddyAgent.install(); FooReloading foo = new FooReloading(); try { new ByteBuddy() .redefine(BarReloading.class) .name(FooReloading.class.getName()) .make() .load(FooReloading.class.getClassLoader(), ClassReloadingStrategy.fromInstalledAgent()); assertThat(foo.m(), is("bar")); } finally { ClassReloadingStrategy.fromInstalledAgent().reset(FooReloading.class); // Assure repeatability. } assertThat(foo.m(), is("foo")); }
Example #6
Source File: ClassReloadingStrategyTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @AgentAttachmentRule.Enforce(retransformsClasses = true) public void testFromAgentClassReloadingStrategy() throws Exception { assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class)); Foo foo = new Foo(); assertThat(foo.foo(), is(FOO)); ClassReloadingStrategy classReloadingStrategy = ClassReloadingStrategy.fromInstalledAgent(); new ByteBuddy() .redefine(Foo.class) .method(named(FOO)) .intercept(FixedValue.value(BAR)) .make() .load(Foo.class.getClassLoader(), classReloadingStrategy); try { assertThat(foo.foo(), is(BAR)); } finally { classReloadingStrategy.reset(Foo.class); assertThat(foo.foo(), is(FOO)); } }
Example #7
Source File: AsyncTraceMethodInstrumentationTest.java From apm-agent-java with Apache License 2.0 | 6 votes |
@BeforeEach void setUp(TestInfo testInfo) { MockTracer.MockInstrumentationSetup mockInstrumentationSetup = MockTracer.getOrCreateInstrumentationTracer(); reporter = mockInstrumentationSetup.getReporter(); ConfigurationRegistry config = mockInstrumentationSetup.getConfig(); coreConfiguration = config.getConfig(CoreConfiguration.class); when(coreConfiguration.getTraceMethods()).thenReturn(Arrays.asList( MethodMatcher.of("private co.elastic.apm.agent.concurrent.AsyncTraceMethodInstrumentationTest$TestAsyncTraceMethodsClass#*")) ); for (String tag : testInfo.getTags()) { TimeDuration duration = TimeDuration.of(tag.split("=")[1]); if (tag.startsWith("span_min_duration=")) { doReturn(duration).when(coreConfiguration).getSpanMinDuration(); } if (tag.startsWith("trace_methods_duration_threshold=")) { doReturn(duration).when(coreConfiguration).getTraceMethodsDurationThreshold(); } } tracer = mockInstrumentationSetup.getTracer(); ElasticApmAgent.initInstrumentation(tracer, ByteBuddyAgent.install()); }
Example #8
Source File: AgentAttachmentRule.java From byte-buddy with Apache License 2.0 | 6 votes |
public Statement apply(Statement base, FrameworkMethod method, Object target) { Enforce enforce = method.getAnnotation(Enforce.class); if (enforce != null) { if (!available) { return new NoOpStatement("The executing JVM does not support runtime attachment"); } Instrumentation instrumentation = ByteBuddyAgent.install(ByteBuddyAgent.AttachmentProvider.DEFAULT); if (enforce.redefinesClasses() && !instrumentation.isRedefineClassesSupported()) { return new NoOpStatement("The executing JVM does not support class redefinition"); } else if (enforce.retransformsClasses() && !instrumentation.isRetransformClassesSupported()) { return new NoOpStatement("The executing JVM does not support class retransformation"); } else if (enforce.nativeMethodPrefix() && !instrumentation.isNativeMethodPrefixSupported()) { return new NoOpStatement("The executing JVM does not support class native method prefixes"); } } return base; }
Example #9
Source File: JaxRsTransactionNameInstrumentationTest.java From apm-agent-java with Apache License 2.0 | 6 votes |
@Test public void testJaxRsTransactionNameFromPathAnnotationInheritanceEnabled() { when(config.getConfig(JaxRsConfiguration.class).isUseJaxRsPathForTransactionName()).thenReturn(true); when(config.getConfig(JaxRsConfiguration.class).isEnableJaxrsAnnotationInheritance()).thenReturn(true); ElasticApmAgent.initInstrumentation(tracer, ByteBuddyAgent.install()); doRequest("test"); doRequest("testAbstract"); doRequest("testInterface"); List<Transaction> actualTransactions = reporter.getTransactions(); assertThat(actualTransactions).hasSize(3); assertThat(actualTransactions.get(0).getNameAsString()).isEqualTo("GET /test"); assertThat(actualTransactions.get(1).getNameAsString()).isEqualTo("GET /testAbstract"); assertThat(actualTransactions.get(2).getNameAsString()).isEqualTo("GET /testInterface"); }
Example #10
Source File: MapReduceOutputFormatTracerTest.java From garmadon with Apache License 2.0 | 6 votes |
@Test @AgentAttachmentRule.Enforce public void OutputFormatTracer_should_intercept_InputFormat_direct_implementor() throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class)); //Install tracer ClassFileTransformer classFileTransformer = new MapReduceTracer.OutputFormatTracer().installOnByteBuddyAgent(); try { //Call InputFormat Class<?> type = classLoader.loadClass(MapReduceOutputFormatTestClasses.OneLevelHierarchy.class.getName()); invokeRecordWriter(type); //Verify mock interaction verify(eventHandler).accept(any(Long.class), argument.capture()); DataAccessEventProtos.PathEvent pathEvent = DataAccessEventProtos.PathEvent .newBuilder() .setPath(outputPath) .setType(PathType.OUTPUT.name()) .build(); assertEquals(pathEvent, argument.getValue()); } finally { ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer); } }
Example #11
Source File: AgentBuilderDefaultApplicationTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(value = 8, j9 = false) @AgentAttachmentRule.Enforce @IntegrationRule.Enforce public void testNonCapturingLambda() throws Exception { assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class)); ClassLoader classLoader = lambdaSamples(); ClassFileTransformer classFileTransformer = new AgentBuilder.Default() .with(poolStrategy) .ignore(none()) .with(AgentBuilder.LambdaInstrumentationStrategy.ENABLED) .type(isSubTypeOf(Callable.class)).transform(new SingleMethodReplacer("call")) .installOn(ByteBuddyAgent.getInstrumentation()); try { Class<?> sampleFactory = classLoader.loadClass(LAMBDA_SAMPLE_FACTORY); @SuppressWarnings("unchecked") Callable<String> instance = (Callable<String>) sampleFactory.getDeclaredMethod("nonCapturing").invoke(sampleFactory.getDeclaredConstructor().newInstance()); assertThat(instance.call(), is(BAR)); } finally { assertThat(ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer), is(true)); AgentBuilder.LambdaInstrumentationStrategy.release(classFileTransformer, ByteBuddyAgent.getInstrumentation()); } }
Example #12
Source File: ClassFileLocatorAgentBasedTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @AgentAttachmentRule.Enforce(retransformsClasses = true) @JavaVersionRule.Enforce(value = 8, atMost = 8) public void testExtractionOfInflatedMethodAccessor() throws Exception { assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class)); Method bar = Foo.class.getDeclaredMethod("bar"); for (int i = 0; i < 20; i++) { bar.invoke(new Foo()); } Field field = Method.class.getDeclaredField("methodAccessor"); field.setAccessible(true); Object methodAccessor = field.get(bar); Field delegate = methodAccessor.getClass().getDeclaredField("delegate"); delegate.setAccessible(true); Class<?> delegateClass = delegate.get(methodAccessor).getClass(); ClassFileLocator classFileLocator = ClassFileLocator.AgentBased.fromInstalledAgent(delegateClass.getClassLoader()); ClassFileLocator.Resolution resolution = classFileLocator.locate(delegateClass.getName()); assertThat(resolution.isResolved(), is(true)); assertThat(resolution.resolve(), notNullValue(byte[].class)); }
Example #13
Source File: MapRedInputFormatTracerTest.java From garmadon with Apache License 2.0 | 6 votes |
@Test public void InputFormatTracer_should_not_intercept_abstract_method() throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class)); //Install tracer ClassFileTransformer classFileTransformer = new MapReduceTracer.DeprecatedInputFormatTracer().installOnByteBuddyAgent(); try { //Prepare JobConf when(jobConf.get("mapreduce.input.fileinputformat.inputdir")).thenReturn("/some/path"); //Call InputFormat Class<?> type = classLoader.loadClass(MapRedInputFormatTestClasses.RealInputFormat.class.getName()); invokeRecordReader(type); //We just want to test no exception because of presence of abstract method and abstract class } finally { ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer); } }
Example #14
Source File: AgentBuilderDefaultApplicationTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(value = 8, j9 = false) @AgentAttachmentRule.Enforce @IntegrationRule.Enforce public void testNonCapturingLambdaIsConstant() throws Exception { assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class)); ClassLoader classLoader = lambdaSamples(); ClassFileTransformer classFileTransformer = new AgentBuilder.Default() .with(poolStrategy) .ignore(none()) .with(AgentBuilder.LambdaInstrumentationStrategy.ENABLED) .type(isSubTypeOf(Callable.class)).transform(new SingleMethodReplacer("call")) .installOn(ByteBuddyAgent.getInstrumentation()); try { Class<?> sampleFactory = classLoader.loadClass(LAMBDA_SAMPLE_FACTORY); assertThat(sampleFactory.getDeclaredMethod("nonCapturing").invoke(sampleFactory.getDeclaredConstructor().newInstance()), sameInstance(sampleFactory.getDeclaredMethod("nonCapturing").invoke(sampleFactory.getDeclaredConstructor().newInstance()))); } finally { assertThat(ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer), is(true)); AgentBuilder.LambdaInstrumentationStrategy.release(classFileTransformer, ByteBuddyAgent.getInstrumentation()); } }
Example #15
Source File: AgentBuilderDefaultApplicationTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(value = 8, j9 = false) @AgentAttachmentRule.Enforce @IntegrationRule.Enforce public void testLambdaFactoryIsReset() throws Exception { assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class)); ClassLoader classLoader = lambdaSamples(); ClassFileTransformer classFileTransformer = new AgentBuilder.Default() .with(poolStrategy) .ignore(none()) .with(AgentBuilder.LambdaInstrumentationStrategy.ENABLED) .installOn(ByteBuddyAgent.getInstrumentation()); ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer); AgentBuilder.LambdaInstrumentationStrategy.release(classFileTransformer, ByteBuddyAgent.getInstrumentation()); Class<?> sampleFactory = classLoader.loadClass(LAMBDA_SAMPLE_FACTORY); @SuppressWarnings("unchecked") Callable<String> instance = (Callable<String>) sampleFactory.getDeclaredMethod("nonCapturing").invoke(sampleFactory.getDeclaredConstructor().newInstance()); assertThat(instance.call(), is(FOO)); }
Example #16
Source File: MapReduceInputFormatTracerTest.java From garmadon with Apache License 2.0 | 6 votes |
@Test @AgentAttachmentRule.Enforce public void InputFormatTracer_should_intercept_InputFormat_direct_implementor() throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class)); //Install tracer ClassFileTransformer classFileTransformer = new MapReduceTracer.InputFormatTracer().installOnByteBuddyAgent(); try { //Call InputFormat Class<?> type = classLoader.loadClass(MapReduceInputFormatTestClasses.OneLevelHierarchy.class.getName()); invokeRecordReader(type); invokeListInputSplits(type); //Verify mock interaction verify(eventHandler, times(2)).accept(any(Long.class), argument.capture()); DataAccessEventProtos.PathEvent pathEvent = DataAccessEventProtos.PathEvent .newBuilder() .setPath(inputPath) .setType(PathType.INPUT.name()) .build(); assertEquals(pathEvent, argument.getValue()); } finally { ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer); } }
Example #17
Source File: AgentBuilderDefaultApplicationTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(value = 8, j9 = false) @AgentAttachmentRule.Enforce @IntegrationRule.Enforce public void testArgumentCapturingLambda() throws Exception { assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class)); ClassLoader classLoader = lambdaSamples(); ClassFileTransformer classFileTransformer = new AgentBuilder.Default() .with(poolStrategy) .ignore(none()) .with(AgentBuilder.LambdaInstrumentationStrategy.ENABLED) .type(isSubTypeOf(Callable.class)).transform(new SingleMethodReplacer("call")) .installOn(ByteBuddyAgent.getInstrumentation()); try { Class<?> sampleFactory = classLoader.loadClass(LAMBDA_SAMPLE_FACTORY); @SuppressWarnings("unchecked") Callable<String> instance = (Callable<String>) sampleFactory.getDeclaredMethod("argumentCapturing", String.class).invoke(sampleFactory.getDeclaredConstructor().newInstance(), FOO); assertThat(instance.call(), is(BAR)); } finally { assertThat(ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer), is(true)); AgentBuilder.LambdaInstrumentationStrategy.release(classFileTransformer, ByteBuddyAgent.getInstrumentation()); } }
Example #18
Source File: AgentBuilderDefaultApplicationTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(value = 8, j9 = false) @AgentAttachmentRule.Enforce @IntegrationRule.Enforce public void testReturnTypeTransformingLambda() throws Exception { assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class)); ClassLoader classLoader = lambdaSamples(); ClassFileTransformer classFileTransformer = new AgentBuilder.Default() .with(poolStrategy) .ignore(none()) .with(AgentBuilder.LambdaInstrumentationStrategy.ENABLED) .type(isSubTypeOf(Callable.class)).transform(new SingleMethodReplacer("call")) .installOn(ByteBuddyAgent.getInstrumentation()); try { Class<?> sampleFactory = classLoader.loadClass(LAMBDA_SAMPLE_FACTORY); Runnable instance = (Runnable) sampleFactory.getDeclaredMethod("returnTypeTransforming").invoke(sampleFactory.getDeclaredConstructor().newInstance()); instance.run(); } finally { assertThat(ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer), is(true)); AgentBuilder.LambdaInstrumentationStrategy.release(classFileTransformer, ByteBuddyAgent.getInstrumentation()); } }
Example #19
Source File: AgentBuilderDefaultApplicationTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(value = 8, j9 = false) @AgentAttachmentRule.Enforce @IntegrationRule.Enforce public void testInstanceCapturingLambda() throws Exception { assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class)); ClassLoader classLoader = lambdaSamples(); ClassFileTransformer classFileTransformer = new AgentBuilder.Default() .with(poolStrategy) .ignore(none()) .with(AgentBuilder.LambdaInstrumentationStrategy.ENABLED) .type(isSubTypeOf(Callable.class)).transform(new SingleMethodReplacer("call")) .installOn(ByteBuddyAgent.getInstrumentation()); try { Class<?> sampleFactory = classLoader.loadClass(LAMBDA_SAMPLE_FACTORY); @SuppressWarnings("unchecked") Callable<String> instance = (Callable<String>) sampleFactory.getDeclaredMethod("instanceCapturing").invoke(sampleFactory.getDeclaredConstructor().newInstance()); assertThat(instance.call(), is(BAR)); } finally { assertThat(ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer), is(true)); AgentBuilder.LambdaInstrumentationStrategy.release(classFileTransformer, ByteBuddyAgent.getInstrumentation()); } }
Example #20
Source File: AgentBuilderDefaultApplicationTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(value = 8, j9 = false) @AgentAttachmentRule.Enforce @IntegrationRule.Enforce public void testNonCapturingLambdaWithArguments() throws Exception { assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class)); ClassLoader classLoader = lambdaSamples(); ClassFileTransformer classFileTransformer = new AgentBuilder.Default() .with(poolStrategy) .ignore(none()) .with(AgentBuilder.LambdaInstrumentationStrategy.ENABLED) .type(isSubTypeOf(Class.forName("java.util.function.Function"))).transform(new SingleMethodReplacer("apply")) .installOn(ByteBuddyAgent.getInstrumentation()); try { Class<?> sampleFactory = classLoader.loadClass(LAMBDA_SAMPLE_FACTORY); Object instance = sampleFactory.getDeclaredMethod("nonCapturingWithArguments").invoke(sampleFactory.getDeclaredConstructor().newInstance()); assertThat(instance.getClass().getMethod("apply", Object.class).invoke(instance, FOO), is((Object) BAR)); } finally { assertThat(ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer), is(true)); AgentBuilder.LambdaInstrumentationStrategy.release(classFileTransformer, ByteBuddyAgent.getInstrumentation()); } }
Example #21
Source File: AgentBuilderDefaultApplicationTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @JavaVersionRule.Enforce(value = 8, j9 = false) @AgentAttachmentRule.Enforce @IntegrationRule.Enforce public void testCapturingLambdaWithArguments() throws Exception { assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class)); ClassLoader classLoader = lambdaSamples(); ClassFileTransformer classFileTransformer = new AgentBuilder.Default() .with(poolStrategy) .ignore(none()) .with(AgentBuilder.LambdaInstrumentationStrategy.ENABLED) .type(isSubTypeOf(Class.forName("java.util.function.Function"))).transform(new SingleMethodReplacer("apply")) .installOn(ByteBuddyAgent.getInstrumentation()); try { Class<?> sampleFactory = classLoader.loadClass(LAMBDA_SAMPLE_FACTORY); Object instance = sampleFactory.getDeclaredMethod("capturingWithArguments", String.class).invoke(sampleFactory.getDeclaredConstructor().newInstance(), FOO); assertThat(instance.getClass().getMethod("apply", Object.class).invoke(instance, FOO), is((Object) BAR)); } finally { assertThat(ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer), is(true)); AgentBuilder.LambdaInstrumentationStrategy.release(classFileTransformer, ByteBuddyAgent.getInstrumentation()); } }
Example #22
Source File: ClassReloadingStrategyTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Test @AgentAttachmentRule.Enforce(retransformsClasses = true) public void testClassRedefinitionRenamingWithStackMapFrames() throws Exception { assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class)); ClassReloadingStrategy classReloadingStrategy = ClassReloadingStrategy.fromInstalledAgent(); Bar bar = new Bar(); new ByteBuddy().redefine(Qux.class) .name(Bar.class.getName()) .make() .load(Bar.class.getClassLoader(), classReloadingStrategy); try { assertThat(bar.foo(), is(BAR)); } finally { classReloadingStrategy.reset(Bar.class); assertThat(bar.foo(), is(FOO)); } }
Example #23
Source File: ClassReloadingStrategyTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test @JavaVersionRule.Enforce(value = 8, atMost = 8, j9 = false) @AgentAttachmentRule.Enforce(retransformsClasses = true) public void testAnonymousType() throws Exception { ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileLocator.ForClassLoader.readToNames(Class.forName(LAMBDA_SAMPLE_FACTORY)), ByteArrayClassLoader.PersistenceHandler.MANIFEST); Instrumentation instrumentation = ByteBuddyAgent.install(); Class<?> factory = classLoader.loadClass(LAMBDA_SAMPLE_FACTORY); @SuppressWarnings("unchecked") Callable<String> instance = (Callable<String>) factory.getDeclaredMethod("nonCapturing").invoke(factory.getDeclaredConstructor().newInstance()); // Anonymous types can only be reset to their original format, if a retransformation is applied. ClassReloadingStrategy classReloadingStrategy = new ClassReloadingStrategy(instrumentation, ClassReloadingStrategy.Strategy.RETRANSFORMATION).preregistered(instance.getClass()); ClassFileLocator classFileLocator = ClassFileLocator.AgentBased.of(instrumentation, instance.getClass()); try { assertThat(instance.call(), is(FOO)); new ByteBuddy() .redefine(instance.getClass(), classFileLocator) .method(named("call")) .intercept(FixedValue.value(BAR)) .make() .load(instance.getClass().getClassLoader(), classReloadingStrategy); assertThat(instance.call(), is(BAR)); } finally { classReloadingStrategy.reset(classFileLocator, instance.getClass()); assertThat(instance.call(), is(FOO)); } }
Example #24
Source File: ClassFileLocatorAgentBasedTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test @AgentAttachmentRule.Enforce(retransformsClasses = true) public void testExtraction() throws Exception { assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class)); ClassFileLocator classFileLocator = ClassFileLocator.AgentBased.fromInstalledAgent(getClass().getClassLoader()); ClassFileLocator.Resolution resolution = classFileLocator.locate(Foo.class.getName()); assertThat(resolution.isResolved(), is(true)); assertThat(resolution.resolve(), notNullValue(byte[].class)); }
Example #25
Source File: JaxRsTransactionNameInstrumentationTest.java From apm-agent-java with Apache License 2.0 | 5 votes |
@Test public void testJaxRsTransactionNameFromPathAnnotationInheritanceEnabledOnMethodWithPathAnnotation() { when(config.getConfig(JaxRsConfiguration.class).isUseJaxRsPathForTransactionName()).thenReturn(true); when(config.getConfig(JaxRsConfiguration.class).isEnableJaxrsAnnotationInheritance()).thenReturn(true); ElasticApmAgent.initInstrumentation(tracer, ByteBuddyAgent.install()); doRequest("testWithPathMethod"); doRequest("testWithPathMethod/15"); List<Transaction> actualTransactions = reporter.getTransactions(); assertThat(actualTransactions).hasSize(2); assertThat(actualTransactions.get(0).getNameAsString()).isEqualTo("GET /testWithPathMethod"); assertThat(actualTransactions.get(1).getNameAsString()).isEqualTo("GET /testWithPathMethod/{id}"); }
Example #26
Source File: JaxRsTransactionNameInstrumentationTest.java From apm-agent-java with Apache License 2.0 | 5 votes |
@Test public void testProxyClassInstrumentationExclusion() { when(config.getConfig(JaxRsConfiguration.class).isEnableJaxrsAnnotationInheritance()).thenReturn(true); ElasticApmAgent.initInstrumentation(tracer, ByteBuddyAgent.install()); doRequest("testViewProxy"); doRequest("testProxyProxy"); List<Transaction> actualTransactions = reporter.getTransactions(); assertThat(actualTransactions).hasSize(2); assertThat(actualTransactions.get(0).getNameAsString()).isEqualTo("unnamed"); assertThat(actualTransactions.get(1).getNameAsString()).isEqualTo("unnamed"); }
Example #27
Source File: JaxRsTransactionNameInstrumentationTest.java From apm-agent-java with Apache License 2.0 | 5 votes |
@Test public void testJaxRsTransactionNameNonSampledTransactions() throws IOException { config.getConfig(CoreConfiguration.class).getSampleRate().update(0.0, SpyConfiguration.CONFIG_SOURCE_NAME); ElasticApmAgent.initInstrumentation(tracer, ByteBuddyAgent.install()); doRequest("test"); List<Transaction> actualTransactions = reporter.getTransactions(); assertThat(actualTransactions).hasSize(1); assertThat(actualTransactions.get(0).getNameAsString()).isEqualTo("ResourceWithPath#testMethod"); }
Example #28
Source File: JaxRsTransactionNameInstrumentationTest.java From apm-agent-java with Apache License 2.0 | 5 votes |
@Test public void testJaxRsTransactionNameFromPathAnnotationInheritanceEnabledOnMethodWithPathAnnotationWithSlash() { when(config.getConfig(JaxRsConfiguration.class).isUseJaxRsPathForTransactionName()).thenReturn(true); when(config.getConfig(JaxRsConfiguration.class).isEnableJaxrsAnnotationInheritance()).thenReturn(true); ElasticApmAgent.initInstrumentation(tracer, ByteBuddyAgent.install()); doRequest("testWithPathMethodSlash"); doRequest("testWithPathMethodSlash/15"); List<Transaction> actualTransactions = reporter.getTransactions(); assertThat(actualTransactions).hasSize(2); assertThat(actualTransactions.get(0).getNameAsString()).isEqualTo("GET /testWithPathMethodSlash"); assertThat(actualTransactions.get(1).getNameAsString()).isEqualTo("GET /testWithPathMethodSlash/{id}"); }
Example #29
Source File: JaxRsTransactionNameInstrumentationTest.java From apm-agent-java with Apache License 2.0 | 5 votes |
@Test public void testJaxRsTransactionNameFromPathAnnotationInheritanceEnabledOnMethodWithComplexPath() { when(config.getConfig(JaxRsConfiguration.class).isUseJaxRsPathForTransactionName()).thenReturn(true); when(config.getConfig(JaxRsConfiguration.class).isEnableJaxrsAnnotationInheritance()).thenReturn(true); ElasticApmAgent.initInstrumentation(tracer, ByteBuddyAgent.install()); doRequest("/foo/bar"); List<Transaction> actualTransactions = reporter.getTransactions(); assertThat(actualTransactions).hasSize(1); assertThat(actualTransactions.get(0).getNameAsString()).isEqualTo("GET /foo/bar"); }
Example #30
Source File: AgentBuilderDefaultApplicationRedefinitionReiterationTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test @AgentAttachmentRule.Enforce(retransformsClasses = true) public void testAdviceWithoutLoadedClasses() throws Exception { assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class)); ClassFileTransformer classFileTransformer = installInstrumentation(); try { assertAdvice(); } finally { assertThat(ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer), is(true)); } }