Search in sources :

Example 61 with ByteBuddy

use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.

the class AdviceTest method testTrivialAdviceDistributedExitOnly.

@Test
public void testTrivialAdviceDistributedExitOnly() throws Exception {
    Class<?> type = new ByteBuddy().redefine(Sample.class).visit(Advice.to(EmptyAdvice.class, TrivialAdvice.class).on(named(FOO))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) FOO));
    assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 0));
    assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 1));
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 62 with ByteBuddy

use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.

the class AdviceTest method testExceptionCatchedAdvice.

@Test
public void testExceptionCatchedAdvice() throws Exception {
    Class<?> type = new ByteBuddy().redefine(ExceptionCatchedAdvice.class).visit(Advice.to(ExceptionCatchedAdvice.class).on(named(FOO))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    try {
        type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance());
        fail();
    } catch (InvocationTargetException exception) {
        assertThat(exception.getCause(), instanceOf(RuntimeException.class));
    }
    assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1));
    assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 1));
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Example 63 with ByteBuddy

use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.

the class AdviceTest method testNonAssignableCasting.

@Test
public void testNonAssignableCasting() throws Exception {
    Class<?> type = new ByteBuddy().redefine(NonAssignableReturnTypeAdvice.class).visit(Advice.to(NonAssignableReturnTypeAdvice.class).on(named(FOO))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    try {
        type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance());
        fail();
    } catch (InvocationTargetException exception) {
        assertThat(exception.getCause(), instanceOf(ClassCastException.class));
    }
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Example 64 with ByteBuddy

use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.

the class AdviceTest method testTrivialAdviceWithHandlerAndSuppression.

@Test
public void testTrivialAdviceWithHandlerAndSuppression() throws Exception {
    Class<?> type = new ByteBuddy().redefine(Sample.class).visit(Advice.to(TrivialAdviceWithSuppression.class).on(named(FOO + BAZ))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredMethod(FOO + BAZ).invoke(type.getDeclaredConstructor().newInstance()), is((Object) FOO));
    assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1));
    assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 1));
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 65 with ByteBuddy

use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.

the class AdviceTest method testAdviceWithEntranceValue.

@Test
public void testAdviceWithEntranceValue() throws Exception {
    Class<?> type = new ByteBuddy().redefine(Sample.class).visit(Advice.to(EntranceValueAdvice.class).on(named(FOO))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) FOO));
    assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1));
    assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 1));
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Aggregations

ByteBuddy (net.bytebuddy.ByteBuddy)369 Test (org.junit.Test)355 DynamicType (net.bytebuddy.dynamic.DynamicType)188 Method (java.lang.reflect.Method)34 TypeDescription (net.bytebuddy.description.type.TypeDescription)26 InvocationTargetException (java.lang.reflect.InvocationTargetException)19 Field (java.lang.reflect.Field)18 AbstractDynamicTypeBuilderTest (net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest)17 URLClassLoader (java.net.URLClassLoader)14 Instrumentation (java.lang.instrument.Instrumentation)10 StubMethod (net.bytebuddy.implementation.StubMethod)7 TextConstant (net.bytebuddy.implementation.bytecode.constant.TextConstant)7 ClassFileLocator (net.bytebuddy.dynamic.ClassFileLocator)6 RandomString (net.bytebuddy.utility.RandomString)6 ClassFileTransformer (java.lang.instrument.ClassFileTransformer)5 MethodDescription (net.bytebuddy.description.method.MethodDescription)5 File (java.io.File)4 Constructor (java.lang.reflect.Constructor)4 FieldDescription (net.bytebuddy.description.field.FieldDescription)4 ByteArrayClassLoader (net.bytebuddy.dynamic.loading.ByteArrayClassLoader)4