Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Sets and Lists to be initialized to null (instead of an empty collection) #203

Merged
merged 1 commit into from
Jun 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public class Jsonschema2PojoTask extends Task implements GenerationConfig {

private boolean useCommonsLang3 = false;

private boolean initializeEmptyCollections = true;

/**
* Execute this task (it's expected that all relevant setters will have been
* called by Ant to provide task configuration <em>before</em> this method
Expand Down Expand Up @@ -405,7 +407,18 @@ public void setUseJodaDates(boolean useJodaDates) {
public void setUseCommonsLang3(boolean useCommonsLang3) {
this.useCommonsLang3 = useCommonsLang3;
}



/**
* Sets the 'initializeEmptyCollections' property of this class
*
* @param initializeEmptyCollections
* Whether to initialize collections with empty instance or null.
*/
public void setInitializeEmptyCollections(boolean initializeEmptyCollections) {
this.initializeEmptyCollections = initializeEmptyCollections;
}

@Override
public boolean isGenerateBuilders() {
return generateBuilders;
Expand Down Expand Up @@ -533,4 +546,9 @@ public FileFilter getFileFilter() {
return new AllFileFilter();
}

@Override
public boolean isInitializeEmptyCollections() {
return initializeEmptyCollections;
}

}
5 changes: 5 additions & 0 deletions jsonschema2pojo-ant/src/site/Jsonschema2PojoTask.html
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ <h3>Parameters</h3>
<td valign="top">Whether to use primitives (<code>long</code>, <code>double</code>, <code>boolean</code>) instead of wrapper types where possible when generating bean properties (has the side-effect of making those.</td>
<td align="center" valign="top">No (default <code>false</code>)</td>
</tr>
<tr>
<td valign="top">initializeEmptyCollections</td>
<td valign="top">Whether to initialize collections with empty instance or <code>null</code>.</td>
<td align="center" valign="top">Yes (default <code>true</code>)</td>
</tr>
</table>

<h3>Examples</h3>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ public class Arguments implements GenerationConfig {
@Parameter(names = { "-c3", "--commons-lang3" }, description = "Whether to use commons-lang 3.x imports instead of commons-lang 2.x imports when adding equals, hashCode and toString methods.")
private boolean useCommonsLang3 = false;

@Parameter(names = { "-N", "--null-collections" }, description = "Initialize Set and List fields to null instead of an empty collection.")
private boolean omitInitializeEmptyCollections = false;

private static final int EXIT_OKAY = 0;
private static final int EXIT_ERROR = 1;

Expand Down Expand Up @@ -234,4 +237,9 @@ public FileFilter getFileFilter() {
return new AllFileFilter();
}

@Override
public boolean isInitializeEmptyCollections() {
return !omitInitializeEmptyCollections;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public boolean isRemoveOldOutput() {
public boolean isUseJodaDates() {
return false;
}

@Override
public boolean isUseCommonsLang3() {
return false;
Expand All @@ -167,4 +167,12 @@ public boolean isUseCommonsLang3() {
public FileFilter getFileFilter() {
return new AllFileFilter();
}
}

/**
* @return <code>true</code>
*/
@Override
public boolean isInitializeEmptyCollections() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,11 @@ public interface GenerationConfig {
* @return the file filter use when scanning for schema files.
*/
FileFilter getFileFilter();


/**
* Gets the 'initializeEmptyCollections' configuration option.
*
* @return Whether to initialize collections with empty instance or null.
*/
boolean isInitializeEmptyCollections();
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ private JExpression getDefaultList(JType fieldType, JsonNode node) {
invokeAsList.arg(getDefaultValue(listGenericType, defaultValue));
}
newListImpl.arg(invokeAsList);
} else if (!ruleFactory.getGenerationConfig().isInitializeEmptyCollections()) {
return JExpr._null();
}

return newListImpl;
Expand Down Expand Up @@ -209,6 +211,8 @@ private JExpression getDefaultSet(JType fieldType, JsonNode node) {
invokeAsList.arg(getDefaultValue(setGenericType, defaultValue));
}
newSetImpl.arg(invokeAsList);
} else if (!ruleFactory.getGenerationConfig().isInitializeEmptyCollections()) {
return JExpr._null();
}

return newSetImpl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class JsonSchemaExtension implements GenerationConfig {
boolean useJodaDates
boolean useCommonsLang3
FileFilter fileFilter
boolean initializeEmptyCollections

public JsonSchemaExtension() {
// See DefaultGenerationConfig
Expand All @@ -69,6 +70,7 @@ public class JsonSchemaExtension implements GenerationConfig {
useJodaDates = false
useCommonsLang3 = false
fileFilter = new AllFileFilter()
initializeEmptyCollections = true
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,14 @@ public class Jsonschema2PojoMojo extends AbstractMojo implements GenerationConfi
*/
private boolean useCommonsLang3 = false;

/**
* Whether to initialize collections with empty instance or null.
*
* @parameter expression="${jsonschema2pojo.initializeEmptyCollections}" default="true"
* @since
*/
private boolean initializeEmptyCollections = true;

/**
* List of file patterns to include.
*
Expand Down Expand Up @@ -513,6 +521,11 @@ public FileFilter getFileFilter() {
return fileFilter;
}

@Override
public boolean isInitializeEmptyCollections() {
return initializeEmptyCollections;
}

boolean filteringEnabled() {
return !((includes == null || includes.length == 0) && (excludes == null || excludes.length == 0));
}
Expand Down