本文整理汇总了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();
}
示例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();
}
示例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();
}
示例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();
}
示例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();
}
示例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();
}
示例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();
}
示例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();
}
示例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();
}