/** Get the syntax of the .proto file. */ public Syntax getSyntax() { if (Syntax.PROTO3.name.equals(proto.getSyntax())) { return Syntax.PROTO3; } return Syntax.PROTO2; }
/** For internal use only. */ public boolean needsUtf8Check() { if (type != Type.STRING) { return false; } if (getContainingType().getOptions().getMapEntry()) { // Always enforce strict UTF-8 checking for map fields. return true; } if (getFile().getSyntax() == Syntax.PROTO3) { return true; } return getFile().getOptions().getJavaStringCheckUtf8(); }
/** Does this field have the {@code [packed = true]} option or is this field * packable in proto3 and not explicitly setted to unpacked? */ public boolean isPacked() { if (!isPackable()) { return false; } if (getFile().getSyntax() == FileDescriptor.Syntax.PROTO2) { return getOptions().getPacked(); } else { return !getOptions().hasPacked() || getOptions().getPacked(); } }
/** Does this field have the {@code [packed = true]} option or is this field * packable in proto3 and not explicitly setted to unpacked? */ @Override public boolean isPacked() { if (!isPackable()) { return false; } if (getFile().getSyntax() == FileDescriptor.Syntax.PROTO2) { return getOptions().getPacked(); } else { return !getOptions().hasPacked() || getOptions().getPacked(); } }
/** * Recursively walks the given {@link Message} class and verifies that every field or message * linked in uses the Protocol Buffers proto2 syntax. */ static void checkProto2Syntax(Class<? extends Message> clazz, ExtensionRegistry registry) { for (GenericDescriptor d : getRecursiveDescriptorsForClass(clazz, registry)) { Syntax s = d.getFile().getSyntax(); checkArgument( s == Syntax.PROTO2, "Message %s or one of its dependencies does not use proto2 syntax: %s in file %s", clazz.getName(), d.getFullName(), d.getFile().getName()); } }
/** * Returns whether or not we should check for presence to emulate jspb nullability semantics in * server side soy. */ static boolean shouldCheckFieldPresenceToEmulateJspbNullability(FieldDescriptor desc) { boolean hasBrokenSemantics = false; if (desc.hasDefaultValue() || desc.isRepeated()) { return false; } else if (desc.getFile().getSyntax() == Syntax.PROTO3 || !hasBrokenSemantics) { // in proto3 or proto2 with non-broken semantics we only need to check for presence for // message typed fields. return desc.getJavaType() == JavaType.MESSAGE; } else { return true; } }
@Override public BuilderType mergeFrom( final CodedInputStream input, final ExtensionRegistryLite extensionRegistry) throws IOException { boolean discardUnknown = getDescriptorForType().getFile().getSyntax() == Syntax.PROTO3 ? input.shouldDiscardUnknownFieldsProto3() : input.shouldDiscardUnknownFields(); final UnknownFieldSet.Builder unknownFields = discardUnknown ? null : UnknownFieldSet.newBuilder(getUnknownFields()); while (true) { final int tag = input.readTag(); if (tag == 0) { break; } MessageReflection.BuilderAdapter builderAdapter = new MessageReflection.BuilderAdapter(this); if (!MessageReflection.mergeFieldFrom(input, unknownFields, extensionRegistry, getDescriptorForType(), builderAdapter, tag)) { // end group tag break; } } if (unknownFields != null) { setUnknownFields(unknownFields.build()); } return (BuilderType) this; }
Syntax(String name) { this.name = name; }
boolean supportsUnknownEnumValue() { return getSyntax() == Syntax.PROTO3; }
/** * Proto3 enums fields can accept and return unknown values via the get<Field>Value() methods, we * use those methods instead of the methods that deal with the enum constants in order to support * unknown enum values. If we didn't, any field with an unknown enum value would throw an * exception when we call {@code getNumber()} on the enum. * * <p>For comparison, in proto2 unknown values always get mapped to 0, so this problem doesn't * exist. Also, in proto2, the 'Value' functions don't exist, so we can't use them. */ private static boolean isProto3EnumField(FieldDescriptor descriptor) { return descriptor.getType() == Descriptors.FieldDescriptor.Type.ENUM && descriptor.getFile().getSyntax() == Syntax.PROTO3; }