Search in sources :

Example 66 with TypeName

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

the class JavaGenerator method fieldType.

private TypeName fieldType(Field field) {
    ProtoType type = field.type();
    if (type.isMap()) {
        return ParameterizedTypeName.get(ClassName.get(Map.class), typeName(type.keyType()), typeName(type.valueType()));
    }
    TypeName messageType = typeName(type);
    return field.isRepeated() ? listOf(messageType) : messageType;
}
Also used : ProtoType(com.squareup.wire.schema.ProtoType) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap)

Example 67 with TypeName

use of com.squareup.javapoet.TypeName in project graylog2-server by Graylog2.

the class WithBeanGetterExtension method generateGetterMethod.

private MethodSpec generateGetterMethod(String name, ExecutableElement element) {
    final TypeName returnType = ClassName.get(element.getReturnType());
    final String prefix = isBoolean(returnType) ? "is" : "get";
    final String getterName = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, name);
    final MethodSpec.Builder builder = MethodSpec.methodBuilder(prefix + getterName).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addAnnotation(AnnotationSpec.builder(JsonIgnore.class).build()).addStatement("return $N()", name).returns(returnType);
    // Copy all annotations but @JsonProperty, @JsonIgnore, and @Override to the new method.
    for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
        final TypeName annotationType = ClassName.get(annotationMirror.getAnnotationType());
        if (SKIP_ANNOTATIONS.contains(annotationType)) {
            continue;
        }
        builder.addAnnotation(AnnotationSpec.get(annotationMirror));
    }
    return builder.build();
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec)

Example 68 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 69 with TypeName

use of com.squareup.javapoet.TypeName in project CoreData by FangCloud-Android.

the class BindCursorMethod method bindCursorToField.

private void bindCursorToField(MethodSpec.Builder builder, Element element, String prefix) {
    // 基本类型
    // String
    // 内嵌类型
    // 关联类型
    // 转换类型
    ClassName stringClassName = ClassName.get(String.class);
    TypeName typeName = ClassName.get(element.asType());
    Element typeElement = processingEnv.getTypeUtils().asElement(element.asType());
    if (typeName.isPrimitive() || typeName.isBoxedPrimitive() || typeName.equals(stringClassName)) {
        // 基础类型 // 封装基础类型 // 字符串类型
        builder.addStatement(Utils.methodSetFormat(element, prefix), cursorGetMethod(element, Utils.getDbType(element)));
    } else {
        Embedded embedded = element.getAnnotation(Embedded.class);
        if (embedded != null) {
            // 内嵌类型循环添加
            // 创建临时变量
            String embeddedTempName = "__" + element.getSimpleName() + "Temp";
            builder.addStatement("$T $N = new $T()", typeName, embeddedTempName, typeName);
            // 赋值
            List<Element> elementsForDbEmbedded = new ArrayList<>();
            Utils.fillElementsForDbAndReturnPrimaryKey(processingEnv, elementsForDbEmbedded, (TypeElement) typeElement);
            ;
            for (Element eleEmbeddedChild : elementsForDbEmbedded) {
                bindCursorToField(builder, eleEmbeddedChild, embeddedTempName);
            }
            builder.addStatement(Utils.methodSetFormat(element, prefix), embeddedTempName);
            return;
        }
        Convert convert = element.getAnnotation(Convert.class);
        if (convert != null) {
            ClassName classConverter = ClassName.bestGuess(Utils.getConverterType(convert).toString());
            TypeName convertDbType = Utils.getConvertDbType(convert);
            if (!convertDbType.isPrimitive()) {
                builder.addCode("if(!cursor.isNull(cursorIndexOf$N)){\n  ", Utils.getColumnName(element));
            }
            builder.addStatement(Utils.methodSetFormat(element, prefix), Utils.converterName(element, classConverter) + ".convertToValue(" + cursorGetMethod(element, Utils.getDbType(element)) + ")");
            if (!convertDbType.isPrimitive()) {
                builder.addCode("}");
            }
            return;
        }
        Relation relation = element.getAnnotation(Relation.class);
        if (relation != null) {
            TypeElement typeRelation = (TypeElement) processingEnv.getTypeUtils().asElement(element.asType());
            EntityDetail relationEntityDetail = EntityDetail.parse(processingEnv, typeRelation);
            Element primaryKeyElement = relationEntityDetail.getPrimaryKey();
            if (primaryKeyElement != null) {
                TypeName typePrimary = ClassName.get(primaryKeyElement.asType());
                String columnName = Utils.getColumnName(element);
                if (!typePrimary.isPrimitive()) {
                    builder.addCode("if(!cursor.isNull(cursorIndexOf$N)){\n  ", columnName);
                }
                builder.addStatement("__$NMap.put($N, entity)", columnName, cursorGetMethod(element, Utils.getDbType(primaryKeyElement)));
                if (!typePrimary.isPrimitive()) {
                    builder.addCode("}");
                }
            }
            return;
        }
    }
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) Relation(com.coredata.annotation.Relation) Convert(com.coredata.annotation.Convert) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ClassName(com.squareup.javapoet.ClassName) ArrayList(java.util.ArrayList) Embedded(com.coredata.annotation.Embedded) EntityDetail(com.coredata.compiler.EntityDetail)

Example 70 with TypeName

use of com.squareup.javapoet.TypeName in project CoreData by FangCloud-Android.

the class BindCursorMethod method cursorGetMethod.

private String cursorGetMethod(Element element, TypeName dbType) {
    // TypeName typeName = Utils.getDbType(element);
    TypeName unbox = null;
    try {
        unbox = dbType.unbox();
    } catch (Exception ignored) {
    }
    String getMethod = null;
    if (unbox != null) {
        if (unbox == TypeName.BYTE) {
            getMethod = "getInt";
        } else if (unbox == TypeName.SHORT) {
            getMethod = "getShort";
        } else if (unbox == TypeName.INT) {
            getMethod = "getInt";
        } else if (unbox == TypeName.LONG) {
            getMethod = "getLong";
        } else if (unbox == TypeName.LONG) {
            getMethod = "getLong";
        } else if (unbox == TypeName.CHAR) {
            getMethod = "getInt";
        } else if (unbox == TypeName.FLOAT) {
            getMethod = "getFloat";
        } else if (unbox == TypeName.FLOAT) {
            getMethod = "getDouble";
        } else if (unbox == TypeName.BOOLEAN) {
            return String.format("cursor.getInt(cursorIndexOf%s) != 0 ? true : false", Utils.getColumnName(element));
        }
    } else {
        getMethod = "getString";
    }
    return String.format("cursor.%s(cursorIndexOf%s)", getMethod, Utils.getColumnName(element));
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName)

Aggregations

TypeName (com.squareup.javapoet.TypeName)75 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)62 ClassName (com.squareup.javapoet.ClassName)35 MethodSpec (com.squareup.javapoet.MethodSpec)27 TypeSpec (com.squareup.javapoet.TypeSpec)22 TypeElement (javax.lang.model.element.TypeElement)21 TypeMirror (javax.lang.model.type.TypeMirror)19 ArrayList (java.util.ArrayList)15 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)14 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)10 CodeBlock (com.squareup.javapoet.CodeBlock)10 Map (java.util.Map)10 FieldSpec (com.squareup.javapoet.FieldSpec)9 WireField (com.squareup.wire.WireField)9 Field (com.squareup.wire.schema.Field)9 List (java.util.List)9 HashSet (java.util.HashSet)7 Set (java.util.Set)7 Element (javax.lang.model.element.Element)7 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)6