Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ public Optional<ProductDto> map(Optional<Product> product) {
return Optional.empty();
}

Product productValue = product.get();
Product productValue = product.orElseThrow();

ProductDto productDto = new ProductDto();

Expand All @@ -734,7 +734,7 @@ public ProductDto unwrap(Optional<Product> product) {
return null;
}

Product productValue = product.get();
Product productValue = product.orElseThrow();

ProductDto productDto = new ProductDto();

Expand Down Expand Up @@ -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() ) );

Expand Down Expand Up @@ -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() );
}
}
----
Expand Down Expand Up @@ -985,7 +985,7 @@ public Optional<PersonDto> map(Optional<Person> person) {
return Optional.empty();
}

Person personValue = person.get();
Person personValue = person.orElseThrow();

PersonDto.Builder builder = PersonDto.builder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -96,5 +101,10 @@ public String getLocale() {
public TypeFactory getTypeFactory() {
return delegate.getTypeFactory();
}

@Override
public VersionInformation getVersionInformation() {
return delegate.getVersionInformation();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ protected Set<Type> getToConversionImportTypes(ConversionContext conversionConte

@Override
protected String getFromExpression(ConversionContext conversionContext) {
StringBuilder sb = new StringBuilder("<SOURCE>.get");
Type optionalBaseType = conversionContext.getSourceType().getOptionalBaseType();
if ( optionalBaseType.isPrimitive() ) {
sb.append( "As" ).append( Strings.capitalize( optionalBaseType.getName() ) );
return "<SOURCE>.getAs" + Strings.capitalize( optionalBaseType.getName() ) + "()";
}
return sb.append( "()" ).toString();
if ( conversionContext.getVersionInformation() != null &&
conversionContext.getVersionInformation().isSourceVersionAtLeast10() ) {
return "<SOURCE>.orElseThrow()";
}
return "<SOURCE>.get()";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -108,6 +109,7 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
private final List<LifecycleCallbackMethodReference> afterMappingReferencesWithOptionalReturnType;
private final Type subclassExhaustiveException;
private final Map<String, Parameter> sourceParametersReassignments;
private final VersionInformation versionInformation;

private final MappingReferences mappingReferences;

Expand Down Expand Up @@ -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<LifecycleCallbackMethodReference> afterMappingMethods = LifecycleMethodResolver.afterMappingMethods(
Expand Down Expand Up @@ -542,7 +545,8 @@ else if ( !method.isUpdateMethod() ) {
subclasses,
presenceChecksByParameter,
subclassExhaustiveExceptionType,
sourceParametersReassignments
sourceParametersReassignments,
ctx.getVersionInformation()
);
}

Expand Down Expand Up @@ -2131,8 +2135,9 @@ private BeanMappingMethod(Method method,
MappingReferences mappingReferences,
List<SubclassMapping> subclassMappings,
Map<String, PresenceCheck> presenceChecksByParameter,
Type subclassExhaustiveException,
Map<String, Parameter> sourceParametersReassignments
Type subclassExhaustiveException,
Map<String, Parameter> sourceParametersReassignments,
VersionInformation versionInformation
) {
super(
method,
Expand Down Expand Up @@ -2206,6 +2211,7 @@ else if ( sourceParameterNames.contains( mapping.getSourceBeanName() ) ) {
: null;
this.subclassMappings = subclassMappings;
this.sourceParametersReassignments = sourceParametersReassignments;
this.versionInformation = versionInformation;
}

public Type getSubclassExhaustiveException() {
Expand Down Expand Up @@ -2412,4 +2418,8 @@ public boolean equals(Object obj) {

return true;
}

public VersionInformation getVersionInformation() {
return versionInformation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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() ) + "()";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -43,4 +45,7 @@ public interface ConversionContext {

TypeFactory getTypeFactory();

default VersionInformation getVersionInformation() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -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();
}

Expand Down Expand Up @@ -89,4 +98,9 @@ public TypeFactory getTypeFactory() {
protected FormattingMessager getMessager() {
return messager;
}

@Override
public VersionInformation getVersionInformation() {
return versionInformation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -85,6 +87,11 @@ public boolean isSourceVersionAtLeast9() {
return sourceVersionAtLeast9;
}

@Override
public boolean isSourceVersionAtLeast10() {
return sourceVersionAtLeast10;
}

@Override
public boolean isSourceVersionAtLeast11() {
return sourceVersionAtLeast11;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading