use of com.squareup.wire.schema.EnumType in project wire by square.
the class JavaGenerator method generateAdapterForCustomType.
/**
* Returns a standalone adapter for {@code type}.
*/
public TypeSpec generateAdapterForCustomType(Type type) {
NameAllocator nameAllocator = nameAllocators.getUnchecked(type);
ClassName adapterTypeName = abstractAdapterName(type.getType());
ClassName typeName = (ClassName) typeName(type.getType());
TypeSpec.Builder adapter;
if (type instanceof MessageType) {
adapter = messageAdapter(nameAllocator, (MessageType) type, typeName, adapterTypeName, null).toBuilder();
} else {
adapter = enumAdapter(nameAllocator, (EnumType) type, typeName, adapterTypeName).toBuilder();
}
if (adapterTypeName.enclosingClassName() != null)
adapter.addModifiers(STATIC);
for (Type nestedType : type.getNestedTypes()) {
if (profile.getAdapter(nestedType.getType()) == null) {
throw new IllegalArgumentException("Missing custom proto adapter for " + nestedType.getType().getEnclosingTypeOrPackage() + "." + nestedType.getType().getSimpleName() + " when enclosing proto has custom proto adapter.");
}
adapter.addType(generateAdapterForCustomType(nestedType));
}
return adapter.build();
}
use of com.squareup.wire.schema.EnumType in project wire by square.
the class JavaGenerator method identityValue.
private CodeBlock identityValue(Field field) {
switch(field.getEncodeMode()) {
case MAP:
return CodeBlock.of("$T.emptyMap()", Collections.class);
case REPEATED:
case PACKED:
return CodeBlock.of("$T.emptyList()", Collections.class);
case NULL_IF_ABSENT:
return CodeBlock.of("null");
case OMIT_IDENTITY:
ProtoType protoType = field.getType();
Type type = schema.getType(protoType);
if (protoType.equals(ProtoType.STRUCT_NULL)) {
return CodeBlock.of("null");
} else if (field.isOneOf()) {
return CodeBlock.of("null");
} else if (protoType.isScalar()) {
CodeBlock value = PROTOTYPE_TO_IDENTITY_VALUES.get(protoType);
if (value == null) {
throw new IllegalArgumentException("Unexpected scalar proto type: " + protoType);
}
return value;
} else if (type instanceof MessageType) {
return CodeBlock.of("null");
} else if (type instanceof EnumType) {
return identity((EnumType) type);
}
case REQUIRED:
default:
throw new IllegalArgumentException("No identity value for field: " + field + "(" + field.getEncodeMode() + ")");
}
}
use of com.squareup.wire.schema.EnumType in project wire by square.
the class JavaGenerator method abstractAdapterName.
/**
* Returns the Java type of the abstract adapter class generated for a corresponding {@code
* protoType}. Returns null if {@code protoType} is not using a custom proto adapter.
*/
@Nullable
public ClassName abstractAdapterName(ProtoType protoType) {
TypeName profileJavaName = profile.javaTarget(protoType);
if (profileJavaName == null)
return null;
TypeName typeName = typeToJavaName.get(protoType);
Type type = schema.getType(protoType);
ClassName javaName;
if (typeName instanceof ClassName) {
javaName = (ClassName) typeName;
} else if (typeName instanceof ParameterizedTypeName) {
javaName = ((ParameterizedTypeName) typeName).rawType;
} else {
throw new IllegalArgumentException("Unexpected typeName :" + typeName);
}
return type instanceof EnumType ? javaName.peerClass(javaName.simpleName() + "Adapter") : javaName.peerClass("Abstract" + javaName.simpleName() + "Adapter");
}
Aggregations