本文整理汇总了Java中com.squareup.javapoet.MethodSpec.Builder.addCode方法的典型用法代码示例。如果您正苦于以下问题:Java Builder.addCode方法的具体用法?Java Builder.addCode怎么用?Java Builder.addCode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.squareup.javapoet.MethodSpec.Builder
的用法示例。
在下文中一共展示了Builder.addCode方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: create
import com.squareup.javapoet.MethodSpec.Builder; //导入方法依赖的package包/类
@NotNull
@Override
public MethodSpec create() {
Collection<ColumnMethod> setters = mColumnMethodRepository.findSettersForTableClass(mTableClass.getFullyQualifiedName());
ClassName entityClassName = ClassName.get(mTableClass.getPackageName(), mTableClass.getClassName());
Builder methodBuilder =
MethodSpec.methodBuilder("read")
.addModifiers(PUBLIC)
.addParameter(CURSOR, "cursor", FINAL)
.returns(entityClassName)
.addStatement(
"$T result = new $T()",
entityClassName,
entityClassName
)
.addCode("\n");
ReadCursorCreatorFactory creatorFactory = new ReadCursorCreatorFactory("result", "cursor", mSerializerClassRepository);
for (ColumnMethod setter : setters) {
ReadCursorCreator readCursorCreator = creatorFactory.createReadCursorCreator(setter);
methodBuilder.addCode(readCursorCreator.create());
}
return methodBuilder.addCode("\n")
.addStatement("return result")
.build();
}
示例2: writeBody
import com.squareup.javapoet.MethodSpec.Builder; //导入方法依赖的package包/类
public void writeBody(Builder p) {
for (Property property : bean.getOwnAndInheritedProperties()) {
TypeMirror key = property.getResolvedType();
String accessName = property.getGetterCall();
boolean isPrimitive = key.getKind().isPrimitive();
for (String bsonName : property.getBsonNames()) {
com.squareup.javapoet.CodeBlock.Builder codeBuilder = CodeBlock
.builder();
codeBuilder.addStatement("writer.writeName(\"$L\")", bsonName);
for(EncodeCodeProvider scp:codeProviders){
if ( scp.applies(property) ){
scp.putEncodeCode(codeBuilder, property.getResolvedType(), codeProviders, "value." + property.getGetterCall());
break;
}
}
CodeBlock codeBlock = codeBuilder.build();
if ("_id".equals(bsonName)) {
codeBlock = writeAsId(codeBlock, false);
}
if (isPrimitive) {
p.addCode(codeBlock);
} else {
writeCheckingForNull(p, accessName, codeBlock);
}
}
}
}
示例3: createSetterClass
import com.squareup.javapoet.MethodSpec.Builder; //导入方法依赖的package包/类
private TypeSpec.Builder createSetterClass(ClassName entityClassName, TypeName setterIface, String setterClassName, Property property) {
TypeSpec.Builder setterBuilder = TypeSpec.classBuilder(setterClassName);
Builder setterMethod = MethodSpec.methodBuilder("set")
.addParameter(ParameterSpec.builder(entityClassName, "instance").build())
.addParameter(CodeUtil.bsonReaderParameter())
.addParameter(CodeUtil.bsonDecoderContextParameter())
.addModifiers(Modifier.PUBLIC);
setterMethod.addCode(decodeCodeProviders.getDecodeCode(property));
return setterBuilder.addMethod(setterMethod.build()).addSuperinterface(setterIface);
}
示例4: addEncodeMethod
import com.squareup.javapoet.MethodSpec.Builder; //导入方法依赖的package包/类
private void addEncodeMethod(com.squareup.javapoet.TypeSpec.Builder codecBuilder, ClassName entityClassName, Bean bean) {
Builder methodSpec = MethodSpec.methodBuilder("encodeVariables")
.addAnnotation(Override.class)
.addAnnotation(Util.suppressWarningsAnnotation())
.addModifiers(Modifier.PROTECTED)
.addParameter(CodeUtil.bsonWriterParameter())
.addParameter(ParameterSpec.builder(entityClassName, "value").build())
.addParameter(CodeUtil.bsonEncoderContextParameter())
.addJavadoc("{@inhericDoc}\n");
GetterGenerator getterGenerator = new GetterGenerator(processingEnv, bean);
if (!ai.hasCustomId()) {
com.squareup.javapoet.CodeBlock.Builder cb = CodeBlock.builder();
if (customGeneratorIsBsonned()) {
cb.addStatement("Object vid = (($T)idGenerator).generate(value)",
ClassName.get(bsoneerIdGenerator));
} else {
cb.addStatement("Object vid = idGenerator.generate()");
}
cb.addStatement("$T cid = registry.get(vid.getClass())",
CodeUtil.bsonCodecTypeName());
cb.addStatement("encoderContext.encodeWithChildContext(cid, writer, vid)");
methodSpec.addCode(getterGenerator.writeAsId(cb.build(), true));
}
getterGenerator.writeBody(methodSpec);
methodSpec.addStatement("super.encodeVariables(writer,value,encoderContext)");
codecBuilder.addMethod(methodSpec.build());
}
示例5: 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();
}
示例6: writeCheckingForNull
import com.squareup.javapoet.MethodSpec.Builder; //导入方法依赖的package包/类
public void writeCheckingForNull(Builder p, String tn, CodeBlock cb) {
p.beginControlFlow("if (value.$L != null)", tn);
p.addCode(cb);
p.endControlFlow();
}