mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#698 Fix missing import for enums in case of default values.
This commit is contained in:
parent
dde84306ab
commit
7c2b4c9541
@ -18,6 +18,14 @@
|
||||
*/
|
||||
package org.mapstruct.ap.internal.model;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.lang.model.element.ExecutableElement;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
|
||||
import org.mapstruct.ap.internal.model.assignment.AdderWrapper;
|
||||
import org.mapstruct.ap.internal.model.assignment.ArrayCopyWrapper;
|
||||
import org.mapstruct.ap.internal.model.assignment.Assignment;
|
||||
@ -36,22 +44,15 @@ import org.mapstruct.ap.internal.model.source.SourceMethod;
|
||||
import org.mapstruct.ap.internal.model.source.SourceReference;
|
||||
import org.mapstruct.ap.internal.model.source.SourceReference.PropertyEntry;
|
||||
import org.mapstruct.ap.internal.util.Executables;
|
||||
import org.mapstruct.ap.internal.util.MapperConfiguration;
|
||||
import org.mapstruct.ap.internal.util.Message;
|
||||
import org.mapstruct.ap.internal.util.Strings;
|
||||
|
||||
import javax.lang.model.element.ExecutableElement;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.mapstruct.ap.internal.model.assignment.Assignment.AssignmentType.DIRECT;
|
||||
import static org.mapstruct.ap.internal.model.assignment.Assignment.AssignmentType.MAPPED;
|
||||
import static org.mapstruct.ap.internal.model.assignment.Assignment.AssignmentType.MAPPED_TYPE_CONVERTED;
|
||||
import static org.mapstruct.ap.internal.model.assignment.Assignment.AssignmentType.TYPE_CONVERTED;
|
||||
import static org.mapstruct.ap.internal.model.assignment.Assignment.AssignmentType.TYPE_CONVERTED_MAPPED;
|
||||
import org.mapstruct.ap.internal.util.MapperConfiguration;
|
||||
|
||||
/**
|
||||
* Represents the mapping between a source and target property, e.g. from {@code String Source#foo} to
|
||||
@ -798,8 +799,15 @@ public class PropertyMapping extends ModelElement {
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Set<Type> getImportTypes() {
|
||||
return assignment.getImportTypes();
|
||||
if ( defaultValueAssignment == null ) {
|
||||
return assignment.getImportTypes();
|
||||
}
|
||||
|
||||
return org.mapstruct.ap.internal.util.Collections.asSet(
|
||||
assignment.getImportTypes(),
|
||||
defaultValueAssignment.getImportTypes() );
|
||||
}
|
||||
|
||||
public List<String> getDependsOn() {
|
||||
|
@ -18,11 +18,14 @@
|
||||
*/
|
||||
package org.mapstruct.ap.test.defaultvalue;
|
||||
|
||||
import org.mapstruct.ap.test.defaultvalue.other.Continent;
|
||||
|
||||
public class CountryDts {
|
||||
private String code;
|
||||
private int id;
|
||||
private long zipcode;
|
||||
private String region;
|
||||
private Continent continent;
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
@ -55,4 +58,12 @@ public class CountryDts {
|
||||
public void setRegion(String region) {
|
||||
this.region = region;
|
||||
}
|
||||
|
||||
public Continent getContinent() {
|
||||
return continent;
|
||||
}
|
||||
|
||||
public void setContinent(Continent continent) {
|
||||
this.continent = continent;
|
||||
}
|
||||
}
|
||||
|
@ -18,11 +18,14 @@
|
||||
*/
|
||||
package org.mapstruct.ap.test.defaultvalue;
|
||||
|
||||
import org.mapstruct.ap.test.defaultvalue.other.Continent;
|
||||
|
||||
public class CountryEntity {
|
||||
private String code;
|
||||
private Integer id;
|
||||
private long zipcode;
|
||||
private Region region;
|
||||
private Continent continent;
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
@ -56,4 +59,11 @@ public class CountryEntity {
|
||||
this.region = region;
|
||||
}
|
||||
|
||||
public Continent getContinent() {
|
||||
return continent;
|
||||
}
|
||||
|
||||
public void setContinent(Continent continent) {
|
||||
this.continent = continent;
|
||||
}
|
||||
}
|
||||
|
@ -33,6 +33,7 @@ public abstract class CountryMapper {
|
||||
@Mapping( target = "id", defaultValue = "42" ),
|
||||
@Mapping( target = "zipcode", defaultValue = "1337" ),
|
||||
@Mapping( target = "region", defaultValue = "someRegion" ),
|
||||
@Mapping( target = "continent", defaultValue = "EUROPE" )
|
||||
} )
|
||||
public abstract CountryDts mapToCountryDts(CountryEntity country);
|
||||
|
||||
@ -40,19 +41,19 @@ public abstract class CountryMapper {
|
||||
@Mapping( target = "code", defaultValue = "DE" ),
|
||||
@Mapping( target = "id", defaultValue = "42" ),
|
||||
@Mapping( target = "zipcode", defaultValue = "1337" ),
|
||||
@Mapping( target = "region", ignore = true )
|
||||
|
||||
@Mapping( target = "region", ignore = true ),
|
||||
@Mapping( target = "continent", defaultValue = "EUROPE" )
|
||||
} )
|
||||
public abstract void mapToCountryDts(CountryDts countryDts, @MappingTarget CountryEntity country);
|
||||
public abstract void mapToCountryEntity(CountryDts countryDts, @MappingTarget CountryEntity country);
|
||||
|
||||
@Mappings( {
|
||||
@Mapping( target = "code", defaultValue = "DE" ),
|
||||
@Mapping( target = "id", defaultValue = "42" ),
|
||||
@Mapping( target = "zipcode", defaultValue = "1337" ),
|
||||
@Mapping( target = "region", ignore = true )
|
||||
|
||||
@Mapping( target = "region", ignore = true ),
|
||||
@Mapping( target = "continent", defaultValue = "EUROPE" )
|
||||
} )
|
||||
public abstract void mapToCountryDts(CountryEntity source, @MappingTarget CountryEntity target);
|
||||
public abstract void mapToCountryEntity(CountryEntity source, @MappingTarget CountryEntity target);
|
||||
|
||||
protected String mapToString(Region region) {
|
||||
return ( region != null ) ? region.getCode() : null;
|
||||
|
@ -22,6 +22,7 @@ import java.text.ParseException;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mapstruct.ap.test.defaultvalue.other.Continent;
|
||||
import org.mapstruct.ap.testutil.IssueKey;
|
||||
import org.mapstruct.ap.testutil.WithClasses;
|
||||
import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult;
|
||||
@ -35,7 +36,8 @@ import static org.fest.assertions.Assertions.assertThat;
|
||||
@RunWith( AnnotationProcessorTestRunner.class )
|
||||
@WithClasses( {
|
||||
CountryEntity.class,
|
||||
CountryDts.class
|
||||
CountryDts.class,
|
||||
Continent.class
|
||||
} )
|
||||
public class DefaultValueTest {
|
||||
@Test
|
||||
@ -63,10 +65,9 @@ public class DefaultValueTest {
|
||||
|
||||
// code is null so it should fall back to the default value
|
||||
assertThat( countryDts.getCode() ).isEqualTo( "DE" );
|
||||
|
||||
assertThat( countryDts.getZipcode() ).isEqualTo( 0 );
|
||||
|
||||
assertThat( countryDts.getRegion() ).isEqualTo( "someRegion" );
|
||||
assertThat( countryDts.getContinent() ).isEqualTo( Continent.EUROPE );
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -80,12 +81,14 @@ public class DefaultValueTest {
|
||||
Region region = new Region();
|
||||
region.setCode( "foobar" );
|
||||
countryEntity.setRegion( region );
|
||||
countryEntity.setContinent( Continent.NORTH_AMERICA );
|
||||
|
||||
CountryDts countryDts = CountryMapper.INSTANCE.mapToCountryDts( countryEntity );
|
||||
|
||||
// the source entity had a code set, so the default value shouldn't be used
|
||||
assertThat( countryDts.getCode() ).isEqualTo( "US" );
|
||||
assertThat( countryDts.getRegion() ).isEqualTo( "foobar" );
|
||||
assertThat( countryDts.getContinent() ).isEqualTo( Continent.NORTH_AMERICA );
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -97,12 +100,13 @@ public class DefaultValueTest {
|
||||
CountryEntity countryEntity = new CountryEntity();
|
||||
CountryDts countryDts = new CountryDts();
|
||||
|
||||
CountryMapper.INSTANCE.mapToCountryDts( countryDts, countryEntity );
|
||||
CountryMapper.INSTANCE.mapToCountryEntity( countryDts, countryEntity );
|
||||
|
||||
assertThat( countryEntity.getId() ).isEqualTo( 0 );
|
||||
// no code is set, so fall back to default value
|
||||
assertThat( countryEntity.getCode() ).isEqualTo( "DE" );
|
||||
assertThat( countryEntity.getZipcode() ).isEqualTo( 0 );
|
||||
assertThat( countryEntity.getContinent() ).isEqualTo( Continent.EUROPE );
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -114,13 +118,14 @@ public class DefaultValueTest {
|
||||
CountryEntity source = new CountryEntity();
|
||||
CountryEntity target = new CountryEntity();
|
||||
|
||||
CountryMapper.INSTANCE.mapToCountryDts( source, target );
|
||||
CountryMapper.INSTANCE.mapToCountryEntity( source, target );
|
||||
|
||||
// no id is set, so fall back to default value
|
||||
assertThat( target.getId() ).isEqualTo( 42 );
|
||||
// no code is set, so fall back to default value
|
||||
assertThat( target.getCode() ).isEqualTo( "DE" );
|
||||
assertThat( target.getZipcode() ).isEqualTo( 0 );
|
||||
assertThat( target.getContinent() ).isEqualTo( Continent.EUROPE );
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.defaultvalue.other;
|
||||
|
||||
/**
|
||||
* @author Andreas Gudian
|
||||
*
|
||||
*/
|
||||
public enum Continent {
|
||||
AFRICA,
|
||||
ANTARCTICA,
|
||||
ASIA,
|
||||
EUROPE,
|
||||
NORTH_AMERICA,
|
||||
OCEANIA,
|
||||
SOUTH_AMERICA
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user