Search in sources :

Example 91 with ClassName

use of com.squareup.javapoet.ClassName in project wire by square.

the class JavaGenerator method generateAbstractAdapter.

/** Returns an abstract adapter for {@code type}. */
public TypeSpec generateAbstractAdapter(MessageType type) {
    NameAllocator nameAllocator = nameAllocators.getUnchecked(type);
    ClassName adapterTypeName = abstractAdapterName(type.type());
    ClassName typeName = (ClassName) typeName(type.type());
    TypeSpec.Builder adapter = messageAdapter(nameAllocator, type, typeName, adapterTypeName, null).toBuilder();
    if (adapterTypeName.enclosingClassName() != null) {
        adapter.addModifiers(STATIC);
    }
    for (Type nestedType : type.nestedTypes()) {
        if (profile.getAdapter(nestedType.type()) == null) {
            throw new IllegalArgumentException("Missing custom proto adapter for " + nestedType.type().enclosingTypeOrPackage() + "." + nestedType.type().simpleName() + " when enclosing proto has custom proto adapter.");
        }
        if (nestedType instanceof MessageType) {
            adapter.addType(generateAbstractAdapter((MessageType) nestedType));
        }
    }
    return adapter.build();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) 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) MessageType(com.squareup.wire.schema.MessageType) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 92 with ClassName

use of com.squareup.javapoet.ClassName 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.
   */
public ClassName abstractAdapterName(ProtoType protoType) {
    TypeName profileJavaName = profile.getTarget(protoType);
    if (profileJavaName == null)
        return null;
    ClassName javaName = nameToJavaName.get(protoType);
    return javaName.peerClass("Abstract" + javaName.simpleName() + "Adapter");
}
Also used : TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ClassName(com.squareup.javapoet.ClassName)

Example 93 with ClassName

use of com.squareup.javapoet.ClassName in project wire by square.

the class JavaGenerator method generateEnclosingType.

private TypeSpec generateEnclosingType(EnclosingType type) {
    ClassName javaType = (ClassName) typeName(type.type());
    TypeSpec.Builder builder = TypeSpec.classBuilder(javaType.simpleName()).addModifiers(PUBLIC, FINAL);
    if (javaType.enclosingClassName() != null) {
        builder.addModifiers(STATIC);
    }
    String documentation = type.documentation();
    if (!documentation.isEmpty()) {
        documentation += "\n\n<p>";
    }
    documentation += "<b>NOTE:</b> This type only exists to maintain class structure" + " for its nested types and is not an actual message.\n";
    builder.addJavadoc(documentation);
    builder.addMethod(MethodSpec.constructorBuilder().addModifiers(PRIVATE).addStatement("throw new $T()", AssertionError.class).build());
    for (Type nestedType : type.nestedTypes()) {
        builder.addType(generateType(nestedType));
    }
    return builder.build();
}
Also used : 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) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 94 with ClassName

use of com.squareup.javapoet.ClassName in project wire by square.

the class CodegenSample method execute.

public void execute() throws IOException {
    Schema schema = loadSchema();
    if (!identifierSet.isEmpty()) {
        schema = retainRoots(schema);
    }
    JavaGenerator javaGenerator = JavaGenerator.get(schema);
    ServiceGenerator serviceGenerator = new ServiceGenerator(javaGenerator);
    for (ProtoFile protoFile : schema.protoFiles()) {
        for (Type type : protoFile.types()) {
            Stopwatch stopwatch = Stopwatch.createStarted();
            TypeSpec typeSpec = javaGenerator.generateType(type);
            ClassName javaTypeName = (ClassName) javaGenerator.typeName(type.type());
            writeJavaFile(javaTypeName, typeSpec, type.location(), stopwatch);
        }
        for (Service service : protoFile.services()) {
            Stopwatch stopwatch = Stopwatch.createStarted();
            ClassName javaTypeName = (ClassName) javaGenerator.typeName(service.type());
            TypeSpec typeSpec = serviceGenerator.api(service);
            writeJavaFile(javaTypeName, typeSpec, service.location(), stopwatch);
        }
    }
}
Also used : Type(com.squareup.wire.schema.Type) Schema(com.squareup.wire.schema.Schema) ProtoFile(com.squareup.wire.schema.ProtoFile) Stopwatch(com.google.common.base.Stopwatch) ClassName(com.squareup.javapoet.ClassName) Service(com.squareup.wire.schema.Service) JavaGenerator(com.squareup.wire.java.JavaGenerator) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 95 with ClassName

use of com.squareup.javapoet.ClassName in project wire by square.

the class RepoBuilder method generateCode.

public String generateCode(String typeName, String profile) throws IOException {
    Schema schema = schema();
    JavaGenerator javaGenerator = JavaGenerator.get(schema);
    if (profile != null) {
        javaGenerator = javaGenerator.withProfile(profile(profile));
    }
    Type type = schema.getType(typeName);
    TypeSpec typeSpec = javaGenerator.generateType(type);
    ClassName typeName1 = javaGenerator.generatedTypeName(type);
    return JavaFile.builder(typeName1.packageName(), typeSpec).build().toString();
}
Also used : ClassName(com.squareup.javapoet.ClassName) JavaGenerator(com.squareup.wire.java.JavaGenerator) TypeSpec(com.squareup.javapoet.TypeSpec)

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