Search in sources :

Example 51 with ByteBuddy

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

the class AdviceExchangedDuplicationTest method testAdvice.

@Test
public void testAdvice() throws Exception {
    Class<?> type = new ByteBuddy().subclass(Object.class).defineMethod(FOO, valueType, Visibility.PUBLIC).intercept(new DuplicationImplementation()).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER_PERSISTENT).getLoaded();
    Class<?> redefined = new ByteBuddy().redefine(type).visit(Advice.to(AdviceExchangedDuplicationTest.class).on(named(FOO))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(redefined.getDeclaredMethod(FOO).invoke(redefined.getDeclaredConstructor().newInstance()), is(value));
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 52 with ByteBuddy

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

the class AdviceFrameTest method testFrameAdviceExpanded.

@Test
public void testFrameAdviceExpanded() throws Exception {
    Class<?> type = new ByteBuddy().redefine(FrameSample.class).visit(Advice.to(advice).on(named(FOO)).readerFlags(ClassReader.EXPAND_FRAMES)).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 53 with ByteBuddy

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

the class AdviceFrameTest method testFrameAdviceComputedMaxima.

@Test
public void testFrameAdviceComputedMaxima() throws Exception {
    Class<?> type = new ByteBuddy().redefine(FrameSample.class).visit(Advice.to(advice).on(named(FOO)).writerFlags(ClassWriter.COMPUTE_MAXS)).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 54 with ByteBuddy

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

the class AdviceFrameTest method testFrameAdviceStaticMethodComputedMaxima.

@Test
public void testFrameAdviceStaticMethodComputedMaxima() throws Exception {
    Class<?> type = new ByteBuddy().redefine(FrameSample.class).visit(Advice.to(advice).on(named(BAR)).writerFlags(ClassWriter.COMPUTE_MAXS)).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 55 with ByteBuddy

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

the class AdviceFrameTest method testFrameAdviceStaticMethod.

@Test
public void testFrameAdviceStaticMethod() throws Exception {
    Class<?> type = new ByteBuddy().redefine(FrameSample.class).visit(Advice.to(advice).on(named(BAR))).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)

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