test_class.declarator: codec = new ThriftCodec()
test_class.modifier: private
test_class.original_string: private ThriftCodec codec = new ThriftCodec();
test_class.type: ThriftCodec
test_class.var_name: codec
test_class.declarator: channel = new MockedChannel(URL.valueOf("thrift://127.0.0.1"))
test_class.modifier: private
test_class.original_string: private Channel channel = new MockedChannel(URL.valueOf("thrift://127.0.0.1"));
test_class.type: Channel
test_class.var_name: channel
test_class.file: dubbo-rpc/dubbo-rpc-thrift/src/test/java/com/alibaba/dubbo/rpc/protocol/thrift/ThriftCodecTest.java
test_class.identifier: ThriftCodecTest
test_case.body: @Test
      public void testEncodeRequest() throws Exception {
  
          Request request = createRequest();
  
          ChannelBuffer output = ChannelBuffers.dynamicBuffer(1024);
  
          codec.encode(channel, output, request);
  
          byte[] bytes = new byte[output.readableBytes()];
          output.readBytes(bytes);
  
          ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
  
          TTransport transport = new TIOStreamTransport(bis);
  
          TBinaryProtocol protocol = new TBinaryProtocol(transport);
  
          // frame
          byte[] length = new byte[4];
          transport.read(length, 0, 4);
  
          if (bis.markSupported()) {
              bis.mark(0);
          }
  
          // magic
          Assert.assertEquals(ThriftCodec.MAGIC, protocol.readI16());
  
          // message length
          int messageLength = protocol.readI32();
          Assert.assertEquals(messageLength + 4, bytes.length);
  
          // header length
          short headerLength = protocol.readI16();
          // version
          Assert.assertEquals(ThriftCodec.VERSION, protocol.readByte());
          // service name
          Assert.assertEquals(Demo.Iface.class.getName(), protocol.readString());
          // dubbo request id
          Assert.assertEquals(request.getId(), protocol.readI64());
  
          // test message header length
          if (bis.markSupported()) {
              bis.reset();
              bis.skip(headerLength);
          }
  
          TMessage message = protocol.readMessageBegin();
  
          Demo.echoString_args args = new Demo.echoString_args();
  
          args.read(protocol);
  
          protocol.readMessageEnd();
  
          Assert.assertEquals("echoString", message.name);
  
          Assert.assertEquals(TMessageType.CALL, message.type);
  
          Assert.assertEquals("Hello, World!", args.getArg());
  
      }
test_case.class_method_signature: ThriftCodecTest.testEncodeRequest()
test_case.full_signature: @Test public void testEncodeRequest()
test_case.identifier: testEncodeRequest
test_case.invocations: createRequest dynamicBuffer encode readableBytes readBytes read markSupported mark assertEquals readI16 readI32 assertEquals readI16 assertEquals readByte assertEquals getName readString assertEquals getId readI64 markSupported reset skip readMessageBegin read readMessageEnd assertEquals assertEquals assertEquals getArg
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void testEncodeRequest()
focal_class.declarator: MESSAGE_LENGTH_INDEX = 2
focal_class.modifier: public static final
focal_class.original_string: public static final int MESSAGE_LENGTH_INDEX = 2;
focal_class.type: int
focal_class.var_name: MESSAGE_LENGTH_INDEX
focal_class.declarator: MESSAGE_HEADER_LENGTH_INDEX = 6
focal_class.modifier: public static final
focal_class.original_string: public static final int MESSAGE_HEADER_LENGTH_INDEX = 6;
focal_class.type: int
focal_class.var_name: MESSAGE_HEADER_LENGTH_INDEX
focal_class.declarator: MESSAGE_SHORTEST_LENGTH = 10
focal_class.modifier: public static final
focal_class.original_string: public static final int MESSAGE_SHORTEST_LENGTH = 10;
focal_class.type: int
focal_class.var_name: MESSAGE_SHORTEST_LENGTH
focal_class.declarator: NAME = "thrift"
focal_class.modifier: public static final
focal_class.original_string: public static final String NAME = "thrift";
focal_class.type: String
focal_class.var_name: NAME
focal_class.declarator: PARAMETER_CLASS_NAME_GENERATOR = "class.name.generator"
focal_class.modifier: public static final
focal_class.original_string: public static final String PARAMETER_CLASS_NAME_GENERATOR = "class.name.generator";
focal_class.type: String
focal_class.var_name: PARAMETER_CLASS_NAME_GENERATOR
focal_class.declarator: VERSION = (byte) 1
focal_class.modifier: public static final
focal_class.original_string: public static final byte VERSION = (byte) 1;
focal_class.type: byte
focal_class.var_name: VERSION
focal_class.declarator: MAGIC = (short) 0xdabc
focal_class.modifier: public static final
focal_class.original_string: public static final short MAGIC = (short) 0xdabc;
focal_class.type: short
focal_class.var_name: MAGIC
focal_class.declarator: cachedRequest =
              new ConcurrentHashMap<Long, RequestData>()
focal_class.modifier: static final
focal_class.original_string: static final ConcurrentMap<Long, RequestData> cachedRequest =
              new ConcurrentHashMap<Long, RequestData>();
focal_class.type: ConcurrentMap<Long, RequestData>
focal_class.var_name: cachedRequest
focal_class.declarator: THRIFT_SEQ_ID = new AtomicInteger(0)
focal_class.modifier: private static final
focal_class.original_string: private static final AtomicInteger THRIFT_SEQ_ID = new AtomicInteger(0);
focal_class.type: AtomicInteger
focal_class.var_name: THRIFT_SEQ_ID
focal_class.declarator: cachedClass =
              new ConcurrentHashMap<String, Class<?>>()
focal_class.modifier: private static final
focal_class.original_string: private static final ConcurrentMap<String, Class<?>> cachedClass =
              new ConcurrentHashMap<String, Class<?>>();
focal_class.type: ConcurrentMap<String, Class<?>>
focal_class.var_name: cachedClass
focal_class.file: dubbo-rpc/dubbo-rpc-thrift/src/main/java/com/alibaba/dubbo/rpc/protocol/thrift/ThriftCodec.java
focal_class.identifier: ThriftCodec
focal_class.interfaces: implements Codec2
focal_class.class_method_signature: ThriftCodec.nextSeqId()
focal_class.full_signature: private static int nextSeqId()
focal_class.identifier: nextSeqId
focal_class.modifiers: private static
focal_class.parameters: ()
focal_class.return: int
focal_class.signature: int nextSeqId()
focal_class.class_method_signature: ThriftCodec.getSeqId()
focal_class.full_signature: static int getSeqId()
focal_class.identifier: getSeqId
focal_class.modifiers: static
focal_class.parameters: ()
focal_class.return: int
focal_class.signature: int getSeqId()
focal_class.class_method_signature: ThriftCodec.encode(Channel channel, ChannelBuffer buffer, Object message)
focal_class.full_signature: @Override public void encode(Channel channel, ChannelBuffer buffer, Object message)
focal_class.identifier: encode
focal_class.modifiers: @Override public
focal_class.parameters: (Channel channel, ChannelBuffer buffer, Object message)
focal_class.return: void
focal_class.signature: void encode(Channel channel, ChannelBuffer buffer, Object message)
focal_class.class_method_signature: ThriftCodec.decode(Channel channel, ChannelBuffer buffer)
focal_class.full_signature: @Override public Object decode(Channel channel, ChannelBuffer buffer)
focal_class.identifier: decode
focal_class.modifiers: @Override public
focal_class.parameters: (Channel channel, ChannelBuffer buffer)
focal_class.return: Object
focal_class.signature: Object decode(Channel channel, ChannelBuffer buffer)
focal_class.class_method_signature: ThriftCodec.decode(TProtocol protocol)
focal_class.full_signature: private Object decode(TProtocol protocol)
focal_class.identifier: decode
focal_class.modifiers: private
focal_class.parameters: (TProtocol protocol)
focal_class.return: Object
focal_class.signature: Object decode(TProtocol protocol)
focal_class.class_method_signature: ThriftCodec.encodeRequest(Channel channel, ChannelBuffer buffer, Request request)
focal_class.full_signature: private void encodeRequest(Channel channel, ChannelBuffer buffer, Request request)
focal_class.identifier: encodeRequest
focal_class.modifiers: private
focal_class.parameters: (Channel channel, ChannelBuffer buffer, Request request)
focal_class.return: void
focal_class.signature: void encodeRequest(Channel channel, ChannelBuffer buffer, Request request)
focal_class.class_method_signature: ThriftCodec.encodeResponse(Channel channel, ChannelBuffer buffer, Response response)
focal_class.full_signature: private void encodeResponse(Channel channel, ChannelBuffer buffer, Response response)
focal_class.identifier: encodeResponse
focal_class.modifiers: private
focal_class.parameters: (Channel channel, ChannelBuffer buffer, Response response)
focal_class.return: void
focal_class.signature: void encodeResponse(Channel channel, ChannelBuffer buffer, Response response)
focal_method.body: private void encodeRequest(Channel channel, ChannelBuffer buffer, Request request)
              throws IOException {
  
          RpcInvocation inv = (RpcInvocation) request.getData();
  
          int seqId = nextSeqId();
  
          String serviceName = inv.getAttachment(Constants.INTERFACE_KEY);
  
          if (StringUtils.isEmpty(serviceName)) {
              throw new IllegalArgumentException("Could not find service name in attachment with key " 
                      + Constants.INTERFACE_KEY);
          }
  
          TMessage message = new TMessage(
                  inv.getMethodName(),
                  TMessageType.CALL,
                  seqId);
  
          String methodArgs = ExtensionLoader.getExtensionLoader(ClassNameGenerator.class)
                  .getExtension(channel.getUrl().getParameter(ThriftConstants.CLASS_NAME_GENERATOR_KEY, ThriftClassNameGenerator.NAME))
                  .generateArgsClassName(serviceName, inv.getMethodName());
  
          if (StringUtils.isEmpty(methodArgs)) {
              throw new RpcException(RpcException.SERIALIZATION_EXCEPTION,
                      "Could not encode request, the specified interface may be incorrect.");
          }
  
          Class<?> clazz = cachedClass.get(methodArgs);
  
          if (clazz == null) {
  
              try {
  
                  clazz = ClassHelper.forNameWithThreadContextClassLoader(methodArgs);
  
                  cachedClass.putIfAbsent(methodArgs, clazz);
  
              } catch (ClassNotFoundException e) {
                  throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
              }
  
          }
  
          TBase args;
  
          try {
              args = (TBase) clazz.newInstance();
          } catch (InstantiationException e) {
              throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
          } catch (IllegalAccessException e) {
              throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
          }
  
          for (int i = 0; i < inv.getArguments().length; i++) {
  
              Object obj = inv.getArguments()[i];
  
              if (obj == null) {
                  continue;
              }
  
              TFieldIdEnum field = args.fieldForId(i + 1);
  
              String setMethodName = ThriftUtils.generateSetMethodName(field.getFieldName());
  
              Method method;
  
              try {
                  method = clazz.getMethod(setMethodName, inv.getParameterTypes()[i]);
              } catch (NoSuchMethodException e) {
                  throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
              }
  
              try {
                  method.invoke(args, obj);
              } catch (IllegalAccessException e) {
                  throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
              } catch (InvocationTargetException e) {
                  throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
              }
  
          }
  
          RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(1024);
  
          TIOStreamTransport transport = new TIOStreamTransport(bos);
  
          TBinaryProtocol protocol = new TBinaryProtocol(transport);
  
          int headerLength, messageLength;
  
          byte[] bytes = new byte[4];
          try {
              // magic
              protocol.writeI16(MAGIC);
              // message length placeholder
              protocol.writeI32(Integer.MAX_VALUE);
              // message header length placeholder
              protocol.writeI16(Short.MAX_VALUE);
              // version
              protocol.writeByte(VERSION);
              // service name
              protocol.writeString(serviceName);
              // dubbo request id
              protocol.writeI64(request.getId());
              protocol.getTransport().flush();
              // header size
              headerLength = bos.size();
  
              // message body
              protocol.writeMessageBegin(message);
              args.write(protocol);
              protocol.writeMessageEnd();
              protocol.getTransport().flush();
              int oldIndex = messageLength = bos.size();
  
              // fill in message length and header length
              try {
                  TFramedTransport.encodeFrameSize(messageLength, bytes);
                  bos.setWriteIndex(MESSAGE_LENGTH_INDEX);
                  protocol.writeI32(messageLength);
                  bos.setWriteIndex(MESSAGE_HEADER_LENGTH_INDEX);
                  protocol.writeI16((short) (0xffff & headerLength));
              } finally {
                  bos.setWriteIndex(oldIndex);
              }
  
          } catch (TException e) {
              throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, e.getMessage(), e);
          }
  
          buffer.writeBytes(bytes);
          buffer.writeBytes(bos.toByteArray());
  
      }
focal_method.class_method_signature: ThriftCodec.encodeRequest(Channel channel, ChannelBuffer buffer, Request request)
focal_method.full_signature: private void encodeRequest(Channel channel, ChannelBuffer buffer, Request request)
focal_method.identifier: encodeRequest
focal_method.invocations: getData nextSeqId getAttachment isEmpty getMethodName generateArgsClassName getExtension getExtensionLoader getParameter getUrl getMethodName isEmpty get forNameWithThreadContextClassLoader putIfAbsent getMessage newInstance getMessage getMessage getArguments getArguments fieldForId generateSetMethodName getFieldName getMethod getParameterTypes getMessage invoke getMessage getMessage writeI16 writeI32 writeI16 writeByte writeString writeI64 getId flush getTransport size writeMessageBegin write writeMessageEnd flush getTransport size encodeFrameSize setWriteIndex writeI32 setWriteIndex writeI16 setWriteIndex getMessage writeBytes writeBytes toByteArray
focal_method.modifiers: private
focal_method.parameters: (Channel channel, ChannelBuffer buffer, Request request)
focal_method.return: void
focal_method.signature: void encodeRequest(Channel channel, ChannelBuffer buffer, Request request)