Search in sources :

Example 21 with ByteBuddy

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

the class AdviceVariableAccessTest method testArray.

@Test
public void testArray() throws Exception {
    Class<?> dynamicType = new ByteBuddy().redefine(sample).visit(Advice.to(AdviceVariableAccessTest.class).on(named(READ).or(named(WRITE)))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    Object instance = dynamicType.getDeclaredConstructor().newInstance();
    assertThat(dynamicType.getDeclaredMethod(WRITE, type).invoke(instance, value), nullValue(Object.class));
    assertThat(dynamicType.getDeclaredMethod(READ).invoke(instance), is(value));
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 22 with ByteBuddy

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

the class AdviceFrameTest method testFrameAdvice.

@Test
public void testFrameAdvice() throws Exception {
    Class<?> type = new ByteBuddy().redefine(FrameSample.class).visit(Advice.to(advice).on(named(FOO))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredMethod(FOO, String.class).invoke(type.getDeclaredConstructor().newInstance(), FOO), is((Object) FOO));
    assertThat(type.getField(COUNT).getInt(null), is((Object) count));
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 23 with ByteBuddy

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

the class AdviceFrameTest method testFrameAdviceStaticMethodExpanded.

@Test
public void testFrameAdviceStaticMethodExpanded() throws Exception {
    Class<?> type = new ByteBuddy().redefine(FrameSample.class).visit(Advice.to(advice).on(named(BAR)).readerFlags(ClassReader.EXPAND_FRAMES)).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredMethod(BAR, String.class).invoke(null, FOO), is((Object) FOO));
    assertThat(type.getField(COUNT).getInt(null), is((Object) count));
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 24 with ByteBuddy

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

the class AdviceNoRegularReturnTest method testNoRegularReturnWithHandler.

@Test
public void testNoRegularReturnWithHandler() throws Exception {
    Class<?> type = new ByteBuddy().redefine(this.type).visit(Advice.to(ExitAdviceWithHandler.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));
    }
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Example 25 with ByteBuddy

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

the class AdviceNoRegularReturnTest method testNoRegularReturnWithSkip.

@Test
public void testNoRegularReturnWithSkip() throws Exception {
    Class<?> type = new ByteBuddy().redefine(this.type).visit(Advice.to(EnterAdviceSkip.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));
    }
}
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