本文整理汇总了Java中com.squareup.javapoet.MethodSpec.Builder类的典型用法代码示例。如果您正苦于以下问题:Java Builder类的具体用法?Java Builder怎么用?Java Builder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Builder类属于com.squareup.javapoet.MethodSpec包,在下文中一共展示了Builder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: serialize
import com.squareup.javapoet.MethodSpec.Builder; //导入依赖的package包/类
@Override
public void serialize(Builder builder, int depth, String fieldName, List<String> processedFieldNames, String getter, boolean isObjectProperty, boolean checkIfNull, boolean writeIfNull, boolean writeCollectionElementIfNull) {
if (checkIfNull) {
builder.beginControlFlow("if ($L != null)", getter);
}
if (isObjectProperty) {
builder.addStatement("$L.writeStringField($S, $L)", JSON_GENERATOR_VARIABLE_NAME, fieldName, getter);
} else {
builder.addStatement("$L.writeString($L)", JSON_GENERATOR_VARIABLE_NAME, getter);
}
if (checkIfNull) {
if (writeIfNull) {
builder.nextControlFlow("else");
if (isObjectProperty) {
builder.addStatement("$L.writeFieldName($S)", JSON_GENERATOR_VARIABLE_NAME, fieldName);
}
builder.addStatement("$L.writeNull()", JSON_GENERATOR_VARIABLE_NAME);
}
builder.endControlFlow();
}
}
示例2: serialize
import com.squareup.javapoet.MethodSpec.Builder; //导入依赖的package包/类
@Override
public void serialize(Builder builder, int depth, String fieldName, List<String> processedFieldNames, String getter, boolean isObjectProperty, boolean checkIfNull, boolean writeIfNull, boolean writeCollectionElementIfNull) {
if (checkIfNull) {
builder.beginControlFlow("if ($L != null)", getter);
}
if (isObjectProperty) {
builder.addStatement("$L.writeFieldName($S)", JSON_GENERATOR_VARIABLE_NAME, fieldName);
}
builder.addStatement("$L.serialize($L, $L, true)", mJsonMapperVariableName, getter, JSON_GENERATOR_VARIABLE_NAME);
if (checkIfNull) {
if (writeIfNull) {
builder.nextControlFlow("else");
if (isObjectProperty) {
builder.addStatement("$L.writeFieldName($S)", JSON_GENERATOR_VARIABLE_NAME, fieldName);
}
builder.addStatement("$L.writeNull()", JSON_GENERATOR_VARIABLE_NAME);
}
builder.endControlFlow();
}
}
示例3: serialize
import com.squareup.javapoet.MethodSpec.Builder; //导入依赖的package包/类
@Override
public void serialize(Builder builder, int depth, String fieldName, List<String> processedFieldNames, String getter, boolean isObjectProperty, boolean checkIfNull, boolean writeIfNull, boolean writeCollectionElementIfNull) {
if (!isPrimitive && checkIfNull) {
builder.beginControlFlow("if ($L != null)", getter);
}
if (isObjectProperty) {
builder.addStatement("$L.writeBooleanField($S, $L)", JSON_GENERATOR_VARIABLE_NAME, fieldName, getter);
} else {
builder.addStatement("$L.writeBoolean($L)", JSON_GENERATOR_VARIABLE_NAME, getter);
}
if (!isPrimitive && checkIfNull) {
if (writeIfNull) {
builder.nextControlFlow("else");
if (isObjectProperty) {
builder.addStatement("$L.writeFieldName($S)", JSON_GENERATOR_VARIABLE_NAME, fieldName);
}
builder.addStatement("$L.writeNull()", JSON_GENERATOR_VARIABLE_NAME);
}
builder.endControlFlow();
}
}
示例4: serialize
import com.squareup.javapoet.MethodSpec.Builder; //导入依赖的package包/类
@Override
public void serialize(Builder builder, int depth, String fieldName, List<String> processedFieldNames, String getter, boolean isObjectProperty, boolean checkIfNull, boolean writeIfNull, boolean writeCollectionElementIfNull) {
if (!isPrimitive && checkIfNull) {
builder.beginControlFlow("if ($L != null)", getter);
}
if (isObjectProperty) {
builder.addStatement("$L.writeNumberField($S, $L)", JSON_GENERATOR_VARIABLE_NAME, fieldName, getter);
} else {
builder.addStatement("$L.writeNumber($L)", JSON_GENERATOR_VARIABLE_NAME, getter);
}
if (!isPrimitive && checkIfNull) {
if (writeIfNull) {
builder.nextControlFlow("else");
if (isObjectProperty) {
builder.addStatement("$L.writeFieldName($S)", JSON_GENERATOR_VARIABLE_NAME, fieldName);
}
builder.addStatement("$L.writeNull()", JSON_GENERATOR_VARIABLE_NAME);
}
builder.endControlFlow();
}
}
示例5: serialize
import com.squareup.javapoet.MethodSpec.Builder; //导入依赖的package包/类
@Override
public void serialize(Builder builder, int depth, String fieldName, List<String> processedFieldNames, String getter, boolean isObjectProperty, boolean checkIfNull, boolean writeIfNull, boolean writeCollectionElementIfNull) {
if (checkIfNull) {
builder.beginControlFlow("if ($L != null)", getter);
}
if (isObjectProperty) {
builder.addStatement("$L.writeFieldName($S)", JSON_GENERATOR_VARIABLE_NAME, fieldName);
}
builder.addStatement("$L.serialize($L, $L, true)", mMapperVariableName, getter, JSON_GENERATOR_VARIABLE_NAME);
if (checkIfNull) {
if (writeIfNull) {
builder.nextControlFlow("else");
if (isObjectProperty) {
builder.addStatement("$L.writeFieldName($S)", JSON_GENERATOR_VARIABLE_NAME, fieldName);
}
builder.addStatement("$L.writeNull()", JSON_GENERATOR_VARIABLE_NAME);
}
builder.endControlFlow();
}
}
示例6: serialize
import com.squareup.javapoet.MethodSpec.Builder; //导入依赖的package包/类
@Override
public void serialize(Builder builder, int depth, String fieldName, List<String> processedFieldNames, String getter, boolean isObjectProperty, boolean checkIfNull, boolean writeIfNull, boolean writeCollectionElementIfNull) {
if (!mTypeName.isPrimitive() && checkIfNull) {
builder.beginControlFlow("if ($L != null)", getter);
}
builder.addStatement("$T.typeConverterFor($T.class).serialize($L, $S, $L, $L)", LoganSquare.class, mTypeName, getter, isObjectProperty
? fieldName : null, isObjectProperty, JSON_GENERATOR_VARIABLE_NAME);
if (!mTypeName.isPrimitive() && checkIfNull) {
if (writeIfNull) {
builder.nextControlFlow("else");
if (isObjectProperty) {
builder.addStatement("$L.writeFieldName($S)", JSON_GENERATOR_VARIABLE_NAME, fieldName);
}
builder.addStatement("$L.writeNull()", JSON_GENERATOR_VARIABLE_NAME);
}
builder.endControlFlow();
}
}
示例7: serialize
import com.squareup.javapoet.MethodSpec.Builder; //导入依赖的package包/类
@Override
public void serialize(Builder builder, int depth, String fieldName, List<String> processedFieldNames, String getter, boolean isObjectProperty, boolean checkIfNull, boolean writeIfNull, boolean writeCollectionElementIfNull) {
if (checkIfNull) {
builder.beginControlFlow("if ($L != null)", getter);
}
builder.addStatement("$L.serialize($L, $L, $L)", ObjectMapperInjector.getMapperVariableName(ObjectMapper.class), getter, JSON_GENERATOR_VARIABLE_NAME, isObjectProperty);
if (checkIfNull) {
if (writeIfNull) {
builder.nextControlFlow("else");
if (isObjectProperty) {
builder.addStatement("$L.writeFieldName($S)", JSON_GENERATOR_VARIABLE_NAME, fieldName);
}
builder.addStatement("$L.writeNull()", JSON_GENERATOR_VARIABLE_NAME);
}
builder.endControlFlow();
}
}
示例8: 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();
}
示例9: buildExtraMethodSpecs
import com.squareup.javapoet.MethodSpec.Builder; //导入依赖的package包/类
protected void buildExtraMethodSpecs(TypeSpec.Builder typespecbuilder) throws AnnotationProcessingException {
MethodInfo methodinfo;
CodeBlock.Builder code;
for (String name : m_extramtdinfo.keySet()) {
methodinfo = m_extramtdinfo.get(name);
code = CodeBlock.builder();
switch (name) {
case "getNativeFieldInfo_static":
code.addStatement("return $1N", m_fieldsinfo.get("nfieldinfo").name);
break;
default:
throw new AnnotationProcessingException(null, "Method %s is not supported.", name);
}
typespecbuilder.addMethod(methodinfo.specbuilder.addCode(code.build()).build());
}
}
示例10: 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();
}
示例11: 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();
}
示例12: getJavaFile
import com.squareup.javapoet.MethodSpec.Builder; //导入依赖的package包/类
public JavaFile getJavaFile() {
ClassName bsoneerCodecProviderClassName = ClassName.get(basePackage, "BsoneeCodecProvider");
TypeSpec.Builder codecProviderBuilder = TypeSpec.classBuilder(bsoneerCodecProviderClassName.simpleName())
.addJavadoc(ProcessorJavadocs.GENERATED_BY_BSONEER)
.addSuperinterface(CodeUtil.bsonCodecProviderTypeName())
.addModifiers(PUBLIC).addAnnotation(AnnotationSpec.builder(Generated.class)
.addMember("value", "$S", BsonProcessor.class.getCanonicalName())
.build());;
addBaseConstructor(codecProviderBuilder);
addGetCodecMethod(codecProviderBuilder);
return JavaFile.builder(bsoneerCodecProviderClassName.packageName(), codecProviderBuilder.build())
.addFileComment(ProcessorJavadocs.GENERATED_BY_BSONEER)
.indent("\t")
.build();
}
示例13: addGetCodecMethod
import com.squareup.javapoet.MethodSpec.Builder; //导入依赖的package包/类
private void addGetCodecMethod(com.squareup.javapoet.TypeSpec.Builder codecProviderBuilder) {
TypeVariableName typeVariableName = TypeVariableName.get("T");
TypeName clazzName = ParameterizedTypeName.get(ClassName.get(Class.class), typeVariableName);
TypeName codecName = ParameterizedTypeName.get((ClassName) CodeUtil.bsonCodecTypeName(), typeVariableName);
Builder methodSpec = MethodSpec.methodBuilder("get")
.addAnnotation(Override.class)
.addAnnotation(Util.suppressWarningsAnnotation(true, false))
.addModifiers(Modifier.PUBLIC)
.addParameter(ParameterSpec.builder(clazzName, "clazz", Modifier.FINAL).build())
.addParameter(CodeUtil.bsonRegistryParameter())
.addTypeVariable(typeVariableName)
.returns(codecName)
.addJavadoc("{@inhericDoc}\n");
for (ClassName entityClassName : generated) {
ClassName bsoneerCodecClassName = Util.bsoneeName(entityClassName, GeneratedClasses.BSONEE_CODEC_SUFFIX);
methodSpec.beginControlFlow("if (clazz == $T.class)", entityClassName);
methodSpec.addStatement("return (Codec<T>) new $T(registry)", bsoneerCodecClassName);
methodSpec.endControlFlow();
}
methodSpec.addStatement("return null");
codecProviderBuilder.addMethod(methodSpec.build());
}
示例14: writeBody
import com.squareup.javapoet.MethodSpec.Builder; //导入依赖的package包/类
public void writeBody(TypeSpec.Builder codecBuilder, ClassName entityClassName) {
TypeName setterIface = ParameterizedTypeName.get(ClassName.get(BsoneeBaseSetter.class), entityClassName);
Map<String, String> setters = new LinkedHashMap<String, String>();
for (Property property : bean.getOwnAndInheritedProperties()) {
String setterClassName = property.getUpperName() + "Setter";
setters.put(property.getBsonNames().iterator().next(), setterClassName);
codecBuilder.addType(createSetterClass(entityClassName, setterIface, setterClassName, property).build());
}
Builder setupSetterBuilder = MethodSpec.methodBuilder("setupSetters").addModifiers(Modifier.PROTECTED);
for (Entry<String, String> entry : setters.entrySet()) {
setupSetterBuilder.addStatement("settersByName.put(\"$L\",new $L())",
entry.getKey(), entry.getValue());
}
codecBuilder.addMethod(setupSetterBuilder.build());
}
示例15: addDecodeStatements
import com.squareup.javapoet.MethodSpec.Builder; //导入依赖的package包/类
@Override
public void addDecodeStatements(CodeGeneratorContext ctx) {
Builder builder = ctx.builder();
IdModel idModel = ctx.field().idModel();
builder.addStatement("$T id = reader.readObjectId()", ObjectId.class);
if (idModel != null) {
if (idModel.conversion() == IdConversion.HEX_OBJECTID) {
builder.addStatement(ctx.setter("id.toHexString()"));
}
else if (idModel.conversion() == IdConversion.BASE64_OBJECTID) {
builder.addStatement(
ctx.setter("$T.getUrlEncoder().encodeToString(id.toByteArray())"),
Base64.class);
}
}
}