@Override public String toString() { return name(); }
/** Returns the set of names that are not unique within {@code fields}. */ private Set<String> collidingFieldNames(ImmutableList<Field> fields) { Set<String> fieldNames = new LinkedHashSet<>(); Set<String> collidingNames = new LinkedHashSet<>(); for (Field field : fields) { if (!fieldNames.add(field.name())) { collidingNames.add(field.name()); } } return collidingNames; }
/** Returns the field named {@code name}, or null if this type has no such field. */ public Field field(String name) { for (Field field : declaredFields) { if (field.name().equals(name)) { return field; } } for (OneOf oneOf : oneOfs) { for (Field field : oneOf.fields()) { if (field.name().equals(name)) { return field; } } } return null; }
static ImmutableList<Field> retainAll( Schema schema, MarkSet markSet, ProtoType enclosingType, Collection<Field> fields) { ImmutableList.Builder<Field> result = ImmutableList.builder(); for (Field field : fields) { Field retainedField = field.retainAll(schema, markSet); if (retainedField != null && markSet.contains(ProtoMember.get(enclosingType, field.name()))) { result.add(retainedField); } } return result.build(); }
public static ProtoMember get(ProtoType type, Field field) { String member = field.isExtension() ? field.qualifiedName() : field.name(); return new ProtoMember(type, member); }
withContext(field).addError("tag %s is reserved (%s)", tag, reserved.location()); if (reserved.matchesName(field.name())) { withContext(field).addError("name '%s' is reserved (%s)", field.name(), reserved.location()); for (Field field : entry.getValue()) { error.append(String.format("\n %s. %s (%s)", index++, field.name(), field.location())); Field first = collidingFields.iterator().next(); StringBuilder error = new StringBuilder(); error.append(String.format("multiple fields share name %s:", first.name())); int index = 1; for (Field field : collidingFields) { error.append(String.format("\n %s. %s (%s)", index++, field.name(), field.location()));
error.append(String.format("%s field %s (%s)", prefix, field.name(), field.location()));
private MethodSpec messageToString(NameAllocator nameAllocator, MessageType type) { NameAllocator localNameAllocator = nameAllocator.clone(); MethodSpec.Builder result = MethodSpec.methodBuilder("toString") .addAnnotation(Override.class) .addModifiers(PUBLIC) .returns(String.class); String builderName = localNameAllocator.newName("builder"); result.addStatement("$1T $2N = new $1T()", StringBuilder.class, builderName); for (Field field : type.fieldsAndOneOfFields()) { String fieldName = nameAllocator.get(field); if (field.isRepeated() || field.type().isMap()) { result.addCode("if (!$N.isEmpty()) ", fieldName); } else if (!field.isRequired()) { result.addCode("if ($N != null) ", fieldName); } if (field.isRedacted()) { result.addStatement("$N.append(\", $N=██\")", builderName, field.name()); } else { result.addStatement("$N.append(\", $N=\").append($L)", builderName, field.name(), fieldName); } } result.addStatement("return builder.replace(0, 2, \"$L{\").append('}').toString()", type.type().simpleName()); return result.build(); }
public ProtoAdapter<Object> get(ProtoType protoType) { if (protoType.isMap()) throw new UnsupportedOperationException("map types not supported"); ProtoAdapter<?> result = adapterMap.get(protoType); if (result != null) { return (ProtoAdapter<Object>) result; } Type type = schema.getType(protoType); if (type == null) { throw new IllegalArgumentException("unknown type: " + protoType); } if (type instanceof EnumType) { EnumAdapter enumAdapter = new EnumAdapter((EnumType) type); adapterMap.put(protoType, enumAdapter); return enumAdapter; } if (type instanceof MessageType) { MessageAdapter messageAdapter = new MessageAdapter(includeUnknown); // Put the adapter in the map early to mitigate the recursive calls to get() made below. adapterMap.put(protoType, messageAdapter); for (com.squareup.wire.schema.Field field : ((MessageType) type).fields()) { Field fieldAdapter = new Field( field.name(), field.tag(), field.isRepeated(), get(field.type())); messageAdapter.fieldsByName.put(field.name(), fieldAdapter); messageAdapter.fieldsByTag.put(field.tag(), fieldAdapter); } return (ProtoAdapter) messageAdapter; } throw new IllegalArgumentException("unexpected type: " + protoType); }
private CodeBlock singleAdapterFor(Field field) { return field.type().isMap() ? CodeBlock.of("$N", field.name()) : singleAdapterFor(field.type()); }
if (field.type().isMap()) { TypeName adapterType = adapterOf(fieldType(field)); adapter.addField(FieldSpec.builder(adapterType, field.name(), PRIVATE, FINAL) .initializer("$T.newMapAdapter($L, $L)", ADAPTER, singleAdapterFor(field.type().keyType()),
private void markField(ProtoType declaringType, Field field) { if (marks.contains(ProtoMember.get(declaringType, field.name()))) { markOptions(field.options()); mark(field.type()); } }
if (allOptionFieldsBuilder.add(protoMember)) { TypeName optionJavaType = typeName(optionField.type()); builder.addField(optionJavaType, optionField.name(), PUBLIC, FINAL); constructorBuilder.addParameter(optionJavaType, optionField.name()); constructorBuilder.addStatement("this.$L = $L", optionField.name(), optionField.name());
if (i > 0) missingArgs.add(",\n"); missingArgs.add("$1L, $2S", nameAllocator.get(requiredField), requiredField.name());
private void markRoots(Type type) { ProtoType protoType = type.type(); if (identifierSet.includes(protoType)) { marks.root(protoType); queue.add(protoType); } else { if (type instanceof MessageType) { for (Field field : ((MessageType) type).fieldsAndOneOfFields()) { markRoots(ProtoMember.get(protoType, field.name())); } } else if (type instanceof EnumType) { for (EnumConstant enumConstant : ((EnumType) type).constants()) { markRoots(ProtoMember.get(protoType, enumConstant.name())); } } else { throw new AssertionError(); } } for (Type nested : type.nestedTypes()) { markRoots(nested); } }
@Override public NameAllocator load(Type type) throws Exception { NameAllocator nameAllocator = new NameAllocator(); if (type instanceof MessageType) { nameAllocator.newName("serialVersionUID", "serialVersionUID"); nameAllocator.newName("ADAPTER", "ADAPTER"); nameAllocator.newName("MESSAGE_OPTIONS", "MESSAGE_OPTIONS"); if (emitAndroid) { nameAllocator.newName("CREATOR", "CREATOR"); } ImmutableList<Field> fieldsAndOneOfFields = ((MessageType) type).fieldsAndOneOfFields(); Set<String> collidingNames = collidingFieldNames(fieldsAndOneOfFields); for (Field field : fieldsAndOneOfFields) { String suggestion = collidingNames.contains(field.name()) ? field.qualifiedName() : field.name(); nameAllocator.newName(suggestion, field); } } else if (type instanceof EnumType) { nameAllocator.newName("value", "value"); nameAllocator.newName("i", "i"); nameAllocator.newName("reader", "reader"); nameAllocator.newName("writer", "writer"); for (EnumConstant constant : ((EnumType) type).constants()) { nameAllocator.newName(constant.name(), constant); } } return nameAllocator; } });
@Test public void retainFieldRetainsOneOf() throws Exception { Schema schema = new RepoBuilder() .add("service.proto", "" + "message Message {\n" + " oneof selection {\n" + " string a = 1;\n" + " string b = 2;\n" + " }\n" + " optional string c = 3;\n" + "}\n") .schema(); Schema pruned = schema.prune(new IdentifierSet.Builder() .include("Message#b") .build()); MessageType message = (MessageType) pruned.getType("Message"); OneOf onlyOneOf = getOnlyElement(message.oneOfs()); assertThat(onlyOneOf.name()).isEqualTo("selection"); assertThat(getOnlyElement(onlyOneOf.fields()).name()).isEqualTo("b"); assertThat(message.field("a")).isNull(); assertThat(message.field("c")).isNull(); }
static ImmutableList<Field> retainAll( Schema schema, MarkSet markSet, ProtoType enclosingType, Collection<Field> fields) { ImmutableList.Builder<Field> result = ImmutableList.builder(); for (Field field : fields) { Field retainedField = field.retainAll(schema, markSet); if (retainedField != null && markSet.contains(ProtoMember.get(enclosingType, field.name()))) { result.add(retainedField); } } return result.build(); }
private AnnotationSpec jsonPropertyAnnotation(Field field) { AnnotationSpec.Builder result = AnnotationSpec.builder(JsonProperty.class); result.addMember("value", "$S", field.name()); return result.build(); }
private void markField(ProtoType declaringType, Field field) { if (marks.contains(ProtoMember.get(declaringType, field.name()))) { markOptions(field.options()); mark(field.type()); } }