本文整理汇总了Java中com.squareup.javapoet.CodeBlock.Builder类的典型用法代码示例。如果您正苦于以下问题:Java Builder类的具体用法?Java Builder怎么用?Java Builder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Builder类属于com.squareup.javapoet.CodeBlock包,在下文中一共展示了Builder类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: putEncodeCode
import com.squareup.javapoet.CodeBlock.Builder; //导入依赖的package包/类
public void putEncodeCode(Builder cb, TypeMirror iterableTypeMirror, Collection<EncodeCodeProvider> allProviders, String varName) {
Elements elements = UtilsProvider.getElements();
TypeElement typeElement = elements.getTypeElement(Map.Entry.class.getCanonicalName());
DeclaredType dt = (DeclaredType)iterableTypeMirror;
Types types = UtilsProvider.getTypes();
DeclaredType declaredType = types.getDeclaredType(typeElement, dt.getTypeArguments().get(0), dt.getTypeArguments().get(1));
typeElement = UtilsProvider.getElements().getTypeElement(Set.class.getCanonicalName());
declaredType = UtilsProvider.getTypes().getDeclaredType(typeElement, declaredType);
// Rely on set encoder
for(EncodeCodeProvider ecp:allProviders){
if (ecp.applies(declaredType)) {
ecp.putEncodeCode(cb, declaredType, allProviders, varName+".entrySet()");
break;
}
}
}
示例2: generateClassJavadoc
import com.squareup.javapoet.CodeBlock.Builder; //导入依赖的package包/类
private CodeBlock generateClassJavadoc(Set<String> glideExtensionClassNames) {
Builder builder = CodeBlock.builder()
.add("Automatically generated from {@link $T} annotated classes.\n",
GlideExtension.class)
.add("\n")
.add("@see $T\n", requestOptionsName);
for (String glideExtensionClass : glideExtensionClassNames) {
builder.add("@see $T\n", ClassName.bestGuess(glideExtensionClass));
}
return builder.build();
}
示例3: generateRequestOptionOverride
import com.squareup.javapoet.CodeBlock.Builder; //导入依赖的package包/类
private MethodSpec generateRequestOptionOverride(ExecutableElement methodToOverride) {
MethodSpec.Builder result = ProcessorUtil.overriding(methodToOverride)
.returns(glideOptionsName)
.addModifiers(Modifier.FINAL)
.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());
if (methodToOverride.getSimpleName().toString().equals("transforms")) {
result
.addAnnotation(SafeVarargs.class)
.addAnnotation(
AnnotationSpec.builder(SuppressWarnings.class)
.addMember("value", "$S", "varargs")
.build());
}
for (AnnotationMirror mirror : methodToOverride.getAnnotationMirrors()) {
result.addAnnotation(AnnotationSpec.get(mirror));
}
return result.build();
}
示例4: getDecodeCode
import com.squareup.javapoet.CodeBlock.Builder; //导入依赖的package包/类
@Override
public CodeBlock getDecodeCode(Property property, DecodeCodeProviders decodeProviders) {
Builder builder = CodeBlock.builder();
TypeMirror tm = property.getResolvedType();
String accessName = property.getSetterCall();
accessName += property.hasSetter() ? "(" : " = ";
builder.add("instance." + accessName);
putDecodeCode(builder, tm, decodeProviders, null, true);
if ( property.hasSetter() ){
builder.add(")");
}
builder.add(";\n");
return builder.build();
}
示例5: putEncodeCode
import com.squareup.javapoet.CodeBlock.Builder; //导入依赖的package包/类
@Override
public void putEncodeCode(Builder cb, TypeMirror iterableTypeMirror, Collection<EncodeCodeProvider> allProviders, String varName) {
TypeMirror iterableTypeArgument = getIterableEnclosedType(iterableTypeMirror);
TypeName iterableTypeArgumentTypeName = getIterableEnclosedTypeName(iterableTypeMirror);
String instanceName = nameGenerator.instanceName(iterableTypeArgument);
cb.addStatement("writer.writeStartArray()");
cb.beginControlFlow("for ($T $L:(($T)$L))", iterableTypeArgumentTypeName, instanceName, getIterableCast(iterableTypeMirror), varName);
boolean primitive = iterableTypeArgument.getKind().isPrimitive();
if ( !primitive ){
cb.beginControlFlow("if ($L == null)", instanceName);
cb.addStatement("writer.writeNull()");
cb.nextControlFlow("else");
}
for(EncodeCodeProvider provider:allProviders){
if (provider.applies(iterableTypeArgument)){
String newVarName = nameGenerator.instanceName(iterableTypeArgument).toLowerCase();
provider.putEncodeCode(cb, iterableTypeArgument, allProviders, newVarName);
break;
}
}
if ( !primitive ){
cb.endControlFlow();
}
cb.endControlFlow();
cb.addStatement("writer.writeEndArray()");
return;
}
示例6: putDecodeCode
import com.squareup.javapoet.CodeBlock.Builder; //导入依赖的package包/类
public void putDecodeCode(Builder cb, TypeMirror tm, String variableToUse, boolean declareVariable) {
for(DecodeCodeProvider provider:allProviders){
if (provider.applies(tm)){
provider.putDecodeCode(cb, tm, this, variableToUse, declareVariable);
break;
}
}
}
示例7: getDecodeCode
import com.squareup.javapoet.CodeBlock.Builder; //导入依赖的package包/类
@Override
public CodeBlock getDecodeCode(Property property, DecodeCodeProviders decodeProviders) {
Builder builder = CodeBlock.builder();
String getAccessName = property.getGetterCall();
String setAccessName = property.getSetterCall();
setAccessName += property.hasSetter() ? "($L)" : " = $L";
TypeMirror resolvedType = property.getResolvedType();
builder.addStatement("$T bsonType = reader.getCurrentBsonType()", CodeUtil.bsonTypeTypeName());
builder.beginControlFlow("if (bsonType == $T.NULL)", CodeUtil.bsonTypeTypeName());
builder.addStatement("reader.readNull()");
builder.addStatement("instance." + setAccessName, "null");
builder.addStatement("return");
builder.endControlFlow();
if ( initializeProperty() ){
TypeMirror iterableImplementationCollectionClass = getIterableImplementationType(resolvedType);
TypeMirror iterableDeclarationCollectionClass = getIterableDeclarationType(resolvedType);
builder.addStatement("$T value = instance.$L", iterableDeclarationCollectionClass, getAccessName);
builder.beginControlFlow("if (value == null)");
builder.addStatement("value = new $T()", TypeName.get(iterableImplementationCollectionClass));
builder.addStatement("instance." + setAccessName, "value");
builder.endControlFlow();
}
putDecodeCode(builder, resolvedType, decodeProviders, !initializeProperty(), "value");
if ( !initializeProperty() ){
builder.addStatement("instance." + setAccessName, "value");
}
return builder.build();
}
示例8: putDecodeCode
import com.squareup.javapoet.CodeBlock.Builder; //导入依赖的package包/类
@Override
public void putDecodeCode(Builder cb, TypeMirror tm, DecodeCodeProviders decodeProviders, String variableToUse, boolean declareVariable) {
DeclaredType entry = (DeclaredType)tm;
TypeMirror keyArgument = entry.getTypeArguments().get(0);
TypeMirror valueArgument = entry.getTypeArguments().get(1);
cb.addStatement("reader.readStartDocument()");
cb.addStatement("reader.readName(\"key\")");
cb.addStatement("$T $L = null", keyArgument, variableToUse+"0");
cb.addStatement("$T $L = null", valueArgument, variableToUse+"1");
cb.addStatement("bsonType = reader.getCurrentBsonType()", CodeUtil.bsonTypeTypeName());
cb.beginControlFlow("if (bsonType == $T.NULL)", CodeUtil.bsonTypeTypeName());
cb.addStatement("reader.readNull()");
cb.addStatement("reader.skipName()");
cb.addStatement("reader.skipValue()");
cb.nextControlFlow("else");
Builder builder = CodeBlock.builder();
decodeProviders.putDecodeCode(builder, keyArgument, variableToUse+"0", false);
boolean hasToInstantiate = decodeProviders.hasToInstantiate(keyArgument);
if ( hasToInstantiate ){
cb.add(builder.build());
}else{
cb.add("$L = ", variableToUse+"0");
cb.add(builder.build()+";\n");
}
cb.addStatement("reader.readName(\"value\")");
builder = CodeBlock.builder();
decodeProviders.putDecodeCode(builder, valueArgument, variableToUse+"1", false);
hasToInstantiate = decodeProviders.hasToInstantiate(valueArgument);
if ( hasToInstantiate ){
cb.add(builder.build());
}else{
cb.add("$L = ", variableToUse+"1");
cb.add(builder.build()+";\n");
}
cb.endControlFlow();
cb.addStatement("reader.readEndDocument()");
}
示例9: putDecodeCode
import com.squareup.javapoet.CodeBlock.Builder; //导入依赖的package包/类
protected void putDecodeCode(Builder cb, TypeMirror iterableTypeMirror, DecodeCodeProviders decodeProviders, String variableToUse, boolean instantiate, boolean declareVariable) {
String temporalVariable = variableToUse+"tmp";
super.putDecodeCode(cb, iterableTypeMirror, decodeProviders, temporalVariable, instantiate, declareVariable);
TypeMirror iterableType = getIterableType(iterableTypeMirror);
if ( !iterableType.getKind().isPrimitive() ){
cb.addStatement("$T $L = $L.toArray(new $T[]{})", iterableTypeMirror, variableToUse, temporalVariable, iterableType);
}else{
cb.addStatement("$T $L = new $T[$L.size()]", iterableTypeMirror, variableToUse, iterableType, temporalVariable);
String idxName = variableToUse+"TmpIdx";
cb.beginControlFlow("for(int $L=0; $L < $L.length; $L++)", idxName, idxName, variableToUse, idxName);
cb.addStatement("$L[$L] = $L.get($L)", variableToUse, idxName, temporalVariable, idxName);
cb.endControlFlow();
}
}
示例10: generate
import com.squareup.javapoet.CodeBlock.Builder; //导入依赖的package包/类
TypeSpec generate(String generatedCodePackageName, Set<String> glideExtensionClassNames) {
glideOptionsName =
ClassName.get(generatedCodePackageName, GENERATED_REQUEST_OPTIONS_SIMPLE_NAME);
List<MethodAndStaticVar> methodsForExtensions =
generateMethodsForExtensions(glideExtensionClassNames);
Set<MethodSignature> extensionMethodSignatures = ImmutableSet.copyOf(
Iterables.transform(methodsForExtensions,
new Function<MethodAndStaticVar, MethodSignature>() {
@Nullable
@Override
public MethodSignature apply(MethodAndStaticVar f) {
return new MethodSignature(f.method);
}
}));
List<MethodAndStaticVar> staticOverrides = generateStaticMethodOverridesForRequestOptions();
List<MethodSpec> instanceOverrides = generateInstanceMethodOverridesForRequestOptions();
List<MethodAndStaticVar> allMethodsAndStaticVars = new ArrayList<>();
for (MethodAndStaticVar item : staticOverrides) {
if (extensionMethodSignatures.contains(new MethodSignature(item.method))) {
continue;
}
allMethodsAndStaticVars.add(item);
}
for (MethodSpec methodSpec : instanceOverrides) {
if (extensionMethodSignatures.contains(new MethodSignature(methodSpec))) {
continue;
}
allMethodsAndStaticVars.add(new MethodAndStaticVar(methodSpec));
}
allMethodsAndStaticVars.addAll(methodsForExtensions);
TypeSpec.Builder classBuilder = TypeSpec.classBuilder(GENERATED_REQUEST_OPTIONS_SIMPLE_NAME)
.addAnnotation(
AnnotationSpec.builder(SuppressWarnings.class)
.addMember("value", "$S", "deprecation")
.build())
.addJavadoc(generateClassJavadoc(glideExtensionClassNames))
.addModifiers(Modifier.FINAL)
.addModifiers(Modifier.PUBLIC)
.superclass(requestOptionsName);
for (MethodAndStaticVar methodAndStaticVar : allMethodsAndStaticVars) {
if (methodAndStaticVar.method != null) {
classBuilder.addMethod(methodAndStaticVar.method);
}
if (methodAndStaticVar.staticField != null) {
classBuilder.addField(methodAndStaticVar.staticField);
}
}
return classBuilder.build();
}
示例11: generateStaticMethodEquivalentForRequestOptionsStaticMethod
import com.squareup.javapoet.CodeBlock.Builder; //导入依赖的package包/类
private MethodAndStaticVar generateStaticMethodEquivalentForRequestOptionsStaticMethod(
ExecutableElement staticMethod) {
boolean memoize = memoizeStaticMethodFromArguments(staticMethod);
String staticMethodName = staticMethod.getSimpleName().toString();
String equivalentInstanceMethodName =
getInstanceMethodNameFromStaticMethodName(staticMethodName);
MethodSpec.Builder methodSpecBuilder =
MethodSpec.methodBuilder(staticMethodName)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addJavadoc(processorUtil.generateSeeMethodJavadoc(staticMethod))
.returns(glideOptionsName);
List<? extends VariableElement> parameters = staticMethod.getParameters();
String createNewOptionAndCall = "new $T().$N(";
if (!parameters.isEmpty()) {
for (VariableElement parameter : parameters) {
methodSpecBuilder.addParameter(getParameterSpec(parameter));
createNewOptionAndCall += parameter.getSimpleName().toString();
// use the Application Context to avoid memory leaks.
if (memoize && isAndroidContext(parameter)) {
createNewOptionAndCall += ".getApplicationContext()";
}
createNewOptionAndCall += ", ";
}
createNewOptionAndCall =
createNewOptionAndCall.substring(0, createNewOptionAndCall.length() - 2);
}
createNewOptionAndCall += ")";
FieldSpec requiredStaticField = null;
if (memoize) {
// Generates code that looks like:
// if (GlideOptions.<methodName> == null) {
// GlideOptions.<methodName> = new GlideOptions().<methodName>().autoClone()
// }
// Mix in an incrementing unique id to handle method overloading.
String staticVariableName = staticMethodName + nextStaticFieldUniqueId++;
requiredStaticField = FieldSpec.builder(glideOptionsName, staticVariableName)
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
.build();
methodSpecBuilder.beginControlFlow(
"if ($T.$N == null)", glideOptionsName, staticVariableName)
.addStatement("$T.$N =\n" + createNewOptionAndCall + ".$N",
glideOptionsName, staticVariableName, glideOptionsName, equivalentInstanceMethodName,
"autoClone()")
.endControlFlow()
.addStatement("return $T.$N", glideOptionsName, staticVariableName);
} else {
// Generates code that looks like:
// return new GlideOptions().<methodName>()
methodSpecBuilder.addStatement(
"return " + createNewOptionAndCall, glideOptionsName, equivalentInstanceMethodName);
}
List<? extends TypeParameterElement> typeParameters = staticMethod.getTypeParameters();
for (TypeParameterElement typeParameterElement : typeParameters) {
methodSpecBuilder.addTypeVariable(
TypeVariableName.get(typeParameterElement.getSimpleName().toString()));
}
return new MethodAndStaticVar(methodSpecBuilder.build(), requiredStaticField);
}
示例12: generate
import com.squareup.javapoet.CodeBlock.Builder; //导入依赖的package包/类
TypeSpec generate(String generatedCodePackageName, Set<String> glideExtensionClassNames) {
glideOptionsName =
ClassName.get(generatedCodePackageName, GENERATED_REQUEST_OPTIONS_SIMPLE_NAME);
List<MethodAndStaticVar> methodsForExtensions =
generateMethodsForExtensions(glideExtensionClassNames);
Set<MethodSignature> extensionMethodSignatures = ImmutableSet.copyOf(
Iterables.transform(methodsForExtensions,
new Function<MethodAndStaticVar, MethodSignature>() {
@Nullable
@Override
public MethodSignature apply(MethodAndStaticVar f) {
return new MethodSignature(f.method);
}
}));
List<MethodAndStaticVar> staticOverrides = generateStaticMethodOverridesForRequestOptions();
List<MethodSpec> instanceOverrides = generateInstanceMethodOverridesForRequestOptions();
List<MethodAndStaticVar> allMethodsAndStaticVars = new ArrayList<>();
for (MethodAndStaticVar item : staticOverrides) {
if (extensionMethodSignatures.contains(new MethodSignature(item.method))) {
continue;
}
allMethodsAndStaticVars.add(item);
}
for (MethodSpec methodSpec : instanceOverrides) {
if (extensionMethodSignatures.contains(new MethodSignature(methodSpec))) {
continue;
}
allMethodsAndStaticVars.add(new MethodAndStaticVar(methodSpec));
}
allMethodsAndStaticVars.addAll(methodsForExtensions);
TypeSpec.Builder classBuilder = TypeSpec.classBuilder(GENERATED_REQUEST_OPTIONS_SIMPLE_NAME)
.addAnnotation(
AnnotationSpec.builder(SuppressWarnings.class)
.addMember("value", "$S", "deprecation")
.build())
.addJavadoc(generateClassJavadoc(glideExtensionClassNames))
.addModifiers(Modifier.FINAL)
.addModifiers(Modifier.PUBLIC)
.addSuperinterface(Cloneable.class)
.superclass(requestOptionsName);
for (MethodAndStaticVar methodAndStaticVar : allMethodsAndStaticVars) {
if (methodAndStaticVar.method != null) {
classBuilder.addMethod(methodAndStaticVar.method);
}
if (methodAndStaticVar.staticField != null) {
classBuilder.addField(methodAndStaticVar.staticField);
}
}
return classBuilder.build();
}
示例13: generateStaticMethodEquivalentForRequestOptionsStaticMethod
import com.squareup.javapoet.CodeBlock.Builder; //导入依赖的package包/类
private MethodAndStaticVar generateStaticMethodEquivalentForRequestOptionsStaticMethod(
ExecutableElement staticMethod) {
boolean memoize = memoizeStaticMethodFromArguments(staticMethod);
String staticMethodName = staticMethod.getSimpleName().toString();
String equivalentInstanceMethodName =
getInstanceMethodNameFromStaticMethodName(staticMethodName);
MethodSpec.Builder methodSpecBuilder =
MethodSpec.methodBuilder(staticMethodName)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addJavadoc(processorUtil.generateSeeMethodJavadoc(staticMethod))
.returns(glideOptionsName);
List<? extends VariableElement> parameters = staticMethod.getParameters();
StringBuilder createNewOptionAndCall = new StringBuilder("new $T().$N(");
if (!parameters.isEmpty()) {
methodSpecBuilder.addParameters(ProcessorUtil.getParameters(staticMethod));
for (VariableElement parameter : parameters) {
createNewOptionAndCall.append(parameter.getSimpleName().toString());
// use the Application Context to avoid memory leaks.
if (memoize && isAndroidContext(parameter)) {
createNewOptionAndCall.append(".getApplicationContext()");
}
createNewOptionAndCall.append(", ");
}
createNewOptionAndCall = new StringBuilder(
createNewOptionAndCall.substring(0, createNewOptionAndCall.length() - 2));
}
createNewOptionAndCall.append(")");
FieldSpec requiredStaticField = null;
if (memoize) {
// Generates code that looks like:
// if (GlideOptions.<methodName> == null) {
// GlideOptions.<methodName> = new GlideOptions().<methodName>().autoClone()
// }
// Mix in an incrementing unique id to handle method overloading.
String staticVariableName = staticMethodName + nextStaticFieldUniqueId++;
requiredStaticField = FieldSpec.builder(glideOptionsName, staticVariableName)
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
.build();
methodSpecBuilder.beginControlFlow(
"if ($T.$N == null)", glideOptionsName, staticVariableName)
.addStatement("$T.$N =\n" + createNewOptionAndCall + ".$N",
glideOptionsName, staticVariableName, glideOptionsName, equivalentInstanceMethodName,
"autoClone()")
.endControlFlow()
.addStatement("return $T.$N", glideOptionsName, staticVariableName);
} else {
// Generates code that looks like:
// return new GlideOptions().<methodName>()
methodSpecBuilder.addStatement(
"return " + createNewOptionAndCall, glideOptionsName, equivalentInstanceMethodName);
}
List<? extends TypeParameterElement> typeParameters = staticMethod.getTypeParameters();
for (TypeParameterElement typeParameterElement : typeParameters) {
methodSpecBuilder.addTypeVariable(
TypeVariableName.get(typeParameterElement.getSimpleName().toString()));
}
methodSpecBuilder.addAnnotation(AnnotationSpec.builder(CHECK_RESULT_CLASS_NAME).build());
return new MethodAndStaticVar(methodSpecBuilder.build(), requiredStaticField);
}
示例14: putDecodeCode
import com.squareup.javapoet.CodeBlock.Builder; //导入依赖的package包/类
@Override
public void putDecodeCode(Builder cb, TypeMirror iterableTypeMirror, DecodeCodeProviders decodeProviders, String variableToUse, boolean declareVariable) {
putDecodeCode(cb, iterableTypeMirror, decodeProviders, variableToUse, true, declareVariable);
}