public static ServerServiceDefinition useMarshalledMessages()

in shenyu-client/shenyu-client-grpc/src/main/java/org/apache/shenyu/client/grpc/json/JsonServerServiceInterceptor.java [73:144]


    public static <T> ServerServiceDefinition useMarshalledMessages(final ServerServiceDefinition serviceDef,
                                                                    final MethodDescriptor.Marshaller<T> marshaller)
            throws IllegalArgumentException, IllegalAccessException {
        List<ServerMethodDefinition<?, ?>> wrappedMethods = new ArrayList<>();
        List<MethodDescriptor<?, ?>> wrappedDescriptors = new ArrayList<>();

        // Wrap the descriptors
        for (final ServerMethodDefinition<?, ?> definition : serviceDef.getMethods()) {
            MethodDescriptor.Marshaller<?> requestMarshaller = definition.getMethodDescriptor().getRequestMarshaller();
            Field defaultInstanceField = ReflectUtils.getField(requestMarshaller.getClass(), "defaultInstance");
            Class<?> grpcRequestParamClass = null;
            if (Objects.isNull(defaultInstanceField)) {
                // Compatible with lower versions. eg: grpc 1.6.0
                if (requestMarshaller instanceof MethodDescriptor.PrototypeMarshaller) {
                    MethodDescriptor.PrototypeMarshaller<?> prototypeMarshaller = (PrototypeMarshaller<?>) requestMarshaller;
                    if (Objects.isNull(prototypeMarshaller.getMessagePrototype())) {
                        throw new ShenyuException(String.format("can not get defaultInstance Field of %s", requestMarshaller.getClass()));
                    }
                    grpcRequestParamClass = prototypeMarshaller.getMessagePrototype().getClass();
                }
            } else {
                defaultInstanceField.setAccessible(true);
                grpcRequestParamClass = defaultInstanceField.get(requestMarshaller).getClass();
            }

            String fullMethodName = definition.getMethodDescriptor().getFullMethodName();
            MethodDescriptor.MethodType methodType = definition.getMethodDescriptor().getType();
            METHOD_TYPE_MAP.put(fullMethodName, methodType);

            String[] splitMethodName = fullMethodName.split("/");
            fullMethodName = splitMethodName[0] + GrpcConstants.GRPC_JSON_SERVICE + "/" + splitMethodName[1];
            REQUEST_CLAZZ_MAP.put(fullMethodName, grpcRequestParamClass);

            final MethodDescriptor<?, ?> originalMethodDescriptor = definition.getMethodDescriptor();
            final MethodDescriptor<T, T> wrappedMethodDescriptor = originalMethodDescriptor
                    .toBuilder(marshaller, marshaller).build();
            wrappedDescriptors.add(wrappedMethodDescriptor);
            wrappedMethods.add(wrapMethod(definition, wrappedMethodDescriptor));
        }

        // Build the new service descriptor
        ServiceDescriptor.Builder build = ServiceDescriptor.newBuilder(serviceDef.getServiceDescriptor().getName() + GrpcConstants.GRPC_JSON_SERVICE);
        for (MethodDescriptor<?, ?> md : wrappedDescriptors) {
            Field fullMethodNameField = ReflectUtils.getField(md.getClass(), "fullMethodName");
            if (Objects.isNull(fullMethodNameField)) {
                throw new ShenyuException(String.format("can not get fullMethodName Field of %s", md.getClass()));
            }
            fullMethodNameField.setAccessible(true);
            String fullMethodName = (String) fullMethodNameField.get(md);
            String[] splitMethodName = fullMethodName.split("/");
            fullMethodName = splitMethodName[0] + GrpcConstants.GRPC_JSON_SERVICE + "/" + splitMethodName[1];
            fullMethodNameField.set(md, fullMethodName);

            // Compatible with lower versions. Lower versions do not have this field. eg: grpc 1.6.0
            Field serviceNameField = ReflectUtils.getField(md.getClass(), "serviceName");
            if (Objects.nonNull(serviceNameField)) {
                serviceNameField.setAccessible(true);
                String serviceName = (String) serviceNameField.get(md);
                serviceName = serviceName + GrpcConstants.GRPC_JSON_SERVICE;
                serviceNameField.set(md, serviceName);
            }
            build.addMethod(md);
        }
        final ServerServiceDefinition.Builder serviceBuilder = ServerServiceDefinition
                .builder(build.build());

        // Create the new service definition
        for (ServerMethodDefinition<?, ?> definition : wrappedMethods) {
            serviceBuilder.addMethod(definition);
        }
        return serviceBuilder.build();
    }