CodecUtils { public static void flatCopyTo(String prefix, Map<String, Object> sourceMap, Map<String, String> dstMap) { for (Map.Entry<String, Object> entry : sourceMap.entrySet()) { String key = prefix + entry.getKey(); Object value = entry.getValue(); if (value instanceof String) { dstMap.put(key, (String) value); } else if (value instanceof Number) { dstMap.put(key, value.toString()); } else if (value instanceof Map) { flatCopyTo(key + ".", (Map<String, Object>) value, dstMap); } } }  static byte[] intToBytes(int num); static int bytesToInt(byte[] ary); static byte[] short2bytes(short num); static byte[] copyOf(byte[] src, int length); static byte[] parseHigh4Low4Bytes(byte b); static byte buildHigh4Low4Bytes(byte high4, byte low4); static byte[] parseHigh2Low6Bytes(byte b); static byte buildHigh2Low6Bytes(byte high2, byte low6); static String byteToBits(byte b); static byte bitsToByte(String bits); static boolean startsWith(byte[] bs, byte[] head); static boolean[] byte2Booleans(byte b); static byte booleansToByte(boolean[] array); static String byte2hex(byte[] bytes); static byte[] hex2byte(String str); static boolean getBooleanFromByte(byte modifiers, int i); static byte setBooleanToByte(byte modifiers, int i, boolean bool); static void flatCopyTo(String prefix, Map<String, Object> sourceMap,
                                  Map<String, String> dstMap); static void treeCopyTo(String prefix, Map<String, String> sourceMap,
                                  Map<String, String> dstMap, boolean remove); static final Object[] EMPTY_OBJECT_ARRAY; static final Class<?>[] EMPTY_CLASS_ARRAY; }
@Test public void flatCopyTo() { Map<String, Object> requestProps = new HashMap<String, Object>(); requestProps.put("xx", "xxxxxxx"); requestProps.put("yyy", new String[] { "yyyy" }); requestProps.put("zzzz", 333); Map<String, String> header = new HashMap<String, String>(); Map<String, String> context = new HashMap<String, String>(); context.put("sofaCallerApp", "test"); context.put("sofaCallerIp", "10.15.233.63"); context.put("sofaPenAttrs", ""); context.put("sofaRpcId", "0"); context.put("sofaTraceId", "0a0fe93f1488349732342100153695"); context.put("sysPenAttrs", ""); context.put("penAttrs", "Hello=world&"); String rpcTraceContext = "rpc_trace_context"; requestProps.put(rpcTraceContext, context); Map<String, String> requestBaggage = new HashMap<String, String>(); requestBaggage.put("aaa", "reqasdhjaksdhaksdyiasdhasdhaskdhaskd"); requestBaggage.put("bbb", "req10.15.233.63"); requestBaggage.put("ccc", "reqwhat 's wrong"); String rpcReqBaggage = "rpc_req_baggage"; requestProps.put(rpcReqBaggage, requestBaggage); Map<String, String> responseBaggage = new HashMap<String, String>(); responseBaggage.put("xxx", "respasdhjaksdhaksdyiasdhasdhaskdhaskd"); responseBaggage.put("yyy", "resp10.15.233.63"); responseBaggage.put("zzz", "resphehehe"); String rpcRespBaggage = "rpc_resp_baggage"; requestProps.put(rpcRespBaggage, responseBaggage); CodecUtils.flatCopyTo("", requestProps, header); Assert.assertTrue(header.size() == 15); for (Map.Entry<String, String> entry : header.entrySet()) { LOGGER.info(entry.getKey() + " : " + entry.getValue()); } LOGGER.info(""); Map<String, Object> newRequestProps = new HashMap<String, Object>(); Map<String, String> newContext = new HashMap<String, String>(); CodecUtils.treeCopyTo(rpcTraceContext + ".", header, newContext, true); newRequestProps.put(rpcTraceContext, newContext); newContext = new HashMap<String, String>(); CodecUtils.treeCopyTo(rpcReqBaggage + ".", header, newContext, true); newRequestProps.put(rpcReqBaggage, newContext); newContext = new HashMap<String, String>(); CodecUtils.treeCopyTo(rpcRespBaggage + ".", header, newContext, true); newRequestProps.put(rpcRespBaggage, newContext); for (Map.Entry<String, Object> entry : newRequestProps.entrySet()) { LOGGER.info(entry.getKey() + " : " + entry.getValue()); } newRequestProps.putAll(header); Assert.assertTrue(newRequestProps.size() == 5); }