当前位置: 首页>>代码示例>>Java>>正文


Java ByteBuddyAgent类代码示例

本文整理汇总了Java中net.bytebuddy.agent.ByteBuddyAgent的典型用法代码示例。如果您正苦于以下问题:Java ByteBuddyAgent类的具体用法?Java ByteBuddyAgent怎么用?Java ByteBuddyAgent使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


ByteBuddyAgent类属于net.bytebuddy.agent包,在下文中一共展示了ByteBuddyAgent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testRedefinitionReloadingStrategy

import net.bytebuddy.agent.ByteBuddyAgent; //导入依赖的package包/类
@Test
@AgentAttachmentRule.Enforce(redefinesClasses = true)
public void testRedefinitionReloadingStrategy() throws Exception {
    assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
    Foo foo = new Foo();
    assertThat(foo.foo(), is(FOO));
    ClassReloadingStrategy classReloadingStrategy = new ClassReloadingStrategy(ByteBuddyAgent.getInstrumentation(), ClassReloadingStrategy.Strategy.REDEFINITION);
    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));
    }
}
 
开发者ID:raphw,项目名称:byte-buddy,代码行数:21,代码来源:ClassReloadingStrategyTest.java

示例2: install

import net.bytebuddy.agent.ByteBuddyAgent; //导入依赖的package包/类
public static void install() {
    ByteBuddyAgent.install();

    ClassLoader targetClassLoader = File.class.getClassLoader();

    // interceptor class must be injected to the same classloader as the target class that is intercepted
    new ByteBuddy().redefine(CountFileSystemOperations.class)
            .make()
            .load(targetClassLoader,
                    ClassReloadingStrategy.fromInstalledAgent());

    new ByteBuddy().redefine(DirectoryFileTree.class)
            .visit(new AsmVisitorWrapper.ForDeclaredMethods().writerFlags(ClassWriter.COMPUTE_FRAMES)
                    .method(ElementMatchers.named("length"), Advice.to(CountFileSystemOperations.LengthMethod.class))
                    .method(ElementMatchers.named("isFile"), Advice.to(CountFileSystemOperations.IsFileMethod.class))
                    .method(ElementMatchers.named("isDirectory"), Advice.to(CountFileSystemOperations.IsDirectoryMethod.class))
                    .method(ElementMatchers.named("lastModified"), Advice.to(CountFileSystemOperations.LastModifiedMethod.class))
                    .method(ElementMatchers.named("exists"), Advice.to(CountFileSystemOperations.ExistsMethod.class))

            )
            .make()
            .load(targetClassLoader,
                    ClassReloadingStrategy.fromInstalledAgent());
}
 
开发者ID:lhotari,项目名称:gradle-profiling,代码行数:25,代码来源:FileSystemOperationsInterceptor.java

示例3: install

import net.bytebuddy.agent.ByteBuddyAgent; //导入依赖的package包/类
public static void install() {
    ByteBuddyAgent.install();

    ClassLoader targetClassLoader = DirectoryFileTree.class.getClassLoader();

    // interceptor class must be injected to the same classloader as the target class that is intercepted
    new ByteBuddy().redefine(CountDirectoryScans.class)
            .make()
            .load(targetClassLoader,
                    ClassReloadingStrategy.fromInstalledAgent());

    new ByteBuddy().redefine(DirectoryFileTree.class)
            .visit(new AsmVisitorWrapper.ForDeclaredMethods().writerFlags(ClassWriter.COMPUTE_FRAMES).method(ElementMatchers.named("visitFrom"), Advice.to(CountDirectoryScans.class)))
            .make()
            .load(targetClassLoader,
                    ClassReloadingStrategy.fromInstalledAgent());
}
 
开发者ID:lhotari,项目名称:gradle-profiling,代码行数:18,代码来源:DirectoryScanningInterceptor.java

示例4: testTutorialGettingStartedClassReloading

import net.bytebuddy.agent.ByteBuddyAgent; //导入依赖的package包/类
@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"));
}
 
开发者ID:raphw,项目名称:byte-buddy,代码行数:18,代码来源:ByteBuddyTutorialExamplesTest.java

示例5: testNonCapturingLambda

import net.bytebuddy.agent.ByteBuddyAgent; //导入依赖的package包/类
@Test
@JavaVersionRule.Enforce(8)
@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 {
        ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
        AgentBuilder.LambdaInstrumentationStrategy.release(classFileTransformer, ByteBuddyAgent.getInstrumentation());
    }
}
 
开发者ID:raphw,项目名称:byte-buddy,代码行数:24,代码来源:AgentBuilderDefaultApplicationTest.java

示例6: testNonCapturingLambdaIsConstant

import net.bytebuddy.agent.ByteBuddyAgent; //导入依赖的package包/类
@Test
@JavaVersionRule.Enforce(8)
@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 {
        ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
        AgentBuilder.LambdaInstrumentationStrategy.release(classFileTransformer, ByteBuddyAgent.getInstrumentation());
    }
}
 
开发者ID:raphw,项目名称:byte-buddy,代码行数:23,代码来源:AgentBuilderDefaultApplicationTest.java

示例7: testLambdaFactoryIsReset

import net.bytebuddy.agent.ByteBuddyAgent; //导入依赖的package包/类
@Test
@JavaVersionRule.Enforce(8)
@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));
}
 
开发者ID:raphw,项目名称:byte-buddy,代码行数:20,代码来源:AgentBuilderDefaultApplicationTest.java

示例8: testArgumentCapturingLambda

import net.bytebuddy.agent.ByteBuddyAgent; //导入依赖的package包/类
@Test
@JavaVersionRule.Enforce(8)
@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 {
        ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
        AgentBuilder.LambdaInstrumentationStrategy.release(classFileTransformer, ByteBuddyAgent.getInstrumentation());
    }
}
 
开发者ID:raphw,项目名称:byte-buddy,代码行数:24,代码来源:AgentBuilderDefaultApplicationTest.java

示例9: testArgumentCapturingLambdaIsNotConstant

import net.bytebuddy.agent.ByteBuddyAgent; //导入依赖的package包/类
@Test
@JavaVersionRule.Enforce(8)
@AgentAttachmentRule.Enforce
@IntegrationRule.Enforce
public void testArgumentCapturingLambdaIsNotConstant() 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("argumentCapturing", String.class).invoke(sampleFactory.getDeclaredConstructor().newInstance(), FOO),
                not(sameInstance(sampleFactory.getDeclaredMethod("argumentCapturing", String.class).invoke(sampleFactory.getDeclaredConstructor().newInstance(), FOO))));
    } finally {
        ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
        AgentBuilder.LambdaInstrumentationStrategy.release(classFileTransformer, ByteBuddyAgent.getInstrumentation());
    }
}
 
开发者ID:raphw,项目名称:byte-buddy,代码行数:23,代码来源:AgentBuilderDefaultApplicationTest.java

示例10: testInstanceCapturingLambda

import net.bytebuddy.agent.ByteBuddyAgent; //导入依赖的package包/类
@Test
@JavaVersionRule.Enforce(8)
@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 {
        ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
        AgentBuilder.LambdaInstrumentationStrategy.release(classFileTransformer, ByteBuddyAgent.getInstrumentation());
    }
}
 
开发者ID:raphw,项目名称:byte-buddy,代码行数:24,代码来源:AgentBuilderDefaultApplicationTest.java

示例11: testNonCapturingLambdaWithArguments

import net.bytebuddy.agent.ByteBuddyAgent; //导入依赖的package包/类
@Test
@JavaVersionRule.Enforce(8)
@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 {
        ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
        AgentBuilder.LambdaInstrumentationStrategy.release(classFileTransformer, ByteBuddyAgent.getInstrumentation());
    }
}
 
开发者ID:raphw,项目名称:byte-buddy,代码行数:23,代码来源:AgentBuilderDefaultApplicationTest.java

示例12: testCapturingLambdaWithArguments

import net.bytebuddy.agent.ByteBuddyAgent; //导入依赖的package包/类
@Test
@JavaVersionRule.Enforce(8)
@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 {
        ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
        AgentBuilder.LambdaInstrumentationStrategy.release(classFileTransformer, ByteBuddyAgent.getInstrumentation());
    }
}
 
开发者ID:raphw,项目名称:byte-buddy,代码行数:23,代码来源:AgentBuilderDefaultApplicationTest.java

示例13: testReturnTypeTransformingLambda

import net.bytebuddy.agent.ByteBuddyAgent; //导入依赖的package包/类
@Test
@JavaVersionRule.Enforce(8)
@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 {
        ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
        AgentBuilder.LambdaInstrumentationStrategy.release(classFileTransformer, ByteBuddyAgent.getInstrumentation());
    }
}
 
开发者ID:raphw,项目名称:byte-buddy,代码行数:23,代码来源:AgentBuilderDefaultApplicationTest.java

示例14: testInstanceReturningLambda

import net.bytebuddy.agent.ByteBuddyAgent; //导入依赖的package包/类
@Test
@JavaVersionRule.Enforce(8)
@AgentAttachmentRule.Enforce
@IntegrationRule.Enforce
public void testInstanceReturningLambda() 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);
        Callable<?> instance = (Callable<?>) sampleFactory.getDeclaredMethod("instanceReturning").invoke(sampleFactory.getDeclaredConstructor().newInstance());
        assertThat(instance.call(), notNullValue(Object.class));
    } finally {
        ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
        AgentBuilder.LambdaInstrumentationStrategy.release(classFileTransformer, ByteBuddyAgent.getInstrumentation());
    }
}
 
开发者ID:raphw,项目名称:byte-buddy,代码行数:23,代码来源:AgentBuilderDefaultApplicationTest.java

示例15: apply

import net.bytebuddy.agent.ByteBuddyAgent; //导入依赖的package包/类
@Override
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;
}
 
开发者ID:raphw,项目名称:byte-buddy,代码行数:19,代码来源:AgentAttachmentRule.java


注:本文中的net.bytebuddy.agent.ByteBuddyAgent类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。