This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from CJSCommonPlatform/fix-builder-optionals
Fix builder optional arguments
- Loading branch information
Showing
12 changed files
with
365 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...n/java/uk/gov/justice/generation/pojo/plugin/classmodifying/builder/FieldSpecFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package uk.gov.justice.generation.pojo.plugin.classmodifying.builder; | ||
|
||
import static javax.lang.model.element.Modifier.PRIVATE; | ||
|
||
import uk.gov.justice.generation.pojo.dom.Definition; | ||
import uk.gov.justice.generation.pojo.generators.ClassNameFactory; | ||
import uk.gov.justice.generation.pojo.plugin.PluginContext; | ||
|
||
import com.squareup.javapoet.FieldSpec; | ||
import com.squareup.javapoet.ParameterizedTypeName; | ||
import com.squareup.javapoet.TypeName; | ||
|
||
public class FieldSpecFactory { | ||
|
||
private final OptionalTypeNameUtil optionalTypeNameUtil; | ||
|
||
public FieldSpecFactory(final OptionalTypeNameUtil optionalTypeNameUtil) { | ||
this.optionalTypeNameUtil = optionalTypeNameUtil; | ||
} | ||
|
||
public FieldSpec createFieldSpecFor(final Definition fieldDefinition, | ||
final ClassNameFactory classNameFactory, | ||
final PluginContext pluginContext) { | ||
|
||
final TypeName typeName = classNameFactory.createTypeNameFrom(fieldDefinition, pluginContext); | ||
|
||
if (optionalTypeNameUtil.isOptionalType(typeName)) { | ||
final TypeName parameterizedType = optionalTypeNameUtil.getOptionalTypeFrom((ParameterizedTypeName) typeName); | ||
return FieldSpec | ||
.builder(parameterizedType, fieldDefinition.getFieldName(), PRIVATE) | ||
.build(); | ||
} | ||
|
||
return FieldSpec | ||
.builder(typeName, fieldDefinition.getFieldName(), PRIVATE) | ||
.build(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...va/uk/gov/justice/generation/pojo/plugin/classmodifying/builder/OptionalTypeNameUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package uk.gov.justice.generation.pojo.plugin.classmodifying.builder; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import com.squareup.javapoet.ParameterizedTypeName; | ||
import com.squareup.javapoet.TypeName; | ||
|
||
public class OptionalTypeNameUtil { | ||
|
||
public TypeName getOptionalTypeFrom(final ParameterizedTypeName typeName) { | ||
return typeName.typeArguments.get(0); | ||
} | ||
|
||
public boolean isOptionalType(final TypeName typeName) { | ||
return typeName instanceof ParameterizedTypeName | ||
&& Objects.equals(((ParameterizedTypeName) typeName).rawType.simpleName(), Optional.class.getSimpleName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.