.beginControlFlow("case $S:", pd.name()) .addStatement("return transformed$$$L", pd.name()) .endControlFlow().build()); .addAnnotation(Override.class) .addCode(CodeBlock.builder() .beginControlFlow("switch (name)") .add(parameterValues.build()) .endControlFlow()
.addCode( CodeBlock.builder() .beginControlFlow("if (_component == null)") .addStatement("return") .endControlFlow()
static TypeSpecDataHolder generateGetEventHandlerMethod( SpecModel specModel, EventDeclarationModel eventDeclaration) { final String scopeMethodName = specModel.getScopeMethodName(); return TypeSpecDataHolder.newBuilder() .addMethod( MethodSpec.methodBuilder("get" + eventDeclaration.name.simpleName() + "Handler") .addModifiers(Modifier.PUBLIC, Modifier.STATIC) .returns(ClassNames.EVENT_HANDLER) .addParameter(specModel.getContextClass(), "context") .addCode( CodeBlock.builder() .beginControlFlow("if (context.$L() == null)", scopeMethodName) .addStatement("return null") .endControlFlow() .build()) .addStatement( "return (($L) context.$L()).$L", specModel.getComponentName(), scopeMethodName, ComponentBodyGenerator.getEventHandlerInstanceName(eventDeclaration.name)) .build()) .build(); }
ClassNames.CLASS_LOADER, ClassNames.HOTSWAP_MANAGER) .beginControlFlow("if (classLoader == null)") .add(directDelegation) .nextControlFlow("else") .addStatement("$T specClass", ClassNames.CLASS) .beginControlFlow("try") .addStatement( "specClass = classLoader.loadClass(\"$L\")", .addStatement("throw new RuntimeException(_e)") .endControlFlow() .beginControlFlow("try") .add("final $T method = specClass.getDeclaredMethod(", ClassNames.JAVA_METHOD) .indent();
.addCode( CodeBlock.builder() .beginControlFlow( "if ($L.isEmpty())", GeneratorConstants.STATE_TRANSITIONS_FIELD_NAME) .addStatement("return $T.EMPTY_LIST", ClassNames.COLLECTIONS)
.beginControlFlow("if ($L == null)", parameterName) .addStatement("return this") .endControlFlow(); codeBlockBuilder.beginControlFlow( "if (this.$L.$L == null || this.$L.$L == $L.$L)", componentMemberInstanceName, propName); } else { codeBlockBuilder.beginControlFlow( "if (this.$L.$L == null)", componentMemberInstanceName, propName); .addStatement("this.$L.$L = new $T()", componentMemberInstanceName, propName, listType) .endControlFlow() .beginControlFlow("for (int i = 0; i < $L.size(); i++)", parameterName) .add("final $T res = ", resType.isBoxedPrimitive() ? resType.unbox() : resType) .addStatement(statement, formatObjects)
.beginControlFlow( "if (!value.getComponentClass().isAssignableFrom($L.class))", enclosedSpecModel.getComponentTypeName())
CodeBlock.Builder codeBlockBuilder = CodeBlock.builder() .beginControlFlow("if ($L == null)", varArgName) .addStatement("return this") .endControlFlow(); codeBlockBuilder.beginControlFlow( "if (this.$L.$L == null || this.$L.$L == $L.$L)", implMemberInstanceName, propName); } else { codeBlockBuilder.beginControlFlow( "if (this.$L.$L == null)", implMemberInstanceName, propName);
codeBlockBuilder.beginControlFlow( "if (this.$L.$L == null || this.$L.$L == $L.$L)", componentMemberInstanceName, propName); } else { codeBlockBuilder.beginControlFlow( "if (this.$L.$L == null)", componentMemberInstanceName, propName);
private void assignmentParameter(CodeBlock.Builder builder, TypeName type, Object... obj) { builder.beginControlFlow("try"); if (mString.equals(type)) { builder.addStatement("$L$L = $L$LStr", obj); } else if (mInt.equals(type)) { builder.addStatement("$L$L = Integer.parseInt($L$LStr)", obj); } else if (mLong.equals(type)) { builder.addStatement("$L$L = Long.parseLong($L$LStr)", obj); } else if (mFloat.equals(type)) { builder.addStatement("$L$L = Float.parseFloat($L$LStr)", obj); } else if (mDouble.equals(type)) { builder.addStatement("$L$L = Double.parseDouble($L$LStr)", obj); } else if (mBoolean.equals(type)) { builder.addStatement("$L$L = Boolean.parseBoolean($L$LStr)", obj); } builder.endControlFlow() .beginControlFlow("catch (Throwable e)") .addStatement("throw new $T(e)", mParamError) .endControlFlow(); }
final CodeBlock code = CodeBlock.builder() .beginControlFlow("if ($L == null)", "previousRenderData") .addStatement("$L = null", PREVIOUS_RENDER_DATA_FIELD_NAME) .addStatement("return") .endControlFlow() .beginControlFlow( "if ($L == null)", PREVIOUS_RENDER_DATA_FIELD_NAME) .addStatement(
.addStatement( "$T<$L> _e2_$L = $L.iterator()", Iterator.class, argumentTypeName, level, ref) .beginControlFlow("while (_e1_$L.hasNext() && _e2_$L.hasNext())", level, level); .beginControlFlow("if (!_e1_$L.next().isEquivalentTo(_e2_$L.next()))", level, level) .addStatement("return false") .endControlFlow() } else { builder .beginControlFlow("if (_e1_$L.next().size() != _e2_$L.next().size())", level, level) .addStatement("return false") .endControlFlow()
private static MethodSpec.Builder getMethodSpecBuilder( SpecModel specModel, final PropModel prop, final String name, final List<ParameterSpec> parameters, final String statement, final Object... formatObjects) { if (prop.hasVarArgs()) { final String propName = prop.getName(); final CodeBlock.Builder codeBlockBuilder = CodeBlock.builder() .beginControlFlow("if ($L == null)", propName) .addStatement("return this") .endControlFlow(); return getMethodSpecBuilder(specModel, parameters, codeBlockBuilder.build(), name); } final String propMatcherName = getPropMatcherName(prop); final CodeBlock formattedStatement = CodeBlock.of(statement, formatObjects); final CodeBlock codeBlock = CodeBlock.builder() .addStatement( "this.$N = $L.is(($T) $L)", propMatcherName, ClassNames.HAMCREST_CORE_IS, getRawType(prop.getTypeName()), formattedStatement) .build(); return getMethodSpecBuilder(specModel, parameters, codeBlock, name); }
private static MethodSpec varArgBuilderBuilder( SpecModel specModel, PropModel prop, int requiredIndex) { String varArgName = prop.getVarArgsSingleName(); final ParameterizedTypeName varArgType = (ParameterizedTypeName) prop.getTypeName(); final TypeName internalType = varArgType.typeArguments.get(0); CodeBlock codeBlock = CodeBlock.builder() .beginControlFlow("if ($L == null)", varArgName + "Builder") .addStatement("return this") .endControlFlow() .addStatement("$L($L.build())", varArgName, varArgName + "Builder") .build(); TypeName builderParameterType = ParameterizedTypeName.get( ClassNames.COMPONENT_BUILDER, getBuilderGenericTypes(internalType, ClassNames.COMPONENT_BUILDER)); return getMethodSpecBuilder( specModel, prop, requiredIndex, varArgName, Arrays.asList(parameter(prop, builderParameterType, varArgName + "Builder")), codeBlock).build(); }
private static CodeBlock generateValuePropMatchBlock( FieldExtractorSpec fieldExtractorSpec, Function<FieldExtractorSpec, CodeBlock> fieldExtractorBlockFn) { final String matcherName = getPropMatcherName(fieldExtractorSpec.propModel); return CodeBlock.builder() .add( fieldExtractorBlockFn.apply( new FieldExtractorSpec( fieldExtractorSpec.specModel, fieldExtractorSpec.propModel, fieldExtractorSpec.varName))) .beginControlFlow( "if ($1N != null && !$1N.matches($2L))", matcherName, fieldExtractorSpec.varName) .add( generateMatchFailureStatement( fieldExtractorSpec.specModel, matcherName, fieldExtractorSpec.propModel)) .addStatement("return false") .endControlFlow() .build(); }
private static MethodSpec varArgBuilder( SpecModel specModel, final PropModel prop, final AnnotationSpec... extraAnnotations) { final ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) prop.getTypeName(); final TypeName singleParameterType = parameterizedTypeName.typeArguments.get(0); final String varArgName = prop.getVarArgsSingleName(); final CodeBlock codeBlock = CodeBlock.builder() .beginControlFlow("if ($L == null)", varArgName) .addStatement("return this") .endControlFlow() .build(); return getMethodSpecBuilder( specModel, Collections.singletonList( parameter(prop, singleParameterType, varArgName, extraAnnotations)), codeBlock, varArgName) .build(); }
private static CodeBlock generateComponentMatchBlock( FieldExtractorSpec fieldExtractorSpec, Function<FieldExtractorSpec, CodeBlock> fieldExtractorBlockFn) { final String matcherName = getPropComponentMatcherName(fieldExtractorSpec.propModel); final String propValueName = getPropValueName(fieldExtractorSpec.propModel) + "Component"; return CodeBlock.builder() .add(fieldExtractorBlockFn.apply(fieldExtractorSpec)) .beginControlFlow( "if ($1N != null && !$1N.matches(value.getNestedInstance($2L)))", matcherName, fieldExtractorSpec.varName) .addStatement("as($N.description())", matcherName) .addStatement("return false") .endControlFlow() .build(); }
@Override public void addInitializer(@NonNull CodeBlock.Builder constructorWhenAnnotationPresent, CodeBlock.Builder constructorWhenAnnotationMissing) { constructorWhenAnnotationPresent.beginControlFlow("if ($L.$L() != 0)", Strings.VAR_ANNOTATION, originalName) .addStatement("$L = $L.getString($L.$L())", getName(), Strings.FIELD_CONTEXT, Strings.VAR_ANNOTATION, originalName) .endControlFlow(); if (hasDefault) { constructorWhenAnnotationMissing.addStatement("$L = $L.getString($L)", getName(), Strings.FIELD_CONTEXT, getDefaultValue()); } }
public Builder addStaticBlock(CodeBlock block) { staticBlock.beginControlFlow("static").add(block).endControlFlow(); return this; }
/** * @param controlFlow the control flow construct and its code, such as "if (foo == 5)". * Shouldn't contain braces or newline characters. */ public Builder beginControlFlow(String controlFlow, Object... args) { code.beginControlFlow(controlFlow, args); return this; }