Situation
Sonar complains about the use of Optional as a parameter in TagImporter.java (rule S3553: "Optional" should not be used for parameters).
Affected code in TagImporter.java:
static TagImporter create(final Optional<PathConfig> config, final InputFile file,
final ImportEventListener listener)
{
final LineConsumer lineConsumer = createLineConsumer(config, file, listener);
return new TagImporter(lineConsumer, file);
}
private static LineConsumer createLineConsumer(final Optional<PathConfig> config,
final InputFile file, final ImportEventListener listener)
{
// ...
}
Using Optional as a parameter is considered a code smell because it forces callers to wrap arguments in an Optional even if they already have the object, and it doesn't provide the safety benefits that Optional returns provide.
Implementation Hints
- Modify
TagImporter.create and TagImporter.createLineConsumer to accept PathConfig directly instead of Optional<PathConfig>.
- The
PathConfig parameter should be nullable. Update the logic to check for null instead of config.isPresent().
- Alternatively, provide overloaded versions of these methods: one with
PathConfig and one without.
- Update
TagImporterFactory.createImporter to pass the value from the Optional or null:
final PathConfig config = findConfig(path).orElse(null);
return TagImporter.create(config, path, listener);
- Check for other usages of these methods in tests and update them accordingly.
Acceptance Criteria
TagImporter methods no longer use Optional as a parameter.
- Sonar S3553 warning for this location is resolved.
mvn verify passes, ensuring that the tag importer still works correctly with and without configuration.
Situation
Sonar complains about the use of
Optionalas a parameter inTagImporter.java(rule S3553: "Optional" should not be used for parameters).Affected code in
TagImporter.java:Using
Optionalas a parameter is considered a code smell because it forces callers to wrap arguments in anOptionaleven if they already have the object, and it doesn't provide the safety benefits thatOptionalreturns provide.Implementation Hints
TagImporter.createandTagImporter.createLineConsumerto acceptPathConfigdirectly instead ofOptional<PathConfig>.PathConfigparameter should be nullable. Update the logic to check fornullinstead ofconfig.isPresent().PathConfigand one without.TagImporterFactory.createImporterto pass the value from theOptionalornull:Acceptance Criteria
TagImportermethods no longer useOptionalas a parameter.mvn verifypasses, ensuring that the tag importer still works correctly with and without configuration.