use of net.bytebuddy.implementation.bind.annotation.This in project alibaba-rsocket-broker by alibaba.
the class RSocketRequesterRpcProxy method invoke.
@Override
@RuntimeType
public Object invoke(@This Object proxy, @Origin Method method, @AllArguments Object[] allArguments) throws Throwable {
// interface default method validation for JDK Proxy only, not necessary for ByteBuddy
if (jdkProxy && method.isDefault()) {
return DefaultMethodHandler.getMethodHandle(method, serviceInterface).bindTo(proxy).invokeWithArguments(allArguments);
} else if (method.getDeclaringClass().equals(Object.class)) {
// delegate hashCode, equals, or toString methods to this
return method.invoke(this);
}
MutableContext mutableContext = new MutableContext();
if (!methodMetadataMap.containsKey(method)) {
methodMetadataMap.put(method, new ReactiveMethodMetadata(group, service, version, method, encodingType, this.acceptEncodingTypes, endpoint, sticky, sourceUri));
}
ReactiveMethodMetadata methodMetadata = methodMetadataMap.get(method);
mutableContext.put(ReactiveMethodMetadata.class, methodMetadata);
Object[] args = allArguments;
if (methodMetadata.isKotlinSuspend()) {
args = Arrays.copyOfRange(args, 0, args.length - 1);
mutableContext.put(Continuation.class, allArguments[allArguments.length - 1]);
}
// ----- return type deal------
if (methodMetadata.getRsocketFrameType() == FrameType.REQUEST_CHANNEL) {
metrics(methodMetadata);
Payload routePayload;
Flux<Object> source;
// 1 param or 2 params
if (args.length == 1) {
routePayload = ByteBufPayload.create(Unpooled.EMPTY_BUFFER, methodMetadata.getCompositeMetadataByteBuf().retainedDuplicate());
source = methodMetadata.getReactiveAdapter().toFlux(args[0]);
} else {
ByteBuf bodyBuffer = encodingFacade.encodingResult(args[0], methodMetadata.getParamEncoding());
routePayload = ByteBufPayload.create(bodyBuffer, methodMetadata.getCompositeMetadataByteBuf().retainedDuplicate());
source = methodMetadata.getReactiveAdapter().toFlux(args[1]);
}
Flux<Payload> payloadFlux = source.startWith(routePayload).map(obj -> {
if (obj instanceof Payload)
return (Payload) obj;
return ByteBufPayload.create(encodingFacade.encodingResult(obj, encodingType), methodMetadata.getCompositeMetadataByteBuf().retainedDuplicate());
});
Flux<Payload> payloads = upstreamManager.getRSocket(this.serviceId).requestChannel(payloadFlux);
Flux<Object> fluxReturn = payloads.concatMap(payload -> {
try {
RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.from(payload.metadata());
return Mono.justOrEmpty(encodingFacade.decodeResult(extractPayloadDataMimeType(compositeMetadata, encodingType), payload.data(), methodMetadata.getInferredClassForReturn()));
} catch (Exception e) {
return Flux.error(e);
}
}).subscriberContext(mutableContext::putAll);
if (methodMetadata.isMonoChannel()) {
return fluxReturn.last();
} else {
return methodMetadata.getReactiveAdapter().fromPublisher(fluxReturn, method.getReturnType());
}
} else {
// body content
ByteBuf bodyBuffer = encodingFacade.encodingParams(args, methodMetadata.getParamEncoding());
Class<?> returnType = method.getReturnType();
if (methodMetadata.getRsocketFrameType() == FrameType.REQUEST_RESPONSE) {
metrics(methodMetadata);
Mono<Payload> payloadMono = remoteRequestResponse(methodMetadata, methodMetadata.getCompositeMetadataByteBuf().retainedDuplicate(), bodyBuffer);
Mono<Object> result = payloadMono.handle((payload, sink) -> {
try {
RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.from(payload.metadata());
Object obj = encodingFacade.decodeResult(extractPayloadDataMimeType(compositeMetadata, encodingType), payload.data(), methodMetadata.getInferredClassForReturn());
if (obj != null) {
sink.next(obj);
}
sink.complete();
} catch (Exception e) {
sink.error(e);
}
});
return methodMetadata.getReactiveAdapter().fromPublisher(result, returnType, mutableContext);
} else if (methodMetadata.getRsocketFrameType() == FrameType.REQUEST_FNF) {
metrics(methodMetadata);
return remoteFireAndForget(methodMetadata, methodMetadata.getCompositeMetadataByteBuf().retainedDuplicate(), bodyBuffer);
} else if (methodMetadata.getRsocketFrameType() == FrameType.REQUEST_STREAM) {
metrics(methodMetadata);
Flux<Payload> flux = remoteRequestStream(methodMetadata, methodMetadata.getCompositeMetadataByteBuf().retainedDuplicate(), bodyBuffer);
Flux<Object> result = flux.concatMap((payload) -> {
try {
RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.from(payload.metadata());
return Mono.justOrEmpty(encodingFacade.decodeResult(extractPayloadDataMimeType(compositeMetadata, encodingType), payload.data(), methodMetadata.getInferredClassForReturn()));
} catch (Exception e) {
return Mono.error(e);
}
});
return methodMetadata.getReactiveAdapter().fromPublisher(result, returnType, mutableContext);
} else {
ReferenceCountUtil.safeRelease(bodyBuffer);
return Mono.error(new Exception(RsocketErrorCode.message("RST-200405", methodMetadata.getRsocketFrameType())));
}
}
}
use of net.bytebuddy.implementation.bind.annotation.This in project mule by mulesoft.
the class ObjectFactoryClassRepository method getObjectFactoryDynamicClass.
@Deprecated
public Class<ObjectFactory> getObjectFactoryDynamicClass(final ComponentBuildingDefinition componentBuildingDefinition, final Class objectFactoryType, final Class createdObjectType, final Supplier<Boolean> isLazyInitFunction) {
/*
* We need this to allow spring create the object using a FactoryBean but using the object factory setters and getters so we
* create as FactoryBean a dynamic class that will have the same attributes and methods as the ObjectFactory that the user
* defined. This way our API does not expose spring specific classes.
*/
Enhancer enhancer = new Enhancer();
// Use SmartFactoryBean since it's the only way to force spring to pre-instantiate FactoryBean for singletons
enhancer.setInterfaces(new Class[] { SmartFactoryBean.class });
enhancer.setSuperclass(objectFactoryType);
enhancer.setCallbackType(MethodInterceptor.class);
if (SmartFactoryBean.class.getClassLoader() != objectFactoryType.getClassLoader()) {
// CGLIB needs access to both the spring interface and the extended factory class.
// If the factory class is defined in a plugin, its classloader has to be passed.
enhancer.setClassLoader(COMPOSITE_CL_CACHE.get(objectFactoryType.getClassLoader()));
}
// The use of the CGLIB cache is turned off when a post creation function is passed as argument in order to
// enrich the created proxy with properties. This is only to enable injecting properties in components
// from the compatibility module.
// Setting this to false will generate an excessive amount of different proxy classes loaded by the container CL
// that will end up in Metaspace OOM.
enhancer.setUseCache(true);
// MG says: this is super important. Generating this variable here prevents the lambda below from
// keeping a reference to the componentBuildingDefinition, which in turn references a classloader and has
// caused leaks in the past
final boolean prototype = componentBuildingDefinition.isPrototype();
Class<ObjectFactory> factoryBeanClass = enhancer.createClass();
registerStaticCallbacks(factoryBeanClass, new Callback[] { (MethodInterceptor) (obj, method, args, proxy) -> {
final boolean eager = !isLazyInitFunction.get();
if (method.getName().equals(IS_SINGLETON)) {
return !prototype;
}
if (method.getName().equals("getObjectType") && !ObjectTypeProvider.class.isAssignableFrom(obj.getClass())) {
return createdObjectType;
}
if (method.getName().equals("getObject")) {
return proxy.invokeSuper(obj, args);
}
if (method.getName().equals(IS_PROTOTYPE)) {
return prototype;
}
if (method.getName().equals(IS_EAGER_INIT)) {
return eager;
}
return proxy.invokeSuper(obj, args);
} });
return factoryBeanClass;
}
use of net.bytebuddy.implementation.bind.annotation.This in project kin-rsocket-broker by huangjianqin.
the class RequesterProxy method invoke.
@Override
@RuntimeType
public Object invoke(@This Object proxy, @Origin Method method, @AllArguments Object[] args) {
if (!RSocketAppContext.ENHANCE && method.isDefault()) {
// jdk代理下, 如果是调用default方法, 直接使用句柄掉漆
try {
return MethodHandleUtils.getInterfaceDefaultMethodHandle(method, serviceInterface).bindTo(proxy).invokeWithArguments(args);
} catch (Throwable throwable) {
ExceptionUtils.throwExt(throwable);
}
}
if (method.getDeclaringClass().equals(Object.class)) {
// 过滤Object方法
try {
return method.invoke(this, args);
} catch (IllegalAccessException | InvocationTargetException e) {
ExceptionUtils.throwExt(e);
}
}
ReactiveMethodMetadata methodMetadata = methodMetadataMap.get(method);
if (Objects.isNull(methodMetadata)) {
// lazy init method metadata
methodMetadata = new ReactiveMethodMetadata(group, service, version, method, defaultEncodingType, defaultAcceptEncodingTypes, endpoint, sticky, sourceUri);
methodMetadataMap.put(method, methodMetadata);
}
MutableContext mutableContext = new MutableContext();
mutableContext.put(ReactiveMethodMetadata.class, methodMetadata);
if (methodMetadata.getFrameType() == FrameType.REQUEST_CHANNEL) {
// request channel
metrics(methodMetadata);
ByteBuf routeBytes;
Flux<Object> paramBodys;
if (args.length == 1) {
// 1 param
routeBytes = Unpooled.EMPTY_BUFFER;
paramBodys = ReactiveObjAdapter.INSTANCE.toFlux(args[0]);
} else {
// 2 params
routeBytes = Codecs.INSTANCE.encodeResult(args[0], methodMetadata.getDataEncodingType());
paramBodys = ReactiveObjAdapter.INSTANCE.toFlux(args[1]);
}
// handle return
ReactiveMethodMetadata finalMethodMetadata1 = methodMetadata;
Flux<Object> result = requestChannel(methodMetadata, methodMetadata.getCompositeMetadataBytes(), routeBytes, paramBodys).concatMap(payload -> {
try {
RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.of(payload.metadata());
return Mono.justOrEmpty(Codecs.INSTANCE.decodeResult(extractPayloadDataMimeType(compositeMetadata, finalMethodMetadata1.getAcceptEncodingTypes()[0]), payload.data(), finalMethodMetadata1.getInferredClassForReturn()));
} catch (Exception e) {
return Flux.error(e);
}
}).contextWrite(c -> mutableContext.putAll(c.readOnly()));
if (methodMetadata.isMonoChannel()) {
return result.last();
} else {
return result;
}
} else {
// body content
ByteBuf paramBodyBytes = Codecs.INSTANCE.encodeParams(args, methodMetadata.getDataEncodingType());
if (methodMetadata.getFrameType() == FrameType.REQUEST_RESPONSE) {
// request response
metrics(methodMetadata);
ReactiveMethodMetadata finalMethodMetadata = methodMetadata;
// handle return
Mono<Object> result = requestResponse(methodMetadata, methodMetadata.getCompositeMetadataBytes(), paramBodyBytes).handle((payload, sink) -> {
try {
RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.of(payload.metadata());
Object obj = Codecs.INSTANCE.decodeResult(extractPayloadDataMimeType(compositeMetadata, finalMethodMetadata.getAcceptEncodingTypes()[0]), payload.data(), finalMethodMetadata.getInferredClassForReturn());
if (obj != null) {
sink.next(obj);
}
sink.complete();
} catch (Exception e) {
sink.error(e);
}
});
return ReactiveObjAdapter.INSTANCE.fromPublisher(result, mutableContext);
} else if (methodMetadata.getFrameType() == FrameType.REQUEST_FNF) {
// request and forget
metrics(methodMetadata);
Mono<Void> result = fireAndForget(methodMetadata, methodMetadata.getCompositeMetadataBytes(), paramBodyBytes);
if (methodMetadata.isReturnVoid()) {
// 返回void
result.subscribe();
return null;
} else {
return result;
}
} else if (methodMetadata.getFrameType() == FrameType.REQUEST_STREAM) {
// request stream
metrics(methodMetadata);
ReactiveMethodMetadata finalMethodMetadata = methodMetadata;
Flux<Object> result = requestStream(methodMetadata, methodMetadata.getCompositeMetadataBytes(), paramBodyBytes).concatMap((payload) -> {
try {
RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.of(payload.metadata());
return Mono.justOrEmpty(Codecs.INSTANCE.decodeResult(extractPayloadDataMimeType(compositeMetadata, finalMethodMetadata.getAcceptEncodingTypes()[0]), payload.data(), finalMethodMetadata.getInferredClassForReturn()));
} catch (Exception e) {
return Mono.error(e);
}
});
return ReactiveObjAdapter.INSTANCE.fromPublisher(result, mutableContext);
} else {
ReferenceCountUtil.safeRelease(paramBodyBytes);
return Mono.error(new Exception("unknown RSocket Frame type: " + methodMetadata.getFrameType().name()));
}
}
}
Aggregations