PageRenderTime 165ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/filter/CompatibleFilter.java

https://gitlab.com/sxyseo/dubbox
Java | 75 lines | 48 code | 7 blank | 20 comment | 11 complexity | 6774c0db3282877c4fc2a21990e873fd MD5 | raw file
  1. /*
  2. * Copyright 1999-2011 Alibaba Group.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.alibaba.dubbo.rpc.filter;
  17. import java.lang.reflect.Method;
  18. import java.lang.reflect.Type;
  19. import com.alibaba.dubbo.common.Constants;
  20. import com.alibaba.dubbo.common.logger.Logger;
  21. import com.alibaba.dubbo.common.logger.LoggerFactory;
  22. import com.alibaba.dubbo.common.utils.CompatibleTypeUtils;
  23. import com.alibaba.dubbo.common.utils.PojoUtils;
  24. import com.alibaba.dubbo.rpc.Filter;
  25. import com.alibaba.dubbo.rpc.Invocation;
  26. import com.alibaba.dubbo.rpc.Invoker;
  27. import com.alibaba.dubbo.rpc.Result;
  28. import com.alibaba.dubbo.rpc.RpcException;
  29. import com.alibaba.dubbo.rpc.RpcResult;
  30. /**
  31. * CompatibleFilter
  32. *
  33. * @author william.liangf
  34. */
  35. public class CompatibleFilter implements Filter {
  36. private static Logger logger = LoggerFactory.getLogger(CompatibleFilter.class);
  37. public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
  38. Result result = invoker.invoke(invocation);
  39. if (! invocation.getMethodName().startsWith("$") && ! result.hasException()) {
  40. Object value = result.getValue();
  41. if (value != null) {
  42. try {
  43. Method method = invoker.getInterface().getMethod(invocation.getMethodName(), invocation.getParameterTypes());
  44. Class<?> type = method.getReturnType();
  45. Object newValue;
  46. String serialization = invoker.getUrl().getParameter(Constants.SERIALIZATION_KEY);
  47. if ("json".equals(serialization)
  48. || "fastjson".equals(serialization)){
  49. Type gtype = method.getGenericReturnType();
  50. newValue = PojoUtils.realize(value, type, gtype);
  51. } else if (! type.isInstance(value)) {
  52. newValue = PojoUtils.isPojo(type)
  53. ? PojoUtils.realize(value, type)
  54. : CompatibleTypeUtils.compatibleTypeConvert(value, type);
  55. } else {
  56. newValue = value;
  57. }
  58. if (newValue != value) {
  59. result = new RpcResult(newValue);
  60. }
  61. } catch (Throwable t) {
  62. logger.warn(t.getMessage(), t);
  63. }
  64. }
  65. }
  66. return result;
  67. }
  68. }