@Override public String getParentFullName() { return protoField.getParent().getFullName(); }
@Override public String apply(Field field) { return field.getSimpleName(); } }));
/** * Returns the type of this field selector, i.e. the type of the last field in the field list. */ public TypeRef getType() { return getLastField().getType(); }
@Override public String toString() { return String.format("%s %s", getType(), getFullName()); } }
/** In proto3, repeated fields of scalar numeric types use packed encoding by default */ private boolean isDefaultPackedEncoding(Field field) { if (field.getSyntax() == Syntax.SYNTAX_PROTO3 && field.isRepeated()) { FieldDescriptorProto.Type fieldType = field.getProto().getType(); if (fieldType != FieldDescriptorProto.Type.TYPE_GROUP && fieldType != FieldDescriptorProto.Type.TYPE_BYTES && fieldType != FieldDescriptorProto.Type.TYPE_STRING && fieldType != FieldDescriptorProto.Type.TYPE_MESSAGE) { return true; } } return false; }
@VisitsBefore void visit(Field field) { // Resolve type of this field. TypeRef type = resolveType(field.getLocation(), field.getProto().getType(), field.getProto().getTypeName()); if (type != null) { if (field.isRepeated()) { type = type.makeRepeated(); } else if (!field.isOptional()) { type = type.makeRequired(); } field.setType(type); } // Check for resolution of oneof. if (field.getProto().hasOneofIndex() && field.getOneof() == null) { // Indicates the oneof index could not be resolved. model .getDiagReporter() .report( Diag.error( field.getLocation(), "Unresolved oneof reference (indicates internal inconsistency of input; oneof " + "index: %s)", field.getProto().getOneofIndex())); } findOptionTypes(field.getOptionFields()); }
com.google.protobuf.Field.Builder coreFieldBuilder = com.google.protobuf.Field.newBuilder() .setName(field.getSimpleName()) .setNumber(field.getNumber()) .setKind(toCoreFieldKind(field.getProto())) .setCardinality(toCoreFieldCardinality(field.getProto())) .setJsonName(field.getJsonName()); if (field.getType().isEnum() || field.getType().isMessage()) { coreFieldBuilder.setTypeUrl(generateTypeUrl(field.getType())); FieldDescriptorProto proto = field.getProto(); coreFieldBuilder.setOneofIndex(field.getProto().getOneofIndex() + 1); DescriptorNormalization.getOptions(field.getProto(), includeDefaults)); coreTypeBuilder.addFields(coreFieldBuilder.build());
.build(); Mockito.when(shelfName.getParent()).thenReturn(shelfMessage); Mockito.when(shelfName.getType()).thenReturn(TypeRef.fromPrimitiveName("string")); Mockito.when(shelfName.getSimpleName()).thenReturn("name"); Mockito.when(shelfMessage.getFields()).thenReturn(ImmutableList.of(shelfName, shelfTheme)); Mockito.when(bookName.getParent()).thenReturn(bookMessage); Mockito.when(bookName.getSimpleName()).thenReturn("name"); Mockito.when(bookName.getType()).thenReturn(TypeRef.fromPrimitiveName("string"));
@VisitsBefore void visit(MessageType message) { // Add the message to the set of known types. addType(message.getLocation(), message.getFullName(), TypeRef.of(message)); // Add the message's package to the set of known packages addPackage(message.getFile().getFullName()); // Build the field-by-name map for this message, and record field simple names. Map<String, Field> fieldByName = Maps.newLinkedHashMap(); for (Field field : message.getFields()) { fieldNames.add(field.getSimpleName()); Field old = fieldByName.put(field.getSimpleName(), field); if (old != null) { model .getDiagReporter() .report( Diag.error( field.getLocation(), "Duplicate declaration of field '%s'. Previous location: %s", field.getSimpleName(), old.getLocation().getDisplayString())); } } message.setFieldByNameMap(ImmutableMap.copyOf(fieldByName)); }
@Visits void reach(MessageType message) { markAsReachable(message); visitInScope(message.getFields()); // Check whether required fields are hidden, and whether the message has unreachable // descendants. for (Field field : message.getFields()) { if (!isReachable(field)) { hasUnreachableDescendants.add(message); if (field.isRequired()) { error(field, "A required field cannot be hidden. %s.", reasonForUnreachable(field)); } } else { TypeRef type = field.getType(); if (type.isMessage() && hasUnreachableDescendants(type.getMessageType()) || type.isEnum() && hasUnreachableDescendants(type.getEnumType())) { hasUnreachableDescendants.add(message); } } } }
@Accepts public void accept(Field field) { if (field.oneofScoped()) { // Only visit each oneof for one time when the first field in the oneof gets visited. if (field == field.getOneof().getFields().get(0)) { visit(field.getOneof()); } } } }
public boolean isOneof(Field field) { return field.getOneof() != null; } }
Field field = Field.create(this, fieldProtos.get(i), childPath, associatedOneof); fieldsBuilder.add(field); if (associatedOneof != null) { fieldsBuilder.add(Field.createAsExtension( this, entry.getValue(), entry.getValue().getPath(), entry.getKey()));
@Override public String getFullName() { return protoField.getFullName(); }
if (!Strings.isNullOrEmpty(resourceName)) { TypeRef resourceType = field.getModel().getSymbolTable().lookupType(resourceName); if (resourceType != null) { ProtoFile protoFile = allResources.get(resource); if (getResourceFullName(resource, protoFile).equals(resourceName) || field.getFile().equals(protoFile) && resource.getName().equals(resourceName)) { return resource.getName(); ProtoFile protoFile = allResourceSets.get(resourceSet); if (getResourceSetFullName(resourceSet, protoFile).equals(resourceName) || field.getFile().equals(protoFile) && resourceSet.getName().equals(resourceName)) { return resourceSet.getName();
@Override public boolean isRepeated() { return protoField.isRepeated(); }
@Test public void testGetLocation_field() { Field field1 = getOuterMessage().getFields().get(0); Field field3 = getInnerMessage().getFields().get(0); verifyLocation("optional string field1 = 1; // Field.", field1.getLocation()); verifyLocation("optional string field3 = 3;", field3.getLocation()); }
/** * Creates a field backed up by the given proto. */ public static Field create(MessageType parent, FieldDescriptorProto proto, String path, @Nullable Oneof oneof) { return new Field(parent, proto, path, oneof, proto.getName()); }
@VisitsBefore void visit(Field field) { // Resolve type of this field. TypeRef type = resolveType(field.getLocation(), field.getProto().getType(), field.getProto().getTypeName()); if (type != null) { if (field.isRepeated()) { type = type.makeRepeated(); } else if (!field.isOptional()) { type = type.makeRequired(); } field.setType(type); } // Check for resolution of oneof. if (field.getProto().hasOneofIndex() && field.getOneof() == null) { // Indicates the oneof index could not be resolved. model .getDiagReporter() .report( Diag.error( field.getLocation(), "Unresolved oneof reference (indicates internal inconsistency of input; oneof " + "index: %s)", field.getProto().getOneofIndex())); } findOptionTypes(field.getOptionFields()); }