Search in sources :

Example 31 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 32 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 33 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 34 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 35 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)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