private static Object printAllFields(Object o) {
if (o instanceof FieldDescriptorProto) {
FieldDescriptorProto p = (FieldDescriptorProto) o;
System.out.printf("%s %s %s\n", p.getName(), p.getType(), p.getTypeName()); // Edw_Create_Datetime TYPE_MESSAGE .NBkgsMeasure.Timestamp
switch (p.getType().toString()) {
case "TYPE_STRING":
return "test";
case "TYPE_INT32":
return 10;
case "TYPE_INT64":
return -100;
case "TYPE_FLOAT":
return 3.14156;
case "TYPE_MESSAGE":
return printAllFieldsFdp(p);
default:
throw new RuntimeException("not implemented");
}
}
if (o instanceof FieldDescriptor) {
FieldDescriptor fd = (FieldDescriptor)o;
System.out.printf("%s %s\n", fd.getName(), fd.getType()); // Summary_Quote_Flg TYPE_STRING
switch (fd.getType().toString()) {
case "TYPE_STRING":
return "test";
case "TYPE_INT32":
return 10;
case "TYPE_INT64":
return -100;
case "TYPE_FLOAT":
return 3.14156;
default:
throw new RuntimeException("not implemented");
}
}
if (o instanceof DescriptorProto) {
return printAllFieldsDp((DescriptorProto)o);
}
if (o instanceof List) {
List l = (List)o;
int n = l.size();
List t = new ArrayList();
for (int i = 0; i < n; i++) {
Object e = l.get(i);
t.add(printAllFields(e));
}
return t;
}
return o;
}
public static DynamicMessage printAllFieldsFdp(FieldDescriptorProto dp) {
Builder builder = DynamicMessage.newBuilder(dp);
for(Map.Entry<FieldDescriptor, Object> entry : builder.getAllFields().entrySet()) {
FieldDescriptor d = entry.getKey();
String name = d.getFullName();
System.out.println("printing fields of " + name);
Object o = entry.getValue();
if (o != null) {
Object v = printAllFields(o);
builder.setField(d, v);
}
}
return builder.build();
}