Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
panxiaojun233 committed Dec 29, 2020
1 parent 3f6c810 commit b67e42b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Marshaller {
public static Marshaller marshaller = new Marshaller();

public Object unmarshaller(Class<?> requestClass, ByteBuf in) {

final Parser<?> parser = ProtoUtil.getParser(requestClass);
Object result = null;
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.apache.dubbo.rpc.protocol.tri;

import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ConcurrentHashMap;

import com.google.protobuf.Message;
import com.google.protobuf.Parser;
Expand All @@ -11,6 +12,8 @@
* guohaoice@gmail.com
*/
public class ProtoUtil {

private static ConcurrentHashMap<Class<?>, Message> instCache = new ConcurrentHashMap<>();
/**
* parse proto from netty {@link ByteBuf}
*
Expand All @@ -26,12 +29,16 @@ public class ProtoUtil {
//}

public static Message defaultInst(Class<?> clz) {
Message defaultInst;
Message defaultInst = instCache.get(clz);
if (defaultInst != null) {
return defaultInst;
}
try {
defaultInst = (Message) clz.getMethod("getDefaultInstance").invoke(null);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException("Create default protobuf instance failed ", e);
}
instCache.put(clz, defaultInst);
return defaultInst;
}

Expand Down

0 comments on commit b67e42b

Please sign in to comment.