Search in sources :

Example 6 with ByteBuddy

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

the class SubclassDynamicTypeBuilderTest method testDoesNotOverrideMethodWithPackagePrivateReturnType.

@Test
public void testDoesNotOverrideMethodWithPackagePrivateReturnType() throws Exception {
    Class<?> type = new ByteBuddy().subclass(PackagePrivateReturnType.class).name("net.bytebuddy.test.generated." + FOO).method(isDeclaredBy(PackagePrivateReturnType.class)).intercept(StubMethod.INSTANCE).make().load(new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileExtraction.of(PackagePrivateReturnType.class, PackagePrivateReturnType.Argument.class)), ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredMethods().length, is(0));
}
Also used : ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) ByteBuddy(net.bytebuddy.ByteBuddy) AbstractDynamicTypeBuilderTest(net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest) Test(org.junit.Test)

Example 7 with ByteBuddy

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

the class FieldAccessorOtherTest method testExplicitNameSetter.

@Test
public void testExplicitNameSetter() throws Exception {
    DynamicType.Loaded<SampleSetter> loaded = new ByteBuddy().subclass(SampleSetter.class).method(isDeclaredBy(SampleSetter.class)).intercept(FieldAccessor.ofField(FOO)).make().load(SampleSetter.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    SampleSetter sampleSetter = loaded.getLoaded().getDeclaredConstructor().newInstance();
    Field field = SampleSetter.class.getDeclaredField(FOO);
    field.setAccessible(true);
    assertThat(field.get(sampleSetter), is((Object) QUX));
    sampleSetter.bar(BAZ);
    assertThat(field.get(sampleSetter), is((Object) BAZ));
    sampleSetter.assertZeroCalls();
}
Also used : Field(java.lang.reflect.Field) DynamicType(net.bytebuddy.dynamic.DynamicType) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 8 with ByteBuddy

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

the class FixedValueConstantPoolTypesTest method testStaticField.

@Test
public void testStaticField() throws Exception {
    DynamicType.Loaded<T> loaded = new ByteBuddy().subclass(helperClass).method(isDeclaredBy(helperClass)).intercept(FixedValue.reference(fixedValue)).make().load(helperClass.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
    assertThat(loaded.getLoaded().getDeclaredMethods().length, is(2));
    assertThat(loaded.getLoaded().getDeclaredFields().length, is(fixedValue == null ? 0 : 1));
    T instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(StringTarget.class)));
    assertThat(instance, instanceOf(helperClass));
    assertThat(loaded.getLoaded().getDeclaredMethod(FOO).invoke(instance), is(fixedValue));
    assertThat(loaded.getLoaded().getDeclaredMethod(BAR).invoke(instance), is(fixedValue));
    instance.assertZeroCalls();
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 9 with ByteBuddy

use of net.bytebuddy.ByteBuddy in project redisson by redisson.

the class RedissonLiveObjectService method registerClassInternal.

private <T> void registerClassInternal(Class<T> entityClass) {
    DynamicType.Builder<T> builder = new ByteBuddy().subclass(entityClass);
    for (FieldDescription.InDefinedShape field : Introspectior.getTypeDescription(LiveObjectTemplate.class).getDeclaredFields()) {
        builder = builder.define(field);
    }
    Class<? extends T> proxied = builder.method(ElementMatchers.isDeclaredBy(Introspectior.getTypeDescription(RLiveObject.class)).and(ElementMatchers.isGetter().or(ElementMatchers.isSetter()).or(ElementMatchers.named("isPhantom")).or(ElementMatchers.named("delete")))).intercept(MethodDelegation.withDefaultConfiguration().withBinders(FieldProxy.Binder.install(LiveObjectInterceptor.Getter.class, LiveObjectInterceptor.Setter.class)).to(new LiveObjectInterceptor(redisson, codecProvider, entityClass, getRIdFieldName(entityClass)))).implement(RLiveObject.class).method(ElementMatchers.isAnnotatedWith(RFieldAccessor.class).and(ElementMatchers.named("get").or(ElementMatchers.named("set")))).intercept(MethodDelegation.to(FieldAccessorInterceptor.class)).method(ElementMatchers.isDeclaredBy(RObject.class).or(ElementMatchers.isDeclaredBy(RObjectAsync.class))).intercept(MethodDelegation.to(RObjectInterceptor.class)).implement(RObject.class).method(ElementMatchers.isDeclaredBy(RExpirable.class).or(ElementMatchers.isDeclaredBy(RExpirableAsync.class))).intercept(MethodDelegation.to(RExpirableInterceptor.class)).implement(RExpirable.class).method(ElementMatchers.isDeclaredBy(Map.class).or(ElementMatchers.isDeclaredBy(ConcurrentMap.class)).or(ElementMatchers.isDeclaredBy(RMapAsync.class)).or(ElementMatchers.isDeclaredBy(RMap.class))).intercept(MethodDelegation.to(RMapInterceptor.class)).implement(RMap.class).method(ElementMatchers.not(ElementMatchers.isDeclaredBy(Object.class)).and(ElementMatchers.not(ElementMatchers.isDeclaredBy(RLiveObject.class))).and(ElementMatchers.not(ElementMatchers.isDeclaredBy(RExpirable.class))).and(ElementMatchers.not(ElementMatchers.isDeclaredBy(RExpirableAsync.class))).and(ElementMatchers.not(ElementMatchers.isDeclaredBy(RObject.class))).and(ElementMatchers.not(ElementMatchers.isDeclaredBy(RObjectAsync.class))).and(ElementMatchers.not(ElementMatchers.isDeclaredBy(ConcurrentMap.class))).and(ElementMatchers.not(ElementMatchers.isDeclaredBy(Map.class))).and(ElementMatchers.isGetter().or(ElementMatchers.isSetter())).and(ElementMatchers.isPublic().or(ElementMatchers.isProtected()))).intercept(MethodDelegation.to(new AccessorInterceptor(redisson, objectBuilder))).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    classCache.putIfAbsent(entityClass, proxied);
}
Also used : RObjectAsync(org.redisson.api.RObjectAsync) FieldAccessorInterceptor(org.redisson.liveobject.core.FieldAccessorInterceptor) DynamicType(net.bytebuddy.dynamic.DynamicType) ConcurrentMap(java.util.concurrent.ConcurrentMap) ByteBuddy(net.bytebuddy.ByteBuddy) LiveObjectTemplate(org.redisson.liveobject.LiveObjectTemplate) RMap(org.redisson.api.RMap) FieldDescription(net.bytebuddy.description.field.FieldDescription) RObjectInterceptor(org.redisson.liveobject.core.RObjectInterceptor) RMapInterceptor(org.redisson.liveobject.core.RMapInterceptor) RExpirableAsync(org.redisson.api.RExpirableAsync) RObject(org.redisson.api.RObject) RLiveObject(org.redisson.api.RLiveObject) RFieldAccessor(org.redisson.api.annotation.RFieldAccessor) RObject(org.redisson.api.RObject) RLiveObject(org.redisson.api.RLiveObject) LiveObjectInterceptor(org.redisson.liveobject.core.LiveObjectInterceptor) Map(java.util.Map) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) RMap(org.redisson.api.RMap) AccessorInterceptor(org.redisson.liveobject.core.AccessorInterceptor) FieldAccessorInterceptor(org.redisson.liveobject.core.FieldAccessorInterceptor)

Example 10 with ByteBuddy

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

the class AdviceBoxedParameterAssignmentTest method testAssignment.

@Test
public void testAssignment() throws Exception {
    Class<?> dynamicType = new ByteBuddy().redefine(type).visit(Advice.to(type).on(named(FOO))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(dynamicType.getDeclaredMethod(FOO, parameterTypes).invoke(dynamicType.getDeclaredConstructor().newInstance(), provided), is(expected));
}
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