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.
- Loading branch information
Showing
9 changed files
with
227 additions
and
44 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
51 changes: 51 additions & 0 deletions
51
...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,51 @@ | ||
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 java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import com.squareup.javapoet.FieldSpec; | ||
import com.squareup.javapoet.ParameterizedTypeName; | ||
import com.squareup.javapoet.TypeName; | ||
|
||
public class FieldSpecFactory { | ||
|
||
public FieldSpec createFieldSpecFor(final Definition fieldDefinition, | ||
final ClassNameFactory classNameFactory, | ||
final PluginContext pluginContext) { | ||
|
||
final TypeName typeName = classNameFactory.createTypeNameFrom(fieldDefinition, pluginContext); | ||
|
||
if (isOptionalType(typeName)) { | ||
final TypeName parameterizedType = getParameterizedType((ParameterizedTypeName) typeName); | ||
return FieldSpec | ||
.builder(parameterizedType, fieldDefinition.getFieldName(), PRIVATE) | ||
.build(); | ||
} | ||
|
||
return FieldSpec | ||
.builder(typeName, fieldDefinition.getFieldName(), PRIVATE) | ||
.build(); | ||
} | ||
|
||
private TypeName getParameterizedType(final ParameterizedTypeName typeName) { | ||
final List<TypeName> typeArguments = typeName.typeArguments; | ||
|
||
if (null != typeArguments && !typeArguments.isEmpty()) { | ||
return typeArguments.get(0); | ||
} | ||
|
||
return typeName; | ||
} | ||
|
||
private 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
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.