Search in sources :

Example 71 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 testTrivialDelegation.

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

Example 72 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 testAdviceOnConstructorExitAdviceWithSuppression.

@Test
public void testAdviceOnConstructorExitAdviceWithSuppression() throws Exception {
    Class<?> type = new ByteBuddy().redefine(Sample.class).visit(Advice.to(TrivialAdviceSkipExceptionWithSuppression.class).on(isConstructor())).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredConstructor().newInstance(), notNullValue(Object.class));
    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 73 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 testVariableMappingAdviceLarger.

@Test
public void testVariableMappingAdviceLarger() throws Exception {
    Class<?> type = new ByteBuddy().redefine(Sample.class).visit(Advice.to(AdviceWithVariableValues.class).on(named(BAR))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredMethod(BAR, String.class).invoke(type.getDeclaredConstructor().newInstance(), FOO + BAR + QUX + BAZ), is((Object) (FOO + BAR + QUX + BAZ)));
    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 74 with ByteBuddy

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy in project hibernate-orm by hibernate.

the class ByteBuddyProxyFactory method buildProxy.

public static Class buildProxy(final Class persistentClass, final Class[] interfaces) {
    Set<Class<?>> key = new HashSet<Class<?>>();
    if (interfaces.length == 1) {
        key.add(persistentClass);
    }
    key.addAll(Arrays.<Class<?>>asList(interfaces));
    return CACHE.findOrInsert(persistentClass.getClassLoader(), new TypeCache.SimpleKey(key), new Callable<Class<?>>() {

        @Override
        public Class<?> call() throws Exception {
            return new ByteBuddy().with(TypeValidation.DISABLED).with(new NamingStrategy.SuffixingRandom("HibernateProxy")).subclass(interfaces.length == 1 ? persistentClass : Object.class, ConstructorStrategy.Default.IMITATE_SUPER_CLASS_OPENING).implement((Type[]) interfaces).method(ElementMatchers.isVirtual().and(ElementMatchers.not(ElementMatchers.isFinalizer()))).intercept(MethodDelegation.to(ProxyConfiguration.InterceptorDispatcher.class)).method(ElementMatchers.nameStartsWith("$$_hibernate_").and(ElementMatchers.isVirtual())).intercept(SuperMethodCall.INSTANCE).defineField(ProxyConfiguration.INTERCEPTOR_FIELD_NAME, ProxyConfiguration.Interceptor.class, Visibility.PRIVATE).implement(ProxyConfiguration.class).intercept(FieldAccessor.ofField(ProxyConfiguration.INTERCEPTOR_FIELD_NAME).withAssigner(Assigner.DEFAULT, Assigner.Typing.DYNAMIC)).make().load(persistentClass.getClassLoader()).getLoaded();
        }
    }, CACHE);
}
Also used : ProxyConfiguration(org.hibernate.proxy.ProxyConfiguration) ByteBuddy(net.bytebuddy.ByteBuddy) TypeCache(net.bytebuddy.TypeCache) HibernateException(org.hibernate.HibernateException) HashSet(java.util.HashSet)

Example 75 with ByteBuddy

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

the class EntryPointDefaultTest method testRedefineLocal.

@Test
@SuppressWarnings("unchecked")
public void testRedefineLocal() throws Exception {
    assertThat(EntryPoint.Default.REDEFINE_LOCAL.getByteBuddy(), is(new ByteBuddy().with(Implementation.Context.Disabled.Factory.INSTANCE)));
    when(byteBuddy.redefine(typeDescription, classFileLocator)).thenReturn((DynamicType.Builder) builder);
    when(builder.ignoreAlso(not(isDeclaredBy(typeDescription)))).thenReturn((DynamicType.Builder) otherBuilder);
    assertThat(EntryPoint.Default.REDEFINE_LOCAL.transform(typeDescription, byteBuddy, classFileLocator, methodNameTransformer), is((DynamicType.Builder) otherBuilder));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) 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