Skip to content
Merged
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 @@ -42,7 +42,6 @@
import org.mapstruct.ap.internal.model.source.Method;
import org.mapstruct.ap.internal.model.source.SelectionParameters;
import org.mapstruct.ap.internal.model.source.SourceMethod;
import org.mapstruct.ap.internal.prism.BeanMappingPrism;
import org.mapstruct.ap.internal.prism.CollectionMappingStrategyPrism;
import org.mapstruct.ap.internal.prism.NullValueMappingStrategyPrism;
import org.mapstruct.ap.internal.prism.ReportingPolicyPrism;
Expand Down Expand Up @@ -393,7 +392,7 @@ private boolean canResultTypeFromBeanMappingBeConstructed(Type resultType) {
if ( resultType.isAbstract() ) {
ctx.getMessager().printMessage(
method.getExecutable(),
BeanMappingPrism.getInstanceOn( method.getExecutable() ).mirror,
method.getMappingOptions().getBeanMapping().getMirror(),
BEANMAPPING_ABSTRACT,
resultType,
method.getResultType()
Expand All @@ -403,7 +402,7 @@ private boolean canResultTypeFromBeanMappingBeConstructed(Type resultType) {
else if ( !resultType.isAssignableTo( method.getResultType() ) ) {
ctx.getMessager().printMessage(
method.getExecutable(),
BeanMappingPrism.getInstanceOn( method.getExecutable() ).mirror,
method.getMappingOptions().getBeanMapping().getMirror(),
BEANMAPPING_NOT_ASSIGNABLE,
resultType,
method.getResultType()
Expand All @@ -413,7 +412,7 @@ else if ( !resultType.isAssignableTo( method.getResultType() ) ) {
else if ( !resultType.hasEmptyAccessibleConstructor() ) {
ctx.getMessager().printMessage(
method.getExecutable(),
BeanMappingPrism.getInstanceOn( method.getExecutable() ).mirror,
method.getMappingOptions().getBeanMapping().getMirror(),
Message.GENERAL_NO_SUITABLE_CONSTRUCTOR,
resultType
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
package org.mapstruct.ap.internal.model.source;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.util.Types;

import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.prism.BeanMappingPrism;
import org.mapstruct.ap.internal.prism.BuilderPrism;
import org.mapstruct.ap.internal.prism.NullValueCheckStrategyPrism;
Expand All @@ -36,87 +39,134 @@ public class BeanMapping {
private final BuilderPrism builder;
private final NullValuePropertyMappingStrategyPrism nullValuePropertyMappingStrategy;

private final AnnotationMirror mirror;

/**
* creates a mapping for inheritance. Will set ignoreByDefault to false.
* creates a mapping for inheritance. Will set
* - ignoreByDefault to false.
* - resultType to null
*
* @param map
* @return
* @return new mapping
*/
public static BeanMapping forInheritance( BeanMapping map ) {
public static BeanMapping forInheritance(BeanMapping beanMapping) {
return new BeanMapping(
map.selectionParameters,
map.nullValueMappingStrategy,
map.nullValuePropertyMappingStrategy,
map.nullValueCheckStrategy,
map.reportingPolicy,
SelectionParameters.forInheritance( beanMapping.selectionParameters ),
beanMapping.nullValueMappingStrategy,
beanMapping.nullValuePropertyMappingStrategy,
beanMapping.nullValueCheckStrategy,
beanMapping.reportingPolicy,
false,
map.ignoreUnmappedSourceProperties,
map.builder
beanMapping.ignoreUnmappedSourceProperties,
beanMapping.builder,
beanMapping.mirror
);
}

public static BeanMapping fromPrism(BeanMappingPrism beanMapping, ExecutableElement method,
FormattingMessager messager, Types typeUtils) {

if ( beanMapping == null ) {
return null;
}

boolean resultTypeIsDefined = !TypeKind.VOID.equals( beanMapping.resultType().getKind() );
public static class Builder {

NullValueMappingStrategyPrism nullValueMappingStrategy =
null == beanMapping.values.nullValueMappingStrategy()
? null
: NullValueMappingStrategyPrism.valueOf( beanMapping.nullValueMappingStrategy() );
private BeanMappingPrism prism;
private ExecutableElement method;
private FormattingMessager messager;
private Types typeUtils;
private TypeFactory typeFactory;

NullValuePropertyMappingStrategyPrism nullValuePropertyMappingStrategy =
null == beanMapping.values.nullValuePropertyMappingStrategy()
? null
: NullValuePropertyMappingStrategyPrism.valueOf( beanMapping.nullValuePropertyMappingStrategy() );

NullValueCheckStrategyPrism nullValueCheckStrategy =
null == beanMapping.values.nullValueCheckStrategy()
? null
: NullValueCheckStrategyPrism.valueOf( beanMapping.nullValueCheckStrategy() );
public Builder beanMappingPrism(BeanMappingPrism beanMappingPrism) {
this.prism = beanMappingPrism;
return this;
}

boolean ignoreByDefault = beanMapping.ignoreByDefault();
BuilderPrism builderMapping = null;
if ( beanMapping.values.builder() != null ) {
builderMapping = beanMapping.builder();
public Builder method(ExecutableElement method) {
this.method = method;
return this;
}

if ( !resultTypeIsDefined && beanMapping.qualifiedBy().isEmpty() && beanMapping.qualifiedByName().isEmpty()
&& beanMapping.ignoreUnmappedSourceProperties().isEmpty()
&& ( nullValueMappingStrategy == null ) && ( nullValuePropertyMappingStrategy == null )
&& ( nullValueCheckStrategy == null ) && !ignoreByDefault && builderMapping == null ) {
public Builder messager(FormattingMessager messager) {
this.messager = messager;
return this;
}

messager.printMessage( method, Message.BEANMAPPING_NO_ELEMENTS );
public Builder typeUtils(Types typeUtils) {
this.typeUtils = typeUtils;
return this;
}

SelectionParameters cmp = new SelectionParameters(
beanMapping.qualifiedBy(),
beanMapping.qualifiedByName(),
resultTypeIsDefined ? beanMapping.resultType() : null,
typeUtils
);
public Builder typeFactory(TypeFactory typeFactory) {
this.typeFactory = typeFactory;
return this;
}

//TODO Do we want to add the reporting policy to the BeanMapping as well? To give more granular support?
return new BeanMapping(
cmp,
nullValueMappingStrategy,
nullValuePropertyMappingStrategy,
nullValueCheckStrategy,
null,
ignoreByDefault,
beanMapping.ignoreUnmappedSourceProperties(),
builderMapping
);
public BeanMapping build() {

if ( prism == null ) {
return null;
}

Objects.requireNonNull( method );
Comment thread
filiphr marked this conversation as resolved.
Objects.requireNonNull( messager );
Objects.requireNonNull( method );
Objects.requireNonNull( typeUtils );
Objects.requireNonNull( typeFactory );

boolean resultTypeIsDefined = !TypeKind.VOID.equals( prism.resultType().getKind() );

NullValueMappingStrategyPrism nullValueMappingStrategy =
null == prism.values.nullValueMappingStrategy()
? null
: NullValueMappingStrategyPrism.valueOf( prism.nullValueMappingStrategy() );

NullValuePropertyMappingStrategyPrism nullValuePropertyMappingStrategy =
null == prism.values.nullValuePropertyMappingStrategy()
? null
:
NullValuePropertyMappingStrategyPrism.valueOf( prism.nullValuePropertyMappingStrategy() );

NullValueCheckStrategyPrism nullValueCheckStrategy =
null == prism.values.nullValueCheckStrategy()
? null
: NullValueCheckStrategyPrism.valueOf( prism.nullValueCheckStrategy() );

boolean ignoreByDefault = prism.ignoreByDefault();
BuilderPrism builderMapping = null;
if ( prism.values.builder() != null ) {
builderMapping = prism.builder();
}

if ( !resultTypeIsDefined && prism.qualifiedBy().isEmpty() &&
prism.qualifiedByName().isEmpty()
&& prism.ignoreUnmappedSourceProperties().isEmpty()
&& ( nullValueMappingStrategy == null ) && ( nullValuePropertyMappingStrategy == null )
&& ( nullValueCheckStrategy == null ) && !ignoreByDefault && builderMapping == null ) {

messager.printMessage( method, Message.BEANMAPPING_NO_ELEMENTS );
}

SelectionParameters cmp = new SelectionParameters(
prism.qualifiedBy(),
prism.qualifiedByName(),
resultTypeIsDefined ? prism.resultType() : null,
typeUtils
);

//TODO Do we want to add the reporting policy to the BeanMapping as well? To give more granular support?
return new BeanMapping(
cmp,
nullValueMappingStrategy,
nullValuePropertyMappingStrategy,
nullValueCheckStrategy,
null,
ignoreByDefault,
prism.ignoreUnmappedSourceProperties(),
builderMapping,
prism.mirror
);
}
}

private BeanMapping(SelectionParameters selectionParameters, NullValueMappingStrategyPrism nvms,
NullValuePropertyMappingStrategyPrism nvpms, NullValueCheckStrategyPrism nvcs,
ReportingPolicyPrism reportingPolicy, boolean ignoreByDefault,
List<String> ignoreUnmappedSourceProperties, BuilderPrism builder) {
List<String> ignoreUnmappedSourceProperties, BuilderPrism builder,
AnnotationMirror mirror) {
this.selectionParameters = selectionParameters;
this.nullValueMappingStrategy = nvms;
this.nullValuePropertyMappingStrategy = nvpms;
Expand All @@ -125,6 +175,7 @@ private BeanMapping(SelectionParameters selectionParameters, NullValueMappingStr
this.ignoreByDefault = ignoreByDefault;
this.ignoreUnmappedSourceProperties = ignoreUnmappedSourceProperties;
this.builder = builder;
this.mirror = mirror;
}

public SelectionParameters getSelectionParameters() {
Expand Down Expand Up @@ -155,6 +206,10 @@ public List<String> getIgnoreUnmappedSourceProperties() {
return ignoreUnmappedSourceProperties;
}

public AnnotationMirror getMirror() {
return mirror;
}

/**
* derives the builder prism given the options and configuration
* @param method containing mandatory configuration and the mapping options (optionally containing a beanmapping)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ public class SelectionParameters {
private final Types typeUtils;
private final SourceRHS sourceRHS;

/**
* Returns new selection parameters
*
* ResultType is not inherited.
*
* @param selectionParameters
* @return
*/
public static SelectionParameters forInheritance(SelectionParameters selectionParameters) {
return new SelectionParameters(
selectionParameters.qualifiers,
selectionParameters.qualifyingNames,
null,
selectionParameters.typeUtils
);
}

public SelectionParameters(List<TypeMirror> qualifiers, List<String> qualifyingNames, TypeMirror resultType,
Types typeUtils) {
this( qualifiers, qualifyingNames, resultType, typeUtils, null );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,14 @@ private SourceMethod getMethodRequiringImplementation(ExecutableType methodType,
.setMapMapping(
MapMapping.fromPrism( MapMappingPrism.getInstanceOn( method ), method, messager, typeUtils ) )
.setBeanMapping(
BeanMapping.fromPrism( BeanMappingPrism.getInstanceOn( method ), method, messager, typeUtils ) )
new BeanMapping.Builder()
.beanMappingPrism( BeanMappingPrism.getInstanceOn( method ) )
.messager( messager )
.method( method )
.typeUtils( typeUtils )
.typeFactory( typeFactory )
.build()
)
.setValueMappings( getValueMappings( method ) )
.setTypeUtils( typeUtils )
.setTypeFactory( typeFactory )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.test.bugs._1821;

import org.mapstruct.BeanMapping;
import org.mapstruct.InheritConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

@Mapper
public interface Issue1821Mapper {

Issue1821Mapper INSTANCE = Mappers.getMapper( Issue1821Mapper.class );

@BeanMapping( resultType = Target.class )
Target map(Source source);

@InheritConfiguration( name = "map" )
ExtendedTarget mapExtended(Source source);

class Target {
}

class ExtendedTarget extends Target {
}

class Source {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.test.bugs._1821;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;

@IssueKey("1797")
@RunWith( AnnotationProcessorTestRunner.class)
@WithClasses( Issue1821Mapper.class )
public class Issue1821Test {

@Test
public void shouldNotGiveNullPtr() {
Issue1821Mapper.INSTANCE.map( new Issue1821Mapper.Source() );
}

}