private void generateInstanceBuilderForConstructorOrFactoryMethod( SourceWriter source, BeanInfo info, ImmutableMap<String,
PropertyInfo> properties ) throws UnableToCompleteException {
List<String> requiredProperties = new ArrayList<String>();
for ( String name : info.getCreatorParameters().keySet() ) {
PropertyInfo propertyInfo = properties.get( name );
source.println( "%s %s = %s;", propertyInfo.getType().getParameterizedQualifiedSourceName(), FORMAT_VARIABLE
.apply( name ), getDefaultValueForType( propertyInfo.getType() ) );
if ( propertyInfo.isRequired() ) {
requiredProperties.add( name );
}
}
source.println();
source.println( "int nbParamToFind = %d;", info.getCreatorParameters().size() );
if ( !requiredProperties.isEmpty() ) {
String requiredList = Joiner.on( ", " ).join( Collections2.transform( requiredProperties, QUOTED_FUNCTION ) );
source.println( "%s<String> requiredProperties = new %s<String>(%s.asList(%s));", Set.class.getName(), HashSet.class
.getName(), Arrays.class.getName(), requiredList );
}
source.println();
source.println( "if(null != bufferedProperties) {" );
source.indent();
source.println( "String value;" );
for ( String name : info.getCreatorParameters().keySet() ) {
PropertyInfo propertyInfo = properties.get( name );
source.println();
source.println( "value = bufferedProperties.remove(\"%s\");", name );
source.println( "if(null != value) {" );
source.indent();
source.println( "%s = %s.deserialize(ctx.newJsonReader(value), ctx);", FORMAT_VARIABLE.apply( name ), String
.format( INSTANCE_BUILDER_DESERIALIZER_FORMAT, name ) );
source.println( "nbParamToFind--;" );
if ( propertyInfo.isRequired() ) {
source.println( "requiredProperties.remove(\"%s\");", name );
}
source.outdent();
source.println( "}" );
}
source.outdent();
source.println( "}" );
source.println();
source.println( "String name;" );
source.println( "while (nbParamToFind > 0 && %s.NAME == reader.peek()) {", JsonToken.class.getName() );
source.indent();
source.println( "name = reader.nextName();" );
source.println();
for ( String name : info.getCreatorParameters().keySet() ) {
PropertyInfo propertyInfo = properties.get( name );
source.println( "if(\"%s\".equals(name)) {", name );
source.indent();
source.println( "%s = %s.deserialize(reader, ctx);", FORMAT_VARIABLE.apply( name ), String
.format( INSTANCE_BUILDER_DESERIALIZER_FORMAT, name ) );
source.println( "nbParamToFind--;" );
if ( propertyInfo.isRequired() ) {
source.println( "requiredProperties.remove(\"%s\");", name );
}
source.println( "continue;" );
source.outdent();
source.println( "}" );