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));
}
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();
}
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();
}
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);
}
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));
}
Aggregations