コード例 #1
0
 @Test
 public void testGenericMethodWithoutGenericExceptionTypes() throws Exception {
   DynamicType.Unloaded<?> unloaded =
       new ByteBuddy()
           .redefine(GenericMethod.class)
           .method(named(BAR))
           .intercept(FixedValue.nullValue())
           .make();
   Class<?> type =
       unloaded
           .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER)
           .getLoaded();
   MethodDescription createdMethod =
       new MethodDescription.ForLoadedMethod(type.getDeclaredMethod(BAR, Object.class));
   MethodDescription originalMethod =
       new MethodDescription.ForLoadedMethod(
           GenericMethod.class.getDeclaredMethod(BAR, Object.class));
   assertThat(createdMethod.getTypeVariables(), is(originalMethod.getTypeVariables()));
   assertThat(createdMethod.getReturnType(), is(originalMethod.getReturnType()));
   assertThat(
       createdMethod.getParameters().getOnly().getType(),
       is(originalMethod.getParameters().getOnly().getType()));
   assertThat(
       createdMethod.getExceptionTypes().getOnly(),
       is(originalMethod.getExceptionTypes().getOnly()));
 }
コード例 #2
0
 @Test
 @ToolsJarRule.Enforce
 public void testAgentWithoutSelfInitializationWithNativeMethodPrefix() throws Exception {
   assertThat(ByteBuddyAgent.installOnOpenJDK(), instanceOf(Instrumentation.class));
   ClassFileTransformer classFileTransformer =
       new AgentBuilder.Default()
           .disableSelfInitialization()
           .withNativeMethodPrefix(QUX)
           .rebase(isAnnotatedWith(ShouldRebase.class), ElementMatchers.is(classLoader))
           .transform(new FooTransformer())
           .installOnByteBuddyAgent();
   try {
     Class<?> type = classLoader.loadClass(Baz.class.getName());
     assertThat(type.getDeclaredMethod(FOO).invoke(type.newInstance()), is((Object) BAR));
     assertThat(type.getDeclaredMethod(QUX + FOO), notNullValue(Method.class));
   } finally {
     ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
   }
 }
コード例 #3
0
 @Test
 @ToolsJarRule.Enforce
 public void testAgentSelfInitializationAuxiliaryTypes() throws Exception {
   assertThat(ByteBuddyAgent.installOnOpenJDK(), instanceOf(Instrumentation.class));
   ClassFileTransformer classFileTransformer =
       new AgentBuilder.Default()
           .rebase(isAnnotatedWith(ShouldRebase.class), ElementMatchers.is(classLoader))
           .transform(new QuxTransformer())
           .installOnByteBuddyAgent();
   try {
     Class<?> type = classLoader.loadClass(Qux.class.getName());
     assertThat(type.getDeclaredMethod(FOO).invoke(type.newInstance()), is((Object) (FOO + BAR)));
   } finally {
     ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
   }
 }