我需要将 POJO 对象转换为其相应的 protobuf 消息。我正在编写一个实用程序,它将使用反射并生成 protobuf 消息。
我有以下 POJO 模型 -
public class Person {
int id;
Map<String,String> properties;
String name;
Address address;
Gender gender;
}
public class Address {
private String line1;
private String line2;
private String line3;
private String City;
private String state;
}
public enum Gender {
MALE,FEMALE
}
相应的原型消息 -
message PersonMsg {
int32 id = 1;
map<string, string> properties = 2;
string name = 3;
AddressMsg address = 4;
Gender gender = 5;
}
message AddressMsg {
string line1 = 1;
string line2 = 2;
string line3 = 3;
string city = 4;
string state = 5;
string country = 6;
string pincode = 7;
}
enum Gender{
Gender_DEFAULT = 0;
Gender_MALE = 1;
Gender_FEMALE = 2;
}
转换器逻辑类似于 - 对于标量/基元类型动态调用 gette/setter 并在 protobuf 生成器中填充数据。如果字段是自定义类型(在本例中为地址字段),则递归调用该方法并填充字段 -
private static GeneratedMessageV3 getMessage(Object o) throws ClassNotFoundException
String protoName = o.getClass().getName()+"Proto";
Class outerClass = Class.forName(protoName);
String msgName = protoName+"$"+ o.getClass().getSimpleName()+"Msg";
Class modelClass = o.getClass();
Class protoMsg = Class.forName(msgName);
try {
Method method = protoMsg.getMethod("newBuilder", null);
com.google.protobuf.GeneratedMessageV3.Builder builder = (com.google.protobuf.GeneratedMessageV3.Builder)method.invoke(null, null);
Field[] fields = o.getClass().getDeclaredFields();
for(Field field : fields){
if(field.getType().isPrimitive() || field.getType().isAssignableFrom(String.class)){
Method getMethod = modelClass.getMethod("get"+ CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, field.getName()));
Object value = getMethod.invoke(o, null);
Method setMethod = findSetterMethod(builder.getClass(), field);
setMethod.invoke(builder, value);
} else if(field.getType().isEnum()){
Method getMethod = modelClass.getMethod("get"+ CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, field.getName()));
Enum modelEnumValue = (Enum) getMethod.invoke(o, null);
String protoEnumClassName = field.getType().getName()+"Proto$"+field.getType().getSimpleName();
Class protoEnumClass = Class.forName(protoEnumClassName);
Method enumMethod = protoEnumClass.getDeclaredMethod("values", null);
Enum[] protoValues = (Enum[]) enumMethod.invoke(null, null);
Enum protoValue = null;
for(Enum value : protoValues){
if(value.name().replace(field.getType().getSimpleName()+"_", "").equals(modelEnumValue.name())){
protoValue = value;
}
}
if(protoValue!=null) {
Method setMethod = findSetterMethod(builder.getClass(), field);
setMethod.invoke(builder, protoValue);
}
} else if(Collection.class.isAssignableFrom(field.getType()) || Map.class.isAssignableFrom(field.getType())){
if(Map.class.isAssignableFrom(field.getType())){
populateMap(o, field, builder);
}
} else{
System.out.println("Field is custom type :"+ field.getName());
Method getMethod = modelClass.getMethod("get"+ CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, field.getName()));
System.out.println("Found Getter Method :"+ getMethod);
Object modelValue = getMethod.invoke(o, null);
System.out.println("Model Value :"+ modelValue);
Method setMethod = findSetterMethod(builder.getClass(), field);
setMethod.invoke(builder, getMessage(modelValue));
}
}
GeneratedMessageV3 msg = (GeneratedMessageV3) builder.build();
return msg;
} catch (NoSuchMethodException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}```
此代码工作正常,并将 pojo 对象转换为 protobuf 消息。但有时它会在运行时间歇性地失败 - “java.lang.IllegalArgumentException: 参数类型不匹配”
这在行失败 - “setMethod.invoke(builder, getMessage(modelValue));”
知道如果在运行之间根本没有更改代码,为什么这会失败吗?如何摆脱/解决此异常?
方法“findSetterMethod”中的逻辑是按名称搜索setter方法。因此,如果名称匹配,则返回找到的第一个方法。Protobuf 生成相同名称的方法,但接受不同的参数 - Builder,Protobuf 消息自己。因此,当选择了类型错误的方法时,它曾经失败。修复了代码以检查正确的参数类型及其现在工作正常。