| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 853 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 864 } | 864 } |
| 865 | 865 |
| 866 /** See {@link FileDescriptor#setProto}. */ | 866 /** See {@link FileDescriptor#setProto}. */ |
| 867 private void setProto(final DescriptorProto proto) { | 867 private void setProto(final DescriptorProto proto) { |
| 868 this.proto = proto; | 868 this.proto = proto; |
| 869 | 869 |
| 870 for (int i = 0; i < nestedTypes.length; i++) { | 870 for (int i = 0; i < nestedTypes.length; i++) { |
| 871 nestedTypes[i].setProto(proto.getNestedType(i)); | 871 nestedTypes[i].setProto(proto.getNestedType(i)); |
| 872 } | 872 } |
| 873 | 873 |
| 874 for (int i = 0; i < oneofs.length; i++) { |
| 875 oneofs[i].setProto(proto.getOneofDecl(i)); |
| 876 } |
| 877 |
| 874 for (int i = 0; i < enumTypes.length; i++) { | 878 for (int i = 0; i < enumTypes.length; i++) { |
| 875 enumTypes[i].setProto(proto.getEnumType(i)); | 879 enumTypes[i].setProto(proto.getEnumType(i)); |
| 876 } | 880 } |
| 877 | 881 |
| 878 for (int i = 0; i < fields.length; i++) { | 882 for (int i = 0; i < fields.length; i++) { |
| 879 fields[i].setProto(proto.getField(i)); | 883 fields[i].setProto(proto.getField(i)); |
| 880 } | 884 } |
| 881 | 885 |
| 882 for (int i = 0; i < extensions.length; i++) { | 886 for (int i = 0; i < extensions.length; i++) { |
| 883 extensions[i].setProto(proto.getExtension(i)); | 887 extensions[i].setProto(proto.getExtension(i)); |
| (...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1201 this.defaultDefault = defaultDefault; | 1205 this.defaultDefault = defaultDefault; |
| 1202 } | 1206 } |
| 1203 | 1207 |
| 1204 /** | 1208 /** |
| 1205 * The default default value for fields of this type, if it's a primitive | 1209 * The default default value for fields of this type, if it's a primitive |
| 1206 * type. This is meant for use inside this file only, hence is private. | 1210 * type. This is meant for use inside this file only, hence is private. |
| 1207 */ | 1211 */ |
| 1208 private final Object defaultDefault; | 1212 private final Object defaultDefault; |
| 1209 } | 1213 } |
| 1210 | 1214 |
| 1211 // TODO(xiaofeng): Implement it consistently across different languages. See
b/24751348. | 1215 // This method should match exactly with the ToJsonName() function in C++ |
| 1212 private static String fieldNameToLowerCamelCase(String name) { | 1216 // descriptor.cc. |
| 1217 private static String fieldNameToJsonName(String name) { |
| 1213 StringBuilder result = new StringBuilder(name.length()); | 1218 StringBuilder result = new StringBuilder(name.length()); |
| 1214 boolean isNextUpperCase = false; | 1219 boolean isNextUpperCase = false; |
| 1215 for (int i = 0; i < name.length(); i++) { | 1220 for (int i = 0; i < name.length(); i++) { |
| 1216 Character ch = name.charAt(i); | 1221 Character ch = name.charAt(i); |
| 1217 if (Character.isLowerCase(ch)) { | 1222 if (ch == '_') { |
| 1218 if (isNextUpperCase) { | 1223 isNextUpperCase = true; |
| 1219 result.append(Character.toUpperCase(ch)); | 1224 } else if (isNextUpperCase) { |
| 1220 } else { | 1225 result.append(Character.toUpperCase(ch)); |
| 1221 result.append(ch); | |
| 1222 } | |
| 1223 isNextUpperCase = false; | |
| 1224 } else if (Character.isUpperCase(ch)) { | |
| 1225 if (i == 0) { | |
| 1226 // Force first letter to lower-case. | |
| 1227 result.append(Character.toLowerCase(ch)); | |
| 1228 } else { | |
| 1229 // Capital letters after the first are left as-is. | |
| 1230 result.append(ch); | |
| 1231 } | |
| 1232 isNextUpperCase = false; | |
| 1233 } else if (Character.isDigit(ch)) { | |
| 1234 result.append(ch); | |
| 1235 isNextUpperCase = false; | 1226 isNextUpperCase = false; |
| 1236 } else { | 1227 } else { |
| 1237 isNextUpperCase = true; | 1228 result.append(ch); |
| 1238 } | 1229 } |
| 1239 } | 1230 } |
| 1240 return result.toString(); | 1231 return result.toString(); |
| 1241 } | 1232 } |
| 1242 | 1233 |
| 1243 private FieldDescriptor(final FieldDescriptorProto proto, | 1234 private FieldDescriptor(final FieldDescriptorProto proto, |
| 1244 final FileDescriptor file, | 1235 final FileDescriptor file, |
| 1245 final Descriptor parent, | 1236 final Descriptor parent, |
| 1246 final int index, | 1237 final int index, |
| 1247 final boolean isExtension) | 1238 final boolean isExtension) |
| 1248 throws DescriptorValidationException { | 1239 throws DescriptorValidationException { |
| 1249 this.index = index; | 1240 this.index = index; |
| 1250 this.proto = proto; | 1241 this.proto = proto; |
| 1251 fullName = computeFullName(file, parent, proto.getName()); | 1242 fullName = computeFullName(file, parent, proto.getName()); |
| 1252 this.file = file; | 1243 this.file = file; |
| 1253 if (proto.hasJsonName()) { | 1244 if (proto.hasJsonName()) { |
| 1254 jsonName = proto.getJsonName(); | 1245 jsonName = proto.getJsonName(); |
| 1255 } else { | 1246 } else { |
| 1256 jsonName = fieldNameToLowerCamelCase(proto.getName()); | 1247 jsonName = fieldNameToJsonName(proto.getName()); |
| 1257 } | 1248 } |
| 1258 | 1249 |
| 1259 if (proto.hasType()) { | 1250 if (proto.hasType()) { |
| 1260 type = Type.valueOf(proto.getType()); | 1251 type = Type.valueOf(proto.getType()); |
| 1261 } | 1252 } |
| 1262 | 1253 |
| 1263 if (getNumber() <= 0) { | 1254 if (getNumber() <= 0) { |
| 1264 throw new DescriptorValidationException(this, | 1255 throw new DescriptorValidationException(this, |
| 1265 "Field numbers must be positive integers."); | 1256 "Field numbers must be positive integers."); |
| 1266 } | 1257 } |
| (...skipping 858 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2125 importPublicDependencies(dependencies[i]); | 2116 importPublicDependencies(dependencies[i]); |
| 2126 } | 2117 } |
| 2127 | 2118 |
| 2128 for (final FileDescriptor dependency : this.dependencies) { | 2119 for (final FileDescriptor dependency : this.dependencies) { |
| 2129 try { | 2120 try { |
| 2130 addPackage(dependency.getPackage(), dependency); | 2121 addPackage(dependency.getPackage(), dependency); |
| 2131 } catch (DescriptorValidationException e) { | 2122 } catch (DescriptorValidationException e) { |
| 2132 // Can't happen, because addPackage() only fails when the name | 2123 // Can't happen, because addPackage() only fails when the name |
| 2133 // conflicts with a non-package, but we have not yet added any | 2124 // conflicts with a non-package, but we have not yet added any |
| 2134 // non-packages at this point. | 2125 // non-packages at this point. |
| 2135 assert false; | 2126 throw new AssertionError(e); |
| 2136 } | 2127 } |
| 2137 } | 2128 } |
| 2138 } | 2129 } |
| 2139 | 2130 |
| 2140 /** Find and put public dependencies of the file into dependencies set.*/ | 2131 /** Find and put public dependencies of the file into dependencies set.*/ |
| 2141 private void importPublicDependencies(final FileDescriptor file) { | 2132 private void importPublicDependencies(final FileDescriptor file) { |
| 2142 for (FileDescriptor dependency : file.getPublicDependencies()) { | 2133 for (FileDescriptor dependency : file.getPublicDependencies()) { |
| 2143 if (dependencies.add(dependency)) { | 2134 if (dependencies.add(dependency)) { |
| 2144 importPublicDependencies(dependency); | 2135 importPublicDependencies(dependency); |
| 2145 } | 2136 } |
| (...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2506 public String getName() { return proto.getName(); } | 2497 public String getName() { return proto.getName(); } |
| 2507 | 2498 |
| 2508 public FileDescriptor getFile() { return file; } | 2499 public FileDescriptor getFile() { return file; } |
| 2509 | 2500 |
| 2510 public String getFullName() { return fullName; } | 2501 public String getFullName() { return fullName; } |
| 2511 | 2502 |
| 2512 public Descriptor getContainingType() { return containingType; } | 2503 public Descriptor getContainingType() { return containingType; } |
| 2513 | 2504 |
| 2514 public int getFieldCount() { return fieldCount; } | 2505 public int getFieldCount() { return fieldCount; } |
| 2515 | 2506 |
| 2507 public OneofOptions getOptions() { |
| 2508 return proto.getOptions(); |
| 2509 } |
| 2510 |
| 2516 /** Get a list of this message type's fields. */ | 2511 /** Get a list of this message type's fields. */ |
| 2517 public List<FieldDescriptor> getFields() { | 2512 public List<FieldDescriptor> getFields() { |
| 2518 return Collections.unmodifiableList(Arrays.asList(fields)); | 2513 return Collections.unmodifiableList(Arrays.asList(fields)); |
| 2519 } | 2514 } |
| 2520 | 2515 |
| 2521 public FieldDescriptor getField(int index) { | 2516 public FieldDescriptor getField(int index) { |
| 2522 return fields[index]; | 2517 return fields[index]; |
| 2523 } | 2518 } |
| 2524 | 2519 |
| 2520 private void setProto(final OneofDescriptorProto proto) { |
| 2521 this.proto = proto; |
| 2522 } |
| 2523 |
| 2525 private OneofDescriptor(final OneofDescriptorProto proto, | 2524 private OneofDescriptor(final OneofDescriptorProto proto, |
| 2526 final FileDescriptor file, | 2525 final FileDescriptor file, |
| 2527 final Descriptor parent, | 2526 final Descriptor parent, |
| 2528 final int index) | 2527 final int index) |
| 2529 throws DescriptorValidationException { | 2528 throws DescriptorValidationException { |
| 2530 this.proto = proto; | 2529 this.proto = proto; |
| 2531 fullName = computeFullName(file, parent, proto.getName()); | 2530 fullName = computeFullName(file, parent, proto.getName()); |
| 2532 this.file = file; | 2531 this.file = file; |
| 2533 this.index = index; | 2532 this.index = index; |
| 2534 | 2533 |
| 2535 containingType = parent; | 2534 containingType = parent; |
| 2536 fieldCount = 0; | 2535 fieldCount = 0; |
| 2537 } | 2536 } |
| 2538 | 2537 |
| 2539 private final int index; | 2538 private final int index; |
| 2540 private OneofDescriptorProto proto; | 2539 private OneofDescriptorProto proto; |
| 2541 private final String fullName; | 2540 private final String fullName; |
| 2542 private final FileDescriptor file; | 2541 private final FileDescriptor file; |
| 2543 | 2542 |
| 2544 private Descriptor containingType; | 2543 private Descriptor containingType; |
| 2545 private int fieldCount; | 2544 private int fieldCount; |
| 2546 private FieldDescriptor[] fields; | 2545 private FieldDescriptor[] fields; |
| 2547 } | 2546 } |
| 2548 } | 2547 } |
| OLD | NEW |