Search in sources :

Example 36 with TypeSpec

use of com.squareup.javapoet.TypeSpec in project ActivityRouter by mzule.

the class RouterProcessor method generateDefaultRouterInit.

private void generateDefaultRouterInit() {
    MethodSpec.Builder initMethod = MethodSpec.methodBuilder("init").addModifiers(Modifier.PUBLIC, Modifier.FINAL, Modifier.STATIC);
    initMethod.addStatement("RouterMapping.map()");
    TypeSpec routerInit = TypeSpec.classBuilder("RouterInit").addModifiers(Modifier.PUBLIC, Modifier.FINAL).addMethod(initMethod.build()).build();
    try {
        JavaFile.builder("com.github.mzule.activityrouter.router", routerInit).build().writeTo(filer);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 37 with TypeSpec

use of com.squareup.javapoet.TypeSpec in project ActivityRouter by mzule.

the class RouterProcessor method generateModulesRouterInit.

private void generateModulesRouterInit(String[] moduleNames) {
    MethodSpec.Builder initMethod = MethodSpec.methodBuilder("init").addModifiers(Modifier.PUBLIC, Modifier.FINAL, Modifier.STATIC);
    for (String module : moduleNames) {
        initMethod.addStatement("RouterMapping_" + module + ".map()");
    }
    TypeSpec routerInit = TypeSpec.classBuilder("RouterInit").addModifiers(Modifier.PUBLIC, Modifier.FINAL).addMethod(initMethod.build()).build();
    try {
        JavaFile.builder("com.github.mzule.activityrouter.router", routerInit).build().writeTo(filer);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 38 with TypeSpec

use of com.squareup.javapoet.TypeSpec in project dagger by square.

the class ModuleAdapterProcessor method generateProvidesAdapter.

private TypeSpec generateProvidesAdapter(ClassName moduleClassName, ClassName adapterName, ExecutableElement providerMethod, Map<ExecutableElement, ClassName> methodToClassName, Map<String, AtomicInteger> methodNameToNextId, boolean library) {
    String methodName = providerMethod.getSimpleName().toString();
    TypeMirror moduleType = providerMethod.getEnclosingElement().asType();
    ClassName className = bindingClassName(adapterName, providerMethod, methodToClassName, methodNameToNextId);
    TypeName returnType = Util.injectableType(providerMethod.getReturnType());
    List<? extends VariableElement> parameters = providerMethod.getParameters();
    boolean dependent = !parameters.isEmpty();
    TypeSpec.Builder result = TypeSpec.classBuilder(className.simpleName()).addJavadoc("$L", bindingTypeDocs(returnType, false, false, dependent)).addModifiers(PUBLIC, STATIC, FINAL).superclass(ParameterizedTypeName.get(ClassName.get(ProvidesBinding.class), returnType));
    result.addField(moduleClassName, "module", PRIVATE, FINAL);
    for (Element parameter : parameters) {
        result.addField(bindingOf(parameter.asType()), parameterName(parameter), PRIVATE);
    }
    boolean singleton = providerMethod.getAnnotation(Singleton.class) != null;
    String key = GeneratorKeys.get(providerMethod);
    result.addMethod(MethodSpec.constructorBuilder().addModifiers(PUBLIC).addParameter(moduleClassName, "module").addStatement("super($S, $L, $S, $S)", key, (singleton ? "IS_SINGLETON" : "NOT_SINGLETON"), typeToString(moduleType), methodName).addStatement("this.module = module").addStatement("setLibrary($L)", library).build());
    if (dependent) {
        MethodSpec.Builder attachBuilder = MethodSpec.methodBuilder("attach").addJavadoc(AdapterJavadocs.ATTACH_METHOD).addAnnotation(Override.class).addAnnotation(Util.UNCHECKED).addModifiers(PUBLIC).addParameter(Linker.class, "linker");
        for (VariableElement parameter : parameters) {
            String parameterKey = GeneratorKeys.get(parameter);
            attachBuilder.addStatement("$N = ($T) linker.requestBinding($S, $T.class, getClass().getClassLoader())", parameterName(parameter), bindingOf(parameter.asType()), parameterKey, moduleClassName);
        }
        result.addMethod(attachBuilder.build());
        MethodSpec.Builder getDependenciesBuilder = MethodSpec.methodBuilder("getDependencies").addJavadoc(AdapterJavadocs.GET_DEPENDENCIES_METHOD).addAnnotation(Override.class).addModifiers(PUBLIC).addParameter(Util.SET_OF_BINDINGS, "getBindings").addParameter(Util.SET_OF_BINDINGS, "injectMembersBindings");
        for (Element parameter : parameters) {
            getDependenciesBuilder.addStatement("getBindings.add($N)", parameterName(parameter));
        }
        result.addMethod(getDependenciesBuilder.build());
    }
    MethodSpec.Builder getBuilder = MethodSpec.methodBuilder("get").addJavadoc(AdapterJavadocs.GET_METHOD, returnType).addAnnotation(Override.class).addModifiers(PUBLIC).returns(returnType).addCode("return module.$N(", methodName);
    boolean first = true;
    for (Element parameter : parameters) {
        if (!first)
            getBuilder.addCode(", ");
        getBuilder.addCode("$N.get()", parameterName(parameter));
        first = false;
    }
    getBuilder.addCode(");\n");
    result.addMethod(getBuilder.build());
    return result.build();
}
Also used : TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) MethodSpec(com.squareup.javapoet.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Util.typeToString(dagger.internal.codegen.Util.typeToString) Util.elementToString(dagger.internal.codegen.Util.elementToString) VariableElement(javax.lang.model.element.VariableElement) TypeMirror(javax.lang.model.type.TypeMirror) Singleton(javax.inject.Singleton) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 39 with TypeSpec

use of com.squareup.javapoet.TypeSpec 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 40 with TypeSpec

use of com.squareup.javapoet.TypeSpec 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)

Aggregations

TypeSpec (com.squareup.javapoet.TypeSpec)44 ClassName (com.squareup.javapoet.ClassName)26 MethodSpec (com.squareup.javapoet.MethodSpec)20 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)10 TypeName (com.squareup.javapoet.TypeName)8 NotNull (org.jetbrains.annotations.NotNull)8 FieldSpec (com.squareup.javapoet.FieldSpec)7 ArrayList (java.util.ArrayList)6 Type (com.squareup.wire.schema.Type)5 TypeElement (javax.lang.model.element.TypeElement)5 ByteString (okio.ByteString)5 WireField (com.squareup.wire.WireField)4 JavaGenerator (com.squareup.wire.java.JavaGenerator)4 Field (com.squareup.wire.schema.Field)4 MessageType (com.squareup.wire.schema.MessageType)4 ProtoType (com.squareup.wire.schema.ProtoType)4 Schema (com.squareup.wire.schema.Schema)4 CodeBlock (com.squareup.javapoet.CodeBlock)3 EnclosingType (com.squareup.wire.schema.EnclosingType)3 EnumType (com.squareup.wire.schema.EnumType)3