use of com.squareup.javapoet.ClassName in project wire by square.
the class JavaGenerator method generateMessage.
/** @deprecated Use {@link #generateType(Type)} */
@Deprecated
public TypeSpec generateMessage(MessageType type) {
NameAllocator nameAllocator = nameAllocators.getUnchecked(type);
ClassName javaType = (ClassName) typeName(type.type());
ClassName builderJavaType = javaType.nestedClass("Builder");
TypeSpec.Builder builder = TypeSpec.classBuilder(javaType.simpleName());
builder.addModifiers(PUBLIC, FINAL);
if (javaType.enclosingClassName() != null) {
builder.addModifiers(STATIC);
}
if (!type.documentation().isEmpty()) {
builder.addJavadoc("$L\n", sanitizeJavadoc(type.documentation()));
}
ClassName messageType = emitAndroid ? ANDROID_MESSAGE : MESSAGE;
builder.superclass(messageOf(messageType, javaType, builderJavaType));
String adapterName = nameAllocator.get("ADAPTER");
String protoAdapterName = "ProtoAdapter_" + javaType.simpleName();
String protoAdapterClassName = nameAllocator.newName(protoAdapterName);
ClassName adapterJavaType = javaType.nestedClass(protoAdapterClassName);
builder.addField(messageAdapterField(adapterName, javaType, adapterJavaType));
if (emitAndroid) {
TypeName creatorType = creatorOf(javaType);
String creatorName = nameAllocator.get("CREATOR");
builder.addField(FieldSpec.builder(creatorType, creatorName, PUBLIC, STATIC, FINAL).initializer("$T.newCreator($L)", ANDROID_MESSAGE, adapterName).build());
}
builder.addField(FieldSpec.builder(TypeName.LONG, nameAllocator.get("serialVersionUID")).addModifiers(PRIVATE, STATIC, FINAL).initializer("$LL", 0L).build());
FieldSpec messageOptions = optionsField(MESSAGE_OPTIONS, nameAllocator.get("MESSAGE_OPTIONS"), type.options());
if (messageOptions != null) {
builder.addField(messageOptions);
}
for (Field field : type.fieldsAndOneOfFields()) {
String fieldName = nameAllocator.get(field);
String optionsFieldName = "FIELD_OPTIONS_" + fieldName.toUpperCase(Locale.US);
FieldSpec fieldOptions = optionsField(FIELD_OPTIONS, optionsFieldName, field.options());
if (fieldOptions != null) {
builder.addField(fieldOptions);
}
}
for (Field field : type.fieldsAndOneOfFields()) {
TypeName fieldJavaType = fieldType(field);
if ((field.type().isScalar() || isEnum(field.type())) && !field.isRepeated() && !field.isPacked()) {
builder.addField(defaultField(nameAllocator, field, fieldJavaType));
}
String fieldName = nameAllocator.get(field);
FieldSpec.Builder fieldBuilder = FieldSpec.builder(fieldJavaType, fieldName, PUBLIC, FINAL);
fieldBuilder.addAnnotation(wireFieldAnnotation(field));
if (!field.documentation().isEmpty()) {
fieldBuilder.addJavadoc("$L\n", sanitizeJavadoc(field.documentation()));
}
if (field.isExtension()) {
fieldBuilder.addJavadoc("Extension source: $L\n", field.location().withPathOnly());
}
if (field.isDeprecated()) {
fieldBuilder.addAnnotation(Deprecated.class);
}
if (emitAndroid && field.isOptional()) {
fieldBuilder.addAnnotation(NULLABLE);
}
builder.addField(fieldBuilder.build());
}
builder.addMethod(messageFieldsConstructor(nameAllocator, type));
builder.addMethod(messageFieldsAndUnknownFieldsConstructor(nameAllocator, type));
builder.addMethod(newBuilder(nameAllocator, type));
builder.addMethod(messageEquals(nameAllocator, type));
builder.addMethod(messageHashCode(nameAllocator, type));
if (!emitCompact) {
builder.addMethod(messageToString(nameAllocator, type));
}
builder.addType(builder(nameAllocator, type, javaType, builderJavaType));
for (Type nestedType : type.nestedTypes()) {
builder.addType(generateType(nestedType));
}
if (!emitCompact) {
// Add the ProtoAdapter implementation at the very bottom since it's ugly serialization code.
builder.addType(messageAdapter(nameAllocator, type, javaType, adapterJavaType, builderJavaType));
}
return builder.build();
}
use of com.squareup.javapoet.ClassName in project wire by square.
the class WireGenerateSourcesMojo method execute.
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
// Add the directory into which generated sources are placed as a compiled source root.
project.addCompileSourceRoot(generatedSourceDirectory);
try {
List<String> directories = protoPaths != null && protoPaths.length > 0 ? Arrays.asList(protoPaths) : Collections.singletonList(protoSourceDirectory);
List<String> protoFilesList = Arrays.asList(protoFiles);
Schema schema = loadSchema(directories, protoFilesList);
Profile profile = loadProfile(schema);
IdentifierSet identifierSet = identifierSet();
if (!identifierSet.isEmpty()) {
schema = retainRoots(identifierSet, schema);
}
JavaGenerator javaGenerator = JavaGenerator.get(schema).withAndroid(emitAndroid).withCompact(emitCompact).withProfile(profile);
for (ProtoFile protoFile : schema.protoFiles()) {
if (!protoFilesList.isEmpty() && !protoFilesList.contains(protoFile.location().path())) {
// Don't emit anything for files not explicitly compiled.
continue;
}
for (Type type : protoFile.types()) {
Stopwatch stopwatch = Stopwatch.createStarted();
TypeSpec typeSpec = javaGenerator.generateType(type);
ClassName javaTypeName = javaGenerator.generatedTypeName(type);
writeJavaFile(javaTypeName, typeSpec, type.location().withPathOnly());
getLog().info(String.format("Generated %s in %s", javaTypeName, stopwatch));
}
}
} catch (Exception e) {
throw new MojoExecutionException("Wire Plugin: Failure compiling proto sources.", e);
}
}
use of com.squareup.javapoet.ClassName in project wire by square.
the class JavaGenerator method newBuilder.
// Example:
//
// @Override
// public Message.Builder newBuilder() {
// Builder builder = new Builder();
// builder.optional_int32 = optional_int32;
// ...
// builder.addUnknownFields(unknownFields());
// return builder;
// }
private MethodSpec newBuilder(NameAllocator nameAllocator, MessageType message) {
NameAllocator localNameAllocator = nameAllocator.clone();
String builderName = localNameAllocator.newName("builder");
ClassName javaType = (ClassName) typeName(message.type());
ClassName builderJavaType = javaType.nestedClass("Builder");
MethodSpec.Builder result = MethodSpec.methodBuilder("newBuilder").addAnnotation(Override.class).addModifiers(PUBLIC).returns(builderJavaType).addStatement("$1T $2L = new $1T()", builderJavaType, builderName);
List<Field> fields = message.fieldsAndOneOfFields();
for (Field field : fields) {
String fieldName = localNameAllocator.get(field);
if (field.isRepeated() || field.type().isMap()) {
result.addStatement("$1L.$2L = $3T.copyOf($2S, $2L)", builderName, fieldName, Internal.class);
} else {
result.addStatement("$1L.$2L = $2L", builderName, fieldName);
}
}
result.addStatement("$L.addUnknownFields(unknownFields())", builderName);
result.addStatement("return $L", builderName);
return result.build();
}
use of com.squareup.javapoet.ClassName in project wire by square.
the class ServiceGenerator method api.
public TypeSpec api(Service service) {
ClassName apiName = (ClassName) javaGenerator.typeName(service.type());
TypeSpec.Builder typeBuilder = TypeSpec.interfaceBuilder(apiName.simpleName());
typeBuilder.addModifiers(PUBLIC);
if (!service.documentation().isEmpty()) {
typeBuilder.addJavadoc("$L\n", service.documentation());
}
for (Rpc rpc : service.rpcs()) {
ProtoType requestType = rpc.requestType();
TypeName requestJavaType = javaGenerator.typeName(requestType);
ProtoType responseType = rpc.responseType();
TypeName responseJavaType = javaGenerator.typeName(responseType);
MethodSpec.Builder rpcBuilder = MethodSpec.methodBuilder(rpc.name());
rpcBuilder.addModifiers(PUBLIC, ABSTRACT);
rpcBuilder.returns(responseJavaType);
rpcBuilder.addParameter(requestJavaType, "request");
if (!rpc.documentation().isEmpty()) {
rpcBuilder.addJavadoc("$L\n", rpc.documentation());
}
typeBuilder.addMethod(rpcBuilder.build());
}
return typeBuilder.build();
}
use of com.squareup.javapoet.ClassName in project requery by requery.
the class EntityPartGenerator method parameterizedCollectionName.
ParameterizedTypeName parameterizedCollectionName(TypeMirror typeMirror) {
TypeMirror genericType = tryFirstTypeArgument(typeMirror);
TypeName elementName = nameResolver.tryGeneratedTypeName(genericType);
TypeElement collectionElement = (TypeElement) types.asElement(typeMirror);
ClassName collectionName = ClassName.get(collectionElement);
return ParameterizedTypeName.get(collectionName, elementName);
}
Aggregations