Search in sources :

Example 66 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project kripton by xcesco.

the class JavadocUtility method generateJavaDocForSelect.

/**
 * Generate java doc for select.
 *
 * @param methodBuilder
 *            the method builder
 * @param sqlParams
 *            the sql params
 * @param method
 *            the method
 * @param annotation
 *            the annotation
 * @param fieldList
 *            the field list
 * @param selectResultType
 *            the select result type
 * @param javadocParts
 *            the javadoc parts
 */
public static void generateJavaDocForSelect(MethodSpec.Builder methodBuilder, List<String> sqlParams, final SQLiteModelMethod method, ModelAnnotation annotation, Set<JQLProjection> projectedColumns, SelectType selectResultType, JavadocPart... javadocParts) {
    final SQLiteEntity entity = method.getEntity();
    TypeName beanTypeName = TypeName.get(entity.getElement().asType());
    String sql = JQLChecker.getInstance().replace(method, method.jql, new JQLReplacerListenerImpl(method) {

        @Override
        public String onColumnName(String columnName) {
            SQLProperty tempProperty = entity.get(columnName);
            AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
            return tempProperty.columnName;
        }

        @Override
        public String onColumnAlias(String alias) {
            SQLProperty tempProperty = entity.findPropertyByName(alias);
            AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, alias);
            return tempProperty.columnName;
        }
    });
    // Set<JQLProjection> projectedColumns =
    // JQLChecker.getInstance().extractProjections(method, method.jql.value,
    // entity);
    methodBuilder.addJavadoc("<h2>Select SQL:</h2>\n\n", annotation.getSimpleName());
    methodBuilder.addJavadoc("<pre>$L</pre>", sql);
    methodBuilder.addJavadoc("\n\n");
    // check for entities
    {
        methodBuilder.addJavadoc("<h2>Mapped class:</h2>\n");
        methodBuilder.addJavadoc("{@link $T}", method.getEntity().getElement());
        methodBuilder.addJavadoc("\n\n");
    }
    // there will be alway some projected column
    {
        methodBuilder.addJavadoc("<h2>Projected columns:</h2>\n");
        methodBuilder.addJavadoc("<dl>\n");
        for (JQLProjection column : projectedColumns) {
            // KRIPTON_DEBUG field info only it exists
            if (column.alias != null) {
                if (column.property != null) {
                    methodBuilder.addJavadoc("\t<dt>$L</dt>", column.property.columnName);
                } else {
                    methodBuilder.addJavadoc("\t<dt>$L</dt>", column.expression);
                }
                // SQLProperty attribute = fieldList.value1.get(i);
                methodBuilder.addJavadoc("<dd>is associated to bean's property <strong>$L</strong></dd>", column.alias);
            } else if (column.column != null) {
                if (column.property != null) {
                    methodBuilder.addJavadoc("\t<dt>$L</dt>", column.property.columnName);
                } else {
                    methodBuilder.addJavadoc("\t<dt>$L</dt>", column.expression);
                }
                // SQLProperty attribute = fieldList.value1.get(i);
                methodBuilder.addJavadoc("<dd>is associated to bean's property <strong>$L</strong></dd>", column.column);
            } else {
                methodBuilder.addJavadoc("\t<dt>$L</dt>", column.expression);
                methodBuilder.addJavadoc("<dd>no bean's property is associated</dd>");
            }
            methodBuilder.addJavadoc("\n");
        }
        methodBuilder.addJavadoc("</dl>");
        methodBuilder.addJavadoc("\n\n");
    }
    // subqueries
    if (method.hasChildrenSelects()) {
        methodBuilder.addJavadoc("<h2>Fields filled by subqueries:</h2>\n");
        methodBuilder.addJavadoc("<dl>\n");
        for (Triple<String, String, SQLiteModelMethod> item : method.childrenSelects) {
            methodBuilder.addJavadoc("\t<dt>$L</dt>", item.value0);
            methodBuilder.addJavadoc("<dd>filled by query {@link $L#$L}</dd>\n", item.value2.getParent().getName(), item.value2.getName());
        }
        methodBuilder.addJavadoc("</dl>");
        methodBuilder.addJavadoc("\n\n");
    }
    // dynamic parts
    if (method.hasDynamicOrderByConditions() || method.hasDynamicWhereConditions() || method.hasDynamicPageSizeConditions()) {
        methodBuilder.addJavadoc("<h2>Method's parameters and associated dynamic parts:</h2>\n");
        methodBuilder.addJavadoc("<dl>\n");
        if (method.hasDynamicWhereConditions()) {
            methodBuilder.addJavadoc("<dt>$L</dt><dd>is part of where conditions resolved at runtime. In above SQL it is displayed as #{$L}</dd>\n", method.dynamicWhereParameterName, JQLDynamicStatementType.DYNAMIC_WHERE);
        }
        if (method.hasDynamicOrderByConditions()) {
            methodBuilder.addJavadoc("<dt>$L</dt>is part of order statement resolved at runtime. In above SQL it is displayed as #{$L}</dd>\n", method.dynamicOrderByParameterName, JQLDynamicStatementType.DYNAMIC_ORDER_BY);
        }
        if (method.hasDynamicPageSizeConditions()) {
            methodBuilder.addJavadoc("<dt>$L</dt>is part of limit statement resolved at runtime. In above SQL it is displayed as #{$L}</dd>\n", method.dynamicPageSizeName, JQLDynamicStatementType.DYNAMIC_PAGE_SIZE);
        }
        methodBuilder.addJavadoc("</dl>");
        methodBuilder.addJavadoc("\n\n");
    }
    // query parameters
    if (sqlParams.size() > 0) {
        methodBuilder.addJavadoc("<h2>Query's parameters:</h2>\n");
        methodBuilder.addJavadoc("<dl>\n");
        for (String param : sqlParams) {
            methodBuilder.addJavadoc("\t<dt>$L</dt><dd>is binded to method's parameter <strong>$L</strong></dd>\n", SqlAnalyzer.PARAM_PREFIX + param + SqlAnalyzer.PARAM_SUFFIX, method.findParameterNameByAlias(param));
        }
        methodBuilder.addJavadoc("</dl>");
        methodBuilder.addJavadoc("\n\n");
    }
    // method params
    ParameterSpec parameterSpec;
    for (Pair<String, TypeName> item : method.getParameters()) {
        parameterSpec = ParameterSpec.builder(item.value1, item.value0).build();
        methodBuilder.addJavadoc("@param $L\n", parameterSpec.name);
        if (beanTypeName.equals(item.value1)) {
            methodBuilder.addJavadoc("\tis used as <code>$L</code>\n", SqlAnalyzer.PARAM_PREFIX + method.findParameterAliasByName(item.value0) + SqlAnalyzer.PARAM_SUFFIX);
        } else if (TypeUtility.isTypeEquals(item.value1, ParameterizedTypeName.get(TypeUtility.className(OnReadBeanListener.class), beanTypeName))) {
            methodBuilder.addJavadoc("\tis the $T listener\n", beanTypeName);
        } else if (TypeUtility.isTypeEquals(item.value1, TypeUtility.className(OnReadCursorListener.class))) {
            methodBuilder.addJavadoc("\tis the cursor listener\n", beanTypeName);
        } else if (item.value0.equals(method.dynamicWhereParameterName)) {
            methodBuilder.addJavadoc("\tis used as <strong>dynamic WHERE statement</strong> and it is formatted by ({@link $T#format})\n", StringUtils.class);
        } else if (item.value0.equals(method.dynamicOrderByParameterName)) {
            methodBuilder.addJavadoc("\tis used as <strong>dynamic ORDER BY statement</strong> and it is formatted by ({@link $T#format})\n", StringUtils.class);
        } else if (item.value0.equals(method.dynamicPageSizeName)) {
            methodBuilder.addJavadoc("\tis used as <strong>dynamic LIMIT statement</strong> and it is formatted by ({@link $T#format})\n", StringUtils.class);
        } else {
            methodBuilder.addJavadoc("\tis binded to <code>$L</code>\n", SqlAnalyzer.PARAM_PREFIX + method.findParameterAliasByName(item.value0) + SqlAnalyzer.PARAM_SUFFIX);
        }
    }
    for (JavadocPart item : javadocParts) {
        if (item.javadocPartType != JavadocPartType.ADD_PARAMETER)
            continue;
        methodBuilder.addJavadoc("@param $L\n", item.name);
        methodBuilder.addJavadoc("\t$L\n", item.description);
    }
    for (JavadocPart item : javadocParts) {
        if (item.javadocPartType != JavadocPartType.RETURN)
            continue;
        methodBuilder.addJavadoc("@return $L\n", item.description);
        // override return
        return;
    }
    // return type
    switch(selectResultType) {
        case BEAN:
            methodBuilder.addJavadoc("@return selected bean or <code>null</code>.\n");
            break;
        case CURSOR:
            methodBuilder.addJavadoc("@return cursor. Closing the cursor is delegated to the calling code.\n");
            break;
        case LIST_BEAN:
            if (entity.isImmutablePojo()) {
                methodBuilder.addJavadoc("@return collection of bean or empty collection. If result type is List, it will be generated as <strong>immutable list</strong>.\n");
            } else {
                methodBuilder.addJavadoc("@return collection of bean or empty collection.\n");
            }
            break;
        case LIST_SCALAR:
            methodBuilder.addJavadoc("@return collection of single value extracted by query.\n");
            break;
        case SCALAR:
            methodBuilder.addJavadoc("@return single value extracted by query.\n");
            break;
        case PAGED_RESULT:
            methodBuilder.addJavadoc("@return paginated result.\n");
            break;
        default:
            // case LISTENER_CURSOR:
            break;
    }
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) OnReadBeanListener(com.abubusoft.kripton.android.sqlite.OnReadBeanListener) ParameterSpec(com.squareup.javapoet.ParameterSpec) OnReadCursorListener(com.abubusoft.kripton.android.sqlite.OnReadCursorListener) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl) JQLProjection(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection) SQLiteModelMethod(com.abubusoft.kripton.processor.sqlite.model.SQLiteModelMethod) StringUtils(com.abubusoft.kripton.common.StringUtils) JavadocPart(com.abubusoft.kripton.processor.sqlite.AbstractSelectCodeGenerator.JavadocPart) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)

Example 67 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project arez by arez.

the class ComponentGenerator method buildInverseRemoveMethod.

@Nonnull
private static MethodSpec buildInverseRemoveMethod(@Nonnull final InverseDescriptor inverse) throws ProcessorException {
    final ObservableDescriptor observable = inverse.getObservable();
    final String methodName = getInverseRemoveMethodName(observable.getName());
    final MethodSpec.Builder builder = MethodSpec.methodBuilder(methodName);
    if (ElementsUtil.areTypesInDifferentPackage(inverse.getTargetType(), inverse.getComponent().getElement())) {
        builder.addModifiers(Modifier.PUBLIC);
    }
    final String otherName = inverse.getOtherName();
    final ParameterSpec parameter = ParameterSpec.builder(TypeName.get(inverse.getTargetType().asType()), otherName, Modifier.FINAL).addAnnotation(GeneratorUtil.NONNULL_CLASSNAME).build();
    builder.addParameter(parameter);
    generateNotDisposedInvariant(builder, methodName);
    builder.addStatement("this.$N.preReportChanged()", observable.getFieldName());
    final CodeBlock.Builder block = CodeBlock.builder();
    block.beginControlFlow("if ( $T.shouldCheckInvariants() )", AREZ_CLASSNAME);
    block.addStatement("$T.invariant( () -> this.$N.contains( $N ), " + "() -> \"Attempted to remove reference '$N' from inverse '$N' " + "but inverse does not contain element. Inverse = \" + $N )", GUARDS_CLASSNAME, observable.getDataFieldName(), otherName, otherName, observable.getName(), observable.getFieldName());
    block.endControlFlow();
    builder.addCode(block.build());
    for (final ExecutableElement hook : inverse.getPreInverseRemoveHooks()) {
        builder.addStatement("$N( $N )", hook.getSimpleName(), otherName);
    }
    builder.addStatement("this.$N.remove( $N )", observable.getDataFieldName(), otherName);
    final CodeBlock.Builder clearCacheBlock = CodeBlock.builder();
    clearCacheBlock.beginControlFlow("if ( $T.areCollectionsPropertiesUnmodifiable() )", AREZ_CLASSNAME);
    clearCacheBlock.addStatement("this.$N = null", observable.getCollectionCacheDataFieldName());
    clearCacheBlock.endControlFlow();
    builder.addCode(clearCacheBlock.build());
    builder.addStatement("this.$N.reportChanged()", observable.getFieldName());
    return builder.build();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) ExecutableElement(javax.lang.model.element.ExecutableElement) CodeBlock(com.squareup.javapoet.CodeBlock) Nonnull(javax.annotation.Nonnull)

Example 68 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project arez by arez.

the class ComponentGenerator method buildInverseAddMethod.

@Nonnull
private static MethodSpec buildInverseAddMethod(@Nonnull final InverseDescriptor inverse) throws ProcessorException {
    final ObservableDescriptor observable = inverse.getObservable();
    final String methodName = getInverseAddMethodName(observable.getName());
    final MethodSpec.Builder builder = MethodSpec.methodBuilder(methodName);
    if (ElementsUtil.areTypesInDifferentPackage(inverse.getTargetType(), inverse.getComponent().getElement())) {
        builder.addModifiers(Modifier.PUBLIC);
    }
    final String otherName = inverse.getOtherName();
    final ParameterSpec parameter = ParameterSpec.builder(TypeName.get(inverse.getTargetType().asType()), otherName, Modifier.FINAL).addAnnotation(GeneratorUtil.NONNULL_CLASSNAME).build();
    builder.addParameter(parameter);
    generateNotDisposedInvariant(builder, methodName);
    builder.addStatement("this.$N.preReportChanged()", observable.getFieldName());
    final CodeBlock.Builder block = CodeBlock.builder();
    block.beginControlFlow("if ( $T.shouldCheckInvariants() )", AREZ_CLASSNAME);
    block.addStatement("$T.invariant( () -> !this.$N.contains( $N ), " + "() -> \"Attempted to add reference '$N' to inverse '$N' " + "but inverse already contained element. Inverse = \" + $N )", GUARDS_CLASSNAME, observable.getDataFieldName(), otherName, otherName, observable.getName(), observable.getFieldName());
    block.endControlFlow();
    builder.addCode(block.build());
    builder.addStatement("this.$N.add( $N )", observable.getDataFieldName(), otherName);
    final CodeBlock.Builder clearCacheBlock = CodeBlock.builder();
    clearCacheBlock.beginControlFlow("if ( $T.areCollectionsPropertiesUnmodifiable() )", AREZ_CLASSNAME);
    clearCacheBlock.addStatement("this.$N = null", observable.getCollectionCacheDataFieldName());
    clearCacheBlock.endControlFlow();
    builder.addCode(clearCacheBlock.build());
    builder.addStatement("this.$N.reportChanged()", observable.getFieldName());
    for (final ExecutableElement hook : inverse.getPostInverseAddHooks()) {
        builder.addStatement("$N( $N )", hook.getSimpleName(), otherName);
    }
    return builder.build();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) ExecutableElement(javax.lang.model.element.ExecutableElement) CodeBlock(com.squareup.javapoet.CodeBlock) Nonnull(javax.annotation.Nonnull)

Example 69 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project mnemonic by apache.

the class AnnotatedDurableEntityClass method buildFactoryMethodSpecs.

protected void buildFactoryMethodSpecs(TypeSpec entityspec, TypeSpec.Builder typespecbuilder) throws AnnotationProcessingException {
    MethodSpec methodspec;
    CodeBlock code;
    TypeName entitytn = ParameterizedTypeName.get(ClassName.get(m_packagename, m_entityname), entityspec.typeVariables.toArray(new TypeVariableName[0]));
    ParameterSpec parameterhold = ParameterSpec.builder(m_parameterholder, "parameterholder").build();
    code = CodeBlock.builder().addStatement("$1T entity = new $1T()", entitytn).addStatement("entity.setupGenericInfo($1N.getEntityFactoryProxies(), $1N.getGenericTypes())", parameterhold.name).addStatement("entity.createDurableEntity($1L.getAllocator(), $1L.getEntityFactoryProxies(), " + "$1L.getGenericTypes(), $1L.getAutoReclaim(), null)", parameterhold.name).addStatement("return entity").build();
    methodspec = MethodSpec.methodBuilder("create").addTypeVariables(entityspec.typeVariables).addException(OutOfHybridMemory.class).addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(TypeName.get(m_elem.asType())).addParameter(parameterhold).addCode(code).build();
    typespecbuilder.addMethod(methodspec);
    ParameterSpec allocparam = ParameterSpec.builder(m_alloctypename, "allocator").build();
    code = CodeBlock.builder().addStatement("return create($1L, false)", allocparam.name).build();
    methodspec = MethodSpec.methodBuilder("create").addTypeVariables(entityspec.typeVariables).addException(OutOfHybridMemory.class).addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(TypeName.get(m_elem.asType())).addParameter(allocparam).addCode(code).build();
    typespecbuilder.addMethod(methodspec);
    ParameterSpec autoreclaimparam = ParameterSpec.builder(TypeName.BOOLEAN, "autoreclaim").build();
    code = CodeBlock.builder().addStatement("return create($1L, null, null, $2L, null)", allocparam.name, autoreclaimparam.name).build();
    methodspec = MethodSpec.methodBuilder("create").addTypeVariables(entityspec.typeVariables).addException(OutOfHybridMemory.class).addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(TypeName.get(m_elem.asType())).addParameter(allocparam).addParameter(autoreclaimparam).addCode(code).build();
    typespecbuilder.addMethod(methodspec);
    ParameterSpec reclaimctxparam = ParameterSpec.builder(m_reclaimctxtypename, "reclaimcontext").build();
    code = CodeBlock.builder().addStatement("return create($1L, null, null, $2L, $3L)", allocparam.name, autoreclaimparam.name, reclaimctxparam.name).build();
    methodspec = MethodSpec.methodBuilder("create").addTypeVariables(entityspec.typeVariables).addException(OutOfHybridMemory.class).addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(TypeName.get(m_elem.asType())).addParameter(allocparam).addParameter(autoreclaimparam).addParameter(reclaimctxparam).addCode(code).build();
    typespecbuilder.addMethod(methodspec);
    ParameterSpec factoryproxysparam = ParameterSpec.builder(m_factoryproxystypename, "factoryproxys").build();
    ParameterSpec gfieldsparam = ParameterSpec.builder(m_gfieldstypename, "gfields").build();
    code = CodeBlock.builder().addStatement("return create($1L, $2L, $3L, $4L, null)", allocparam.name, factoryproxysparam.name, gfieldsparam.name, autoreclaimparam.name).build();
    methodspec = MethodSpec.methodBuilder("create").addTypeVariables(entityspec.typeVariables).addException(OutOfHybridMemory.class).addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(TypeName.get(m_elem.asType())).addParameter(allocparam).addParameter(factoryproxysparam).addParameter(gfieldsparam).addParameter(autoreclaimparam).addCode(code).build();
    typespecbuilder.addMethod(methodspec);
    code = CodeBlock.builder().addStatement("$1T entity = new $1T()", entitytn).addStatement("entity.setupGenericInfo($1N, $2N)", factoryproxysparam.name, gfieldsparam.name).addStatement("entity.createDurableEntity($1L, $2L, $3L, $4L, $5L)", allocparam.name, factoryproxysparam.name, gfieldsparam.name, autoreclaimparam.name, reclaimctxparam.name).addStatement("return entity").build();
    methodspec = MethodSpec.methodBuilder("create").addTypeVariables(entityspec.typeVariables).addException(OutOfHybridMemory.class).addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(TypeName.get(m_elem.asType())).addParameter(allocparam).addParameter(factoryproxysparam).addParameter(gfieldsparam).addParameter(autoreclaimparam).addParameter(reclaimctxparam).addCode(code).build();
    typespecbuilder.addMethod(methodspec);
    ParameterSpec phandlerparam = ParameterSpec.builder(TypeName.LONG, "phandler").build();
    code = CodeBlock.builder().addStatement("return restore($1L, $2L, false)", allocparam.name, phandlerparam.name).build();
    methodspec = MethodSpec.methodBuilder("restore").addTypeVariables(entityspec.typeVariables).addException(RestoreDurableEntityError.class).addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(TypeName.get(m_elem.asType())).addParameter(allocparam).addParameter(phandlerparam).addCode(code).build();
    typespecbuilder.addMethod(methodspec);
    code = CodeBlock.builder().addStatement("$1T entity = new $1T()", entitytn).addStatement("entity.setupGenericInfo($1N.getEntityFactoryProxies(), $1N.getGenericTypes())", parameterhold.name).addStatement("entity.restoreDurableEntity($1L.getAllocator(), $1L.getEntityFactoryProxies()," + "$1L.getGenericTypes(), $1L.getHandler(), $1L.getAutoReclaim(), null)", parameterhold.name).addStatement("return entity").build();
    methodspec = MethodSpec.methodBuilder("restore").addTypeVariables(entityspec.typeVariables).addException(RestoreDurableEntityError.class).addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(TypeName.get(m_elem.asType())).addParameter(parameterhold).addCode(code).build();
    typespecbuilder.addMethod(methodspec);
    code = CodeBlock.builder().addStatement("return restore($1L, null, null, $2L, $3L, null)", allocparam.name, phandlerparam.name, autoreclaimparam.name).build();
    methodspec = MethodSpec.methodBuilder("restore").addTypeVariables(entityspec.typeVariables).addException(RestoreDurableEntityError.class).addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(TypeName.get(m_elem.asType())).addParameter(allocparam).addParameter(phandlerparam).addParameter(autoreclaimparam).addCode(code).build();
    typespecbuilder.addMethod(methodspec);
    code = CodeBlock.builder().addStatement("return restore($1L, null, null, $2L, $3L, $4L)", allocparam.name, phandlerparam.name, autoreclaimparam.name, reclaimctxparam.name).build();
    methodspec = MethodSpec.methodBuilder("restore").addTypeVariables(entityspec.typeVariables).addException(RestoreDurableEntityError.class).addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(TypeName.get(m_elem.asType())).addParameter(allocparam).addParameter(phandlerparam).addParameter(autoreclaimparam).addParameter(reclaimctxparam).addCode(code).build();
    typespecbuilder.addMethod(methodspec);
    code = CodeBlock.builder().addStatement("return restore($1L, $2L, $3L, $4L, $5L, null)", allocparam.name, factoryproxysparam.name, gfieldsparam.name, phandlerparam.name, autoreclaimparam.name).build();
    methodspec = MethodSpec.methodBuilder("restore").addTypeVariables(entityspec.typeVariables).addException(RestoreDurableEntityError.class).addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(TypeName.get(m_elem.asType())).addParameter(allocparam).addParameter(factoryproxysparam).addParameter(gfieldsparam).addParameter(phandlerparam).addParameter(autoreclaimparam).addCode(code).build();
    typespecbuilder.addMethod(methodspec);
    code = CodeBlock.builder().addStatement("$1T entity = new $1T()", entitytn).addStatement("entity.setupGenericInfo($1N, $2N)", factoryproxysparam.name, gfieldsparam.name).addStatement("entity.restoreDurableEntity($1L, $2L, $3L, $4L, $5L, $6L)", allocparam.name, factoryproxysparam.name, gfieldsparam.name, phandlerparam.name, autoreclaimparam.name, reclaimctxparam.name).addStatement("return entity").build();
    methodspec = MethodSpec.methodBuilder("restore").addTypeVariables(entityspec.typeVariables).addException(RestoreDurableEntityError.class).addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(TypeName.get(m_elem.asType())).addParameter(allocparam).addParameter(factoryproxysparam).addParameter(gfieldsparam).addParameter(phandlerparam).addParameter(autoreclaimparam).addParameter(reclaimctxparam).addCode(code).build();
    typespecbuilder.addMethod(methodspec);
    MethodInfo mi = m_extramtdinfo.get("getNativeFieldInfo_static");
    assert null != mi;
    code = CodeBlock.builder().addStatement("return $1T.getNativeFieldInfo_static()", ClassName.get(m_packagename, m_entityname)).build();
    methodspec = MethodSpec.methodBuilder("getNativeFieldInfo").addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(mi.rettype).addCode(code).build();
    typespecbuilder.addMethod(methodspec);
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) CodeBlock(com.squareup.javapoet.CodeBlock) TypeVariableName(com.squareup.javapoet.TypeVariableName)

Example 70 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project firebase-android-sdk by firebase.

the class AnnotationBuilder method createAnnotationImpl.

private static AnnotationImpl createAnnotationImpl(TypeElement annotation) {
    String implName = annotation.getSimpleName().toString() + "Impl";
    TypeSpec.Builder annotationImpl = TypeSpec.classBuilder(implName).addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL).addSuperinterface(TypeName.get(annotation.asType())).addMethod(MethodSpec.methodBuilder("annotationType").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).returns(ParameterizedTypeName.get(ClassName.get(Class.class), WildcardTypeName.subtypeOf(Annotation.class))).addCode("return $T.class;\n", annotation).build());
    List<ParameterSpec> parameters = new ArrayList<>();
    Map<FieldSpec, AnnotationValue> defaults = new HashMap<>();
    for (ExecutableElement method : ElementFilter.methodsIn(annotation.getEnclosedElements())) {
        FieldSpec field = FieldSpec.builder(TypeName.get(method.getReturnType()), method.getSimpleName().toString(), Modifier.PRIVATE, Modifier.FINAL).build();
        if (method.getDefaultValue() != null) {
            defaults.put(field, method.getDefaultValue());
        }
        annotationImpl.addField(field).addMethod(MethodSpec.methodBuilder(field.name).addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(field.type).addCode("return $N;\n", field).build());
        parameters.add(ParameterSpec.builder(TypeName.get(method.getReturnType()), method.getSimpleName().toString()).build());
    }
    MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder().addParameters(parameters);
    for (ParameterSpec parameter : parameters) {
        constructorBuilder.addCode("this.$1N = $1N;\n", parameter.name);
    }
    annotationImpl.addMethod(constructorBuilder.build());
    annotationImpl.addMethod(EqualsMethod.generate(annotation));
    annotationImpl.addMethod(HashCodeMethod.generate(annotation));
    annotationImpl.addMethod(ToStringMethod.generate(annotation));
    return new AnnotationImpl(annotationImpl.build(), defaults);
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec) HashMap(java.util.HashMap) MethodSpec(com.squareup.javapoet.MethodSpec) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) FieldSpec(com.squareup.javapoet.FieldSpec) AnnotationValue(javax.lang.model.element.AnnotationValue) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

ParameterSpec (com.squareup.javapoet.ParameterSpec)97 MethodSpec (com.squareup.javapoet.MethodSpec)59 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)40 ArrayList (java.util.ArrayList)34 TypeName (com.squareup.javapoet.TypeName)30 ClassName (com.squareup.javapoet.ClassName)27 TypeSpec (com.squareup.javapoet.TypeSpec)26 Map (java.util.Map)26 CodeBlock (com.squareup.javapoet.CodeBlock)23 FieldSpec (com.squareup.javapoet.FieldSpec)22 List (java.util.List)20 HashMap (java.util.HashMap)18 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)17 JavaFile (com.squareup.javapoet.JavaFile)13 Modifier (javax.lang.model.element.Modifier)13 TypeElement (javax.lang.model.element.TypeElement)13 Collectors (java.util.stream.Collectors)12 IOException (java.io.IOException)11 Element (javax.lang.model.element.Element)11 TypeMirror (javax.lang.model.type.TypeMirror)11