From f26d6c4d05497e264d1a314e9154b8c524d38e91 Mon Sep 17 00:00:00 2001 From: Ankita-Advitot Date: Fri, 2 Oct 2026 23:08:57 +0530 Subject: [PATCH] #3976 Use Optional#orElseThrow instead of Optional#get on Java 10+ --- .../chapter-3-defining-a-mapper.asciidoc | 14 +- .../OptionalWrapperConversionProvider.java | 12 +- .../conversion/TypeToOptionalConversion.java | 9 +- .../ap/internal/model/BeanMappingMethod.java | 18 +- .../model/FromOptionalTypeConversion.java | 12 +- .../model/NestedPropertyMappingMethod.java | 4 +- .../ap/internal/model/PropertyMapping.java | 4 +- .../model/assignment/OptionalGetWrapper.java | 15 ++ .../model/common/ConversionContext.java | 5 + .../common/DefaultConversionContext.java | 14 ++ .../processor/DefaultVersionInformation.java | 7 + .../processor/MapperCreationProcessor.java | 3 +- .../creation/MappingResolverImpl.java | 16 +- .../internal/version/VersionInformation.java | 2 + .../ap/internal/model/BeanMappingMethod.ftl | 8 +- .../model/assignment/OptionalGetWrapper.ftl | 2 + .../DefaultVersionInformationTest.java | 93 ++++++++++ .../OptionalSourceTargetMapperImpl.java | 168 +++++++++--------- .../OptionalBeforeAfterMapperImpl.java | 6 +- .../OptionalDifferentTypesMapperImpl.java | 4 +- .../nested/OptionalNestedMapperImpl.java | 10 +- ...nalNestedPresenceCheckFirstMapperImpl.java | 24 +-- ...OptionalNestedPresenceCheckMapperImpl.java | 16 +- .../OptionalNullCheckAlwaysMapperImpl.java | 2 +- .../sametype/OptionalSameTypeMapperImpl.java | 6 +- 25 files changed, 331 insertions(+), 143 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/internal/processor/DefaultVersionInformationTest.java diff --git a/documentation/src/main/asciidoc/chapter-3-defining-a-mapper.asciidoc b/documentation/src/main/asciidoc/chapter-3-defining-a-mapper.asciidoc index 96c76fc20d..3dbdb16320 100644 --- a/documentation/src/main/asciidoc/chapter-3-defining-a-mapper.asciidoc +++ b/documentation/src/main/asciidoc/chapter-3-defining-a-mapper.asciidoc @@ -710,7 +710,7 @@ public Optional map(Optional product) { return Optional.empty(); } - Product productValue = product.get(); + Product productValue = product.orElseThrow(); ProductDto productDto = new ProductDto(); @@ -734,7 +734,7 @@ public ProductDto unwrap(Optional product) { return null; } - Product productValue = product.get(); + Product productValue = product.orElseThrow(); ProductDto productDto = new ProductDto(); @@ -822,10 +822,10 @@ public class CarMapperImpl implements CarMapper { CarDto carDto = new CarDto(); if ( car.getMake().isPresent() ) { - carDto.setManufacturer( car.getMake().get() ); + carDto.setManufacturer( car.getMake().orElseThrow() ); } if ( car.getNumberOfSeats().isPresent() ) { - carDto.setSeatCount( car.getNumberOfSeats().get() ); + carDto.setSeatCount( car.getNumberOfSeats().orElseThrow() ); } carDto.setDriver( personToPersonDto( car.getDriver() ) ); @@ -886,10 +886,10 @@ public void updateProduct(ProductUpdateDto dto, Product product) { } if ( dto.getName().isPresent() ) { - product.setName( dto.getName().get() ); + product.setName( dto.getName().orElseThrow() ); } if ( dto.getPrice().isPresent() ) { - product.setPrice( dto.getPrice().get() ); + product.setPrice( dto.getPrice().orElseThrow() ); } } ---- @@ -985,7 +985,7 @@ public Optional map(Optional person) { return Optional.empty(); } - Person personValue = person.get(); + Person personValue = person.orElseThrow(); PersonDto.Builder builder = PersonDto.builder(); diff --git a/processor/src/main/java/org/mapstruct/ap/internal/conversion/OptionalWrapperConversionProvider.java b/processor/src/main/java/org/mapstruct/ap/internal/conversion/OptionalWrapperConversionProvider.java index db9480662d..27698ffb2f 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/conversion/OptionalWrapperConversionProvider.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/conversion/OptionalWrapperConversionProvider.java @@ -15,6 +15,7 @@ import org.mapstruct.ap.internal.model.common.FieldReference; import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; +import org.mapstruct.ap.internal.version.VersionInformation; /** * A conversion provider that wraps / unwraps the underlying conversion in Optional. @@ -39,7 +40,11 @@ public Assignment to(ConversionContext conversionContext) { @Override public Assignment from(ConversionContext conversionContext) { Assignment assignment = conversionProvider.to( new OptionalConversionContext( conversionContext ) ); - return new FromOptionalTypeConversion( conversionContext.getSourceType(), assignment ); + return new FromOptionalTypeConversion( + conversionContext.getSourceType(), + assignment, + conversionContext.getVersionInformation() + ); } @Override @@ -96,5 +101,10 @@ public String getLocale() { public TypeFactory getTypeFactory() { return delegate.getTypeFactory(); } + + @Override + public VersionInformation getVersionInformation() { + return delegate.getVersionInformation(); + } } } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/conversion/TypeToOptionalConversion.java b/processor/src/main/java/org/mapstruct/ap/internal/conversion/TypeToOptionalConversion.java index dbab6bc4ca..99be4fcf74 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/conversion/TypeToOptionalConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/conversion/TypeToOptionalConversion.java @@ -36,11 +36,14 @@ protected Set getToConversionImportTypes(ConversionContext conversionConte @Override protected String getFromExpression(ConversionContext conversionContext) { - StringBuilder sb = new StringBuilder(".get"); Type optionalBaseType = conversionContext.getSourceType().getOptionalBaseType(); if ( optionalBaseType.isPrimitive() ) { - sb.append( "As" ).append( Strings.capitalize( optionalBaseType.getName() ) ); + return ".getAs" + Strings.capitalize( optionalBaseType.getName() ) + "()"; } - return sb.append( "()" ).toString(); + if ( conversionContext.getVersionInformation() != null && + conversionContext.getVersionInformation().isSourceVersionAtLeast10() ) { + return ".orElseThrow()"; + } + return ".get()"; } } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java index 080eab1e76..2f43248c86 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java @@ -71,6 +71,7 @@ import org.mapstruct.ap.internal.util.accessor.PresenceCheckAccessor; import org.mapstruct.ap.internal.util.accessor.ReadAccessor; import org.mapstruct.ap.internal.util.kotlin.KotlinMetadata; +import org.mapstruct.ap.internal.version.VersionInformation; import static org.mapstruct.ap.internal.model.beanmapping.MappingReferences.forSourceMethod; import static org.mapstruct.ap.internal.util.Collections.first; @@ -108,6 +109,7 @@ public class BeanMappingMethod extends NormalTypeMappingMethod { private final List afterMappingReferencesWithOptionalReturnType; private final Type subclassExhaustiveException; private final Map sourceParametersReassignments; + private final VersionInformation versionInformation; private final MappingReferences mappingReferences; @@ -396,7 +398,8 @@ else if ( !method.isUpdateMethod() ) { ParameterBinding.fromParameter( parameter ) : ParameterBinding.fromTypeAndName( parameter.getType(), - parameter.getOriginalName() + ".get()" + parameter.getOriginalName() + ( ctx.getVersionInformation().isSourceVersionAtLeast10() ? + ".orElseThrow()" : ".get()" ) ) ) .collect( Collectors.toList() ); List afterMappingMethods = LifecycleMethodResolver.afterMappingMethods( @@ -542,7 +545,8 @@ else if ( !method.isUpdateMethod() ) { subclasses, presenceChecksByParameter, subclassExhaustiveExceptionType, - sourceParametersReassignments + sourceParametersReassignments, + ctx.getVersionInformation() ); } @@ -2131,8 +2135,9 @@ private BeanMappingMethod(Method method, MappingReferences mappingReferences, List subclassMappings, Map presenceChecksByParameter, - Type subclassExhaustiveException, - Map sourceParametersReassignments + Type subclassExhaustiveException, + Map sourceParametersReassignments, + VersionInformation versionInformation ) { super( method, @@ -2206,6 +2211,7 @@ else if ( sourceParameterNames.contains( mapping.getSourceBeanName() ) ) { : null; this.subclassMappings = subclassMappings; this.sourceParametersReassignments = sourceParametersReassignments; + this.versionInformation = versionInformation; } public Type getSubclassExhaustiveException() { @@ -2412,4 +2418,8 @@ public boolean equals(Object obj) { return true; } + + public VersionInformation getVersionInformation() { + return versionInformation; + } } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/FromOptionalTypeConversion.java b/processor/src/main/java/org/mapstruct/ap/internal/model/FromOptionalTypeConversion.java index e1c320e16b..54b6616900 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/FromOptionalTypeConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/FromOptionalTypeConversion.java @@ -13,6 +13,7 @@ import org.mapstruct.ap.internal.model.common.ModelElement; import org.mapstruct.ap.internal.model.common.PresenceCheck; import org.mapstruct.ap.internal.model.common.Type; +import org.mapstruct.ap.internal.version.VersionInformation; /** * An inline conversion from an optional source to it's value. @@ -23,10 +24,17 @@ public class FromOptionalTypeConversion extends ModelElement implements Assignme private final Assignment conversionAssignment; private final Type optionalType; + private final VersionInformation versionInformation; public FromOptionalTypeConversion(Type optionalType, Assignment conversionAssignment) { + this( optionalType, conversionAssignment, null ); + } + + public FromOptionalTypeConversion(Type optionalType, Assignment conversionAssignment, + VersionInformation versionInformation) { this.conversionAssignment = conversionAssignment; this.optionalType = optionalType; + this.versionInformation = versionInformation; } @Override @@ -95,7 +103,9 @@ public String getSourceParameterName() { @Override public void setAssignment(Assignment assignment) { - this.conversionAssignment.setAssignment( new OptionalGetWrapper( assignment, optionalType ) ); + this.conversionAssignment.setAssignment( + new OptionalGetWrapper( assignment, optionalType, versionInformation ) + ); } @Override diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/NestedPropertyMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/NestedPropertyMappingMethod.java index 79341b8921..2cd3330f39 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/NestedPropertyMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/NestedPropertyMappingMethod.java @@ -92,7 +92,9 @@ public NestedPropertyMappingMethod build() { presenceCheck = getPresenceCheck( propertyEntry, optionalValueSafeName ); - String optionalValueSource = previousPropertyName + ".get()"; + String optionalValueSource = previousPropertyName + ( + ctx.getVersionInformation().isSourceVersionAtLeast10() ? ".orElseThrow()" : ".get()" + ); boolean doesNotNeedFollowUpProperty = false; if ( i == propertyEntries.size() - 1 ) { // If this is the last property, and we do not have a presence check, diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java b/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java index c8497ef40c..8d1f167ac0 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java @@ -907,7 +907,9 @@ private PresenceCheck getSourcePresenceCheckerRef(SourceReference sourceReferenc variableName, ctx.getVersionInformation() ) ); - variableName = variableName + ".get()"; + variableName = variableName + ( + ctx.getVersionInformation().isSourceVersionAtLeast10() ? ".orElseThrow()" : ".get()" + ); } else { presenceChecks.add( new NullPresenceCheck( variableName ) ); diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/assignment/OptionalGetWrapper.java b/processor/src/main/java/org/mapstruct/ap/internal/model/assignment/OptionalGetWrapper.java index 6c405c871d..b326fd8e78 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/assignment/OptionalGetWrapper.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/assignment/OptionalGetWrapper.java @@ -8,6 +8,7 @@ import org.mapstruct.ap.internal.model.common.Assignment; import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.util.Strings; +import org.mapstruct.ap.internal.version.VersionInformation; /** * Decorates the assignment as an {@link java.util.Optional#get()} call. @@ -17,19 +18,33 @@ public class OptionalGetWrapper extends AssignmentWrapper { private final Type optionalType; + private final VersionInformation versionInformation; public OptionalGetWrapper(Assignment decoratedAssignment, Type optionalType) { + this( decoratedAssignment, optionalType, null ); + } + + public OptionalGetWrapper(Assignment decoratedAssignment, Type optionalType, + VersionInformation versionInformation) { super( decoratedAssignment, false ); this.optionalType = optionalType; + this.versionInformation = versionInformation; } public Type getOptionalType() { return optionalType; } + public VersionInformation getVersionInformation() { + return versionInformation; + } + @Override public String toString() { if ( optionalType.getFullyQualifiedName().equals( "java.util.Optional" ) ) { + if ( versionInformation != null && versionInformation.isSourceVersionAtLeast10() ) { + return getAssignment() + ".orElseThrow()"; + } return getAssignment() + ".get()"; } return getAssignment() + ".getAs" + Strings.capitalize( optionalType.getOptionalBaseType().getName() ) + "()"; diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/common/ConversionContext.java b/processor/src/main/java/org/mapstruct/ap/internal/model/common/ConversionContext.java index d3a29e30a4..3ab80224af 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/common/ConversionContext.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/common/ConversionContext.java @@ -7,6 +7,8 @@ import java.util.Date; +import org.mapstruct.ap.internal.version.VersionInformation; + /** * Context object passed to conversion providers and built-in methods. * @@ -43,4 +45,7 @@ public interface ConversionContext { TypeFactory getTypeFactory(); + default VersionInformation getVersionInformation() { + return null; + } } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/common/DefaultConversionContext.java b/processor/src/main/java/org/mapstruct/ap/internal/model/common/DefaultConversionContext.java index 4e5eed47a8..b00754894c 100755 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/common/DefaultConversionContext.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/common/DefaultConversionContext.java @@ -7,6 +7,7 @@ import org.mapstruct.ap.internal.util.FormattingMessager; import org.mapstruct.ap.internal.util.Strings; +import org.mapstruct.ap.internal.version.VersionInformation; /** * Default implementation of the {@link ConversionContext} passed to conversion providers. @@ -23,9 +24,16 @@ public class DefaultConversionContext implements ConversionContext { private final String numberFormat; private final String locale; private final TypeFactory typeFactory; + private final VersionInformation versionInformation; public DefaultConversionContext(TypeFactory typeFactory, FormattingMessager messager, Type sourceType, Type targetType, FormattingParameters formattingParameters) { + this( typeFactory, messager, sourceType, targetType, formattingParameters, null ); + } + + public DefaultConversionContext(TypeFactory typeFactory, FormattingMessager messager, Type sourceType, + Type targetType, FormattingParameters formattingParameters, + VersionInformation versionInformation) { this.typeFactory = typeFactory; this.messager = messager; this.sourceType = sourceType; @@ -34,6 +42,7 @@ public DefaultConversionContext(TypeFactory typeFactory, FormattingMessager mess this.dateFormat = this.formattingParameters.getDate(); this.numberFormat = this.formattingParameters.getNumber(); this.locale = this.formattingParameters.getLocale(); + this.versionInformation = versionInformation; validateDateFormat(); } @@ -89,4 +98,9 @@ public TypeFactory getTypeFactory() { protected FormattingMessager getMessager() { return messager; } + + @Override + public VersionInformation getVersionInformation() { + return versionInformation; + } } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/processor/DefaultVersionInformation.java b/processor/src/main/java/org/mapstruct/ap/internal/processor/DefaultVersionInformation.java index f8053c92ab..9f7b08dc1b 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/processor/DefaultVersionInformation.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/processor/DefaultVersionInformation.java @@ -40,6 +40,7 @@ public class DefaultVersionInformation implements VersionInformation { private final String runtimeVendor; private final String compiler; private final boolean sourceVersionAtLeast9; + private final boolean sourceVersionAtLeast10; private final boolean sourceVersionAtLeast11; private final boolean sourceVersionAtLeast14; private final boolean sourceVersionAtLeast19; @@ -55,6 +56,7 @@ public class DefaultVersionInformation implements VersionInformation { this.javac = compiler.startsWith( COMPILER_NAME_JAVAC ); // If the difference between the source version and RELEASE_6 is more that 2 than we are at least on 9 this.sourceVersionAtLeast9 = sourceVersion.compareTo( SourceVersion.RELEASE_6 ) > 2; + this.sourceVersionAtLeast10 = sourceVersion.compareTo( SourceVersion.RELEASE_6 ) > 3; this.sourceVersionAtLeast11 = sourceVersion.compareTo( SourceVersion.RELEASE_6 ) > 4; this.sourceVersionAtLeast14 = sourceVersion.compareTo( SourceVersion.RELEASE_6 ) > 7; this.sourceVersionAtLeast19 = sourceVersion.compareTo( SourceVersion.RELEASE_6 ) > 12; @@ -85,6 +87,11 @@ public boolean isSourceVersionAtLeast9() { return sourceVersionAtLeast9; } + @Override + public boolean isSourceVersionAtLeast10() { + return sourceVersionAtLeast10; + } + @Override public boolean isSourceVersionAtLeast11() { return sourceVersionAtLeast11; diff --git a/processor/src/main/java/org/mapstruct/ap/internal/processor/MapperCreationProcessor.java b/processor/src/main/java/org/mapstruct/ap/internal/processor/MapperCreationProcessor.java index db16d52106..f6da6a4040 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/processor/MapperCreationProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/processor/MapperCreationProcessor.java @@ -132,7 +132,8 @@ public Mapper process(ProcessorContext context, TypeElement mapperTypeElement, L typeFactory, new ArrayList<>( sourceModel ), mapperReferences, - options.isVerbose() + options.isVerbose(), + versionInformation ), mapperTypeElement, //sourceModel is passed only to fetch the after/before mapping methods in lifecycleCallbackFactory; diff --git a/processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java b/processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java index fb380dc146..61369b2b62 100755 --- a/processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java @@ -62,6 +62,7 @@ import org.mapstruct.ap.internal.util.NativeTypes; import org.mapstruct.ap.internal.util.Strings; import org.mapstruct.ap.internal.util.TypeUtils; +import org.mapstruct.ap.internal.version.VersionInformation; import static org.mapstruct.ap.internal.util.Collections.first; import static org.mapstruct.ap.internal.util.Collections.firstKey; @@ -90,6 +91,7 @@ public class MappingResolverImpl implements MappingResolver { private final MethodSelectors methodSelectors; private final boolean verboseLogging; + private final VersionInformation versionInformation; private static final String JL_OBJECT_NAME = Object.class.getName(); @@ -107,6 +109,13 @@ public class MappingResolverImpl implements MappingResolver { public MappingResolverImpl(FormattingMessager messager, ElementUtils elementUtils, TypeUtils typeUtils, TypeFactory typeFactory, List sourceModel, List mapperReferences, boolean verboseLogging) { + this( messager, elementUtils, typeUtils, typeFactory, sourceModel, mapperReferences, verboseLogging, null ); + } + + public MappingResolverImpl(FormattingMessager messager, ElementUtils elementUtils, TypeUtils typeUtils, + TypeFactory typeFactory, List sourceModel, + List mapperReferences, boolean verboseLogging, + VersionInformation versionInformation) { this.messager = messager; this.typeUtils = typeUtils; this.typeFactory = typeFactory; @@ -119,6 +128,7 @@ public MappingResolverImpl(FormattingMessager messager, ElementUtils elementUtil this.methodSelectors = new MethodSelectors( typeUtils, elementUtils, messager, null ); this.verboseLogging = verboseLogging; + this.versionInformation = versionInformation; } @Override @@ -442,7 +452,8 @@ private ConversionAssignment resolveViaConversion(Type sourceType, Type targetTy messager, sourceType, targetType, - formattingParameters + formattingParameters, + versionInformation ); // add helper methods required in conversion @@ -555,7 +566,8 @@ private Assignment toBuildInRef(SelectedMethod selectedMethod) { messager, method.getMappingSourceType(), method.getResultType(), - formattingParameters + formattingParameters, + versionInformation ); Assignment methodReference = MethodReference.forBuiltInMethod( method, ctx ); methodReference.setAssignment( sourceRHS ); diff --git a/processor/src/main/java/org/mapstruct/ap/internal/version/VersionInformation.java b/processor/src/main/java/org/mapstruct/ap/internal/version/VersionInformation.java index a30a383c0c..8eb7b6c912 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/version/VersionInformation.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/version/VersionInformation.java @@ -21,6 +21,8 @@ public interface VersionInformation { boolean isSourceVersionAtLeast9(); + boolean isSourceVersionAtLeast10(); + boolean isSourceVersionAtLeast11(); boolean isSourceVersionAtLeast14(); diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/BeanMappingMethod.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/BeanMappingMethod.ftl index fd101f0d95..373e33e4fc 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/BeanMappingMethod.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/BeanMappingMethod.ftl @@ -62,7 +62,7 @@ if ( <@includeModel object=getPresenceCheckByParameter(sourceParam) /> ) { <#assign sourceParamReassignment = getSourceParameterReassignment(sourceParam)!'' /> <#if sourceParamReassignment?has_content> - <@includeModel object=sourceParamReassignment.type /> ${sourceParamReassignment.name} = ${sourceParam.name}.get(); + <@includeModel object=sourceParamReassignment.type /> ${sourceParamReassignment.name} = ${sourceParam.name}.<#if versionInformation?? && versionInformation.isSourceVersionAtLeast10()>orElseThrow()<#else>get(); <#list constructorPropertyMappingsByParameter(sourceParam) as propertyMapping> @@ -86,7 +86,7 @@ <#if mapNullToDefault>if ( <@includeModel object=getPresenceCheckByParameter(sourceParameters[0]) /> ) { <#assign sourceParamReassignment = getSourceParameterReassignment(sourceParameters[0])!'' /> <#if sourceParamReassignment?has_content> - <@includeModel object=sourceParamReassignment.type /> ${sourceParamReassignment.name} = ${sourceParameters[0].name}.get(); + <@includeModel object=sourceParamReassignment.type /> ${sourceParamReassignment.name} = ${sourceParameters[0].name}.<#if versionInformation?? && versionInformation.isSourceVersionAtLeast10()>orElseThrow()<#else>get(); <#list constructorPropertyMappingsByParameter(sourceParameters[0]) as propertyMapping> @@ -122,7 +122,7 @@ if ( <@includeModel object=getPresenceCheckByParameter(sourceParam) /> ) { <#assign sourceParamReassignment = getSourceParameterReassignment(sourceParam)!'' /> <#if sourceParamReassignment?has_content> - <@includeModel object=sourceParamReassignment.type /> ${sourceParamReassignment.name} = ${sourceParam.name}.get(); + <@includeModel object=sourceParamReassignment.type /> ${sourceParamReassignment.name} = ${sourceParam.name}.<#if versionInformation?? && versionInformation.isSourceVersionAtLeast10()>orElseThrow()<#else>get(); <#list propertyMappingsByParameter(sourceParam) as propertyMapping> @@ -142,7 +142,7 @@ <#if mapNullToDefault>if ( <@includeModel object=getPresenceCheckByParameter(sourceParameters[0]) /> ) { <#assign sourceParamReassignment = getSourceParameterReassignment(sourceParameters[0])!'' /> <#if sourceParamReassignment?has_content> - <@includeModel object=sourceParamReassignment.type /> ${sourceParamReassignment.name} = ${sourceParameters[0].name}.get(); + <@includeModel object=sourceParamReassignment.type /> ${sourceParamReassignment.name} = ${sourceParameters[0].name}.<#if versionInformation?? && versionInformation.isSourceVersionAtLeast10()>orElseThrow()<#else>get(); <#list propertyMappingsByParameter(sourceParameters[0]) as propertyMapping> diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/OptionalGetWrapper.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/OptionalGetWrapper.ftl index 7bbc089d7c..8b89763da2 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/OptionalGetWrapper.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/OptionalGetWrapper.ftl @@ -9,6 +9,8 @@ <@compress single_line=true> <#if optionalType.optionalBaseType.isPrimitive()> ${assignment}.getAs${optionalType.optionalBaseType.name?cap_first}() +<#elseif versionInformation?? && versionInformation.isSourceVersionAtLeast10()> +${assignment}.orElseThrow() <#else> ${assignment}.get() diff --git a/processor/src/test/java/org/mapstruct/ap/internal/processor/DefaultVersionInformationTest.java b/processor/src/test/java/org/mapstruct/ap/internal/processor/DefaultVersionInformationTest.java new file mode 100644 index 0000000000..b5f7adb76b --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/internal/processor/DefaultVersionInformationTest.java @@ -0,0 +1,93 @@ +/* + * Copyright MapStruct Authors. + * + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +package org.mapstruct.ap.internal.processor; + +import javax.lang.model.SourceVersion; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Ankita Advitot + */ +class DefaultVersionInformationTest { + + @Test + void testSourceVersions() { + DefaultVersionInformation v8 = new DefaultVersionInformation( + "1.8.0", + "vendor", + "javac", + SourceVersion.RELEASE_8 + ); + assertThat( v8.isSourceVersionAtLeast9() ).isFalse(); + assertThat( v8.isSourceVersionAtLeast10() ).isFalse(); + assertThat( v8.isSourceVersionAtLeast11() ).isFalse(); + assertThat( v8.isSourceVersionAtLeast14() ).isFalse(); + assertThat( v8.isSourceVersionAtLeast19() ).isFalse(); + + DefaultVersionInformation v9 = new DefaultVersionInformation( + "9.0.0", + "vendor", + "javac", + SourceVersion.RELEASE_9 + ); + assertThat( v9.isSourceVersionAtLeast9() ).isTrue(); + assertThat( v9.isSourceVersionAtLeast10() ).isFalse(); + assertThat( v9.isSourceVersionAtLeast11() ).isFalse(); + assertThat( v9.isSourceVersionAtLeast14() ).isFalse(); + assertThat( v9.isSourceVersionAtLeast19() ).isFalse(); + + DefaultVersionInformation v10 = new DefaultVersionInformation( + "10.0.0", + "vendor", + "javac", + SourceVersion.RELEASE_10 + ); + assertThat( v10.isSourceVersionAtLeast9() ).isTrue(); + assertThat( v10.isSourceVersionAtLeast10() ).isTrue(); + assertThat( v10.isSourceVersionAtLeast11() ).isFalse(); + assertThat( v10.isSourceVersionAtLeast14() ).isFalse(); + assertThat( v10.isSourceVersionAtLeast19() ).isFalse(); + + DefaultVersionInformation v11 = new DefaultVersionInformation( + "11.0.0", + "vendor", + "javac", + SourceVersion.RELEASE_11 + ); + assertThat( v11.isSourceVersionAtLeast9() ).isTrue(); + assertThat( v11.isSourceVersionAtLeast10() ).isTrue(); + assertThat( v11.isSourceVersionAtLeast11() ).isTrue(); + assertThat( v11.isSourceVersionAtLeast14() ).isFalse(); + assertThat( v11.isSourceVersionAtLeast19() ).isFalse(); + + DefaultVersionInformation v14 = new DefaultVersionInformation( + "14.0.0", + "vendor", + "javac", + SourceVersion.RELEASE_14 + ); + assertThat( v14.isSourceVersionAtLeast9() ).isTrue(); + assertThat( v14.isSourceVersionAtLeast10() ).isTrue(); + assertThat( v14.isSourceVersionAtLeast11() ).isTrue(); + assertThat( v14.isSourceVersionAtLeast14() ).isTrue(); + assertThat( v14.isSourceVersionAtLeast19() ).isFalse(); + + DefaultVersionInformation v19 = new DefaultVersionInformation( + "19.0.0", + "vendor", + "javac", + SourceVersion.valueOf( "RELEASE_19" ) + ); + assertThat( v19.isSourceVersionAtLeast9() ).isTrue(); + assertThat( v19.isSourceVersionAtLeast10() ).isTrue(); + assertThat( v19.isSourceVersionAtLeast11() ).isTrue(); + assertThat( v19.isSourceVersionAtLeast14() ).isTrue(); + assertThat( v19.isSourceVersionAtLeast19() ).isTrue(); + } +} diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/conversion/java8time/OptionalSourceTargetMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/conversion/java8time/OptionalSourceTargetMapperImpl.java index 01191a9819..647f9cc18a 100644 --- a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/conversion/java8time/OptionalSourceTargetMapperImpl.java +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/conversion/java8time/OptionalSourceTargetMapperImpl.java @@ -42,46 +42,46 @@ public Target sourceToTarget(OptionalSource source) { Target target = new Target(); if ( source.getZonedDateTime().isPresent() ) { - target.setZonedDateTime( dateTimeFormatter_dd_MM_yyyy_HH_mm_z_01894582668.format( source.getZonedDateTime().get() ) ); + target.setZonedDateTime( dateTimeFormatter_dd_MM_yyyy_HH_mm_z_01894582668.format( source.getZonedDateTime().orElseThrow() ) ); } if ( source.getLocalDateTime().isPresent() ) { - target.setLocalDateTime( dateTimeFormatter_dd_MM_yyyy_HH_mm_12071769242.format( source.getLocalDateTime().get() ) ); + target.setLocalDateTime( dateTimeFormatter_dd_MM_yyyy_HH_mm_12071769242.format( source.getLocalDateTime().orElseThrow() ) ); } if ( source.getLocalDate().isPresent() ) { - target.setLocalDate( dateTimeFormatter_dd_MM_yyyy_11900521056.format( source.getLocalDate().get() ) ); + target.setLocalDate( dateTimeFormatter_dd_MM_yyyy_11900521056.format( source.getLocalDate().orElseThrow() ) ); } if ( source.getLocalTime().isPresent() ) { - target.setLocalTime( dateTimeFormatter_HH_mm_168697690.format( source.getLocalTime().get() ) ); + target.setLocalTime( dateTimeFormatter_HH_mm_168697690.format( source.getLocalTime().orElseThrow() ) ); } if ( source.getForCalendarConversion().isPresent() ) { - target.setForCalendarConversion( zonedDateTimeToCalendar( source.getForCalendarConversion().get() ) ); + target.setForCalendarConversion( zonedDateTimeToCalendar( source.getForCalendarConversion().orElseThrow() ) ); } if ( source.getForDateConversionWithZonedDateTime().isPresent() ) { - target.setForDateConversionWithZonedDateTime( Date.from( source.getForDateConversionWithZonedDateTime().get().toInstant() ) ); + target.setForDateConversionWithZonedDateTime( Date.from( source.getForDateConversionWithZonedDateTime().orElseThrow().toInstant() ) ); } if ( source.getForDateConversionWithLocalDateTime().isPresent() ) { - target.setForDateConversionWithLocalDateTime( Date.from( source.getForDateConversionWithLocalDateTime().get().toInstant( ZoneOffset.UTC ) ) ); + target.setForDateConversionWithLocalDateTime( Date.from( source.getForDateConversionWithLocalDateTime().orElseThrow().toInstant( ZoneOffset.UTC ) ) ); } if ( source.getForDateConversionWithLocalDate().isPresent() ) { - target.setForDateConversionWithLocalDate( Date.from( source.getForDateConversionWithLocalDate().get().atStartOfDay( ZoneOffset.UTC ).toInstant() ) ); + target.setForDateConversionWithLocalDate( Date.from( source.getForDateConversionWithLocalDate().orElseThrow().atStartOfDay( ZoneOffset.UTC ).toInstant() ) ); } if ( source.getForSqlDateConversionWithLocalDate().isPresent() ) { - target.setForSqlDateConversionWithLocalDate( new java.sql.Date( source.getForSqlDateConversionWithLocalDate().get().atStartOfDay( ZoneOffset.UTC ).toInstant().toEpochMilli() ) ); + target.setForSqlDateConversionWithLocalDate( new java.sql.Date( source.getForSqlDateConversionWithLocalDate().orElseThrow().atStartOfDay( ZoneOffset.UTC ).toInstant().toEpochMilli() ) ); } if ( source.getForDateConversionWithInstant().isPresent() ) { - target.setForDateConversionWithInstant( Date.from( source.getForDateConversionWithInstant().get() ) ); + target.setForDateConversionWithInstant( Date.from( source.getForDateConversionWithInstant().orElseThrow() ) ); } if ( source.getForLocalDateTimeConversionWithLocalDate().isPresent() ) { - target.setForLocalDateTimeConversionWithLocalDate( source.getForLocalDateTimeConversionWithLocalDate().get().atStartOfDay() ); + target.setForLocalDateTimeConversionWithLocalDate( source.getForLocalDateTimeConversionWithLocalDate().orElseThrow().atStartOfDay() ); } if ( source.getForInstantConversionWithString().isPresent() ) { - target.setForInstantConversionWithString( source.getForInstantConversionWithString().get().toString() ); + target.setForInstantConversionWithString( source.getForInstantConversionWithString().orElseThrow().toString() ); } if ( source.getForPeriodConversionWithString().isPresent() ) { - target.setForPeriodConversionWithString( source.getForPeriodConversionWithString().get().toString() ); + target.setForPeriodConversionWithString( source.getForPeriodConversionWithString().orElseThrow().toString() ); } if ( source.getForDurationConversionWithString().isPresent() ) { - target.setForDurationConversionWithString( source.getForDurationConversionWithString().get().toString() ); + target.setForDurationConversionWithString( source.getForDurationConversionWithString().orElseThrow().toString() ); } return target; @@ -96,46 +96,46 @@ public Target sourceToTargetDefaultMapping(OptionalSource source) { Target target = new Target(); if ( source.getZonedDateTime().isPresent() ) { - target.setZonedDateTime( dateTimeFormatter_dd_MM_yyyy_HH_mm_z_01894582668.format( source.getZonedDateTime().get() ) ); + target.setZonedDateTime( dateTimeFormatter_dd_MM_yyyy_HH_mm_z_01894582668.format( source.getZonedDateTime().orElseThrow() ) ); } if ( source.getLocalDateTime().isPresent() ) { - target.setLocalDateTime( dateTimeFormatter_dd_MM_yyyy_HH_mm_12071769242.format( source.getLocalDateTime().get() ) ); + target.setLocalDateTime( dateTimeFormatter_dd_MM_yyyy_HH_mm_12071769242.format( source.getLocalDateTime().orElseThrow() ) ); } if ( source.getLocalDate().isPresent() ) { - target.setLocalDate( dateTimeFormatter_dd_MM_yyyy_11900521056.format( source.getLocalDate().get() ) ); + target.setLocalDate( dateTimeFormatter_dd_MM_yyyy_11900521056.format( source.getLocalDate().orElseThrow() ) ); } if ( source.getLocalTime().isPresent() ) { - target.setLocalTime( dateTimeFormatter_HH_mm_168697690.format( source.getLocalTime().get() ) ); + target.setLocalTime( dateTimeFormatter_HH_mm_168697690.format( source.getLocalTime().orElseThrow() ) ); } if ( source.getForCalendarConversion().isPresent() ) { - target.setForCalendarConversion( zonedDateTimeToCalendar( source.getForCalendarConversion().get() ) ); + target.setForCalendarConversion( zonedDateTimeToCalendar( source.getForCalendarConversion().orElseThrow() ) ); } if ( source.getForDateConversionWithZonedDateTime().isPresent() ) { - target.setForDateConversionWithZonedDateTime( Date.from( source.getForDateConversionWithZonedDateTime().get().toInstant() ) ); + target.setForDateConversionWithZonedDateTime( Date.from( source.getForDateConversionWithZonedDateTime().orElseThrow().toInstant() ) ); } if ( source.getForDateConversionWithLocalDateTime().isPresent() ) { - target.setForDateConversionWithLocalDateTime( Date.from( source.getForDateConversionWithLocalDateTime().get().toInstant( ZoneOffset.UTC ) ) ); + target.setForDateConversionWithLocalDateTime( Date.from( source.getForDateConversionWithLocalDateTime().orElseThrow().toInstant( ZoneOffset.UTC ) ) ); } if ( source.getForDateConversionWithLocalDate().isPresent() ) { - target.setForDateConversionWithLocalDate( Date.from( source.getForDateConversionWithLocalDate().get().atStartOfDay( ZoneOffset.UTC ).toInstant() ) ); + target.setForDateConversionWithLocalDate( Date.from( source.getForDateConversionWithLocalDate().orElseThrow().atStartOfDay( ZoneOffset.UTC ).toInstant() ) ); } if ( source.getForSqlDateConversionWithLocalDate().isPresent() ) { - target.setForSqlDateConversionWithLocalDate( new java.sql.Date( source.getForSqlDateConversionWithLocalDate().get().atStartOfDay( ZoneOffset.UTC ).toInstant().toEpochMilli() ) ); + target.setForSqlDateConversionWithLocalDate( new java.sql.Date( source.getForSqlDateConversionWithLocalDate().orElseThrow().atStartOfDay( ZoneOffset.UTC ).toInstant().toEpochMilli() ) ); } if ( source.getForDateConversionWithInstant().isPresent() ) { - target.setForDateConversionWithInstant( Date.from( source.getForDateConversionWithInstant().get() ) ); + target.setForDateConversionWithInstant( Date.from( source.getForDateConversionWithInstant().orElseThrow() ) ); } if ( source.getForLocalDateTimeConversionWithLocalDate().isPresent() ) { - target.setForLocalDateTimeConversionWithLocalDate( source.getForLocalDateTimeConversionWithLocalDate().get().atStartOfDay() ); + target.setForLocalDateTimeConversionWithLocalDate( source.getForLocalDateTimeConversionWithLocalDate().orElseThrow().atStartOfDay() ); } if ( source.getForInstantConversionWithString().isPresent() ) { - target.setForInstantConversionWithString( source.getForInstantConversionWithString().get().toString() ); + target.setForInstantConversionWithString( source.getForInstantConversionWithString().orElseThrow().toString() ); } if ( source.getForPeriodConversionWithString().isPresent() ) { - target.setForPeriodConversionWithString( source.getForPeriodConversionWithString().get().toString() ); + target.setForPeriodConversionWithString( source.getForPeriodConversionWithString().orElseThrow().toString() ); } if ( source.getForDurationConversionWithString().isPresent() ) { - target.setForDurationConversionWithString( source.getForDurationConversionWithString().get().toString() ); + target.setForDurationConversionWithString( source.getForDurationConversionWithString().orElseThrow().toString() ); } return target; @@ -150,46 +150,46 @@ public Target sourceToTargetDateTimeMapped(OptionalSource source) { Target target = new Target(); if ( source.getZonedDateTime().isPresent() ) { - target.setZonedDateTime( dateTimeFormatter_dd_MM_yyyy_HH_mm_z_01894582668.format( source.getZonedDateTime().get() ) ); + target.setZonedDateTime( dateTimeFormatter_dd_MM_yyyy_HH_mm_z_01894582668.format( source.getZonedDateTime().orElseThrow() ) ); } if ( source.getLocalDateTime().isPresent() ) { - target.setLocalDateTime( DateTimeFormatter.ISO_LOCAL_DATE_TIME.format( source.getLocalDateTime().get() ) ); + target.setLocalDateTime( DateTimeFormatter.ISO_LOCAL_DATE_TIME.format( source.getLocalDateTime().orElseThrow() ) ); } if ( source.getLocalDate().isPresent() ) { - target.setLocalDate( DateTimeFormatter.ISO_LOCAL_DATE.format( source.getLocalDate().get() ) ); + target.setLocalDate( DateTimeFormatter.ISO_LOCAL_DATE.format( source.getLocalDate().orElseThrow() ) ); } if ( source.getLocalTime().isPresent() ) { - target.setLocalTime( DateTimeFormatter.ISO_LOCAL_TIME.format( source.getLocalTime().get() ) ); + target.setLocalTime( DateTimeFormatter.ISO_LOCAL_TIME.format( source.getLocalTime().orElseThrow() ) ); } if ( source.getForCalendarConversion().isPresent() ) { - target.setForCalendarConversion( zonedDateTimeToCalendar( source.getForCalendarConversion().get() ) ); + target.setForCalendarConversion( zonedDateTimeToCalendar( source.getForCalendarConversion().orElseThrow() ) ); } if ( source.getForDateConversionWithZonedDateTime().isPresent() ) { - target.setForDateConversionWithZonedDateTime( Date.from( source.getForDateConversionWithZonedDateTime().get().toInstant() ) ); + target.setForDateConversionWithZonedDateTime( Date.from( source.getForDateConversionWithZonedDateTime().orElseThrow().toInstant() ) ); } if ( source.getForDateConversionWithLocalDateTime().isPresent() ) { - target.setForDateConversionWithLocalDateTime( Date.from( source.getForDateConversionWithLocalDateTime().get().toInstant( ZoneOffset.UTC ) ) ); + target.setForDateConversionWithLocalDateTime( Date.from( source.getForDateConversionWithLocalDateTime().orElseThrow().toInstant( ZoneOffset.UTC ) ) ); } if ( source.getForDateConversionWithLocalDate().isPresent() ) { - target.setForDateConversionWithLocalDate( Date.from( source.getForDateConversionWithLocalDate().get().atStartOfDay( ZoneOffset.UTC ).toInstant() ) ); + target.setForDateConversionWithLocalDate( Date.from( source.getForDateConversionWithLocalDate().orElseThrow().atStartOfDay( ZoneOffset.UTC ).toInstant() ) ); } if ( source.getForSqlDateConversionWithLocalDate().isPresent() ) { - target.setForSqlDateConversionWithLocalDate( new java.sql.Date( source.getForSqlDateConversionWithLocalDate().get().atStartOfDay( ZoneOffset.UTC ).toInstant().toEpochMilli() ) ); + target.setForSqlDateConversionWithLocalDate( new java.sql.Date( source.getForSqlDateConversionWithLocalDate().orElseThrow().atStartOfDay( ZoneOffset.UTC ).toInstant().toEpochMilli() ) ); } if ( source.getForDateConversionWithInstant().isPresent() ) { - target.setForDateConversionWithInstant( Date.from( source.getForDateConversionWithInstant().get() ) ); + target.setForDateConversionWithInstant( Date.from( source.getForDateConversionWithInstant().orElseThrow() ) ); } if ( source.getForLocalDateTimeConversionWithLocalDate().isPresent() ) { - target.setForLocalDateTimeConversionWithLocalDate( source.getForLocalDateTimeConversionWithLocalDate().get().atStartOfDay() ); + target.setForLocalDateTimeConversionWithLocalDate( source.getForLocalDateTimeConversionWithLocalDate().orElseThrow().atStartOfDay() ); } if ( source.getForInstantConversionWithString().isPresent() ) { - target.setForInstantConversionWithString( source.getForInstantConversionWithString().get().toString() ); + target.setForInstantConversionWithString( source.getForInstantConversionWithString().orElseThrow().toString() ); } if ( source.getForPeriodConversionWithString().isPresent() ) { - target.setForPeriodConversionWithString( source.getForPeriodConversionWithString().get().toString() ); + target.setForPeriodConversionWithString( source.getForPeriodConversionWithString().orElseThrow().toString() ); } if ( source.getForDurationConversionWithString().isPresent() ) { - target.setForDurationConversionWithString( source.getForDurationConversionWithString().get().toString() ); + target.setForDurationConversionWithString( source.getForDurationConversionWithString().orElseThrow().toString() ); } return target; @@ -204,46 +204,46 @@ public Target sourceToTargetLocalDateTimeMapped(OptionalSource source) { Target target = new Target(); if ( source.getLocalDateTime().isPresent() ) { - target.setLocalDateTime( dateTimeFormatter_dd_MM_yyyy_HH_mm_12071769242.format( source.getLocalDateTime().get() ) ); + target.setLocalDateTime( dateTimeFormatter_dd_MM_yyyy_HH_mm_12071769242.format( source.getLocalDateTime().orElseThrow() ) ); } if ( source.getZonedDateTime().isPresent() ) { - target.setZonedDateTime( DateTimeFormatter.ISO_DATE_TIME.format( source.getZonedDateTime().get() ) ); + target.setZonedDateTime( DateTimeFormatter.ISO_DATE_TIME.format( source.getZonedDateTime().orElseThrow() ) ); } if ( source.getLocalDate().isPresent() ) { - target.setLocalDate( DateTimeFormatter.ISO_LOCAL_DATE.format( source.getLocalDate().get() ) ); + target.setLocalDate( DateTimeFormatter.ISO_LOCAL_DATE.format( source.getLocalDate().orElseThrow() ) ); } if ( source.getLocalTime().isPresent() ) { - target.setLocalTime( DateTimeFormatter.ISO_LOCAL_TIME.format( source.getLocalTime().get() ) ); + target.setLocalTime( DateTimeFormatter.ISO_LOCAL_TIME.format( source.getLocalTime().orElseThrow() ) ); } if ( source.getForCalendarConversion().isPresent() ) { - target.setForCalendarConversion( zonedDateTimeToCalendar( source.getForCalendarConversion().get() ) ); + target.setForCalendarConversion( zonedDateTimeToCalendar( source.getForCalendarConversion().orElseThrow() ) ); } if ( source.getForDateConversionWithZonedDateTime().isPresent() ) { - target.setForDateConversionWithZonedDateTime( Date.from( source.getForDateConversionWithZonedDateTime().get().toInstant() ) ); + target.setForDateConversionWithZonedDateTime( Date.from( source.getForDateConversionWithZonedDateTime().orElseThrow().toInstant() ) ); } if ( source.getForDateConversionWithLocalDateTime().isPresent() ) { - target.setForDateConversionWithLocalDateTime( Date.from( source.getForDateConversionWithLocalDateTime().get().toInstant( ZoneOffset.UTC ) ) ); + target.setForDateConversionWithLocalDateTime( Date.from( source.getForDateConversionWithLocalDateTime().orElseThrow().toInstant( ZoneOffset.UTC ) ) ); } if ( source.getForDateConversionWithLocalDate().isPresent() ) { - target.setForDateConversionWithLocalDate( Date.from( source.getForDateConversionWithLocalDate().get().atStartOfDay( ZoneOffset.UTC ).toInstant() ) ); + target.setForDateConversionWithLocalDate( Date.from( source.getForDateConversionWithLocalDate().orElseThrow().atStartOfDay( ZoneOffset.UTC ).toInstant() ) ); } if ( source.getForSqlDateConversionWithLocalDate().isPresent() ) { - target.setForSqlDateConversionWithLocalDate( new java.sql.Date( source.getForSqlDateConversionWithLocalDate().get().atStartOfDay( ZoneOffset.UTC ).toInstant().toEpochMilli() ) ); + target.setForSqlDateConversionWithLocalDate( new java.sql.Date( source.getForSqlDateConversionWithLocalDate().orElseThrow().atStartOfDay( ZoneOffset.UTC ).toInstant().toEpochMilli() ) ); } if ( source.getForDateConversionWithInstant().isPresent() ) { - target.setForDateConversionWithInstant( Date.from( source.getForDateConversionWithInstant().get() ) ); + target.setForDateConversionWithInstant( Date.from( source.getForDateConversionWithInstant().orElseThrow() ) ); } if ( source.getForLocalDateTimeConversionWithLocalDate().isPresent() ) { - target.setForLocalDateTimeConversionWithLocalDate( source.getForLocalDateTimeConversionWithLocalDate().get().atStartOfDay() ); + target.setForLocalDateTimeConversionWithLocalDate( source.getForLocalDateTimeConversionWithLocalDate().orElseThrow().atStartOfDay() ); } if ( source.getForInstantConversionWithString().isPresent() ) { - target.setForInstantConversionWithString( source.getForInstantConversionWithString().get().toString() ); + target.setForInstantConversionWithString( source.getForInstantConversionWithString().orElseThrow().toString() ); } if ( source.getForPeriodConversionWithString().isPresent() ) { - target.setForPeriodConversionWithString( source.getForPeriodConversionWithString().get().toString() ); + target.setForPeriodConversionWithString( source.getForPeriodConversionWithString().orElseThrow().toString() ); } if ( source.getForDurationConversionWithString().isPresent() ) { - target.setForDurationConversionWithString( source.getForDurationConversionWithString().get().toString() ); + target.setForDurationConversionWithString( source.getForDurationConversionWithString().orElseThrow().toString() ); } return target; @@ -258,46 +258,46 @@ public Target sourceToTargetLocalDateMapped(OptionalSource source) { Target target = new Target(); if ( source.getLocalDate().isPresent() ) { - target.setLocalDate( dateTimeFormatter_dd_MM_yyyy_11900521056.format( source.getLocalDate().get() ) ); + target.setLocalDate( dateTimeFormatter_dd_MM_yyyy_11900521056.format( source.getLocalDate().orElseThrow() ) ); } if ( source.getZonedDateTime().isPresent() ) { - target.setZonedDateTime( DateTimeFormatter.ISO_DATE_TIME.format( source.getZonedDateTime().get() ) ); + target.setZonedDateTime( DateTimeFormatter.ISO_DATE_TIME.format( source.getZonedDateTime().orElseThrow() ) ); } if ( source.getLocalDateTime().isPresent() ) { - target.setLocalDateTime( DateTimeFormatter.ISO_LOCAL_DATE_TIME.format( source.getLocalDateTime().get() ) ); + target.setLocalDateTime( DateTimeFormatter.ISO_LOCAL_DATE_TIME.format( source.getLocalDateTime().orElseThrow() ) ); } if ( source.getLocalTime().isPresent() ) { - target.setLocalTime( DateTimeFormatter.ISO_LOCAL_TIME.format( source.getLocalTime().get() ) ); + target.setLocalTime( DateTimeFormatter.ISO_LOCAL_TIME.format( source.getLocalTime().orElseThrow() ) ); } if ( source.getForCalendarConversion().isPresent() ) { - target.setForCalendarConversion( zonedDateTimeToCalendar( source.getForCalendarConversion().get() ) ); + target.setForCalendarConversion( zonedDateTimeToCalendar( source.getForCalendarConversion().orElseThrow() ) ); } if ( source.getForDateConversionWithZonedDateTime().isPresent() ) { - target.setForDateConversionWithZonedDateTime( Date.from( source.getForDateConversionWithZonedDateTime().get().toInstant() ) ); + target.setForDateConversionWithZonedDateTime( Date.from( source.getForDateConversionWithZonedDateTime().orElseThrow().toInstant() ) ); } if ( source.getForDateConversionWithLocalDateTime().isPresent() ) { - target.setForDateConversionWithLocalDateTime( Date.from( source.getForDateConversionWithLocalDateTime().get().toInstant( ZoneOffset.UTC ) ) ); + target.setForDateConversionWithLocalDateTime( Date.from( source.getForDateConversionWithLocalDateTime().orElseThrow().toInstant( ZoneOffset.UTC ) ) ); } if ( source.getForDateConversionWithLocalDate().isPresent() ) { - target.setForDateConversionWithLocalDate( Date.from( source.getForDateConversionWithLocalDate().get().atStartOfDay( ZoneOffset.UTC ).toInstant() ) ); + target.setForDateConversionWithLocalDate( Date.from( source.getForDateConversionWithLocalDate().orElseThrow().atStartOfDay( ZoneOffset.UTC ).toInstant() ) ); } if ( source.getForSqlDateConversionWithLocalDate().isPresent() ) { - target.setForSqlDateConversionWithLocalDate( new java.sql.Date( source.getForSqlDateConversionWithLocalDate().get().atStartOfDay( ZoneOffset.UTC ).toInstant().toEpochMilli() ) ); + target.setForSqlDateConversionWithLocalDate( new java.sql.Date( source.getForSqlDateConversionWithLocalDate().orElseThrow().atStartOfDay( ZoneOffset.UTC ).toInstant().toEpochMilli() ) ); } if ( source.getForDateConversionWithInstant().isPresent() ) { - target.setForDateConversionWithInstant( Date.from( source.getForDateConversionWithInstant().get() ) ); + target.setForDateConversionWithInstant( Date.from( source.getForDateConversionWithInstant().orElseThrow() ) ); } if ( source.getForLocalDateTimeConversionWithLocalDate().isPresent() ) { - target.setForLocalDateTimeConversionWithLocalDate( source.getForLocalDateTimeConversionWithLocalDate().get().atStartOfDay() ); + target.setForLocalDateTimeConversionWithLocalDate( source.getForLocalDateTimeConversionWithLocalDate().orElseThrow().atStartOfDay() ); } if ( source.getForInstantConversionWithString().isPresent() ) { - target.setForInstantConversionWithString( source.getForInstantConversionWithString().get().toString() ); + target.setForInstantConversionWithString( source.getForInstantConversionWithString().orElseThrow().toString() ); } if ( source.getForPeriodConversionWithString().isPresent() ) { - target.setForPeriodConversionWithString( source.getForPeriodConversionWithString().get().toString() ); + target.setForPeriodConversionWithString( source.getForPeriodConversionWithString().orElseThrow().toString() ); } if ( source.getForDurationConversionWithString().isPresent() ) { - target.setForDurationConversionWithString( source.getForDurationConversionWithString().get().toString() ); + target.setForDurationConversionWithString( source.getForDurationConversionWithString().orElseThrow().toString() ); } return target; @@ -312,46 +312,46 @@ public Target sourceToTargetLocalTimeMapped(OptionalSource source) { Target target = new Target(); if ( source.getLocalTime().isPresent() ) { - target.setLocalTime( dateTimeFormatter_HH_mm_168697690.format( source.getLocalTime().get() ) ); + target.setLocalTime( dateTimeFormatter_HH_mm_168697690.format( source.getLocalTime().orElseThrow() ) ); } if ( source.getZonedDateTime().isPresent() ) { - target.setZonedDateTime( DateTimeFormatter.ISO_DATE_TIME.format( source.getZonedDateTime().get() ) ); + target.setZonedDateTime( DateTimeFormatter.ISO_DATE_TIME.format( source.getZonedDateTime().orElseThrow() ) ); } if ( source.getLocalDateTime().isPresent() ) { - target.setLocalDateTime( DateTimeFormatter.ISO_LOCAL_DATE_TIME.format( source.getLocalDateTime().get() ) ); + target.setLocalDateTime( DateTimeFormatter.ISO_LOCAL_DATE_TIME.format( source.getLocalDateTime().orElseThrow() ) ); } if ( source.getLocalDate().isPresent() ) { - target.setLocalDate( DateTimeFormatter.ISO_LOCAL_DATE.format( source.getLocalDate().get() ) ); + target.setLocalDate( DateTimeFormatter.ISO_LOCAL_DATE.format( source.getLocalDate().orElseThrow() ) ); } if ( source.getForCalendarConversion().isPresent() ) { - target.setForCalendarConversion( zonedDateTimeToCalendar( source.getForCalendarConversion().get() ) ); + target.setForCalendarConversion( zonedDateTimeToCalendar( source.getForCalendarConversion().orElseThrow() ) ); } if ( source.getForDateConversionWithZonedDateTime().isPresent() ) { - target.setForDateConversionWithZonedDateTime( Date.from( source.getForDateConversionWithZonedDateTime().get().toInstant() ) ); + target.setForDateConversionWithZonedDateTime( Date.from( source.getForDateConversionWithZonedDateTime().orElseThrow().toInstant() ) ); } if ( source.getForDateConversionWithLocalDateTime().isPresent() ) { - target.setForDateConversionWithLocalDateTime( Date.from( source.getForDateConversionWithLocalDateTime().get().toInstant( ZoneOffset.UTC ) ) ); + target.setForDateConversionWithLocalDateTime( Date.from( source.getForDateConversionWithLocalDateTime().orElseThrow().toInstant( ZoneOffset.UTC ) ) ); } if ( source.getForDateConversionWithLocalDate().isPresent() ) { - target.setForDateConversionWithLocalDate( Date.from( source.getForDateConversionWithLocalDate().get().atStartOfDay( ZoneOffset.UTC ).toInstant() ) ); + target.setForDateConversionWithLocalDate( Date.from( source.getForDateConversionWithLocalDate().orElseThrow().atStartOfDay( ZoneOffset.UTC ).toInstant() ) ); } if ( source.getForSqlDateConversionWithLocalDate().isPresent() ) { - target.setForSqlDateConversionWithLocalDate( new java.sql.Date( source.getForSqlDateConversionWithLocalDate().get().atStartOfDay( ZoneOffset.UTC ).toInstant().toEpochMilli() ) ); + target.setForSqlDateConversionWithLocalDate( new java.sql.Date( source.getForSqlDateConversionWithLocalDate().orElseThrow().atStartOfDay( ZoneOffset.UTC ).toInstant().toEpochMilli() ) ); } if ( source.getForDateConversionWithInstant().isPresent() ) { - target.setForDateConversionWithInstant( Date.from( source.getForDateConversionWithInstant().get() ) ); + target.setForDateConversionWithInstant( Date.from( source.getForDateConversionWithInstant().orElseThrow() ) ); } if ( source.getForLocalDateTimeConversionWithLocalDate().isPresent() ) { - target.setForLocalDateTimeConversionWithLocalDate( source.getForLocalDateTimeConversionWithLocalDate().get().atStartOfDay() ); + target.setForLocalDateTimeConversionWithLocalDate( source.getForLocalDateTimeConversionWithLocalDate().orElseThrow().atStartOfDay() ); } if ( source.getForInstantConversionWithString().isPresent() ) { - target.setForInstantConversionWithString( source.getForInstantConversionWithString().get().toString() ); + target.setForInstantConversionWithString( source.getForInstantConversionWithString().orElseThrow().toString() ); } if ( source.getForPeriodConversionWithString().isPresent() ) { - target.setForPeriodConversionWithString( source.getForPeriodConversionWithString().get().toString() ); + target.setForPeriodConversionWithString( source.getForPeriodConversionWithString().orElseThrow().toString() ); } if ( source.getForDurationConversionWithString().isPresent() ) { - target.setForDurationConversionWithString( source.getForDurationConversionWithString().get().toString() ); + target.setForDurationConversionWithString( source.getForDurationConversionWithString().orElseThrow().toString() ); } return target; diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/beforeafter/OptionalBeforeAfterMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/beforeafter/OptionalBeforeAfterMapperImpl.java index 8dd1037e08..35c74b3c6b 100644 --- a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/beforeafter/OptionalBeforeAfterMapperImpl.java +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/beforeafter/OptionalBeforeAfterMapperImpl.java @@ -33,7 +33,7 @@ public Target toTarget(Source source) { deepNonOptionalToOptional = subTypeToSubTypeOptional( source.getDeepNonOptionalToOptional() ); shallowOptionalToOptional = source.getShallowOptionalToOptional(); if ( source.getShallowOptionalToNonOptional().isPresent() ) { - shallowOptionalToNonOptional = source.getShallowOptionalToNonOptional().get(); + shallowOptionalToNonOptional = source.getShallowOptionalToNonOptional().orElseThrow(); } if ( source.getShallowNonOptionalToOptional() != null ) { shallowNonOptionalToOptional = Optional.of( source.getShallowNonOptionalToOptional() ); @@ -54,7 +54,7 @@ protected Optional subTypeOptionalToSubTypeOptional(Optional optio String value = null; - Source.SubType optionalValue = optional.get(); + Source.SubType optionalValue = optional.orElseThrow(); value = optionalValue.getValue(); diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/differenttypes/OptionalDifferentTypesMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/differenttypes/OptionalDifferentTypesMapperImpl.java index db36c6e750..856212f1f1 100644 --- a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/differenttypes/OptionalDifferentTypesMapperImpl.java +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/differenttypes/OptionalDifferentTypesMapperImpl.java @@ -48,7 +48,7 @@ protected Optional subTypeOptionalToSubTypeOptional(Optional optio String value = null; - Source.SubType optionalValue = optional.get(); + Source.SubType optionalValue = optional.orElseThrow(); value = optionalValue.getValue(); diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/nested/OptionalNestedMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/nested/OptionalNestedMapperImpl.java index 9b3d7f442e..70f2caf16a 100644 --- a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/nested/OptionalNestedMapperImpl.java +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/nested/OptionalNestedMapperImpl.java @@ -25,7 +25,7 @@ public Target toTarget(Source source) { Optional value = sourceOptionalToNonOptionalValue( source ); if ( value.isPresent() ) { - target.setOptionalToNonOptional( value.get() ); + target.setOptionalToNonOptional( value.orElseThrow() ); } target.setOptionalToOptional( sourceOptionalToOptionalValue( source ) ); target.setNonOptionalToNonOptional( sourceNonOptionalToNonOptionalValue( source ) ); @@ -42,7 +42,7 @@ private Optional sourceOptionalToNonOptionalValue(Source source) { if ( optionalToNonOptional.isEmpty() ) { return Optional.empty(); } - return optionalToNonOptional.get().getValue(); + return optionalToNonOptional.orElseThrow().getValue(); } private Optional sourceOptionalToOptionalValue(Source source) { @@ -50,7 +50,7 @@ private Optional sourceOptionalToOptionalValue(Source source) { if ( optionalToOptional.isEmpty() ) { return Optional.empty(); } - return optionalToOptional.get().getValue(); + return optionalToOptional.orElseThrow().getValue(); } private String sourceNonOptionalToNonOptionalValue(Source source) { @@ -58,7 +58,7 @@ private String sourceNonOptionalToNonOptionalValue(Source source) { if ( nonOptionalToNonOptional.isEmpty() ) { return null; } - return nonOptionalToNonOptional.get().getValue(); + return nonOptionalToNonOptional.orElseThrow().getValue(); } private String sourceNonOptionalToOptionalValue(Source source) { @@ -66,6 +66,6 @@ private String sourceNonOptionalToOptionalValue(Source source) { if ( nonOptionalToOptional.isEmpty() ) { return null; } - return nonOptionalToOptional.get().getValue(); + return nonOptionalToOptional.orElseThrow().getValue(); } } diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/nested/OptionalNestedPresenceCheckFirstMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/nested/OptionalNestedPresenceCheckFirstMapperImpl.java index 30378d3cb2..e387af79af 100644 --- a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/nested/OptionalNestedPresenceCheckFirstMapperImpl.java +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/nested/OptionalNestedPresenceCheckFirstMapperImpl.java @@ -24,19 +24,19 @@ public TargetAggregate map(Source song) { TargetAggregate targetAggregate = new TargetAggregate(); if ( song.hasTitle() ) { - targetAggregate.setSongTitle( song.getTitle().get() ); + targetAggregate.setSongTitle( song.getTitle().orElseThrow() ); } String name = songArtistName( song ); - if ( song.hasArtist() && song.getArtist().isPresent() && song.getArtist().get().hasName() ) { + if ( song.hasArtist() && song.getArtist().isPresent() && song.getArtist().orElseThrow().hasName() ) { targetAggregate.setArtistName( name ); } String name1 = songArtistLabelStudioName( song ); - if ( song.hasArtist() && song.getArtist().isPresent() && song.getArtist().get().hasLabel() && song.getArtist().get().getLabel().isPresent() && song.getArtist().get().getLabel().get().hasStudio() ) { + if ( song.hasArtist() && song.getArtist().isPresent() && song.getArtist().orElseThrow().hasLabel() && song.getArtist().orElseThrow().getLabel().isPresent() && song.getArtist().orElseThrow().getLabel().orElseThrow().hasStudio() ) { targetAggregate.setRecordedAt( name1 ); } Optional city = songArtistLabelStudioCity( song ); - if ( song.hasArtist() && song.getArtist().isPresent() && song.getArtist().get().hasLabel() && song.getArtist().get().getLabel().isPresent() && song.getArtist().get().getLabel().get().hasStudio() && song.getArtist().get().getLabel().get().getStudio().isPresent() && song.getArtist().get().getLabel().get().getStudio().get().hasCity() ) { - targetAggregate.setCity( city.get() ); + if ( song.hasArtist() && song.getArtist().isPresent() && song.getArtist().orElseThrow().hasLabel() && song.getArtist().orElseThrow().getLabel().isPresent() && song.getArtist().orElseThrow().getLabel().orElseThrow().hasStudio() && song.getArtist().orElseThrow().getLabel().orElseThrow().getStudio().isPresent() && song.getArtist().orElseThrow().getLabel().orElseThrow().getStudio().orElseThrow().hasCity() ) { + targetAggregate.setCity( city.orElseThrow() ); } return targetAggregate; @@ -50,7 +50,7 @@ private String songArtistName(Source source) { if ( artist.isEmpty() ) { return null; } - Artist artistValue = artist.get(); + Artist artistValue = artist.orElseThrow(); if ( !artistValue.hasName() ) { return null; } @@ -65,7 +65,7 @@ private String songArtistLabelStudioName(Source source) { if ( artist.isEmpty() ) { return null; } - Artist artistValue = artist.get(); + Artist artistValue = artist.orElseThrow(); if ( !artistValue.hasLabel() ) { return null; } @@ -73,7 +73,7 @@ private String songArtistLabelStudioName(Source source) { if ( label.isEmpty() ) { return null; } - Artist.Label labelValue = label.get(); + Artist.Label labelValue = label.orElseThrow(); if ( !labelValue.hasStudio() ) { return null; } @@ -81,7 +81,7 @@ private String songArtistLabelStudioName(Source source) { if ( studio.isEmpty() ) { return null; } - return studio.get().getName(); + return studio.orElseThrow().getName(); } private Optional songArtistLabelStudioCity(Source source) { @@ -92,7 +92,7 @@ private Optional songArtistLabelStudioCity(Source source) { if ( artist.isEmpty() ) { return Optional.empty(); } - Artist artistValue = artist.get(); + Artist artistValue = artist.orElseThrow(); if ( !artistValue.hasLabel() ) { return Optional.empty(); } @@ -100,7 +100,7 @@ private Optional songArtistLabelStudioCity(Source source) { if ( label.isEmpty() ) { return Optional.empty(); } - Artist.Label labelValue = label.get(); + Artist.Label labelValue = label.orElseThrow(); if ( !labelValue.hasStudio() ) { return Optional.empty(); } @@ -108,7 +108,7 @@ private Optional songArtistLabelStudioCity(Source source) { if ( studio.isEmpty() ) { return Optional.empty(); } - Artist.Studio studioValue = studio.get(); + Artist.Studio studioValue = studio.orElseThrow(); if ( !studioValue.hasCity() ) { return Optional.empty(); } diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/nested/OptionalNestedPresenceCheckMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/nested/OptionalNestedPresenceCheckMapperImpl.java index a827592526..987a68e93c 100644 --- a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/nested/OptionalNestedPresenceCheckMapperImpl.java +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/nested/OptionalNestedPresenceCheckMapperImpl.java @@ -28,7 +28,7 @@ public TargetAggregate map(Source song) { targetAggregate.setRecordedAt( songArtistLabelStudioName( song ) ); Optional city = songArtistLabelStudioCity( song ); if ( city.isPresent() ) { - targetAggregate.setCity( city.get() ); + targetAggregate.setCity( city.orElseThrow() ); } return targetAggregate; @@ -39,7 +39,7 @@ private String songArtistName(Source source) { if ( artist.isEmpty() ) { return null; } - Artist artistValue = artist.get(); + Artist artistValue = artist.orElseThrow(); if ( !artistValue.hasName() ) { return null; } @@ -51,7 +51,7 @@ private String songArtistLabelStudioName(Source source) { if ( artist.isEmpty() ) { return null; } - Artist artistValue = artist.get(); + Artist artistValue = artist.orElseThrow(); if ( !artistValue.hasLabel() ) { return null; } @@ -59,7 +59,7 @@ private String songArtistLabelStudioName(Source source) { if ( label.isEmpty() ) { return null; } - Artist.Label labelValue = label.get(); + Artist.Label labelValue = label.orElseThrow(); if ( !labelValue.hasStudio() ) { return null; } @@ -67,7 +67,7 @@ private String songArtistLabelStudioName(Source source) { if ( studio.isEmpty() ) { return null; } - return studio.get().getName(); + return studio.orElseThrow().getName(); } private Optional songArtistLabelStudioCity(Source source) { @@ -75,7 +75,7 @@ private Optional songArtistLabelStudioCity(Source source) { if ( artist.isEmpty() ) { return Optional.empty(); } - Artist artistValue = artist.get(); + Artist artistValue = artist.orElseThrow(); if ( !artistValue.hasLabel() ) { return Optional.empty(); } @@ -83,7 +83,7 @@ private Optional songArtistLabelStudioCity(Source source) { if ( label.isEmpty() ) { return Optional.empty(); } - Artist.Label labelValue = label.get(); + Artist.Label labelValue = label.orElseThrow(); if ( !labelValue.hasStudio() ) { return Optional.empty(); } @@ -91,7 +91,7 @@ private Optional songArtistLabelStudioCity(Source source) { if ( studio.isEmpty() ) { return Optional.empty(); } - Artist.Studio studioValue = studio.get(); + Artist.Studio studioValue = studio.orElseThrow(); if ( !studioValue.hasCity() ) { return Optional.empty(); } diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/nullcheckalways/OptionalNullCheckAlwaysMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/nullcheckalways/OptionalNullCheckAlwaysMapperImpl.java index b5fbac4c25..5ba5f7931f 100644 --- a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/nullcheckalways/OptionalNullCheckAlwaysMapperImpl.java +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/nullcheckalways/OptionalNullCheckAlwaysMapperImpl.java @@ -27,7 +27,7 @@ public Target toTarget(Source source) { target.setOptionalToOptional( source.getOptionalToOptional() ); } if ( source.getOptionalToNonOptional().isPresent() ) { - target.setOptionalToNonOptional( source.getOptionalToNonOptional().get() ); + target.setOptionalToNonOptional( source.getOptionalToNonOptional().orElseThrow() ); } if ( source.getNonOptionalToOptional() != null ) { target.setNonOptionalToOptional( Optional.of( source.getNonOptionalToOptional() ) ); diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/sametype/OptionalSameTypeMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/sametype/OptionalSameTypeMapperImpl.java index 99c0ad4f23..03c77f15d2 100644 --- a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/sametype/OptionalSameTypeMapperImpl.java +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/optional/sametype/OptionalSameTypeMapperImpl.java @@ -27,7 +27,7 @@ public Target toTarget(Source source) { constructorOptionalToOptional = source.getConstructorOptionalToOptional(); if ( source.getConstructorOptionalToNonOptional().isPresent() ) { - constructorOptionalToNonOptional = source.getConstructorOptionalToNonOptional().get(); + constructorOptionalToNonOptional = source.getConstructorOptionalToNonOptional().orElseThrow(); } if ( source.getConstructorNonOptionalToOptional() != null ) { constructorNonOptionalToOptional = Optional.of( source.getConstructorNonOptionalToOptional() ); @@ -37,14 +37,14 @@ public Target toTarget(Source source) { target.setOptionalToOptional( source.getOptionalToOptional() ); if ( source.getOptionalToNonOptional().isPresent() ) { - target.setOptionalToNonOptional( source.getOptionalToNonOptional().get() ); + target.setOptionalToNonOptional( source.getOptionalToNonOptional().orElseThrow() ); } if ( source.getNonOptionalToOptional() != null ) { target.setNonOptionalToOptional( Optional.of( source.getNonOptionalToOptional() ) ); } target.publicOptionalToOptional = source.publicOptionalToOptional; if ( source.publicOptionalToNonOptional.isPresent() ) { - target.publicOptionalToNonOptional = source.publicOptionalToNonOptional.get(); + target.publicOptionalToNonOptional = source.publicOptionalToNonOptional.orElseThrow(); } if ( source.publicNonOptionalToOptional != null ) { target.publicNonOptionalToOptional = Optional.of( source.publicNonOptionalToOptional );