Search in sources :

Example 86 with MethodSpec

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

the class GetResolverGenerator method createMapFromCursorMethodSpec.

@NotNull
private MethodSpec createMapFromCursorMethodSpec(@NotNull StorIOSQLiteTypeMeta storIOSQLiteTypeMeta, @NotNull ClassName storIOSQLiteTypeClassName) {
    final MethodSpec.Builder builder = MethodSpec.methodBuilder("mapFromCursor").addJavadoc("{@inheritDoc}\n").addAnnotation(Override.class).addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).addModifiers(PUBLIC).returns(storIOSQLiteTypeClassName).addParameter(ParameterSpec.builder(ClassName.get("android.database", "Cursor"), "cursor").addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).build()).addStatement("$T object = new $T()", storIOSQLiteTypeClassName, storIOSQLiteTypeClassName).addCode("\n");
    for (final StorIOSQLiteColumnMeta columnMeta : storIOSQLiteTypeMeta.columns.values()) {
        final String columnIndex = "cursor.getColumnIndex(\"" + columnMeta.storIOColumn.name() + "\")";
        final JavaType javaType = columnMeta.javaType;
        final String getFromCursor = getFromCursorString(columnMeta, javaType, columnIndex);
        final boolean isBoxed = javaType.isBoxedType();
        if (isBoxed) {
            // otherwise -> if primitive and value from cursor null -> fail early
            builder.beginControlFlow("if (!cursor.isNull($L))", columnIndex);
        }
        builder.addStatement("object.$L = cursor.$L", columnMeta.elementName, getFromCursor);
        if (isBoxed) {
            builder.endControlFlow();
        }
    }
    return builder.addCode("\n").addStatement("return object").build();
}
Also used : StorIOSQLiteColumnMeta(com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteColumnMeta) JavaType(com.pushtorefresh.storio.common.annotations.processor.introspection.JavaType) MethodSpec(com.squareup.javapoet.MethodSpec) Common.getFromCursorString(com.pushtorefresh.storio.common.annotations.processor.generate.Common.getFromCursorString) NotNull(org.jetbrains.annotations.NotNull)

Example 87 with MethodSpec

use of com.squareup.javapoet.MethodSpec 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 88 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project raml-for-jax-rs by mulesoft-labs.

the class Jsr303ResourceExtension method onMethod.

@Override
public MethodSpec.Builder onMethod(ResourceContext context, GMethod method, GRequest gRequest, MethodSpec.Builder methodSpec) {
    MethodSpec spec = methodSpec.build();
    MethodSpec.Builder builder = MethodSpec.methodBuilder(spec.name);
    builder.addAnnotations(spec.annotations);
    if (spec.code != null) {
        builder.addCode(spec.code);
    }
    if (spec.defaultValue != null) {
        builder.defaultValue(spec.defaultValue);
    }
    builder.addExceptions(spec.exceptions);
    if (spec.javadoc != null) {
        builder.addJavadoc("$L", spec.javadoc);
    }
    builder.addModifiers(spec.modifiers);
    for (ParameterSpec parameter : spec.parameters) {
        if (!(parameter.type.isPrimitive() || parameter.type.isBoxedPrimitive() || parameter.type.withoutAnnotations().toString().equals("java.lang.String"))) {
            builder.addParameter(parameter.toBuilder().addAnnotation(Valid.class).build());
        } else {
            builder.addParameter(parameter);
        }
    }
    if (spec.returnType != null) {
        builder.returns(spec.returnType);
    }
    builder.addTypeVariables(spec.typeVariables);
    builder.varargs(spec.varargs);
    return builder;
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec)

Example 89 with MethodSpec

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

the class SqlModifyBuilder method generate.

/**
 * @param elementUtils
 * @param builder
 * @param method
 * @param updateMode
 */
public static void generate(TypeSpec.Builder classBuilder, SQLiteModelMethod method) {
    ModifyType updateResultType = detectModifyType(method, method.jql.operationType);
    // if true, field must be associate to ben attributes
    TypeName returnType = method.getReturnClass();
    if (updateResultType == null) {
        throw (new InvalidMethodSignException(method));
    }
    // generate method code
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(method.getName()).addAnnotation(Override.class).addModifiers(Modifier.PUBLIC);
    ParameterSpec parameterSpec;
    for (Pair<String, TypeName> item : method.getParameters()) {
        parameterSpec = ParameterSpec.builder(item.value1, item.value0).build();
        methodBuilder.addParameter(parameterSpec);
    }
    methodBuilder.returns(returnType);
    // generate inner code
    updateResultType.generate(classBuilder, methodBuilder, method, returnType);
    MethodSpec methodSpec = methodBuilder.build();
    classBuilder.addMethod(methodSpec);
    if (method.contentProviderEntryPathEnabled) {
        // delete-select, update-select can be used with content provider
        // insert-select no
        // if (method.jql.containsSelectOperation && updateResultType==) {
        // AssertKripton.failWithInvalidMethodSignException(true, method, "
        // SQL with inner SELECT can not be used in content provider");
        // }
        generateModifierForContentProvider(BaseProcessor.elementUtils, classBuilder, method, updateResultType);
    }
}
Also used : TypeName(com.squareup.javapoet.TypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) InvalidMethodSignException(com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException)

Example 90 with MethodSpec

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

the class BindM2MBuilder method generateSelects.

private void generateSelects(M2MEntity entity, String packageName) {
    String idPart = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, entity.idName);
    String methodName = "";
    String workId = "";
    methodName = "selectBy" + idPart;
    if (!isMethodAlreadyDefined(entity, methodName)) {
        // @formatter:off
        MethodSpec methodSpec = MethodSpec.methodBuilder(methodName).addModifiers(Modifier.PUBLIC).addModifiers(Modifier.ABSTRACT).addAnnotation(AnnotationSpec.builder(BindSqlSelect.class).addMember("where", "$S", entity.idName + "=${" + entity.idName + "}").build()).addParameter(ParameterSpec.builder(Long.TYPE, entity.idName).addAnnotation(AnnotationSpec.builder(BindSqlParam.class).addMember("value", "$S", entity.idName).build()).build()).returns(TypeUtility.className(packageName, entity.name)).build();
        // @formatter:on
        classBuilder.addMethod(methodSpec);
    }
    methodName = entity.entity1Name.simpleName() + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, entity.idName);
    workId = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, methodName);
    methodName = "selectBy" + methodName;
    if (!isMethodAlreadyDefined(entity, methodName)) {
        // @formatter:off
        MethodSpec methodSpec = MethodSpec.methodBuilder(methodName).addModifiers(Modifier.PUBLIC).addModifiers(Modifier.ABSTRACT).addAnnotation(AnnotationSpec.builder(BindSqlSelect.class).addMember("where", "$S", workId + "=${" + workId + "}").build()).addParameter(ParameterSpec.builder(Long.TYPE, workId).addAnnotation(AnnotationSpec.builder(BindSqlParam.class).addMember("value", "$S", workId).build()).build()).returns(TypeUtility.parameterizedTypeName(TypeUtility.className(List.class), TypeUtility.className(packageName, entity.name))).build();
        // @formatter:on
        classBuilder.addMethod(methodSpec);
    }
    methodName = entity.entity2Name.simpleName() + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, entity.idName);
    workId = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, methodName);
    methodName = "selectBy" + methodName;
    if (!isMethodAlreadyDefined(entity, methodName)) {
        // @formatter:off
        MethodSpec methodSpec = MethodSpec.methodBuilder(methodName).addModifiers(Modifier.PUBLIC).addModifiers(Modifier.ABSTRACT).addAnnotation(AnnotationSpec.builder(BindSqlSelect.class).addMember("where", "$S", workId + "=${" + workId + "}").build()).addParameter(ParameterSpec.builder(Long.TYPE, workId).addAnnotation(AnnotationSpec.builder(BindSqlParam.class).addMember("value", "$S", workId).build()).build()).returns(TypeUtility.parameterizedTypeName(TypeUtility.className(List.class), TypeUtility.className(packageName, entity.name))).build();
        // @formatter:on
        classBuilder.addMethod(methodSpec);
    }
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) BindSqlParam(com.abubusoft.kripton.android.annotation.BindSqlParam) ArrayList(java.util.ArrayList) List(java.util.List)

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