mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#512 more descriptive error message for forged collection and map mappings
This commit is contained in:
parent
fbb80bfb25
commit
3be68b233e
@ -118,8 +118,14 @@ public class IterableMappingMethod extends MappingMethod {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if ( assignment == null ) {
|
if ( assignment == null ) {
|
||||||
|
if ( method instanceof ForgedMethod ) {
|
||||||
|
// leave messaging to calling property mapping
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else {
|
||||||
ctx.getMessager().printMessage( method.getExecutable(), Message.ITERABLEMAPPING_MAPPING_NOT_FOUND );
|
ctx.getMessager().printMessage( method.getExecutable(), Message.ITERABLEMAPPING_MAPPING_NOT_FOUND );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
if ( method instanceof ForgedMethod ) {
|
if ( method instanceof ForgedMethod ) {
|
||||||
ForgedMethod forgedMethod = (ForgedMethod) method;
|
ForgedMethod forgedMethod = (ForgedMethod) method;
|
||||||
|
@ -131,7 +131,14 @@ public class MapMappingMethod extends MappingMethod {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if ( keyAssignment == null ) {
|
if ( keyAssignment == null ) {
|
||||||
ctx.getMessager().printMessage( method.getExecutable(), Message.MAPMAPPING_KEY_MAPPING_NOT_FOUND );
|
if ( method instanceof ForgedMethod ) {
|
||||||
|
// leave messaging to calling property mapping
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ctx.getMessager().printMessage( method.getExecutable(),
|
||||||
|
Message.MAPMAPPING_KEY_MAPPING_NOT_FOUND );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// find mapping method or conversion for value
|
// find mapping method or conversion for value
|
||||||
@ -162,7 +169,14 @@ public class MapMappingMethod extends MappingMethod {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ( valueAssignment == null ) {
|
if ( valueAssignment == null ) {
|
||||||
ctx.getMessager().printMessage( method.getExecutable(), Message.MAPMAPPING_VALUE_MAPPING_NOT_FOUND );
|
if ( method instanceof ForgedMethod ) {
|
||||||
|
// leave messaging to calling property mapping
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ctx.getMessager().printMessage( method.getExecutable(),
|
||||||
|
Message.MAPMAPPING_VALUE_MAPPING_NOT_FOUND );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// mapNullToDefault
|
// mapNullToDefault
|
||||||
|
@ -467,6 +467,7 @@ public class PropertyMapping extends ModelElement {
|
|||||||
.method( methodRef )
|
.method( methodRef )
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
if ( iterableMappingMethod != null ) {
|
||||||
if ( !ctx.getMappingsToGenerate().contains( iterableMappingMethod ) ) {
|
if ( !ctx.getMappingsToGenerate().contains( iterableMappingMethod ) ) {
|
||||||
ctx.getMappingsToGenerate().add( iterableMappingMethod );
|
ctx.getMappingsToGenerate().add( iterableMappingMethod );
|
||||||
}
|
}
|
||||||
@ -477,7 +478,7 @@ public class PropertyMapping extends ModelElement {
|
|||||||
|
|
||||||
assignment = AssignmentFactory.createMethodReference( methodRef, null, targetType );
|
assignment = AssignmentFactory.createMethodReference( methodRef, null, targetType );
|
||||||
assignment.setAssignment( AssignmentFactory.createDirect( sourceReference ) );
|
assignment.setAssignment( AssignmentFactory.createDirect( sourceReference ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if ( sourceType.isMapType() && targetType.isMapType() ) {
|
else if ( sourceType.isMapType() && targetType.isMapType() ) {
|
||||||
|
|
||||||
@ -489,6 +490,7 @@ public class PropertyMapping extends ModelElement {
|
|||||||
.method( methodRef )
|
.method( methodRef )
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
if ( mapMappingMethod != null ) {
|
||||||
if ( !ctx.getMappingsToGenerate().contains( mapMappingMethod ) ) {
|
if ( !ctx.getMappingsToGenerate().contains( mapMappingMethod ) ) {
|
||||||
ctx.getMappingsToGenerate().add( mapMappingMethod );
|
ctx.getMappingsToGenerate().add( mapMappingMethod );
|
||||||
}
|
}
|
||||||
@ -499,6 +501,7 @@ public class PropertyMapping extends ModelElement {
|
|||||||
assignment = AssignmentFactory.createMethodReference( methodRef, null, targetType );
|
assignment = AssignmentFactory.createMethodReference( methodRef, null, targetType );
|
||||||
assignment.setAssignment( AssignmentFactory.createDirect( sourceReference ) );
|
assignment.setAssignment( AssignmentFactory.createDirect( sourceReference ) );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return assignment;
|
return assignment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
/**
|
||||||
|
* 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.collection.forged;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class Bar {
|
||||||
|
|
||||||
|
}
|
@ -21,11 +21,15 @@ package org.mapstruct.ap.test.collection.forged;
|
|||||||
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import javax.tools.Diagnostic.Kind;
|
||||||
import static org.fest.assertions.Assertions.assertThat;
|
import static org.fest.assertions.Assertions.assertThat;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mapstruct.ap.testutil.IssueKey;
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
import org.mapstruct.ap.testutil.WithClasses;
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
|
import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult;
|
||||||
|
import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic;
|
||||||
|
import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome;
|
||||||
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||||
import org.mapstruct.ap.util.Collections;
|
import org.mapstruct.ap.util.Collections;
|
||||||
|
|
||||||
@ -72,4 +76,46 @@ public class CollectionMappingTest {
|
|||||||
assertThat( source2.getBarMap() ).isEqualTo( sourceMap );
|
assertThat( source2.getBarMap() ).isEqualTo( sourceMap );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithClasses({ ErroneousCollectionNonMappableSetMapper.class,
|
||||||
|
ErroneousNonMappableSetSource.class,
|
||||||
|
ErroneousNonMappableSetTarget.class,
|
||||||
|
Foo.class,
|
||||||
|
Bar.class
|
||||||
|
})
|
||||||
|
@ExpectedCompilationOutcome(
|
||||||
|
value = CompilationResult.FAILED,
|
||||||
|
diagnostics = {
|
||||||
|
@Diagnostic(type = ErroneousCollectionNonMappableSetMapper.class,
|
||||||
|
kind = Kind.ERROR,
|
||||||
|
line = 30,
|
||||||
|
messageRegExp = "Can't map property \".* nonMappableSet\" to \".* nonMappableSet\". "
|
||||||
|
+ "Consider to declare/implement a mapping method: .*."),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
public void shouldGenerateNonMappleMethodForSetMapping() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithClasses({ ErroneousCollectionNonMappableMapMapper.class,
|
||||||
|
ErroneousNonMappableMapSource.class,
|
||||||
|
ErroneousNonMappableMapTarget.class,
|
||||||
|
Foo.class,
|
||||||
|
Bar.class
|
||||||
|
})
|
||||||
|
@ExpectedCompilationOutcome(
|
||||||
|
value = CompilationResult.FAILED,
|
||||||
|
diagnostics = {
|
||||||
|
@Diagnostic(type = ErroneousCollectionNonMappableMapMapper.class,
|
||||||
|
kind = Kind.ERROR,
|
||||||
|
line = 30,
|
||||||
|
messageRegExp = "Can't map property \".* nonMappableMap\" to \".* nonMappableMap\". "
|
||||||
|
+ "Consider to declare/implement a mapping method: .*."),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
public void shouldGenerateNonMappleMethodForMapMapping() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,31 @@
|
|||||||
|
/**
|
||||||
|
* 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.collection.forged;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface ErroneousCollectionNonMappableMapMapper {
|
||||||
|
|
||||||
|
ErroneousCollectionNonMappableMapMapper INSTANCE =
|
||||||
|
Mappers.getMapper( ErroneousCollectionNonMappableMapMapper.class );
|
||||||
|
|
||||||
|
ErroneousNonMappableMapTarget sourceToTarget(ErroneousNonMappableMapSource source);
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
/**
|
||||||
|
* 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.collection.forged;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface ErroneousCollectionNonMappableSetMapper {
|
||||||
|
|
||||||
|
ErroneousCollectionNonMappableSetMapper INSTANCE =
|
||||||
|
Mappers.getMapper( ErroneousCollectionNonMappableSetMapper.class );
|
||||||
|
|
||||||
|
ErroneousNonMappableSetTarget sourceToTarget(ErroneousNonMappableSetSource source);
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
/**
|
||||||
|
* 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.collection.forged;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class ErroneousNonMappableMapSource {
|
||||||
|
|
||||||
|
private Map<Foo, Foo> nonMappableMap;
|
||||||
|
|
||||||
|
public Map<Foo, Foo> getNonMappableMap() {
|
||||||
|
return nonMappableMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNonMappableMap(Map<Foo, Foo> nonMappableMap) {
|
||||||
|
this.nonMappableMap = nonMappableMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
/**
|
||||||
|
* 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.collection.forged;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class ErroneousNonMappableMapTarget {
|
||||||
|
|
||||||
|
private Map<Bar, Bar> nonMappableMap;
|
||||||
|
|
||||||
|
public Map<Bar, Bar> getNonMappableMap() {
|
||||||
|
return nonMappableMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNonMappableMap(Map<Bar, Bar> nonMappableMap) {
|
||||||
|
this.nonMappableMap = nonMappableMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
/**
|
||||||
|
* 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.collection.forged;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class ErroneousNonMappableSetSource {
|
||||||
|
|
||||||
|
private Set<Foo> nonMappableSet;
|
||||||
|
|
||||||
|
public Set<Foo> getNonMappableSet() {
|
||||||
|
return nonMappableSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNonMappableSet(Set<Foo> nonMappableSet) {
|
||||||
|
this.nonMappableSet = nonMappableSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
/**
|
||||||
|
* 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.collection.forged;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class ErroneousNonMappableSetTarget {
|
||||||
|
|
||||||
|
private Set<Bar> nonMappableSet;
|
||||||
|
|
||||||
|
public Set<Bar> getNonMappableSet() {
|
||||||
|
return nonMappableSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNonMappableSet(Set<Bar> nonMappableSet) {
|
||||||
|
this.nonMappableSet = nonMappableSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
/**
|
||||||
|
* 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.collection.forged;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class Foo {
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user