Search in sources :

Example 66 with ByteBuddy

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy in project byte-buddy by raphw.

the class AdviceTest method testExceptionPriniting.

@Test
public void testExceptionPriniting() throws Exception {
    Class<?> type = new ByteBuddy().redefine(Sample.class).visit(Advice.to(ExceptionWriterTest.class).withExceptionPrinting().on(named(FOO))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    PrintStream printStream = mock(PrintStream.class);
    PrintStream err = System.err;
    synchronized (System.err) {
        System.setErr(printStream);
        try {
            assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) FOO));
        } finally {
            System.setErr(err);
        }
    }
    verify(printStream, times(2)).println(Mockito.any(RuntimeException.class));
}
Also used : PrintStream(java.io.PrintStream) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 67 with ByteBuddy

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy in project byte-buddy by raphw.

the class AdviceTest method testAdviceSkipExceptionExplicit.

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

Example 68 with ByteBuddy

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy in project byte-buddy by raphw.

the class AdviceTest method testAdviceWithExceptionHandler.

@Test
public void testAdviceWithExceptionHandler() throws Exception {
    Class<?> type = new ByteBuddy().redefine(Sample.class).visit(Advice.to(ExceptionHandlerAdvice.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)

Example 69 with ByteBuddy

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy in project byte-buddy by raphw.

the class AdviceTest method testFrameAdviceSimpleShiftExpanded.

@Test
public void testFrameAdviceSimpleShiftExpanded() throws Exception {
    Class<?> type = new ByteBuddy().redefine(Sample.class).visit(Advice.to(FrameShiftAdvice.class).on(named(FOO)).readerFlags(ClassReader.EXPAND_FRAMES)).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) FOO));
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 70 with ByteBuddy

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy in project byte-buddy by raphw.

the class AdviceTest method testAdviceThrowNotSuppressedOnExit.

@Test
public void testAdviceThrowNotSuppressedOnExit() throws Exception {
    Class<?> type = new ByteBuddy().redefine(TracableSample.class).visit(Advice.to(ThrowNotSuppressedOnExit.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(Exception.class));
    }
    assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1));
    assertThat(type.getDeclaredField(INSIDE).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)

Aggregations

ByteBuddy (net.bytebuddy.ByteBuddy)384 Test (org.junit.Test)361 DynamicType (net.bytebuddy.dynamic.DynamicType)190 Method (java.lang.reflect.Method)36 TypeDescription (net.bytebuddy.description.type.TypeDescription)25 InvocationTargetException (java.lang.reflect.InvocationTargetException)21 Field (java.lang.reflect.Field)18 AbstractDynamicTypeBuilderTest (net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest)17 URLClassLoader (java.net.URLClassLoader)15 Instrumentation (java.lang.instrument.Instrumentation)10 StubMethod (net.bytebuddy.implementation.StubMethod)8 TextConstant (net.bytebuddy.implementation.bytecode.constant.TextConstant)7 ClassFileLocator (net.bytebuddy.dynamic.ClassFileLocator)6 File (java.io.File)5 ClassFileTransformer (java.lang.instrument.ClassFileTransformer)5 FieldDescription (net.bytebuddy.description.field.FieldDescription)5 ByteArrayClassLoader (net.bytebuddy.dynamic.loading.ByteArrayClassLoader)5 RandomString (net.bytebuddy.utility.RandomString)5 Constructor (java.lang.reflect.Constructor)4 Callable (java.util.concurrent.Callable)4