use of com.squareup.wire.schema.ProtoMember in project wire by square.
the class JavaGenerator method get.
public static JavaGenerator get(Schema schema) {
Map<ProtoType, TypeName> nameToJavaName = new LinkedHashMap<>();
Map<ProtoMember, TypeName> memberToJavaName = new LinkedHashMap<>();
for (ProtoFile protoFile : schema.getProtoFiles()) {
String javaPackage = javaPackage(protoFile);
putAll(nameToJavaName, javaPackage, null, protoFile.getTypes());
for (Service service : protoFile.getServices()) {
ClassName className = ClassName.get(javaPackage, service.type().getSimpleName());
nameToJavaName.put(service.type(), className);
}
for (Extend extend : protoFile.getExtendList()) {
if (annotationTargetType(extend) == null)
continue;
for (Field field : extend.getFields()) {
if (!eligibleAsAnnotationMember(schema, field))
continue;
ProtoMember protoMember = field.getMember();
String simpleName = camelCase(protoMember.getSimpleName(), true) + "Option";
ClassName className = ClassName.get(javaPackage, simpleName);
memberToJavaName.put(protoMember, className);
}
}
}
nameToJavaName.putAll(BUILT_IN_TYPES_MAP);
return new JavaGenerator(schema, nameToJavaName, memberToJavaName, new Profile(), false, /* emitAndroid */
false, /* emitAndroidAnnotations */
false, /* emitCompact */
false, /* emitDeclaredOptions */
false);
}
use of com.squareup.wire.schema.ProtoMember in project wire by square.
the class JavaGenerator method optionsField.
// Example:
//
// public static final FieldOptions FIELD_OPTIONS_FOO = new FieldOptions.Builder()
// .setExtension(Ext_custom_options.count, 42)
// .build();
//
private FieldSpec optionsField(ProtoType optionsType, String fieldName, Options options) {
TypeName optionsJavaType = typeName(optionsType);
CodeBlock.Builder initializer = CodeBlock.builder();
initializer.add("$[new $T.Builder()", optionsJavaType);
boolean empty = true;
for (Map.Entry<ProtoMember, ?> entry : options.map().entrySet()) {
if (entry.getKey().equals(FIELD_DEPRECATED) || entry.getKey().equals(PACKED)) {
continue;
}
Field optionField = schema.getField(entry.getKey());
initializer.add("\n.$L($L)", fieldName(optionsType, optionField), fieldInitializer(optionField.type(), entry.getValue()));
empty = false;
}
initializer.add("\n.build()$]");
if (empty)
return null;
return FieldSpec.builder(optionsJavaType, fieldName).addModifiers(PUBLIC, STATIC, FINAL).initializer(initializer.build()).build();
}
use of com.squareup.wire.schema.ProtoMember in project wire by square.
the class JavaGenerator method fieldInitializer.
private CodeBlock fieldInitializer(ProtoType type, @Nullable Object value) {
TypeName javaType = typeName(type);
if (value instanceof List) {
CodeBlock.Builder builder = CodeBlock.builder();
builder.add("$T.asList(", Arrays.class);
boolean first = true;
for (Object o : (List<?>) value) {
if (!first)
builder.add(",");
first = false;
builder.add("\n$>$>$L$<$<", fieldInitializer(type, o));
}
builder.add(")");
return builder.build();
} else if (value instanceof Map) {
CodeBlock.Builder builder = CodeBlock.builder();
builder.add("new $T.Builder()", javaType);
for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
ProtoMember protoMember = (ProtoMember) entry.getKey();
Field field = schema.getField(protoMember);
CodeBlock valueInitializer = fieldInitializer(field.getType(), entry.getValue());
builder.add("\n$>$>.$L($L)$<$<", fieldName(type, field), valueInitializer);
}
builder.add("\n$>$>.build()$<$<");
return builder.build();
} else if (javaType.equals(TypeName.BOOLEAN)) {
return CodeBlock.of("$L", value != null ? value : false);
} else if (javaType.equals(TypeName.INT)) {
return CodeBlock.of("$L", optionValueToInt(value));
} else if (javaType.equals(TypeName.LONG)) {
return CodeBlock.of("$LL", optionValueToLong(value));
} else if (javaType.equals(TypeName.FLOAT)) {
if (value == null) {
return CodeBlock.of("0.0f");
} else if ("inf".equals(value)) {
return CodeBlock.of("Float.POSITIVE_INFINITY");
} else if ("-inf".equals(value)) {
return CodeBlock.of("Float.NEGATIVE_INFINITY");
} else if ("nan".equals(value) || "-nan".equals(value)) {
return CodeBlock.of("Float.NaN");
} else {
return CodeBlock.of("$Lf", String.valueOf(value));
}
} else if (javaType.equals(TypeName.DOUBLE)) {
if (value == null) {
return CodeBlock.of("0.0d");
} else if ("inf".equals(value)) {
return CodeBlock.of("Double.POSITIVE_INFINITY");
} else if ("-inf".equals(value)) {
return CodeBlock.of("Double.NEGATIVE_INFINITY");
} else if ("nan".equals(value) || "-nan".equals(value)) {
return CodeBlock.of("Double.NaN");
} else {
return CodeBlock.of("$Ld", String.valueOf(value));
}
} else if (javaType.equals(STRING)) {
return CodeBlock.of("$S", value != null ? value : "");
} else if (javaType.equals(BYTE_STRING)) {
if (value == null) {
return CodeBlock.of("$T.EMPTY", ByteString.class);
} else {
return CodeBlock.of("$T.decodeBase64($S)", ByteString.class, ByteString.encodeString(String.valueOf(value), Charsets.ISO_8859_1).base64());
}
} else if (isEnum(type) && value != null) {
return CodeBlock.of("$T.$L", javaType, value);
} else {
throw new IllegalStateException(type + " is not an allowed scalar type");
}
}
Aggregations