本文整理汇总了Java中com.squareup.javapoet.CodeBlock类的典型用法代码示例。如果您正苦于以下问题:Java CodeBlock类的具体用法?Java CodeBlock怎么用?Java CodeBlock使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CodeBlock类属于com.squareup.javapoet包,在下文中一共展示了CodeBlock类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generateSeeMethodJavadocInternal
import com.squareup.javapoet.CodeBlock; //导入依赖的package包/类
private CodeBlock generateSeeMethodJavadocInternal(
TypeName nameOfClassContainingMethod, String methodName,
List<Object> safeParameterNames) {
StringBuilder javadocString = new StringBuilder("@see $T#$L(");
List<Object> javadocArgs = new ArrayList<>();
javadocArgs.add(nameOfClassContainingMethod);
javadocArgs.add(methodName);
for (Object param : safeParameterNames) {
javadocString.append("$T, ");
javadocArgs.add(param);
}
if (javadocArgs.size() > 2) {
javadocString = new StringBuilder(javadocString.substring(0, javadocString.length() - 2));
}
javadocString.append(")\n");
return CodeBlock.of(javadocString.toString(), javadocArgs.toArray(new Object[0]));
}
示例2: createIsJsObject
import com.squareup.javapoet.CodeBlock; //导入依赖的package包/类
private MethodSpec createIsJsObject() {
CodeBlock.Builder builder = CodeBlock.builder();
builder.addStatement("$1T jsType = type.getAnnotation($1T.class)", ClassNames.JSINTEROP_JSTYPE);
Stream.of(
"jsType == null",
"!jsType.isNative()",
"!JsPackage.GLOBAL.equals(jsType.namespace())",
"!\"Object\".equals(jsType.name())")
.forEachOrdered(statement -> builder
.beginControlFlow("if (" + statement + ")")
.addStatement("return false")
.endControlFlow());
builder.addStatement("return true");
return MethodSpec.methodBuilder("isJsObject")
.addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
.addAnnotation(ClassNames.GWT_INCOMPATIBLE)
.addParameter(ParameterizedTypeName.get(ClassName.get(Class.class), WildcardTypeName.subtypeOf(Object.class)), "type")
.returns(Boolean.TYPE)
.addCode(builder.build())
.build();
}
示例3: initProtocolFactory
import com.squareup.javapoet.CodeBlock; //导入依赖的package包/类
@Override
public MethodSpec initProtocolFactory(IntermediateModel model) {
MethodSpec.Builder methodSpec = MethodSpec.methodBuilder("init")
.returns(listOfUnmarshallersType)
.addModifiers(Modifier.PRIVATE);
methodSpec.addStatement("$T<$T> unmarshallers = new $T<>()", List.class, unmarshallerType, ArrayList.class);
errorUnmarshallers(model).forEach(methodSpec::addCode);
methodSpec.addCode(CodeBlock.builder().add("unmarshallers.add(new $T($T.class));",
getErrorUnmarshallerClass(model),
poetExtensions.getModelClass(model.getSdkModeledExceptionBaseClassName()))
.build());
methodSpec.addStatement("return $N", "unmarshallers");
return methodSpec.build();
}
示例4: generateSeeMethodJavadocInternal
import com.squareup.javapoet.CodeBlock; //导入依赖的package包/类
private CodeBlock generateSeeMethodJavadocInternal(
TypeName nameOfClassContainingMethod, String methodName,
List<Object> safeParameterNames) {
String javadocString = "@see $T#$L(";
List<Object> javadocArgs = new ArrayList<>();
javadocArgs.add(nameOfClassContainingMethod);
javadocArgs.add(methodName);
for (Object param : safeParameterNames) {
javadocString += "$T, ";
javadocArgs.add(param);
}
if (javadocArgs.size() > 2) {
javadocString = javadocString.substring(0, javadocString.length() - 2);
}
javadocString += ")\n";
return CodeBlock.of(javadocString, javadocArgs.toArray(new Object[0]));
}
示例5: generateRequestOptionOverride
import com.squareup.javapoet.CodeBlock; //导入依赖的package包/类
private MethodSpec generateRequestOptionOverride(ExecutableElement methodToOverride) {
return MethodSpec.overriding(methodToOverride)
.returns(glideOptionsName)
.addCode(CodeBlock.builder()
.add("return ($T) super.$N(", glideOptionsName, methodToOverride.getSimpleName())
.add(FluentIterable.from(methodToOverride.getParameters())
.transform(new Function<VariableElement, String>() {
@Override
public String apply(VariableElement input) {
return input.getSimpleName().toString();
}
})
.join(Joiner.on(", ")))
.add(");\n")
.build())
.build();
}
示例6: responseHandler
import com.squareup.javapoet.CodeBlock; //导入依赖的package包/类
@Override
public CodeBlock responseHandler(OperationModel opModel) {
ClassName unmarshaller = poetExtensions.getTransformClass(opModel.getReturnType().getReturnType() + "Unmarshaller");
ClassName returnType = poetExtensions.getModelClass(opModel.getReturnType().getReturnType());
return CodeBlock
.builder()
.add("\n\n$T<$T> responseHandler = $L.createResponseHandler(new $T()" +
" .withPayloadJson($L)" +
" .withHasStreamingSuccessResponse($L), new $T());",
HttpResponseHandler.class,
returnType,
"protocolFactory",
JsonOperationMetadata.class,
!opModel.getHasBlobMemberAsPayload(),
opModel.hasStreamingOutput(),
unmarshaller)
.build();
}
示例7: regionId
import com.squareup.javapoet.CodeBlock; //导入依赖的package包/类
private FieldSpec regionId(Set<String> allRegionIds) {
CodeBlock.Builder builder = CodeBlock.builder()
.add("$T.asList(\n$>$>", Arrays.class);
Iterator<String> iterator = allRegionIds.iterator();
while (iterator.hasNext()) {
builder.add("$S", iterator.next());
if (iterator.hasNext()) {
builder.add(",\n");
}
}
builder.add("$<$<)");
TypeName listType = ParameterizedTypeName.get(List.class, String.class);
return FieldSpec.builder(listType, "REGION_IDS", STATIC, FINAL)
.initializer(builder.build())
.build();
}
示例8: generateSharedProperties
import com.squareup.javapoet.CodeBlock; //导入依赖的package包/类
public static boolean generateSharedProperties(Collection<FieldData> fields,
Elements elements, Filer filer, ProcessorPrinter pp){
final ClassName cn_sp = ClassName.get(PKG_DM_INTERNAL, SIMPLE_NAME_SHARED_PROP);
CodeBlock.Builder staticBuilder = CodeBlock.builder();
for(FieldData fd : fields){
staticBuilder.add("$T.putToCache($S, $S, $L);\n", cn_sp, fd.getTypeCompat().toString(),
fd.getPropertyName(), fd.getComplexType());
}
String classSimpleName = SIMPLE_NAME_SHARED_PROP + "_" + findBestIndex(elements);
TypeSpec typeSpec = TypeSpec.classBuilder(classSimpleName)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addStaticBlock(staticBuilder.build())
.addJavadoc(CodeBlock.of(DOC))
.build();
try {
JavaFile javaFile = JavaFile.builder(PKG_DM_INTERNAL, typeSpec)
.build();
// System.out.println(javaFile.toString());
javaFile.writeTo(filer);
} catch (IOException e) {
pp.error(TAG, "generateSharedProperties", Util.toString(e));
return false;
}
return true;
}
示例9: warningComment
import com.squareup.javapoet.CodeBlock; //导入依赖的package包/类
private CodeBlock warningComment() {
// This warning comment exists because it doesn't look like we can use reflection
// before compilation to determine if the Annotated element actually has
// the expected method. If the developer runs the annotation processor and the method
// actually doesn't exist, they will see this comment right above the generated code to
// hopefully guide them in the correct direction.
final String elementVarName = Utils.tryToExtractClassName(typeUtils, annotatedElement);
final String expectedMethod = annotatedElement.getAnnotation(LifecycleAware.class).method();
return CodeBlock.builder()
.add("/*\n")
.add("* This will have a problem compiling if the method $L::$L()\n", elementVarName, expectedMethod)
.add("* 1. does not exist.\n")
.add("* 2. is not visible.\n")
.add("*/\n")
.build();
}
示例10: wrapObserverIfNecessary
import com.squareup.javapoet.CodeBlock; //导入依赖的package包/类
/**
* Returns the element formatted as a code block if the element is an observer.
* If the element is not an observer, wrap it in an observer and return that instead.
*
* @param element
* @return
*/
private CodeBlock wrapObserverIfNecessary(Element element) {
boolean needsWrapper = !Utils.implementsInterface(elementUtils, typeUtils, element, LifecycleAwareObserver.class);
if (needsWrapper) {
return CodeBlock.builder()
.add("new $T($L.$L)",
wrappedProducer.getClassName(element),
PARAM_NAME_TARGET,
element.getSimpleName())
.build();
} else {
return CodeBlock.builder()
.add("$L.$L", PARAM_NAME_TARGET, element.getSimpleName())
.build();
}
}
示例11: builderGroupBuild
import com.squareup.javapoet.CodeBlock; //导入依赖的package包/类
public static MethodSpec builderGroupBuild(ClassName className, List<MethodSpec> methodSpecList, List<FieldSpec> fieldSpecList) {
String name = className.simpleName();
CodeBlock.Builder codeBlock = CodeBlock.builder();
for (int i = 0; i < methodSpecList.size(); i++) {
codeBlock.add("$N.$N(this.$N);\n", name.toLowerCase(), methodSpecList.get(i).name, fieldSpecList.get(i).name);
}
return MethodSpec
.methodBuilder("build")
.addModifiers(Modifier.PUBLIC)
.addStatement("$N $N = new $N($N)", name, name.toLowerCase(), name, "this.kiiGroup")
.addCode(codeBlock.build())
.returns(className)
.addStatement("return $N", name.toLowerCase())
.build();
}
示例12: createNotEmptyValidationCodeBlock
import com.squareup.javapoet.CodeBlock; //导入依赖的package包/类
private static void createNotEmptyValidationCodeBlock(MethodSpec.Builder constructorBuilder, ValidationField validationField) {
CodeBlock notEmptyValidationCodeBlock = CodeBlock.builder()
.add("\n")
.add("{")
.add("\n")
.indent()
.add(createElementDeclarationCode(validationField))
.add(createErrorMessageDeclarationCode(validationField.getId()))
.addStatement(
"this.$N.addValidator(new $T($N, $N))",
"validationSet",
Constants.NOT_EMPTY_VALIDATOR,
validationField.getName(),
"errorMessage"
)
.unindent()
.add("}")
.add("\n")
.build();
constructorBuilder.addCode(notEmptyValidationCodeBlock);
}
示例13: buildIsDisposed
import com.squareup.javapoet.CodeBlock; //导入依赖的package包/类
/**
* Generate the isDisposed method.
*/
@Nonnull
private MethodSpec buildIsDisposed()
throws ArezProcessorException
{
final MethodSpec.Builder builder =
MethodSpec.methodBuilder( "isDisposed" ).
addModifiers( Modifier.PUBLIC ).
addAnnotation( Override.class ).
returns( TypeName.BOOLEAN );
final CodeBlock.Builder block = CodeBlock.builder();
block.beginControlFlow( "if ( $N().isTransactionActive() && !this.$N.isDisposed() ) ",
getContextMethodName(),
GeneratorUtil.DISPOSED_OBSERVABLE_FIELD_NAME );
block.addStatement( "this.$N.reportObserved()", GeneratorUtil.DISPOSED_OBSERVABLE_FIELD_NAME );
block.addStatement( "return this.$N", GeneratorUtil.DISPOSED_FIELD_NAME );
block.nextControlFlow( "else" );
block.addStatement( "return this.$N", GeneratorUtil.DISPOSED_FIELD_NAME );
block.endControlFlow();
builder.addCode( block.build() );
return builder.build();
}
示例14: supplyFor
import com.squareup.javapoet.CodeBlock; //导入依赖的package包/类
@Override
public CodeBlock supplyFor(final AnnotationMirror object) {
return CodeBlock
.builder()
.addStatement(
"final boolean defaultConsistentlyReturned = \n" +
"$1N().getInt($2L, 1) == 1 && \n" +
"$1N().getInt($2L, 2) == 2",
CallerDef.GET_ATTRS,
getLiteralFromAnnotation(object, "attributeId"))
.add("\n")
.addStatement(
"return defaultConsistentlyReturned ? \n" +
"false : \n" +
"($N().getInt($L, 0) & $L) > 0",
CallerDef.GET_ATTRS,
getLiteralFromAnnotation(object, "attributeId"),
getLiteralFromAnnotation(object, "handledFlags"))
.build();
}
示例15: builderBuild
import com.squareup.javapoet.CodeBlock; //导入依赖的package包/类
public static MethodSpec builderBuild(ClassName className, List<MethodSpec> methodSpecList, List<FieldSpec> fieldSpecList) {
String name = className.simpleName();
CodeBlock.Builder codeBlock = CodeBlock.builder();
for (int i = 0; i < methodSpecList.size(); i++) {
codeBlock.add("$N.$N(this.$N);\n", name.toLowerCase(), methodSpecList.get(i).name, fieldSpecList.get(i).name);
}
return MethodSpec
.methodBuilder("build")
.addModifiers(Modifier.PUBLIC)
.addStatement("$N $N = new $N()", name, name.toLowerCase(), name)
.addCode(codeBlock.build())
.returns(className)
.addStatement("return $N", name.toLowerCase())
.build();
}