Search in sources :

Example 31 with ClassName

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();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) Type(com.squareup.wire.schema.Type) ProtoType(com.squareup.wire.schema.ProtoType) MessageType(com.squareup.wire.schema.MessageType) EnclosingType(com.squareup.wire.schema.EnclosingType) EnumType(com.squareup.wire.schema.EnumType) ClassName(com.squareup.javapoet.ClassName) ByteString(okio.ByteString) FieldSpec(com.squareup.javapoet.FieldSpec) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 32 with ClassName

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);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Schema(com.squareup.wire.schema.Schema) ProtoFile(com.squareup.wire.schema.ProtoFile) Stopwatch(com.google.common.base.Stopwatch) JavaGenerator(com.squareup.wire.java.JavaGenerator) IdentifierSet(com.squareup.wire.schema.IdentifierSet) Profile(com.squareup.wire.java.Profile) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Type(com.squareup.wire.schema.Type) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 33 with ClassName

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();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName) ByteString(okio.ByteString)

Example 34 with ClassName

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();
}
Also used : ProtoType(com.squareup.wire.schema.ProtoType) TypeName(com.squareup.javapoet.TypeName) Rpc(com.squareup.wire.schema.Rpc) MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 35 with ClassName

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);
}
Also used : TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ClassName(com.squareup.javapoet.ClassName)

Aggregations

ClassName (com.squareup.javapoet.ClassName)99 TypeSpec (com.squareup.javapoet.TypeSpec)60 MethodSpec (com.squareup.javapoet.MethodSpec)41 TypeElement (javax.lang.model.element.TypeElement)37 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)35 TypeName (com.squareup.javapoet.TypeName)30 Element (javax.lang.model.element.Element)19 TypeMirror (javax.lang.model.type.TypeMirror)17 Builder (com.squareup.javapoet.TypeSpec.Builder)16 FieldSpec (com.squareup.javapoet.FieldSpec)15 ArrayList (java.util.ArrayList)15 CodeBlock (com.squareup.javapoet.CodeBlock)11 ExecutableElement (javax.lang.model.element.ExecutableElement)11 NotNull (org.jetbrains.annotations.NotNull)10 VariableElement (javax.lang.model.element.VariableElement)9 JavaFile (com.squareup.javapoet.JavaFile)8 Map (java.util.Map)8 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)7 IOException (java.io.IOException)7 HashMap (java.util.HashMap)7