mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#128 Raising an error in case an enum constant is mapped more than once
This commit is contained in:
parent
f0f3335e28
commit
033def2054
@ -26,6 +26,7 @@ import java.util.LinkedList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.annotation.processing.Messager;
|
import javax.annotation.processing.Messager;
|
||||||
import javax.lang.model.element.Element;
|
import javax.lang.model.element.Element;
|
||||||
import javax.lang.model.element.ExecutableElement;
|
import javax.lang.model.element.ExecutableElement;
|
||||||
@ -770,7 +771,20 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
enumMappings.add( new EnumMapping( enumConstant, mappedConstants.iterator().next().getTargetName() ) );
|
enumMappings.add( new EnumMapping( enumConstant, mappedConstants.iterator().next().getTargetName() ) );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
//TODO Raise error
|
List<String> targetConstants = new ArrayList<String>( mappedConstants.size() );
|
||||||
|
for ( Mapping mapping : mappedConstants ) {
|
||||||
|
targetConstants.add( mapping.getTargetName() );
|
||||||
|
}
|
||||||
|
messager.printMessage(
|
||||||
|
Kind.ERROR,
|
||||||
|
String.format(
|
||||||
|
"One enum constant must not be mapped to more than one target constant, but constant %s is " +
|
||||||
|
"mapped to %s.",
|
||||||
|
enumConstant,
|
||||||
|
Strings.join( targetConstants, ", " )
|
||||||
|
),
|
||||||
|
method.getExecutable()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,9 +18,14 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.test.enums;
|
package org.mapstruct.ap.test.enums;
|
||||||
|
|
||||||
|
import javax.tools.Diagnostic.Kind;
|
||||||
|
|
||||||
import org.mapstruct.ap.testutil.IssueKey;
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
import org.mapstruct.ap.testutil.MapperTestBase;
|
import org.mapstruct.ap.testutil.MapperTestBase;
|
||||||
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.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import static org.fest.assertions.Assertions.assertThat;
|
import static org.fest.assertions.Assertions.assertThat;
|
||||||
@ -64,4 +69,20 @@ public class EnumMappingTest extends MapperTestBase {
|
|||||||
assertThat( orderDto ).isNotNull();
|
assertThat( orderDto ).isNotNull();
|
||||||
assertThat( orderDto.getOrderType() ).isEqualTo( ExternalOrderType.SPECIAL );
|
assertThat( orderDto.getOrderType() ).isEqualTo( ExternalOrderType.SPECIAL );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithClasses(ErroneousOrderMapperMappingSameConstantTwice.class)
|
||||||
|
@ExpectedCompilationOutcome(
|
||||||
|
value = CompilationResult.FAILED,
|
||||||
|
diagnostics = {
|
||||||
|
@Diagnostic(type = ErroneousOrderMapperMappingSameConstantTwice.class,
|
||||||
|
kind = Kind.ERROR,
|
||||||
|
line = 39,
|
||||||
|
messageRegExp = "One enum constant must not be mapped to more than one target constant, but " +
|
||||||
|
"constant EXTRA is mapped to SPECIAL, DEFAULT\\.")
|
||||||
|
}
|
||||||
|
)
|
||||||
|
public void shouldRaiseErrorIfSameSourceEnumConstantIsMappedTwice() {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,41 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2014 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.enums;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.Mappings;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Gunnar Morling
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface ErroneousOrderMapperMappingSameConstantTwice {
|
||||||
|
|
||||||
|
ErroneousOrderMapperMappingSameConstantTwice INSTANCE = Mappers.getMapper(
|
||||||
|
ErroneousOrderMapperMappingSameConstantTwice.class
|
||||||
|
);
|
||||||
|
|
||||||
|
@Mappings({
|
||||||
|
@Mapping(source = "EXTRA", target = "SPECIAL"),
|
||||||
|
@Mapping(source = "EXTRA", target = "DEFAULT"),
|
||||||
|
})
|
||||||
|
ExternalOrderType orderTypeToExternalOrderType(OrderType orderType);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user