This commit is contained in:
Gunnar Morling 2014-10-14 22:03:07 +02:00
parent 32eeceb0cf
commit 4d408d5560
10 changed files with 243 additions and 240 deletions

View File

@ -25,7 +25,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.TreeSet; import java.util.TreeSet;
import javax.annotation.processing.Messager; import javax.annotation.processing.Messager;
import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement; import javax.lang.model.element.TypeElement;
@ -406,12 +405,12 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
} }
} }
private SourceMethod getReverseMappingMethod(List<SourceMethod> rawMethods, SourceMethod method) { private SourceMethod getReverseMappingMethod(List<SourceMethod> rawMethods, SourceMethod method) {
SourceMethod result = null; SourceMethod result = null;
InheritInverseConfigurationPrism reversePrism = InheritInverseConfigurationPrism.getInstanceOn(
method.getExecutable()
);
InheritInverseConfigurationPrism reversePrism = InheritInverseConfigurationPrism.getInstanceOn( method.getExecutable() );
if ( reversePrism != null ) { if ( reversePrism != null ) {
// method is configured as being reverse method, collect candidates // method is configured as being reverse method, collect candidates
@ -458,25 +457,32 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
} }
if ( result != null ) { if ( result != null ) {
reportErrorIfForwardMethodHasInheritInverseConfigurationAnnotation( result, method, reversePrism ); reportErrorIfInverseMethodHasInheritAnnotation( result, method, reversePrism );
} }
} }
return result; return result;
} }
private void reportErrorIfForwardMethodHasInheritInverseConfigurationAnnotation( SourceMethod candidate, private void reportErrorIfInverseMethodHasInheritAnnotation(SourceMethod candidate,
SourceMethod method, InheritInverseConfigurationPrism reversePrism ) { SourceMethod method,
InheritInverseConfigurationPrism reversePrism) {
InheritInverseConfigurationPrism candidatePrism = InheritInverseConfigurationPrism.getInstanceOn(
candidate.getExecutable()
);
InheritInverseConfigurationPrism candidatePrism = InheritInverseConfigurationPrism.getInstanceOn( candidate.getExecutable() );
if ( candidatePrism != null ) { if ( candidatePrism != null ) {
messager.printMessage( Diagnostic.Kind.ERROR, messager.printMessage(
String.format( "Resolved inverse mapping method %s() should not carry the " Diagnostic.Kind.ERROR,
String.format(
"Resolved inverse mapping method %s() should not carry the "
+ "@InheritInverseConfiguration annotation itself.", + "@InheritInverseConfiguration annotation itself.",
candidate.getName() candidate.getName()
), ),
method.getExecutable(), method.getExecutable(),
reversePrism.mirror ); reversePrism.mirror
);
} }
} }
@ -490,42 +496,54 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
String name = reversePrism.name(); String name = reversePrism.name();
if ( name.isEmpty() ) { if ( name.isEmpty() ) {
messager.printMessage( Diagnostic.Kind.ERROR, messager.printMessage(
String.format( "Several matching inverse methods exist: %s(). Specify a name explicitly.", Diagnostic.Kind.ERROR,
String.format(
"Several matching inverse methods exist: %s(). Specify a name explicitly.",
Strings.join( candidateNames, "(), " ) Strings.join( candidateNames, "(), " )
), ),
method.getExecutable(), method.getExecutable(),
reversePrism.mirror ); reversePrism.mirror
);
} }
else { else {
messager.printMessage( Diagnostic.Kind.ERROR, messager.printMessage(
String.format( "None of the candidates %s() matches given name: \"%s\".", Diagnostic.Kind.ERROR,
String.format(
"None of the candidates %s() matches given name: \"%s\".",
Strings.join( candidateNames, "(), " ), name Strings.join( candidateNames, "(), " ), name
), ),
method.getExecutable(), method.getExecutable(),
reversePrism.mirror ); reversePrism.mirror
);
} }
} }
private void reportErrorWhenSeveralNamesMatch(List<SourceMethod> candidates, SourceMethod method, private void reportErrorWhenSeveralNamesMatch(List<SourceMethod> candidates, SourceMethod method,
InheritInverseConfigurationPrism reversePrism) { InheritInverseConfigurationPrism reversePrism) {
messager.printMessage( Diagnostic.Kind.ERROR, messager.printMessage(
String.format( "Given name \"%s\" matches several candidate methods: %s().", Diagnostic.Kind.ERROR,
String.format(
"Given name \"%s\" matches several candidate methods: %s().",
reversePrism.name(), Strings.join( candidates, "(), " ) reversePrism.name(), Strings.join( candidates, "(), " )
), ),
method.getExecutable(), method.getExecutable(),
reversePrism.mirror ); reversePrism.mirror
);
} }
private void reportErrorWhenNonMatchingName(SourceMethod onlyCandidate, SourceMethod method, private void reportErrorWhenNonMatchingName(SourceMethod onlyCandidate, SourceMethod method,
InheritInverseConfigurationPrism reversePrism) { InheritInverseConfigurationPrism reversePrism) {
messager.printMessage( Diagnostic.Kind.ERROR, messager.printMessage(
String.format( "Given name \"%s\" does not match the only candidate. Did you mean: \"%s\".", Diagnostic.Kind.ERROR,
String.format(
"Given name \"%s\" does not match the only candidate. Did you mean: \"%s\".",
reversePrism.name(), onlyCandidate.getName() reversePrism.name(), onlyCandidate.getName()
), ),
method.getExecutable(), method.getExecutable(),
reversePrism.mirror ); reversePrism.mirror
);
} }
} }

View File

@ -18,8 +18,6 @@
*/ */
package org.mapstruct.ap.test.reverse; package org.mapstruct.ap.test.reverse;
import static org.fest.assertions.Assertions.assertThat;
import javax.tools.Diagnostic.Kind; import javax.tools.Diagnostic.Kind;
import org.junit.Test; import org.junit.Test;
@ -31,9 +29,10 @@ import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic;
import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome; 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;
/** /**
* @author Sjaak Derksen * @author Sjaak Derksen
*
*/ */
@IssueKey("252") @IssueKey("252")
@WithClasses({ Source.class, Target.class }) @WithClasses({ Source.class, Target.class })
@ -70,12 +69,12 @@ public class InheritInverseConfigurationTest {
diagnostics = { diagnostics = {
@Diagnostic(type = SourceTargetMapperAmbiguous1.class, @Diagnostic(type = SourceTargetMapperAmbiguous1.class,
kind = Kind.ERROR, kind = Kind.ERROR,
line = 51, line = 49,
messageRegExp = "Several matching inverse methods exist: forward\\(\\), " messageRegExp = "Several matching inverse methods exist: forward\\(\\), "
+ "forwardNotToReverse\\(\\). Specify a name explicitly."), + "forwardNotToReverse\\(\\). Specify a name explicitly."),
@Diagnostic(type = SourceTargetMapperAmbiguous1.class, @Diagnostic(type = SourceTargetMapperAmbiguous1.class,
kind = Kind.WARNING, kind = Kind.WARNING,
line = 56, line = 54,
messageRegExp = "Unmapped target properties: \"stringPropX, integerPropX\"") messageRegExp = "Unmapped target properties: \"stringPropX, integerPropX\"")
} }
) )
@ -89,12 +88,12 @@ public class InheritInverseConfigurationTest {
diagnostics = { diagnostics = {
@Diagnostic(type = SourceTargetMapperAmbiguous2.class, @Diagnostic(type = SourceTargetMapperAmbiguous2.class,
kind = Kind.ERROR, kind = Kind.ERROR,
line = 51, line = 49,
messageRegExp = "None of the candidates forward\\(\\), forwardNotToReverse\\(\\) matches given " messageRegExp = "None of the candidates forward\\(\\), forwardNotToReverse\\(\\) matches given "
+ "name: \"blah\"."), + "name: \"blah\"."),
@Diagnostic(type = SourceTargetMapperAmbiguous2.class, @Diagnostic(type = SourceTargetMapperAmbiguous2.class,
kind = Kind.WARNING, kind = Kind.WARNING,
line = 56, line = 54,
messageRegExp = "Unmapped target properties: \"stringPropX, integerPropX\"") messageRegExp = "Unmapped target properties: \"stringPropX, integerPropX\"")
} }
) )
@ -108,12 +107,12 @@ public class InheritInverseConfigurationTest {
diagnostics = { diagnostics = {
@Diagnostic(type = SourceTargetMapperAmbiguous3.class, @Diagnostic(type = SourceTargetMapperAmbiguous3.class,
kind = Kind.ERROR, kind = Kind.ERROR,
line = 52, line = 50,
messageRegExp = "Given name \"forward\" matches several candidate methods: .*forward.*\\(\\), " messageRegExp = "Given name \"forward\" matches several candidate methods: .*forward.*\\(\\), "
+ ".*forward.*\\(\\)"), + ".*forward.*\\(\\)"),
@Diagnostic(type = SourceTargetMapperAmbiguous3.class, @Diagnostic(type = SourceTargetMapperAmbiguous3.class,
kind = Kind.WARNING, kind = Kind.WARNING,
line = 57, line = 55,
messageRegExp = "Unmapped target properties: \"stringPropX, integerPropX\"") messageRegExp = "Unmapped target properties: \"stringPropX, integerPropX\"")
} }
) )
@ -127,7 +126,7 @@ public class InheritInverseConfigurationTest {
diagnostics = { diagnostics = {
@Diagnostic(type = SourceTargetMapperErroneouslyAnnotated.class, @Diagnostic(type = SourceTargetMapperErroneouslyAnnotated.class,
kind = Kind.ERROR, kind = Kind.ERROR,
line = 51, line = 49,
messageRegExp = "Resolved inverse mapping method reverse\\(\\) should not carry the " messageRegExp = "Resolved inverse mapping method reverse\\(\\) should not carry the "
+ "@InheritInverseConfiguration annotation itself.") + "@InheritInverseConfiguration annotation itself.")
} }
@ -142,12 +141,12 @@ public class InheritInverseConfigurationTest {
diagnostics = { diagnostics = {
@Diagnostic(type = SourceTargetMapperNonMatchingName.class, @Diagnostic(type = SourceTargetMapperNonMatchingName.class,
kind = Kind.ERROR, kind = Kind.ERROR,
line = 44, line = 42,
messageRegExp = "Given name \"blah\" does not match the only candidate. Did you mean: " messageRegExp = "Given name \"blah\" does not match the only candidate. Did you mean: "
+ "\"forward\"."), + "\"forward\"."),
@Diagnostic(type = SourceTargetMapperNonMatchingName.class, @Diagnostic(type = SourceTargetMapperNonMatchingName.class,
kind = Kind.WARNING, kind = Kind.WARNING,
line = 49, line = 47,
messageRegExp = "Unmapped target properties: \"stringPropX, integerPropX\"") messageRegExp = "Unmapped target properties: \"stringPropX, integerPropX\"")
} }
) )

View File

@ -19,7 +19,6 @@
package org.mapstruct.ap.test.reverse; package org.mapstruct.ap.test.reverse;
/** /**
*
* @author Sjaak Derksen * @author Sjaak Derksen
*/ */
public class Source { public class Source {

View File

@ -25,10 +25,8 @@ import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
/** /**
*
* @author Sjaak Derksen * @author Sjaak Derksen
*/ */
@Mapper @Mapper
public interface SourceTargetMapper { public interface SourceTargetMapper {

View File

@ -18,17 +18,15 @@
*/ */
package org.mapstruct.ap.test.reverse; package org.mapstruct.ap.test.reverse;
import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.Mapping; import org.mapstruct.Mapping;
import org.mapstruct.Mappings; import org.mapstruct.Mappings;
import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
/** /**
*
* @author Sjaak Derksen * @author Sjaak Derksen
*/ */
@Mapper @Mapper
public interface SourceTargetMapperAmbiguous1 { public interface SourceTargetMapperAmbiguous1 {

View File

@ -25,10 +25,8 @@ import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
/** /**
*
* @author Sjaak Derksen * @author Sjaak Derksen
*/ */
@Mapper @Mapper
public interface SourceTargetMapperAmbiguous2 { public interface SourceTargetMapperAmbiguous2 {

View File

@ -26,10 +26,8 @@ import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
/** /**
*
* @author Sjaak Derksen * @author Sjaak Derksen
*/ */
@Mapper @Mapper
public interface SourceTargetMapperAmbiguous3 { public interface SourceTargetMapperAmbiguous3 {

View File

@ -25,10 +25,8 @@ import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
/** /**
*
* @author Sjaak Derksen * @author Sjaak Derksen
*/ */
@Mapper @Mapper
public interface SourceTargetMapperErroneouslyAnnotated { public interface SourceTargetMapperErroneouslyAnnotated {

View File

@ -25,10 +25,8 @@ import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
/** /**
*
* @author Sjaak Derksen * @author Sjaak Derksen
*/ */
@Mapper @Mapper
public interface SourceTargetMapperNonMatchingName { public interface SourceTargetMapperNonMatchingName {

View File

@ -19,7 +19,6 @@
package org.mapstruct.ap.test.reverse; package org.mapstruct.ap.test.reverse;
/** /**
*
* @author Sjaak Derksen * @author Sjaak Derksen
*/ */
public class Target { public class Target {