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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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