Search in sources :

Example 81 with ClassName

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

the class InjectAdapterProcessor method generateStaticInjection.

/**
   * Write a companion class for {@code type} that extends {@link StaticInjection}.
   */
private void generateStaticInjection(TypeElement type, List<Element> fields) throws IOException {
    ClassName typeName = ClassName.get(type);
    ClassName adapterClassName = adapterName(ClassName.get(type), STATIC_INJECTION_SUFFIX);
    TypeSpec.Builder result = TypeSpec.classBuilder(adapterClassName.simpleName()).addOriginatingElement(type).addJavadoc(AdapterJavadocs.STATIC_INJECTION_TYPE, type).addModifiers(PUBLIC, FINAL).superclass(StaticInjection.class);
    for (Element field : fields) {
        result.addField(memberBindingField(false, field));
    }
    result.addMethod(attachMethod(null, fields, false, typeName, null, true));
    result.addMethod(staticInjectMethod(fields, typeName));
    String packageName = getPackage(type).getQualifiedName().toString();
    JavaFile javaFile = JavaFile.builder(packageName, result.build()).addFileComment(AdapterJavadocs.GENERATED_BY_DAGGER).build();
    javaFile.writeTo(processingEnv.getFiler());
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ClassName(com.squareup.javapoet.ClassName) JavaFile(com.squareup.javapoet.JavaFile) Util.rawTypeToString(dagger.internal.codegen.Util.rawTypeToString) Util.elementToString(dagger.internal.codegen.Util.elementToString) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 82 with ClassName

use of com.squareup.javapoet.ClassName 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 83 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 84 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 85 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)

Aggregations

ClassName (com.squareup.javapoet.ClassName)89 TypeSpec (com.squareup.javapoet.TypeSpec)56 MethodSpec (com.squareup.javapoet.MethodSpec)37 TypeElement (javax.lang.model.element.TypeElement)32 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)30 TypeName (com.squareup.javapoet.TypeName)25 Builder (com.squareup.javapoet.TypeSpec.Builder)16 TypeMirror (javax.lang.model.type.TypeMirror)15 Element (javax.lang.model.element.Element)12 ExecutableElement (javax.lang.model.element.ExecutableElement)11 NotNull (org.jetbrains.annotations.NotNull)10 CodeBlock (com.squareup.javapoet.CodeBlock)9 FieldSpec (com.squareup.javapoet.FieldSpec)9 ArrayList (java.util.ArrayList)9 VariableElement (javax.lang.model.element.VariableElement)9 Map (java.util.Map)8 JavaFile (com.squareup.javapoet.JavaFile)7 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)7 IOException (java.io.IOException)7 ProtoType (com.squareup.wire.schema.ProtoType)6