Search in sources :

Example 6 with Type

use of com.squareup.wire.schema.Type 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 7 with Type

use of com.squareup.wire.schema.Type in project wire by square.

the class WireCompiler method compile.

void compile() throws IOException {
    SchemaLoader schemaLoader = new SchemaLoader();
    for (String protoPath : protoPaths) {
        schemaLoader.addSource(fs.getPath(protoPath));
    }
    for (String sourceFileName : sourceFileNames) {
        schemaLoader.addProto(sourceFileName);
    }
    Schema schema = schemaLoader.load();
    String profileName = emitAndroid ? "android" : "java";
    Profile profile = new ProfileLoader(profileName).schema(schema).load();
    if (!identifierSet.isEmpty()) {
        log.info("Analyzing dependencies of root types.");
        schema = schema.prune(identifierSet);
        for (String rule : identifierSet.unusedIncludes()) {
            log.info("Unused include: " + rule);
        }
        for (String rule : identifierSet.unusedExcludes()) {
            log.info("Unused exclude: " + rule);
        }
    }
    JavaGenerator javaGenerator = JavaGenerator.get(schema).withProfile(profile).withAndroid(emitAndroid).withCompact(emitCompact);
    ConcurrentLinkedQueue<Type> types = new ConcurrentLinkedQueue<>();
    for (ProtoFile protoFile : schema.protoFiles()) {
        // Check if we're skipping files not explicitly named.
        if (!sourceFileNames.isEmpty() && !sourceFileNames.contains(protoFile.location().path())) {
            if (namedFilesOnly || protoFile.location().path().equals(DESCRIPTOR_PROTO))
                continue;
        }
        types.addAll(protoFile.types());
    }
    ExecutorService executor = Executors.newCachedThreadPool();
    List<Future<Void>> futures = new ArrayList<>(MAX_WRITE_CONCURRENCY);
    for (int i = 0; i < MAX_WRITE_CONCURRENCY; ++i) {
        futures.add(i, executor.submit(new JavaFileWriter(javaGenerator, types)));
    }
    executor.shutdown();
    try {
        for (Future<Void> future : futures) {
            future.get();
        }
    } catch (ExecutionException e) {
        throw new IOException(e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : SchemaLoader(com.squareup.wire.schema.SchemaLoader) ProfileLoader(com.squareup.wire.java.ProfileLoader) Schema(com.squareup.wire.schema.Schema) ProtoFile(com.squareup.wire.schema.ProtoFile) ArrayList(java.util.ArrayList) JavaGenerator(com.squareup.wire.java.JavaGenerator) IOException(java.io.IOException) Profile(com.squareup.wire.java.Profile) Type(com.squareup.wire.schema.Type) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ExecutionException(java.util.concurrent.ExecutionException)

Example 8 with Type

use of com.squareup.wire.schema.Type 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)

Aggregations

Type (com.squareup.wire.schema.Type)8 ClassName (com.squareup.javapoet.ClassName)6 TypeSpec (com.squareup.javapoet.TypeSpec)5 ProtoType (com.squareup.wire.schema.ProtoType)5 EnclosingType (com.squareup.wire.schema.EnclosingType)4 EnumType (com.squareup.wire.schema.EnumType)4 MessageType (com.squareup.wire.schema.MessageType)4 JavaGenerator (com.squareup.wire.java.JavaGenerator)3 ProtoFile (com.squareup.wire.schema.ProtoFile)3 Schema (com.squareup.wire.schema.Schema)3 Stopwatch (com.google.common.base.Stopwatch)2 NameAllocator (com.squareup.javapoet.NameAllocator)2 Profile (com.squareup.wire.java.Profile)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ByteString (okio.ByteString)2 FieldSpec (com.squareup.javapoet.FieldSpec)1 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)1 TypeName (com.squareup.javapoet.TypeName)1 WireField (com.squareup.wire.WireField)1