Search in sources :

Example 26 with ByteBuddy

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

the class AdviceNoRegularReturnWithinAdviceTest method testNoRegularReturn.

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

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

the class AdviceSizeConversionTest method testAdvice.

@Test
public void testAdvice() throws Exception {
    Class<?> type = new ByteBuddy().redefine(target).visit(Advice.to(AdviceSizeConversionTest.class).on(named(FOO))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredMethod(FOO, parameter).invoke(type.getDeclaredConstructor().newInstance(), input), is(output));
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 28 with ByteBuddy

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

the class AdviceSkipOnDefaultValueTest method testAdvice.

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

Example 29 with ByteBuddy

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

the class AdviceCustomAnnotationOnFieldTest method testPrimitiveField.

@Test
public void testPrimitiveField() throws Exception {
    Class<?> type = new ByteBuddy().redefine(target).visit(Advice.withCustomMapping().bind(FieldValue.class, new FieldDescription.ForLoadedField(target.getDeclaredField(FOO))).to(target).on(named(FOO))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is(expected));
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) FieldDescription(net.bytebuddy.description.field.FieldDescription) Test(org.junit.Test)

Example 30 with ByteBuddy

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

the class AdviceDeadCodeTest method testAdviceContainsDeadCode.

@Test
public void testAdviceContainsDeadCode() throws Exception {
    Class<?> advice = new ByteBuddy(classFileVersion).subclass(Object.class).defineMethod(FOO, void.class, Ownership.STATIC).intercept(new DeadVoidAppender()).annotateMethod(AnnotationDescription.Builder.ofType(Advice.OnMethodEnter.class).define("suppress", RuntimeException.class).build()).annotateMethod(AnnotationDescription.Builder.ofType(Advice.OnMethodExit.class).define("suppress", RuntimeException.class).build()).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER_PERSISTENT).getLoaded();
    Class<?> foo = new ByteBuddy(classFileVersion).subclass(Object.class).defineMethod("foo", String.class, Visibility.PUBLIC).intercept(FixedValue.value(FOO)).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER_PERSISTENT).getLoaded();
    Class<?> redefined = new ByteBuddy().redefine(foo).visit(Advice.to(advice).on(named(FOO))).make().load(null, ClassLoadingStrategy.Default.CHILD_FIRST).getLoaded();
    assertThat(redefined, not(sameInstance((Object) foo)));
    assertThat(redefined.getDeclaredMethod(FOO).invoke(redefined.getDeclaredConstructor().newInstance()), is((Object) FOO));
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Aggregations

ByteBuddy (net.bytebuddy.ByteBuddy)385 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