Search in sources :

Example 51 with TypeName

use of com.squareup.javapoet.TypeName in project RxCache by VictorAlbertos.

the class BrewJavaFile method getActionProvider.

private MethodSpec getActionProvider(ClassName providersClassName, ProvidersClass.Method method) {
    String methodName = method.name;
    TypeName list = ClassName.get(method.enclosingTypeObservable);
    TypeName enclosingTypeList = ClassName.get(method.enclosingTypeObservable.getTypeArguments().get(0));
    String enclosingTypeListName = enclosingTypeList.toString();
    ParameterizedTypeName action = ParameterizedTypeName.get(ClassName.get(ActionsList.class), enclosingTypeList);
    ParameterizedTypeName evict = ParameterizedTypeName.get(ClassName.get(ActionsList.Evict.class), enclosingTypeList);
    ParameterizedTypeName arrayList = ParameterizedTypeName.get(ClassName.get(ArrayList.class), enclosingTypeList);
    ParameterizedTypeName observable = ParameterizedTypeName.get(ClassName.get(Observable.class), list);
    ParameterSpec rxProvidersInstance = ParameterSpec.builder(providersClassName, "proxy").addModifiers(Modifier.FINAL).build();
    MethodSpec.Builder builder = MethodSpec.methodBuilder(methodName).addModifiers(Modifier.PUBLIC, Modifier.STATIC).addParameter(rxProvidersInstance).returns(action);
    if (method.hasDynamicKey) {
        builder.addParameter(DynamicKey.class, "dynamicKey", Modifier.FINAL);
    } else if (method.hasDynamicKeyGroup) {
        builder.addParameter(DynamicKeyGroup.class, "dynamicKeyGroup", Modifier.FINAL);
    }
    builder.beginControlFlow("$T evict = new $T()", evict, evict).beginControlFlow("@Override public $T call($T elements)", observable, observable);
    if (method.hasDynamicKey) {
        setReturnEvictForEvictDynamicKey(builder, methodName);
    } else if (method.hasDynamicKeyGroup) {
        setReturnEvictForEvictDynamicKeyGroup(builder, methodName);
    } else {
        setReturnEvictForEvictProvider(builder, methodName);
    }
    builder.endControlFlow().endControlFlow(";");
    if (method.hasDynamicKey) {
        setCacheForEvictDynamicKey(builder, observable, arrayList, methodName, enclosingTypeListName);
    } else if (method.hasDynamicKeyGroup) {
        setCacheForEvictDynamicKeyGroup(builder, observable, arrayList, methodName, enclosingTypeListName);
    } else {
        setCacheForEvictProvider(builder, observable, arrayList, methodName, enclosingTypeListName);
    }
    builder.addStatement("return ActionsList.with(evict, oCache)");
    return builder.build();
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec) ArrayList(java.util.ArrayList) Observable(io.reactivex.Observable) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 52 with TypeName

use of com.squareup.javapoet.TypeName 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 53 with TypeName

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

the class JavaGenerator method optionsField.

// Example:
//
// public static final FieldOptions FIELD_OPTIONS_FOO = new FieldOptions.Builder()
//     .setExtension(Ext_custom_options.count, 42)
//     .build();
//
private FieldSpec optionsField(ProtoType optionsType, String fieldName, Options options) {
    TypeName optionsJavaType = typeName(optionsType);
    CodeBlock.Builder initializer = CodeBlock.builder();
    initializer.add("$[new $T.Builder()", optionsJavaType);
    boolean empty = true;
    for (Map.Entry<ProtoMember, ?> entry : options.map().entrySet()) {
        if (entry.getKey().equals(FIELD_DEPRECATED) || entry.getKey().equals(PACKED)) {
            continue;
        }
        Field optionField = schema.getField(entry.getKey());
        initializer.add("\n.$L($L)", fieldName(optionsType, optionField), fieldInitializer(optionField.type(), entry.getValue()));
        empty = false;
    }
    initializer.add("\n.build()$]");
    if (empty)
        return null;
    return FieldSpec.builder(optionsJavaType, fieldName).addModifiers(PUBLIC, STATIC, FINAL).initializer(initializer.build()).build();
}
Also used : WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) CodeBlock(com.squareup.javapoet.CodeBlock) ProtoMember(com.squareup.wire.schema.ProtoMember) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap)

Example 54 with TypeName

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

the class JavaGenerator method messageEquals.

// Example:
//
// @Override
// public boolean equals(Object other) {
//   if (other == this) return true;
//   if (!(other instanceof SimpleMessage)) return false;
//   SimpleMessage o = (SimpleMessage) other;
//   return equals(unknownFields(), o.unknownFields())
//       && equals(optional_int32, o.optional_int32);
//
private MethodSpec messageEquals(NameAllocator nameAllocator, MessageType type) {
    NameAllocator localNameAllocator = nameAllocator.clone();
    String otherName = localNameAllocator.newName("other");
    String oName = localNameAllocator.newName("o");
    TypeName javaType = typeName(type.type());
    MethodSpec.Builder result = MethodSpec.methodBuilder("equals").addAnnotation(Override.class).addModifiers(PUBLIC).returns(boolean.class).addParameter(Object.class, otherName);
    List<Field> fields = type.fieldsAndOneOfFields();
    if (fields.isEmpty()) {
        result.addStatement("return $N instanceof $T", otherName, javaType);
        return result.build();
    }
    result.addStatement("if ($N == this) return true", otherName);
    result.addStatement("if (!($N instanceof $T)) return false", otherName, javaType);
    result.addStatement("$T $N = ($T) $N", javaType, oName, javaType, otherName);
    result.addCode("$[return unknownFields().equals($N.unknownFields())", oName);
    for (Field field : fields) {
        String fieldName = localNameAllocator.get(field);
        if (field.isRequired() || field.isRepeated() || field.type().isMap()) {
            result.addCode("\n&& $1L.equals($2N.$1L)", fieldName, oName);
        } else {
            result.addCode("\n&& $1T.equals($2L, $3N.$2L)", Internal.class, fieldName, oName);
        }
    }
    result.addCode(";\n$]");
    return result.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) MethodSpec(com.squareup.javapoet.MethodSpec) ByteString(okio.ByteString)

Example 55 with TypeName

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

the class JavaGenerator method messageFieldsConstructor.

// Example:
//
// public SimpleMessage(int optional_int32, long optional_int64) {
//   this(builder.optional_int32, builder.optional_int64, null);
// }
//
private MethodSpec messageFieldsConstructor(NameAllocator nameAllocator, MessageType type) {
    MethodSpec.Builder result = MethodSpec.constructorBuilder();
    result.addModifiers(PUBLIC);
    result.addCode("this(");
    for (Field field : type.fieldsAndOneOfFields()) {
        TypeName javaType = fieldType(field);
        String fieldName = nameAllocator.get(field);
        ParameterSpec.Builder param = ParameterSpec.builder(javaType, fieldName);
        if (emitAndroid && field.isOptional()) {
            param.addAnnotation(NULLABLE);
        }
        result.addParameter(param.build());
        result.addCode("$L, ", fieldName);
    }
    result.addCode("$T.EMPTY);\n", BYTE_STRING);
    return result.build();
}
Also used : WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) ByteString(okio.ByteString)

Aggregations

TypeName (com.squareup.javapoet.TypeName)63 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)54 ClassName (com.squareup.javapoet.ClassName)30 MethodSpec (com.squareup.javapoet.MethodSpec)27 TypeSpec (com.squareup.javapoet.TypeSpec)22 TypeMirror (javax.lang.model.type.TypeMirror)17 TypeElement (javax.lang.model.element.TypeElement)16 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)14 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)10 Map (java.util.Map)10 CodeBlock (com.squareup.javapoet.CodeBlock)9 FieldSpec (com.squareup.javapoet.FieldSpec)9 WireField (com.squareup.wire.WireField)9 Field (com.squareup.wire.schema.Field)9 ArrayList (java.util.ArrayList)9 HashSet (java.util.HashSet)7 List (java.util.List)7 Set (java.util.Set)7 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)6 LinkedHashMap (java.util.LinkedHashMap)6