Пример #1
0
  @Override
  public void generateContentValuesBuilderMethod(
      TypeSpec.Builder builder, TypeName type, String contentValuesVarName) {

    builder.addMethod(
        MethodSpec.methodBuilder(field.getFieldName())
            .addJavadoc("Adds the given value to this ContentValues\n")
            .addJavadoc("@param value The value\n")
            .addJavadoc("@return $T\n", type)
            .addModifiers(Modifier.PUBLIC)
            .addParameter(TypeName.get(field.getField().asType()), "value", Modifier.FINAL)
            .returns(type)
            .addStatement("$L.put($S, value)", contentValuesVarName, field.getColumnName())
            .addStatement("return this")
            .build());

    builder.addMethod(
        MethodSpec.methodBuilder(field.getFieldName() + "AsNull")
            .addJavadoc("Adds a null value to this ContentValues\n")
            .addJavadoc("@return $T\n", type)
            .addModifiers(Modifier.PUBLIC)
            .returns(type)
            .addStatement("$L.putNull( $S )", contentValuesVarName, field.getColumnName())
            .addStatement("return this")
            .build());
  }
Пример #2
0
 private static List<MethodSpec> createIntentBuildMethods(
     AutoBundleBindingClass target, String fieldName) {
   List<MethodSpec> methodSpecs = new ArrayList<>(2);
   ClassName contextClass = ClassName.get("android.content", "Context");
   ClassName intentClass = ClassName.get("android.content", "Intent");
   MethodSpec buildWithContext =
       MethodSpec.methodBuilder("build")
           .addModifiers(Modifier.PUBLIC)
           .addParameter(contextClass, "context")
           .returns(intentClass)
           .addStatement(
               "$T intent = new $T(context, $T.class)",
               intentClass,
               intentClass,
               target.getTargetType())
           .addStatement("intent.putExtras($N)", fieldName)
           .addStatement("return intent")
           .build();
   MethodSpec buildWithIntent =
       MethodSpec.methodBuilder("build")
           .addModifiers(Modifier.PUBLIC)
           .addParameter(intentClass, "intent")
           .returns(intentClass)
           .addStatement("intent.putExtras($N)", fieldName)
           .addStatement("return intent")
           .build();
   methodSpecs.add(buildWithContext);
   methodSpecs.add(buildWithIntent);
   return methodSpecs;
 }
Пример #3
0
  private static MethodSpec createBuilderConstructor(
      AutoBundleBindingClass target, String fieldName) {
    MethodSpec.Builder builder =
        MethodSpec.constructorBuilder()
            .addModifiers(Modifier.PUBLIC)
            .addStatement("this.$N = new $T()", fieldName, CLASS_BUNDLE);

    for (AutoBundleBindingField arg : target.getRequiredArgs()) {
      String key = arg.getArgKey();
      TypeName type = arg.getArgType();
      String operationName = arg.getOperationName("put");
      builder.addParameter(type, key);
      if (arg.hasCustomConverter()) {
        TypeName converter = arg.getConverter();
        builder
            .addStatement("$T $NConverter = new $T()", converter, key, converter)
            .addStatement(
                "this.$N.$N($S, $NConverter.convert($N))", fieldName, operationName, key, key, key);
      } else {
        builder.addStatement("this.$N.$N($S, $N)", fieldName, operationName, key, key);
      }
    }

    return builder.build();
  }
Пример #4
0
  private static List<MethodSpec> createBuilderMethods(
      AutoBundleBindingClass target, String fieldName) {
    List<MethodSpec> methodSpecs = new ArrayList<>();
    for (AutoBundleBindingField arg : target.getNotRequiredArgs()) {
      String argKey = arg.getArgKey();
      TypeName argType = arg.getArgType();
      String operationName = arg.getOperationName("put");

      MethodSpec.Builder builder =
          MethodSpec.methodBuilder(argKey)
              .addModifiers(Modifier.PUBLIC)
              .addParameter(argType, argKey)
              .returns(ClassName.get(target.getPackageName(), target.getBuilderClassName()));

      if (arg.hasCustomConverter()) {
        TypeName converter = arg.getConverter();
        builder
            .addStatement("$T $NConverter = new $T()", converter, argKey, converter)
            .addStatement(
                "$N.$N($S, $NConverter.convert($N))",
                fieldName,
                operationName,
                argKey,
                argKey,
                argKey);
      } else {
        builder.addStatement("$N.$N($S, $N)", fieldName, operationName, argKey, argKey);
      }

      builder.addStatement("return this");
      methodSpecs.add(builder.build());
    }
    return methodSpecs;
  }
Пример #5
0
  private void addWriteToParcelMethod(HelperClassBuilder builder) throws ProcessorError {
    ClassName targetClassName = builder.getArgClassName();
    String target = "target";
    String parcel = "parcel";

    MethodSpec.Builder method =
        MethodSpec.methodBuilder(METHOD_NAME_WRITE_TO_PARCEL)
            .addAnnotation(
                WeaveBuilder.weave()
                    .method(METHOD_NAME_WRITE_TO_PARCEL, Parcel.class, int.class)
                    .placed(WeaveBuilder.MethodWeaveType.AFTER_SUPER)
                    .withStatement(
                        "%s.%s(this, $1);",
                        fullName(builder.getClassName()), METHOD_NAME_WRITE_TO_PARCEL)
                    .build())
            .addModifiers(Modifier.PUBLIC, Modifier.STATIC);

    addClassAsParameter(method, targetClassName, target);
    method.addParameter(Parcel.class, parcel);

    for (int i = 0, c = mFields.size(); i < c; i++) {
      Field field = mFields.get(i);
      CallFormat writeCall = Parceler.writeCall(field.element);

      if (writeCall == null) {
        continue; // TODO : should throw exception or ignore field ?!
      }

      Pair<String, List<Object>> mappedWriteCall = mapCall(field, writeCall, target, parcel);
      method.addStatement(
          mappedWriteCall.fst, mappedWriteCall.snd.toArray(new Object[mappedWriteCall.snd.size()]));
    }

    builder.getBuilder().addMethod(method.build());
  }
Пример #6
0
 private MethodSpec constructor() {
   return MethodSpec.constructorBuilder()
       .addModifiers(Modifier.PRIVATE)
       .addCode(schemaTypeInfo.exportConstructorTablesInit())
       .addCode(versionSetInit())
       .build();
 }
Пример #7
0
 private MethodSpec getMethod() {
   return MethodSpec.methodBuilder("get")
       .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
       .returns(exportClassName)
       .addStatement("return INSTANCE")
       .build();
 }
Пример #8
0
  private TypeSpec buildModule(ModuleSpec spec) {
    CodeBlock.Builder blockBuilder =
        CodeBlock.builder().add("return new $T(", spec.getPresenterTypeName());
    int i = 0;
    for (ParameterSpec parameterSpec : spec.getPresenterArgs()) {
      blockBuilder.add(parameterSpec.name);

      if (i++ < spec.getPresenterArgs().size() - 1) {
        blockBuilder.add(", ");
      }
    }
    blockBuilder.add(");\n");

    MethodSpec.Builder methodSpecBuilder =
        MethodSpec.methodBuilder("providesPresenter")
            .addModifiers(Modifier.PUBLIC)
            .returns(spec.getPresenterTypeName())
            .addAnnotation(Provides.class)
            .addParameters(spec.getProvideParameters())
            .addCode(blockBuilder.build());

    if (spec.getScopeAnnotationSpec() != null) {
      methodSpecBuilder.addAnnotation(spec.getScopeAnnotationSpec());
    }

    return TypeSpec.classBuilder(spec.getClassName().simpleName())
        .addModifiers(Modifier.PUBLIC)
        .addAnnotation(Module.class)
        .addMethod(methodSpecBuilder.build())
        .build();
  }
Пример #9
0
 private MethodSpec toStringMethod() {
   return MethodSpec.methodBuilder("toString")
       .addModifiers(Modifier.PUBLIC)
       .addParameter(Dialect.class, "dialect")
       .returns(String.class)
       .addStatement("return versionSet.toString(dialect)")
       .build();
 }
Пример #10
0
 private MethodSpec.Builder createOverridingMethod(ExecutableElement element) {
   // TODO fix the cast
   MethodSpec.Builder result =
       MethodSpec.overriding(element, (DeclaredType) getSourceType().asType(), types);
   String literal = ClassGenerationUtil.defaultReturnLiteral(element.getReturnType());
   if (literal != null) result.addStatement("return $L", literal);
   return result;
 }
Пример #11
0
 public MethodSpec createShow() {
   return MethodSpec.methodBuilder("show")
       .addAnnotation(Override.class)
       .addModifiers(Modifier.PUBLIC)
       .returns(String.class)
       .addCode(
           CodeBlock.builder()
               .addStatement("return String.format(\"<$L>%s</$L>\", value)", tag, tag)
               .build())
       .build();
 }
Пример #12
0
 public MethodSpec createCompareTo() {
   final String other = "other";
   return MethodSpec.methodBuilder("compareTo")
       .addAnnotation(Override.class)
       .addModifiers(Modifier.PUBLIC)
       .returns(int.class)
       .addParameter(ParameterSpec.builder(terminalTypeName, other, Modifier.FINAL).build())
       .addCode(
           CodeBlock.builder().addStatement("return value.compareTo($L.value)", other).build())
       .build();
 }
Пример #13
0
 public MethodSpec createValueGetter() {
   return MethodSpec.methodBuilder("value")
       .addAnnotation(Override.class)
       .addJavadoc(
           "Returns this {@code $L}'s value.\n\n@return this {@code $L}'s value.\n",
           terminalName,
           terminalName)
       .addModifiers(Modifier.PUBLIC)
       .returns(String.class)
       .addCode(CodeBlock.builder().addStatement("return value").build())
       .build();
 }
Пример #14
0
 public MethodSpec createHashCode() {
   return MethodSpec.methodBuilder("hashCode")
       .addAnnotation(Override.class)
       .addModifiers(Modifier.PUBLIC)
       .returns(int.class)
       .addCode(
           CodeBlock.builder()
               .addStatement("final int prime = $L", prime)
               .addStatement("return prime + value.hashCode()")
               .build())
       .build();
 }
Пример #15
0
 private TypeSpec schemaType() {
   MigrationVersionMetadata migrationVersion = schemaName().migrationVersion();
   return TypeSpec.classBuilder(schemaName().simpleName())
       .addAnnotation(schemaAnnotation(migrationVersion))
       .addModifiers(Modifier.PUBLIC)
       .addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).build())
       .addMethods(
           tableInfoList()
               .stream()
               .map(TableInfoMetadata::schemaMethod)
               .collect(MoreCollectors.toImmutableList()))
       .build();
 }
Пример #16
0
  private static MethodSpec createBindWithSourceMethod(AutoBundleBindingClass target) {
    List<AutoBundleBindingField> args = new ArrayList<>();
    args.addAll(target.getRequiredArgs());
    args.addAll(target.getNotRequiredArgs());

    MethodSpec.Builder builder =
        MethodSpec.methodBuilder("bind")
            .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
            .returns(void.class)
            .addParameter(target.getTargetType(), "target")
            .addParameter(CLASS_BUNDLE, "source");

    for (AutoBundleBindingField arg : args) {
      String key = arg.getArgKey();
      String fieldName = arg.getFieldName();
      TypeName argType = arg.getArgType();
      String operationName = arg.getOperationName("get");
      builder.beginControlFlow("if (source.containsKey($S))", key);

      if (arg.hasCustomConverter()) {
        TypeName converter = arg.getConverter();
        builder
            .addStatement("$T $NConverter = new $T()", converter, key, converter)
            .addStatement(
                "target.$N = ($T) $NConverter.original(source.$N($S))",
                fieldName,
                argType,
                key,
                operationName,
                key);
      } else {
        if (arg.noCast()) {
          builder.addStatement("target.$N = source.$N($S)", fieldName, operationName, key);
        } else {
          builder.addStatement(
              "target.$N = ($T) source.$N($S)", fieldName, argType, operationName, key);
        }
      }

      if (arg.isRequired()) {
        String exceptionMessage =
            String.format("%s is required, but not found in the bundle.", key);
        builder
            .nextControlFlow("else")
            .addStatement("throw new IllegalStateException($S)", exceptionMessage);
      }
      builder.endControlFlow();
    }
    return builder.build();
  }
Пример #17
0
  public static MethodSpec.Builder createParseFieldsFunction(
      String paramPackageName, String paramClassName) {
    ClassName clazzReturn = ClassName.get("java.lang", "String");
    ClassName stringBuilder = ClassName.get("java.lang", "StringBuilder");
    ClassName parameter = ClassName.get(paramPackageName, paramClassName);
    MethodSpec.Builder mapFieldsBuilder =
        MethodSpec.methodBuilder(parseFieldsFunction)
            .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
            .addException(fieldDecoratorEx)
            .addParameter(parameter, instanceName)
            .returns(clazzReturn)
            .addStatement("$T " + stringBuilderName + " = new $T()", stringBuilder, stringBuilder);

    return mapFieldsBuilder;
  }
Пример #18
0
 private static List<MethodSpec> createFragmentBuildMethods(
     AutoBundleBindingClass target, String fieldName) {
   List<MethodSpec> methodSpecs = new ArrayList<>(2);
   ClassName targetClass = target.getTargetType();
   MethodSpec buildWithNoParam =
       MethodSpec.methodBuilder("build")
           .addModifiers(Modifier.PUBLIC)
           .returns(targetClass)
           .addStatement("$T fragment = new $T()", targetClass, targetClass)
           .addStatement("fragment.setArguments($N)", fieldName)
           .addStatement("return fragment")
           .build();
   MethodSpec buildWithFragment =
       MethodSpec.methodBuilder("build")
           .addModifiers(Modifier.PUBLIC)
           .addParameter(targetClass, "fragment")
           .returns(targetClass)
           .addStatement("fragment.setArguments($N)", fieldName)
           .addStatement("return fragment")
           .build();
   methodSpecs.add(buildWithNoParam);
   methodSpecs.add(buildWithFragment);
   return methodSpecs;
 }
  public MethodSpec write() {
    MethodSpec.Builder b = MethodSpec.constructorBuilder();

    b.addModifiers(accessInfo.modifiers());

    for (ParameterSpec parameterSpec : parameterSpecLIst) {
      b.addParameter(parameterSpec);
    }

    for (CodeBlock codeBlock : codeBlockList) {
      b.addCode(codeBlock);
    }

    return b.build();
  }
Пример #20
0
 private static MethodSpec createCallBuilderMethod(AutoBundleBindingClass target) {
   ClassName builderClass = ClassName.get(target.getPackageName(), target.getBuilderClassName());
   MethodSpec.Builder builder =
       MethodSpec.methodBuilder("create" + target.getBuilderType().name() + "Builder")
           .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
           .returns(builderClass)
           .addCode("return new $T(", builderClass);
   for (int i = 0, count = target.getRequiredArgs().size(); i < count; i++) {
     if (i > 0) {
       builder.addCode(",");
     }
     AutoBundleBindingField arg = target.getRequiredArgs().get(i);
     builder.addParameter(arg.getArgType(), arg.getArgKey()).addCode("$N", arg.getArgKey());
   }
   return builder.addCode(");\n").build();
 }
Пример #21
0
  private MethodSpec createFillResponseMethod(RestActionClass actionClass) {
    MethodSpec.Builder builder =
        MethodSpec.methodBuilder("fillResponse")
            .addModifiers(Modifier.PUBLIC)
            .addAnnotation(Override.class)
            .returns(ClassName.get(actionClass.getTypeElement().asType()))
            .addParameter(actionClass.getTypeName(), "action")
            .addParameter(Response.class, "response")
            .addParameter(Converter.class, "converter");

    addStatusField(actionClass, builder);
    addResponses(actionClass, builder);
    addBasicHeadersMap(actionClass, builder);
    addResponseHeaders(actionClass, builder);
    builder.addStatement("return action");
    return builder.build();
  }
Пример #22
0
  private void addReadFromParcelMethod(HelperClassBuilder builder) throws ProcessorError {
    ClassName targetClassName = builder.getArgClassName();
    String target = "target";
    String parcel = "parcel";

    MethodSpec.Builder method =
        MethodSpec.methodBuilder(METHOD_NAME_READ_FROM_PARCEL)
            .addAnnotation(
                WeaveBuilder.weave()
                    .constructor(Parcel.class)
                    .withStatement(
                        "%s.%s(this, $1);",
                        fullName(builder.getClassName()), METHOD_NAME_READ_FROM_PARCEL)
                    .build())
            .addModifiers(Modifier.PUBLIC, Modifier.STATIC);

    addClassAsParameter(method, targetClassName, target);
    method.addParameter(Parcel.class, parcel);

    for (int i = 0, c = mFields.size(); i < c; i++) {
      Field field = mFields.get(i);
      CallFormat readCall = Parceler.readCall(field.element);

      if (readCall == null) {
        continue; // TODO : should throw exception or ignore field ?!
      }

      Pair<String, List<Object>> mappedReadCall = mapCall(field, readCall, target, parcel);
      String format = mappedReadCall.fst;
      List<Object> args = mappedReadCall.snd;
      if (field.isAccessible()) {
        format = String.format("$N.$N = %s", format);
        args.add(0, field.element.getSimpleName());
        args.add(0, target);
      } else {
        format = String.format("$N.$N(%s)", format);
        args.add(0, field.setter.getSimpleName());
        args.add(0, target);
      }

      method.addStatement(format, args.toArray(new Object[args.size()]));
    }

    builder.getBuilder().addMethod(method.build());
  }
Пример #23
0
 private MethodSpec createFillErrorMethod(RestActionClass actionClass) {
   MethodSpec.Builder builder =
       MethodSpec.methodBuilder("fillError")
           .addModifiers(Modifier.PUBLIC)
           .addAnnotation(Override.class)
           .returns(actionClass.getTypeName())
           .addParameter(actionClass.getTypeName(), "action")
           .addParameter(Throwable.class, "error");
   for (Element element : actionClass.getAnnotatedElements(Error.class)) {
     String fieldAddress = getFieldAddress(actionClass, element);
     if (TypeUtils.containsType(element, Throwable.class)) {
       builder.addStatement(fieldAddress + " = error", element);
     } else if (TypeUtils.containsType(element, Exception.class)) {
       builder.addStatement(fieldAddress + " = ($T) error", element, Exception.class);
     }
   }
   builder.addStatement("return action");
   return builder.build();
 }
Пример #24
0
 public MethodSpec createPublicConstructor() {
   final ParameterSpec value =
       ParameterSpec.builder(String.class, "value", Modifier.FINAL)
           .addAnnotation(
               AnnotationSpec.builder(Text.class).addMember("required", "false").build())
           .build();
   return MethodSpec.constructorBuilder()
       .addJavadoc(
           "Creates a new {@code $L} instance.\n\n@param value the value representing the actual $L.\n",
           terminalName,
           simpleName)
       .addModifiers(Modifier.PUBLIC)
       .addParameter(value)
       .addCode(
           CodeBlock.builder()
               .addStatement("$T.checkNotNull(value, \"Missing 'value'.\")", Preconditions.class)
               .addStatement("this.value = value.trim()")
               .build())
       .build();
 }
Пример #25
0
  private static MethodSpec createBindMethod(AutoBundleBindingClass target) {
    MethodSpec.Builder builder =
        MethodSpec.methodBuilder("bind")
            .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
            .returns(void.class)
            .addParameter(target.getTargetType(), "target");

    switch (target.getBuilderType()) {
      case Fragment:
        builder.addStatement("bind(target, target.getArguments())");
        break;
      case Intent:
        builder.addParameter(ClassName.get("android.content", "Intent"), "intent");
        builder
            .beginControlFlow("if (intent.getExtras() != null)")
            .addStatement("bind(target, intent.getExtras())")
            .endControlFlow();
        break;
    }
    return builder.build();
  }
Пример #26
0
 public MethodSpec createEquals() {
   final ParameterSpec object =
       ParameterSpec.builder(Object.class, "object", Modifier.FINAL).build();
   return MethodSpec.methodBuilder("equals")
       .addAnnotation(Override.class)
       .addModifiers(Modifier.PUBLIC)
       .returns(boolean.class)
       .addParameter(object)
       .addCode(
           CodeBlock.builder()
               .beginControlFlow("if (this == $L)", object.name)
               .addStatement("return true")
               .endControlFlow()
               .beginControlFlow(
                   "if ($L == null || getClass() != $L.getClass())", object.name, object.name)
               .addStatement("return false")
               .endControlFlow()
               .addStatement(
                   "final $T other = ($T) $L", terminalTypeName, terminalTypeName, object.name)
               .addStatement("return value.equals(other.value)")
               .build())
       .build();
 }
Пример #27
0
 private MethodSpec injectorMethodForSubclasses(
     ImmutableMap<BindingKey, FieldSpec> dependencyFields,
     List<TypeVariableName> typeParameters,
     TypeName injectedTypeName,
     Element injectionElement,
     ImmutableSet<DependencyRequest> dependencies) {
   MethodSpec.Builder methodBuilder =
       MethodSpec.methodBuilder(injectionSiteDelegateMethodName(injectionElement))
           .addModifiers(PUBLIC, STATIC)
           .addParameter(injectedTypeName, "instance")
           .addTypeVariables(typeParameters);
   ImmutableList.Builder<CodeBlock> providedParameters = ImmutableList.builder();
   Set<String> parameterNames = new HashSet<>();
   for (DependencyRequest dependency : dependencies) {
     FieldSpec field = dependencyFields.get(dependency.bindingKey());
     ParameterSpec parameter =
         ParameterSpec.builder(
                 field.type,
                 staticInjectMethodDependencyParameterName(parameterNames, dependency, field))
             .build();
     methodBuilder.addParameter(parameter);
     providedParameters.add(
         frameworkTypeUsageStatement(CodeBlocks.format("$N", parameter), dependency.kind()));
   }
   if (injectionElement.getKind().isField()) {
     methodBuilder.addStatement(
         "instance.$L = $L",
         injectionElement.getSimpleName(),
         getOnlyElement(providedParameters.build()));
   } else {
     methodBuilder.addStatement(
         "instance.$L($L)",
         injectionElement.getSimpleName(),
         makeParametersCodeBlock(providedParameters.build()));
   }
   return methodBuilder.build();
 }
Пример #28
0
  private static MethodSpec createPackMethod(AutoBundleBindingClass target) {
    List<AutoBundleBindingField> args = new ArrayList<>();
    args.addAll(target.getRequiredArgs());
    args.addAll(target.getNotRequiredArgs());

    MethodSpec.Builder builder =
        MethodSpec.methodBuilder("pack")
            .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
            .returns(void.class)
            .addParameter(target.getTargetType(), "source")
            .addParameter(CLASS_BUNDLE, "args");

    for (AutoBundleBindingField arg : args) {
      String key = arg.getArgKey();
      String fieldName = arg.getFieldName();
      TypeName argType = arg.getArgType();
      String operationName = arg.getOperationName("put");

      if (!argType.isPrimitive()) {
        String exceptionMessage = String.format("%s must not be null.", fieldName);
        builder
            .beginControlFlow("if (source.$N == null)", fieldName)
            .addStatement("throw new IllegalStateException($S)", exceptionMessage)
            .endControlFlow();
      }
      if (arg.hasCustomConverter()) {
        TypeName converter = arg.getConverter();
        builder
            .addStatement("$T $NConverter = new $T()", converter, key, converter)
            .addStatement(
                "args.$N($S, $NConverter.convert(source.$N))", operationName, key, key, fieldName);
      } else {
        builder.addStatement("args.$N($S, source.$N)", operationName, key, fieldName);
      }
    }
    return builder.build();
  }
Пример #29
0
 private MethodSpec createRequestMethod(RestActionClass actionClass) {
   MethodSpec.Builder builder =
       MethodSpec.methodBuilder("createRequest")
           .addModifiers(Modifier.PUBLIC)
           .addAnnotation(Override.class)
           .returns(Request.class)
           .addParameter(TypeName.get(actionClass.getTypeElement().asType()), "action")
           .addParameter(RequestBuilder.class, "requestBuilder")
           .addStatement(
               "requestBuilder.setMethod($T.$L)", RestAction.Method.class, actionClass.getMethod())
           .addStatement(
               "requestBuilder.setRequestType($T.$L)",
               RestAction.Type.class,
               actionClass.getRequestType())
           .addStatement("requestBuilder.setPath($S)", actionClass.getPath());
   addPathParams(actionClass, builder);
   addParts(actionClass, builder);
   addRequestHeaders(actionClass, builder);
   addRequestFields(actionClass, builder);
   addRequestQueries(actionClass, builder);
   addRequestBody(actionClass, builder);
   builder.addStatement("return requestBuilder.build()");
   return builder.build();
 }
Пример #30
0
  public List<MethodSpec> buildMethodSpecs() {
    List<MethodSpec> methodSpecs = new ArrayList<>();

    List<AnnotationSpec> overrideAndNonNull =
        Arrays.asList(Specs.buildNonNullAnnotationSpec(), Specs.buildOverrideAnnotationSpec());

    List<AnnotationSpec> overrideAndNullable =
        Arrays.asList(Specs.buildNullableAnnotationSpec(), Specs.buildOverrideAnnotationSpec());

    methodSpecs.add(
        MethodSpec.methodBuilder("getModelClass")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(
                ParameterizedTypeName.get(ClassName.get(Class.class), schema.getModelClassName()))
            .addStatement("return $T.class", schema.getModelClassName())
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getTableName")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.String)
            .addStatement("return $L", TABLE_NAME)
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getPrimaryKey")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.WildcardColumnDef)
            .addStatement("return $N", primaryKey)
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getColumns")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.ColumnList)
            .addStatement("return $L", COLUMNS)
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getEscapedColumnNames")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.StringArray)
            .addStatement("return $L", ESCAPED_COLUMN_NAMES)
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getCreateTableStatement")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.String)
            .addStatement("return $S", sql.buildCreateTableStatement(schema))
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getCreateIndexStatements")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.getList(Types.String))
            .addCode(sql.buildCreateIndexStatements(schema))
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getDropTableStatement")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.String)
            .addStatement("return $S", sql.buildDropTableStatement(schema))
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getInsertStatement")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.String)
            .addStatement("return $S", sql.buildInsertStatement(schema))
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("bindArgs")
            .addModifiers(Modifier.PUBLIC)
            .returns(TypeName.VOID)
            .addParameter(
                ParameterSpec.builder(Types.OrmaConnection, "conn")
                    .addAnnotation(Specs.buildNonNullAnnotationSpec())
                    .build())
            .addParameter(
                ParameterSpec.builder(Types.SQLiteStatement, "statement")
                    .addAnnotation(Specs.buildNonNullAnnotationSpec())
                    .build())
            .addParameter(
                ParameterSpec.builder(schema.getModelClassName(), "model")
                    .addAnnotation(Specs.buildNonNullAnnotationSpec())
                    .build())
            .addCode(buildBindArgs())
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("createModelFromCursor")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(schema.getModelClassName())
            .addParameter(
                ParameterSpec.builder(Types.OrmaConnection, "conn")
                    .addAnnotation(Specs.buildNonNullAnnotationSpec())
                    .build())
            .addParameter(
                ParameterSpec.builder(Types.Cursor, "cursor")
                    .addAnnotation(Specs.buildNonNullAnnotationSpec())
                    .build())
            .addCode(buildCreateModelFromCursor())
            .build());

    return methodSpecs;
  }