Search in sources :

Example 11 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project storio by pushtorefresh.

the class PutResolverGenerator method createMapToContentValuesMethodSpec.

@NotNull
private MethodSpec createMapToContentValuesMethodSpec(@NotNull StorIOSQLiteTypeMeta storIOSQLiteTypeMeta, @NotNull ClassName storIOSQLiteTypeClassName) {
    final MethodSpec.Builder builder = MethodSpec.methodBuilder("mapToContentValues").addJavadoc("{@inheritDoc}\n").addAnnotation(Override.class).addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).addModifiers(PUBLIC).returns(ClassName.get("android.content", "ContentValues")).addParameter(ParameterSpec.builder(storIOSQLiteTypeClassName, "object").addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).build()).addStatement("ContentValues contentValues = new ContentValues($L)", storIOSQLiteTypeMeta.columns.size()).addCode("\n");
    for (StorIOSQLiteColumnMeta columnMeta : storIOSQLiteTypeMeta.columns.values()) {
        final boolean ignoreNull = columnMeta.storIOColumn.ignoreNull();
        if (ignoreNull) {
            builder.beginControlFlow("if (object.$L != null)", columnMeta.elementName + (columnMeta.isMethod() ? "()" : ""));
        }
        builder.addStatement("contentValues.put($S, object.$L)", columnMeta.storIOColumn.name(), columnMeta.elementName + (columnMeta.isMethod() ? "()" : ""));
        if (ignoreNull) {
            builder.endControlFlow();
        }
    }
    return builder.addCode("\n").addStatement("return contentValues").build();
}
Also used : StorIOSQLiteColumnMeta(com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteColumnMeta) MethodSpec(com.squareup.javapoet.MethodSpec) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with MethodSpec

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

the class JavaGenerator method messageFieldsAndUnknownFieldsConstructor.

// Example:
//
// public SimpleMessage(int optional_int32, long optional_int64, ByteString unknownFields) {
//   super(ADAPTER, unknownFields);
//   this.optional_int32 = optional_int32;
//   this.optional_int64 = optional_int64;
// }
//
private MethodSpec messageFieldsAndUnknownFieldsConstructor(NameAllocator nameAllocator, MessageType type) {
    NameAllocator localNameAllocator = nameAllocator.clone();
    String adapterName = localNameAllocator.get("ADAPTER");
    String unknownFieldsName = localNameAllocator.newName("unknownFields");
    MethodSpec.Builder result = MethodSpec.constructorBuilder().addModifiers(PUBLIC).addStatement("super($N, $N)", adapterName, unknownFieldsName);
    for (OneOf oneOf : type.oneOfs()) {
        if (oneOf.fields().size() < 2)
            continue;
        CodeBlock.Builder fieldNamesBuilder = CodeBlock.builder();
        boolean first = true;
        for (Field field : oneOf.fields()) {
            if (!first)
                fieldNamesBuilder.add(", ");
            fieldNamesBuilder.add("$N", localNameAllocator.get(field));
            first = false;
        }
        CodeBlock fieldNames = fieldNamesBuilder.build();
        result.beginControlFlow("if ($T.countNonNull($L) > 1)", Internal.class, fieldNames);
        result.addStatement("throw new IllegalArgumentException($S)", "at most one of " + fieldNames + " may be non-null");
        result.endControlFlow();
    }
    for (Field field : type.fieldsAndOneOfFields()) {
        TypeName javaType = fieldType(field);
        String fieldName = localNameAllocator.get(field);
        ParameterSpec.Builder param = ParameterSpec.builder(javaType, fieldName);
        if (emitAndroid && field.isOptional()) {
            param.addAnnotation(NULLABLE);
        }
        result.addParameter(param.build());
        if (field.isRepeated() || field.type().isMap()) {
            result.addStatement("this.$1L = $2T.immutableCopyOf($1S, $1L)", fieldName, Internal.class);
        } else {
            result.addStatement("this.$1L = $1L", fieldName);
        }
    }
    result.addParameter(BYTE_STRING, unknownFieldsName);
    return result.build();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) OneOf(com.squareup.wire.schema.OneOf) 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) CodeBlock(com.squareup.javapoet.CodeBlock) ByteString(okio.ByteString)

Example 13 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project react4j by react4j.

the class Generator method buildStaticStepMethodMethod.

@Nonnull
private static MethodSpec buildStaticStepMethodMethod(@Nonnull final ComponentDescriptor descriptor, @Nonnull final Step step, @Nonnull final StepMethod stepMethod) {
    final MethodSpec.Builder method = MethodSpec.methodBuilder(stepMethod.getName()).addAnnotation(NONNULL_CLASSNAME);
    method.addModifiers(Modifier.STATIC);
    if (descriptor.getDeclaredType().asElement().getModifiers().contains(Modifier.PUBLIC)) {
        method.addModifiers(Modifier.PUBLIC);
    }
    final ExecutableType propMethodType = stepMethod.getPropMethodType();
    if (null != propMethodType) {
        ProcessorUtil.copyTypeParameters(propMethodType, method);
    }
    ProcessorUtil.copyTypeParameters(descriptor.getElement(), method);
    if (!stepMethod.isBuildIntrinsic()) {
        final ParameterSpec.Builder parameter = ParameterSpec.builder(stepMethod.getType(), stepMethod.getName(), Modifier.FINAL);
        final ExecutableElement propMethod = stepMethod.getPropMethod();
        if (null != propMethod) {
            ProcessorUtil.copyDocumentedAnnotations(propMethod, parameter);
        } else if (stepMethod.isKeyIntrinsic() || stepMethod.isChildrenStreamIntrinsic()) {
            parameter.addAnnotation(NONNULL_CLASSNAME);
        } else if (stepMethod.isChildOfChildrenIntrinsic()) {
            parameter.addAnnotation(NULLABLE_CLASSNAME);
        }
        method.addParameter(parameter.build());
    }
    final String infix = asTypeArgumentsInfix(descriptor.getDeclaredType());
    if (stepMethod.isBuildIntrinsic()) {
        method.addStatement("return new $T" + infix + "().build()", ClassName.bestGuess("Builder"));
    } else {
        method.addStatement("return new $T" + infix + "().$N( $N )", ClassName.bestGuess("Builder"), stepMethod.getName(), stepMethod.getName());
    }
    configureStepMethodReturns(method, step, stepMethod.getStepMethodType());
    return method.build();
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) ExecutableElement(javax.lang.model.element.ExecutableElement) Nonnull(javax.annotation.Nonnull)

Example 14 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project react4j by react4j.

the class Generator method buildBuilderStepImpl.

@Nonnull
private static MethodSpec buildBuilderStepImpl(@Nonnull final Step step, @Nonnull final StepMethod stepMethod) {
    final MethodSpec.Builder method = MethodSpec.methodBuilder(stepMethod.getName());
    method.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
    method.addAnnotation(Override.class);
    method.addAnnotation(NONNULL_CLASSNAME);
    final ExecutableType propMethodType = stepMethod.getPropMethodType();
    if (null != propMethodType) {
        ProcessorUtil.copyTypeParameters(propMethodType, method);
    }
    final ParameterSpec.Builder parameter = ParameterSpec.builder(stepMethod.getType(), stepMethod.getName(), Modifier.FINAL);
    final ExecutableElement propMethod = stepMethod.getPropMethod();
    if (null != propMethod) {
        ProcessorUtil.copyDocumentedAnnotations(propMethod, parameter);
    } else if (stepMethod.isKeyIntrinsic() || stepMethod.isChildrenStreamIntrinsic()) {
        parameter.addAnnotation(NONNULL_CLASSNAME);
    } else if (stepMethod.isChildOfChildrenIntrinsic()) {
        parameter.addAnnotation(NULLABLE_CLASSNAME);
    }
    method.addParameter(parameter.build());
    if (stepMethod.isChildrenIntrinsic()) {
        method.varargs();
        final CodeBlock.Builder block = CodeBlock.builder();
        block.beginControlFlow("for ( final $T child : $N )", REACT_NODE_CLASSNAME, stepMethod.getName());
        block.addStatement("child( child )");
        block.endControlFlow();
        method.addCode(block.build());
    } else if (stepMethod.isChildOfChildrenIntrinsic()) {
        final CodeBlock.Builder block = CodeBlock.builder();
        block.beginControlFlow("if ( null != $N )", stepMethod.getName());
        block.addStatement("_children.push( $N )", stepMethod.getName());
        block.endControlFlow();
        method.addCode(block.build());
    } else if (stepMethod.isChildrenStreamIntrinsic()) {
        method.addStatement("children( $N.toArray( $T[]::new ) )", stepMethod.getName(), REACT_NODE_CLASSNAME);
    } else if (stepMethod.isChildIntrinsic()) {
        assert null != propMethod;
        if (null != ProcessorUtil.findAnnotationByType(propMethod, Constants.NONNULL_ANNOTATION_CLASSNAME)) {
            method.addStatement("_child = $T.requireNonNull( $N )", Objects.class, stepMethod.getName());
        } else {
            method.addStatement("_child = $N", stepMethod.getName());
        }
    } else if (stepMethod.isKeyIntrinsic() || (null != propMethod && null != ProcessorUtil.findAnnotationByType(propMethod, Constants.NONNULL_ANNOTATION_CLASSNAME))) {
        method.addStatement("_props.set( $S, $T.requireNonNull( $N ) )", stepMethod.getName(), Objects.class, stepMethod.getName());
    } else {
        method.addStatement("_props.set( $S, $N )", stepMethod.getName(), stepMethod.getName());
    }
    if (StepMethodType.TERMINATE == stepMethod.getStepMethodType()) {
        method.addStatement("return build()");
    } else {
        method.addStatement("return this");
    }
    configureStepMethodReturns(method, step, stepMethod.getStepMethodType());
    return method.build();
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) ExecutableElement(javax.lang.model.element.ExecutableElement) CodeBlock(com.squareup.javapoet.CodeBlock) Objects(java.util.Objects) Nonnull(javax.annotation.Nonnull)

Example 15 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project web3sdk by FISCO-BCOS.

the class AbiTypesGenerator method generateFixedTypes.

private <T extends Type> void generateFixedTypes(Class<T> superclass, Path path) throws IOException {
    String packageName = createPackageName(superclass);
    ClassName className;
    for (int mBitSize = 8; mBitSize < Type.MAX_BIT_LENGTH; mBitSize += 8) {
        inner: for (int nBitSize = 8; nBitSize < Type.MAX_BIT_LENGTH; nBitSize += 8) {
            if (mBitSize + nBitSize > Type.MAX_BIT_LENGTH) {
                break inner;
            }
            MethodSpec constructorSpec1 = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(BigInteger.class, "value").addStatement("super($L, $L, $N)", mBitSize, nBitSize, "value").build();
            MethodSpec constructorSpec2 = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(int.class, "mBitSize").addParameter(int.class, "nBitSize").addParameter(BigInteger.class, "m").addParameter(BigInteger.class, "n").addStatement("super($L, $L, $N, $N)", mBitSize, nBitSize, "m", "n").build();
            className = ClassName.get(packageName, superclass.getSimpleName() + mBitSize + "x" + nBitSize);
            FieldSpec defaultFieldSpec = FieldSpec.builder(className, DEFAULT, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer("new $T(BigInteger.ZERO)", className).build();
            TypeSpec fixedType = TypeSpec.classBuilder(className.simpleName()).addJavadoc(CODEGEN_WARNING).superclass(superclass).addModifiers(Modifier.PUBLIC).addField(defaultFieldSpec).addMethod(constructorSpec1).addMethod(constructorSpec2).build();
            write(packageName, fixedType, path);
        }
    }
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName) BigInteger(java.math.BigInteger) Uint(org.bcos.web3j.abi.datatypes.Uint) FieldSpec(com.squareup.javapoet.FieldSpec) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

MethodSpec (com.squareup.javapoet.MethodSpec)155 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)43 TypeName (com.squareup.javapoet.TypeName)42 ArrayList (java.util.ArrayList)38 ClassName (com.squareup.javapoet.ClassName)34 TypeSpec (com.squareup.javapoet.TypeSpec)31 ParameterSpec (com.squareup.javapoet.ParameterSpec)24 CodeBlock (com.squareup.javapoet.CodeBlock)23 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)20 Nonnull (javax.annotation.Nonnull)17 TypeMirror (javax.lang.model.type.TypeMirror)15 WireField (com.squareup.wire.WireField)14 Field (com.squareup.wire.schema.Field)14 TypeElement (javax.lang.model.element.TypeElement)14 ByteString (okio.ByteString)13 JvmLanguages.builtInAdapterString (com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)12 List (java.util.List)11 VariableElement (javax.lang.model.element.VariableElement)11 Utf8String (org.fisco.bcos.web3j.abi.datatypes.Utf8String)11 FieldSpec (com.squareup.javapoet.FieldSpec)10