mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#304 Raising an error in case a cycle is specified via dependsOn()
This commit is contained in:
parent
6a43ee9391
commit
a332533dda
@ -177,6 +177,18 @@ public class BeanMappingMethod extends MappingMethod {
|
|||||||
|
|
||||||
graphAnalyzer.analyze();
|
graphAnalyzer.analyze();
|
||||||
|
|
||||||
|
if ( !graphAnalyzer.getCycles().isEmpty() ) {
|
||||||
|
Set<String> cycles = new HashSet<String>( graphAnalyzer.getCycles().size() );
|
||||||
|
for ( List<String> cycle : graphAnalyzer.getCycles() ) {
|
||||||
|
cycles.add( Strings.join( cycle, " -> " ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.getMessager().printMessage(
|
||||||
|
method.getExecutable(),
|
||||||
|
Message.BEANMAPPING_CYCLE_BETWEEN_PROPERTIES, Strings.join( cycles, ", " )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else {
|
||||||
Collections.sort(
|
Collections.sort(
|
||||||
propertyMappings, new Comparator<PropertyMapping>() {
|
propertyMappings, new Comparator<PropertyMapping>() {
|
||||||
|
|
||||||
@ -195,6 +207,7 @@ public class BeanMappingMethod extends MappingMethod {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Iterates over all defined mapping methods ({@code @Mapping(s)}), either directly given or inherited from the
|
* Iterates over all defined mapping methods ({@code @Mapping(s)}), either directly given or inherited from the
|
||||||
|
@ -35,6 +35,7 @@ public enum Message {
|
|||||||
BEANMAPPING_SEVERAL_POSSIBLE_TARGET_ACCESSORS( "Found several matching getters for property \"%s\"." ),
|
BEANMAPPING_SEVERAL_POSSIBLE_TARGET_ACCESSORS( "Found several matching getters for property \"%s\"." ),
|
||||||
BEANMAPPING_UNMAPPED_TARGETS_WARNING( "Unmapped target %s.", Diagnostic.Kind.WARNING ),
|
BEANMAPPING_UNMAPPED_TARGETS_WARNING( "Unmapped target %s.", Diagnostic.Kind.WARNING ),
|
||||||
BEANMAPPING_UNMAPPED_TARGETS_ERROR( "Unmapped target %s." ),
|
BEANMAPPING_UNMAPPED_TARGETS_ERROR( "Unmapped target %s." ),
|
||||||
|
BEANMAPPING_CYCLE_BETWEEN_PROPERTIES( "Cycle(s) between properties given via dependsOn(): %s." ),
|
||||||
|
|
||||||
PROPERTYMAPPING_MAPPING_NOT_FOUND( "Can't map %s to \"%s %s\". Consider to declare/implement a mapping method: \"%s map(%s value)\"." ),
|
PROPERTYMAPPING_MAPPING_NOT_FOUND( "Can't map %s to \"%s %s\". Consider to declare/implement a mapping method: \"%s map(%s value)\"." ),
|
||||||
PROPERTYMAPPING_DUPLICATE_TARGETS( "Target property \"%s\" must not be mapped more than once." ),
|
PROPERTYMAPPING_DUPLICATE_TARGETS( "Target property \"%s\" must not be mapped more than once." ),
|
||||||
|
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* 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.dependency;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.Mappings;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface AddressMapperWithCyclicDependency {
|
||||||
|
|
||||||
|
AddressMapperWithCyclicDependency INSTANCE = Mappers.getMapper( AddressMapperWithCyclicDependency.class );
|
||||||
|
|
||||||
|
@Mappings({
|
||||||
|
@Mapping(target = "lastName", dependsOn = "middleName"),
|
||||||
|
@Mapping(target = "middleName", dependsOn = "firstName"),
|
||||||
|
@Mapping(target = "firstName", dependsOn = "lastName")
|
||||||
|
})
|
||||||
|
PersonDto personToDto(Person person);
|
||||||
|
}
|
@ -23,6 +23,9 @@ import org.junit.runner.RunWith;
|
|||||||
import org.mapstruct.Mapping;
|
import org.mapstruct.Mapping;
|
||||||
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 static org.fest.assertions.Assertions.assertThat;
|
import static org.fest.assertions.Assertions.assertThat;
|
||||||
@ -63,4 +66,21 @@ public class OrderingTest {
|
|||||||
assertThat( target ).isNotNull();
|
assertThat( target ).isNotNull();
|
||||||
assertThat( target.getFullName() ).isEqualTo( "Bob J. McRobb" );
|
assertThat( target.getFullName() ).isEqualTo( "Bob J. McRobb" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@IssueKey("304")
|
||||||
|
@WithClasses(AddressMapperWithCyclicDependency.class)
|
||||||
|
@ExpectedCompilationOutcome(
|
||||||
|
value = CompilationResult.FAILED,
|
||||||
|
diagnostics = {
|
||||||
|
@Diagnostic(type = AddressMapperWithCyclicDependency.class,
|
||||||
|
kind = javax.tools.Diagnostic.Kind.ERROR,
|
||||||
|
line = 36,
|
||||||
|
messageRegExp = "Cycle\\(s\\) between properties given via dependsOn\\(\\): firstName -> lastName -> "
|
||||||
|
+ "middleName -> firstName"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
public void shouldReportErrorIfDependenciesContainCycle() {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user