}
private void emitBuildMethod(TypeM builderType, TypeM pojoType, TypeM interfaceType,
boolean hasBuilderProperties, PropertyListM properties, FactoryMethodM factoryMethod,
BuildMethodM buildMethod, ValidatorM validator) throws IOException {
properties = new PropertyListM(properties);
String pojoTypeDeclaration = writer.compressType(pojoType.getGenericTypeDeclaration());
String pojoClassname = writer.compressType(pojoType.getName());
// @formatter:off
writer
.emitEmptyLine()
.emitJavadoc(
"Creates a new {@link %s} based on this builder's settings.\n\n"
+"@return the created %s"
, pojoClassname, pojoClassname);
if ( buildMethod.isOverrides()) {
writer
.emitAnnotation(Override.class);
}
writer
.beginMethod(pojoTypeDeclaration, "build", EnumSet.of(PUBLIC))
.beginControlFlow("try");
if ( !hasBuilderProperties) {
if ( factoryMethod == null) {
String arguments = properties.filterOutPropertiesWritableViaConstructorParameter(builderType).toArgumentString();
writer
.emitStatement("%s result = new %s(%s)", pojoTypeDeclaration, pojoTypeDeclaration, arguments);
} else {
String arguments = properties.filterOutPropertiesWritableViaFactoryMethodParameter(builderType).toArgumentString();
String factoryClass = writer.compressType(factoryMethod.getDeclaringClass().getName());
writer
.emitStatement("%s result = %s.%s(%s)", pojoTypeDeclaration, factoryClass, factoryMethod.getName(), arguments);
}
} else {
if ( factoryMethod == null) {
ArgumentListM constructorArguments = properties.filterOutPropertiesWritableViaConstructorParameter(builderType);
StringBuilder arguments = new StringBuilder();
for (PropertyM prop : constructorArguments.sortByPosition().getPropertyList()) {
writer
.emitStatement("%s _%s = !%s && %s!=null?%s.build():%s",
writer.compressType(prop.getPropertyType().getGenericTypeDeclaration()),
prop.getConstructorParameter().getName(),
prop.getIsSetFieldName(),
prop.getBuilderFieldName(),
prop.getBuilderFieldName(),
prop.getValueFieldName()
);
if ( arguments.length()>0) {
arguments.append(", ");
}
arguments.append(String.format("_%s", prop.getConstructorParameter().getName()));
}
writer
.emitStatement("%s result = new %s(%s)", pojoTypeDeclaration, pojoTypeDeclaration, arguments.toString());
} else {
ArgumentListM constructorArguments = properties.filterOutPropertiesWritableViaFactoryMethodParameter(builderType);
StringBuilder arguments = new StringBuilder();
for (PropertyM prop : constructorArguments.sortByPosition().getPropertyList()) {
writer
.emitStatement("%s _%s = !%s && %s!=null?%s.build():%s",
writer.compressType(prop.getPropertyType().getGenericType()),
prop.getFactoryMethodParameter().getName(),
prop.getIsSetFieldName(),
prop.getBuilderFieldName(),
prop.getBuilderFieldName(),
prop.getValueFieldName()
);
if ( arguments.length()>0) {
arguments.append(", ");
}
arguments.append(String.format("_%s", prop.getFactoryMethodParameter().getName()));
}
String factoryClass = writer.compressType(factoryMethod.getDeclaringClass().getName());
writer
.emitStatement("%s result = %s.%s(%s)", pojoTypeDeclaration, factoryClass, factoryMethod.getName(), arguments.toString());
}
}
PropertyListM setterProperties = properties.filterOutPropertiesWritableBy(Type.SETTER, builderType);
for( PropertyM prop : setterProperties) {
writer
.beginControlFlow("if (%s)", prop.getIsSetFieldName())
.emitStatement("result.%s(%s)", prop.getSetterMethod().getName(), prop.getValueFieldName());
if ( hasBuilderProperties) {
writer
.nextControlFlow("else if (%s!=null)", prop.getBuilderFieldName())
.emitStatement("result.%s(%s.build())", prop.getSetterMethod().getName(), prop.getBuilderFieldName());
}
writer
.endControlFlow();
}
PropertyListM writableProperties = properties.filterOutPropertiesWritableBy(Type.FIELD, builderType);
for( PropertyM prop : writableProperties) {
writer
.beginControlFlow("if (%s)", prop.getIsSetFieldName())
.emitStatement("result.%s = %s", prop.getPropertyName(), prop.getValueFieldName());
if ( hasBuilderProperties) {