Skip to content

#3976 Use Optional#orElseThrow instead of Optional#get on Java 10+ - #4138

Open
AnkitaAdvitot wants to merge 1 commit into
mapstruct:mainfrom
AnkitaAdvitot:fix/optional-orelsethrow-3976
Open

AnkitaAdvitot wants to merge 1 commit into
mapstruct:mainfrom
AnkitaAdvitot:fix/optional-orelsethrow-3976

Conversation

@AnkitaAdvitot

Copy link
Copy Markdown

Fixes #3976

Follow-up on #674. In generated mapper code, use Optional#orElseThrow instead of Optional#get when compiling on Java 10 or later, while preserving Optional#get when targeting Java 8 or Java 9.

Changes

  • Added isSourceVersionAtLeast10() to VersionInformation and DefaultVersionInformation, with unit tests in DefaultVersionInformationTest.
  • Passed VersionInformation to DefaultConversionContext and MappingResolverImpl.
  • Updated codegen locations emitting .get() on Optional to emit .orElseThrow() on Java 10+:
    • BeanMappingMethod.ftl & BeanMappingMethod.java (parameter unwrapping and after-mapping parameter bindings)
    • NestedPropertyMappingMethod.java
    • PropertyMapping.java
    • TypeToOptionalConversion.java
    • OptionalGetWrapper.java & OptionalGetWrapper.ftl
  • Primitive optionals (OptionalInt, OptionalLong, OptionalDouble) remain unchanged using getAsInt(), getAsLong(), getAsDouble().
  • Updated test fixtures and asciidoc reference documentation examples to reflect orElseThrow().

Testing

  • ./mvnw test -Dtest="*Optional*Test,DefaultVersionInformationTest" -pl processor (247 tests passing)
  • ./mvnw checkstyle:check -pl processor (0 violations)
  • ./mvnw compile -pl documentation (asciidoctor HTML & PDF generation verified)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use Optional#orElseThrow instead of Optional#get

2 participants