/** * {@inheritDoc} */ public DynamicType make(String auxiliaryTypeName, ClassFileVersion classFileVersion, MethodAccessorFactory methodAccessorFactory) { LinkedHashMap<String, TypeDescription> parameterFields = extractFields(sourceMethod); DynamicType.Builder<?> builder = new ByteBuddy(classFileVersion) .with(TypeValidation.DISABLED) .subclass(forwardingType, ConstructorStrategy.Default.NO_CONSTRUCTORS) .name(auxiliaryTypeName) .modifiers(DEFAULT_TYPE_MODIFIER) .implement(serializableProxy ? new Class<?>[]{Serializable.class} : new Class<?>[0]) .method(ElementMatchers.<MethodDescription>isAbstract().and(isDeclaredBy(forwardingType))) .intercept(new MethodCall(sourceMethod, assigner)) .defineConstructor().withParameters(parameterFields.values()) .intercept(ConstructorCall.INSTANCE); for (Map.Entry<String, TypeDescription> field : parameterFields.entrySet()) { builder = builder.defineField(field.getKey(), field.getValue(), Visibility.PRIVATE); } return builder.make(); }
private <T extends ViewContext> T generateViewContextProxy(Class<T> clazz, Map<String, ContextProperty> properties) throws InstantiationException, IllegalAccessException, NoSuchMethodException { final Constructor<ViewContextBase> targetCtor = ViewContextBase.class.getConstructor(Map.class); return clazz.cast(new ByteBuddy() .subclass(ViewContextBase.class, ConstructorStrategy.Default.NO_CONSTRUCTORS) .implement(clazz, Serializable.class) .method(named("getViewContextType")).intercept(value(clazz)) .method(returns(ContextProperty.class)) .intercept(to(new StatePropertyGetterDelegate())) .defineConstructor(Modifier.PUBLIC) .intercept(invoke(targetCtor) .with(properties) .andThen(to(new ContextInitializer()))) .make() .load(getClass().getClassLoader(), WRAPPER) .getLoaded() .newInstance()); }