当前位置: 首页>>代码示例>>Java>>正文


Java Builder.build方法代码示例

本文整理汇总了Java中com.squareup.javapoet.MethodSpec.Builder.build方法的典型用法代码示例。如果您正苦于以下问题:Java Builder.build方法的具体用法?Java Builder.build怎么用?Java Builder.build使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.squareup.javapoet.MethodSpec.Builder的用法示例。


在下文中一共展示了Builder.build方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createAsSpecMethod

import com.squareup.javapoet.MethodSpec.Builder; //导入方法依赖的package包/类
private static MethodSpec createAsSpecMethod(OutputValue value, OutputSpec spec) {
  List<TypeVariableName> missingTypeVariables = extractMissingTypeVariablesForValue(value, spec);

  Builder builder =
      MethodSpec.methodBuilder("as" + spec.outputClass().simpleName())
          .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
          .returns(spec.parameterizedOutputClass())
          .addTypeVariables(missingTypeVariables)
          .addStatement("return ($T) this", spec.parameterizedOutputClass());

  // if there are type variables that this sub-type doesn't use, they will lead to 'unchecked
  // cast'
  // warnings when compiling the generated code. These warnings are safe to suppress, since this
  // sub type will never use those type variables.
  if (!missingTypeVariables.isEmpty()) {
    builder.addAnnotation(SUPPRESS_UNCHECKED_WARNINGS);
  }

  return builder.build();
}
 
开发者ID:spotify,项目名称:dataenum,代码行数:21,代码来源:ValueTypeFactory.java

示例2: createOnSensorChangedListenerMethod

import com.squareup.javapoet.MethodSpec.Builder; //导入方法依赖的package包/类
/**
 * Creates the implementation of {@code SensorEventListener#onSensorChanged(SensorEvent)} which
 * calls the annotated method on our target class.
 *
 * @param annotatedMethod Method annotated with {@code OnSensorChanged}.
 * @return {@link MethodSpec} of {@code SensorEventListener#onSensorChanged(SensorEvent)}.
 */
@NonNull
private static MethodSpec createOnSensorChangedListenerMethod(
    @Nullable AnnotatedMethod annotatedMethod) {
    ParameterSpec sensorEventParameter = ParameterSpec.builder(SENSOR_EVENT, "event").build();
    Builder methodBuilder =
        getBaseMethodBuilder("onSensorChanged").addParameter(sensorEventParameter);

    if (annotatedMethod != null) {
        ExecutableElement sensorChangedExecutableElement =
            annotatedMethod.getExecutableElement();
        methodBuilder.addStatement("target.$L($N)",
            sensorChangedExecutableElement.getSimpleName(), sensorEventParameter);
    }

    return methodBuilder.build();
}
 
开发者ID:dvoiss,项目名称:SensorAnnotations,代码行数:24,代码来源:SensorAnnotationsFileBuilder.java

示例3: createOnAccuracyChangedListenerMethod

import com.squareup.javapoet.MethodSpec.Builder; //导入方法依赖的package包/类
/**
 * Creates the implementation of {@code SensorEventListener#onAccuracyChanged(Sensor, int)}
 * which calls the annotated method on our target class.
 *
 * @param annotatedMethod Method annotated with {@link OnAccuracyChanged}.
 * @return {@link MethodSpec} of {@code SensorEventListener#onAccuracyChanged(Sensor, int)}.
 */
@NonNull
private static MethodSpec createOnAccuracyChangedListenerMethod(
    @Nullable AnnotatedMethod annotatedMethod) {
    ParameterSpec sensorParameter = ParameterSpec.builder(SENSOR, "sensor").build();
    ParameterSpec accuracyParameter = ParameterSpec.builder(TypeName.INT, "accuracy").build();
    Builder methodBuilder =
        getBaseMethodBuilder("onAccuracyChanged").addParameter(sensorParameter)
            .addParameter(accuracyParameter);

    if (annotatedMethod != null) {
        ExecutableElement accuracyChangedExecutableElement =
            annotatedMethod.getExecutableElement();
        methodBuilder.addStatement("target.$L($N, $N)",
            accuracyChangedExecutableElement.getSimpleName(), sensorParameter,
            accuracyParameter);
    }

    return methodBuilder.build();
}
 
开发者ID:dvoiss,项目名称:SensorAnnotations,代码行数:27,代码来源:SensorAnnotationsFileBuilder.java

示例4: createAsMethod

import com.squareup.javapoet.MethodSpec.Builder; //导入方法依赖的package包/类
public MethodSpec createAsMethod(OutputSpec spec) {
  Builder builder =
      MethodSpec.methodBuilder("as" + value.name())
          .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
          .returns(value.parameterizedOutputClass())
          .addStatement("return ($T) this", value.parameterizedOutputClass());

  if (!ValueTypeFactory.extractMissingTypeVariablesForValue(value, spec).isEmpty()
      && value.hasTypeVariables()) {
    builder.addAnnotation(ValueTypeFactory.SUPPRESS_UNCHECKED_WARNINGS);
  }

  return builder.build();
}
 
开发者ID:spotify,项目名称:dataenum,代码行数:15,代码来源:ValueMethods.java

示例5: create

import com.squareup.javapoet.MethodSpec.Builder; //导入方法依赖的package包/类
@NotNull
@Override
public MethodSpec create() {
  Parameter parameter = mMethod.getParameter();

  Builder builder = MethodSpec.methodBuilder(mMethod.getMethodName())
      .addJavadoc(createJavadoc())
      .addAnnotation(Override.class)
      .addModifiers(PUBLIC)
      .addParameter(ClassName.bestGuess(parameter.getFullyQualifiedType()), parameter.getVariableName(), FINAL)
      .returns(Long.class)
      .addStatement("$T result = null", Long.class)
      .addCode("\n")
      .addStatement("$T contentValues = $N($L)", CONTENT_VALUES, mCreateContentValuesSpec, parameter.getVariableName())
      .addStatement("$T id = mDatabase.insert($S, null, contentValues)", long.class, mTableClass.getTableName())
      .beginControlFlow("if (id != -1)");

  if (mPrimaryKeySetter != null && Long.class.getName().equals(mPrimaryKeySetter.getType())) {
    builder.addStatement("$L.$L(id)", parameter.getVariableName(), mPrimaryKeySetter.getMethodName());
  }

  builder.addStatement("result = id")
      .endControlFlow()
      .addCode("\n")
      .addStatement("return result");

  return builder.build();
}
 
开发者ID:nhaarman,项目名称:trinity,代码行数:29,代码来源:CreateMethodCreator.java

示例6: create

import com.squareup.javapoet.MethodSpec.Builder; //导入方法依赖的package包/类
@NotNull
@Override
public MethodSpec create() {
  ClassName entityClassName = ClassName.get(mTableClass.getPackageName(), mTableClass.getClassName());
  Parameter parameter = mMethod.getParameter();
  if (parameter == null) {
    throw new IllegalStateException("Missing parameter.");
  }

  Builder builder = MethodSpec.methodBuilder(mMethod.getMethodName())
      .addJavadoc(createJavadoc())
      .addAnnotation(Override.class)
      .addModifiers(PUBLIC)
      .addParameter(TypeName.get(parameter.getType()), parameter.getVariableName(), FINAL)
      .beginControlFlow("try")
      .addStatement("$N.beginTransaction()", mDatabaseFieldSpec)
      .addCode("\n")
      .beginControlFlow("for ($T entity: $L)", entityClassName, parameter.getVariableName())
      .addStatement("$T contentValues = $N(entity)", CONTENT_VALUES, mCreateContentValuesSpec)
      .addStatement("$N.insert($S, null, contentValues)", mDatabaseFieldSpec, mTableClass.getTableName())
      .endControlFlow()
      .addCode("\n")
      .addStatement("$N.setTransactionSuccessful()", mDatabaseFieldSpec)
      .nextControlFlow("finally")
      .addStatement("$N.endTransaction()", mDatabaseFieldSpec)
      .endControlFlow();

  return builder.build();
}
 
开发者ID:nhaarman,项目名称:trinity,代码行数:30,代码来源:CreateAllMethodCreator.java

示例7: createBuildMethod

import com.squareup.javapoet.MethodSpec.Builder; //导入方法依赖的package包/类
private MethodSpec createBuildMethod(TypeName objectType) {
    Builder method = methodBuilder(BUILD_METHOD).addModifiers(PUBLIC).returns(objectType);
    method.addStatement("$T $N = new $T()", objectType, "object", objectType);
    method.addStatement("$N($N)", INIT_METHOD, "object");
    method.addStatement("return object");
    return method.build();
}
 
开发者ID:josketres,项目名称:builderator,代码行数:8,代码来源:Renderer.java

示例8: createInitMethod

import com.squareup.javapoet.MethodSpec.Builder; //导入方法依赖的package包/类
private MethodSpec createInitMethod(TargetClass target, TypeName objectType, boolean parentBuilderClass) {
    Builder method = methodBuilder(INIT_METHOD)
        .addModifiers(PROTECTED)
        .addParameter(ParameterSpec.builder(objectType, "object").build());
    if (parentBuilderClass) {
        method.addStatement("super.$N($N)", INIT_METHOD, "object");
    }

    for (Property property : target.getProperties()) {
        method.addStatement("$N.$N($N)", "object", property.getSetterName(), property.getName());
    }
    return method.build();
}
 
开发者ID:josketres,项目名称:builderator,代码行数:14,代码来源:Renderer.java

示例9: createConstructor

import com.squareup.javapoet.MethodSpec.Builder; //导入方法依赖的package包/类
/**
 * Create the constructor for our generated class.
 *
 * @param targetParameter The target class that has annotated methods.
 * @param itemsMap A map of sensor types found in the annotations with the annotated methods.
 * @return {@link MethodSpec} representing the constructor of our generated class.
 */
@NonNull
private static MethodSpec createConstructor(@NonNull ParameterSpec targetParameter,
    @NonNull Map<Integer, Map<Class, AnnotatedMethod>> itemsMap) throws ProcessingException {
    ParameterSpec contextParameter = ParameterSpec.builder(CONTEXT, "context").build();
    Builder constructorBuilder = MethodSpec.constructorBuilder()
        .addModifiers(Modifier.PUBLIC)
        .addParameter(contextParameter)
        .addParameter(targetParameter)
        .addStatement("this.$N = ($T) $N.getSystemService(SENSOR_SERVICE)",
            SENSOR_MANAGER_FIELD, SENSOR_MANAGER, contextParameter)
        .addStatement("this.$N = new $T()", LISTENER_WRAPPERS_FIELD, ARRAY_LIST);

    // Loop through the sensor types that we have annotations for and create the listeners which
    // will call the annotated methods on our target class.
    for (Integer sensorType : itemsMap.keySet()) {
        Map<Class, AnnotatedMethod> annotationMap = itemsMap.get(sensorType);
        AnnotatedMethod sensorChangedAnnotatedMethod = annotationMap.get(OnSensorChanged.class);
        AnnotatedMethod accuracyChangedAnnotatedMethod =
            annotationMap.get(OnAccuracyChanged.class);
        AnnotatedMethod triggerAnnotatedMethod = annotationMap.get(OnTrigger.class);

        if (sensorType == TYPE_SIGNIFICANT_MOTION && (accuracyChangedAnnotatedMethod != null
            || sensorChangedAnnotatedMethod != null)) {
            throw new ProcessingException(null, String.format(
                "@%s and @%s are not supported for the \"TYPE_SIGNIFICANT_MOTION\" type. Use @%s for this type.",
                OnSensorChanged.class.getSimpleName(), OnAccuracyChanged.class.getSimpleName(),
                OnTrigger.class.getSimpleName()));
        } else if (sensorType != TYPE_SIGNIFICANT_MOTION && triggerAnnotatedMethod != null) {
            throw new ProcessingException(null, String.format(
                "The @%s is only supported for the \"TYPE_SIGNIFICANT_MOTION\" type.",
                OnTrigger.class.getSimpleName()));
        }

        CodeBlock listenerWrapperCodeBlock;
        if (triggerAnnotatedMethod != null) {
            listenerWrapperCodeBlock = createTriggerListenerWrapper(triggerAnnotatedMethod);
            constructorBuilder.addCode(listenerWrapperCodeBlock);
        } else if (sensorChangedAnnotatedMethod != null
            || accuracyChangedAnnotatedMethod != null) {
            listenerWrapperCodeBlock =
                createSensorListenerWrapper(sensorType, sensorChangedAnnotatedMethod,
                    accuracyChangedAnnotatedMethod);
            constructorBuilder.addCode(listenerWrapperCodeBlock);
        }
    }

    return constructorBuilder.build();
}
 
开发者ID:dvoiss,项目名称:SensorAnnotations,代码行数:56,代码来源:SensorAnnotationsFileBuilder.java


注:本文中的com.squareup.javapoet.MethodSpec.Builder.build方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。