#34 Fixing some typos; Removing Type#isAnnotatedMapper() in favor of the more generic isAnnotatedWith() method; Formatting

This commit is contained in:
Gunnar Morling 2014-01-08 22:26:51 +01:00
parent aa3fa638b9
commit a7be616ee9
11 changed files with 58 additions and 46 deletions

View File

@ -40,7 +40,7 @@ public class DefaultMapperReference extends AbstractModelElement implements Mapp
public DefaultMapperReference(Type type, TypeFactory typeFactory) { public DefaultMapperReference(Type type, TypeFactory typeFactory) {
this.type = type; this.type = type;
isAnnotatedMapper = type.isAnnotatedMapper(); isAnnotatedMapper = type.isAnnotatedWith( "org.mapstruct.Mapper" );
importTypes = Collections.asSet( type ); importTypes = Collections.asSet( type );
if ( isAnnotatedMapper() ) { if ( isAnnotatedMapper() ) {

View File

@ -21,7 +21,6 @@ package org.mapstruct.ap.model;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ElementKind; import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Name; import javax.lang.model.element.Name;
@ -33,7 +32,6 @@ import javax.lang.model.util.Elements;
import javax.lang.model.util.SimpleElementVisitor6; import javax.lang.model.util.SimpleElementVisitor6;
import javax.lang.model.util.Types; import javax.lang.model.util.Types;
import org.mapstruct.ap.MapperPrism;
import org.mapstruct.ap.util.TypeFactory; import org.mapstruct.ap.util.TypeFactory;
/** /**
@ -167,6 +165,7 @@ public class Type extends AbstractModelElement implements Comparable<Type> {
/** /**
* @param annotationTypeName the fully qualified name of the annotation type * @param annotationTypeName the fully qualified name of the annotation type
*
* @return true, if the type is annotated with an annotation of the specified type (super-types are not inspected) * @return true, if the type is annotated with an annotation of the specified type (super-types are not inspected)
*/ */
public boolean isAnnotatedWith(String annotationTypeName) { public boolean isAnnotatedWith(String annotationTypeName) {
@ -182,13 +181,6 @@ public class Type extends AbstractModelElement implements Comparable<Type> {
return false; return false;
} }
/**
* @return true, if the type is annotated with {@code @Mapper}
*/
public boolean isAnnotatedMapper() {
return null != MapperPrism.getInstanceOn( typeElement );
}
/** /**
* Whether this type is assignable to the given other type. * Whether this type is assignable to the given other type.
* *
@ -212,6 +204,7 @@ public class Type extends AbstractModelElement implements Comparable<Type> {
* the other type. Returns {@code 1}, if the other type is a direct super type of this type, and so on. * the other type. Returns {@code 1}, if the other type is a direct super type of this type, and so on.
* *
* @param assignableOther the other type * @param assignableOther the other type
*
* @return the length of the shortest path in the type hierarchy between this type and the specified other type * @return the length of the shortest path in the type hierarchy between this type and the specified other type
*/ */
public int distanceTo(Type assignableOther) { public int distanceTo(Type assignableOther) {

View File

@ -206,7 +206,16 @@ public class Method {
@Override @Override
public String toString() { public String toString() {
return returnType + " " + declaringMapper + "." + getName() + "(" + Strings.join( parameters, ", " ) + ")"; StringBuilder sb = new StringBuilder( returnType.toString() );
sb.append( " " );
if ( declaringMapper != null ) {
sb.append( declaringMapper ).append( "." );
}
sb.append( getName() ).append( "(" ).append( Strings.join( parameters, ", " ) ).append( ")" );
return sb.toString();
} }
public Mapping getMapping(String targetPropertyName) { public Mapping getMapping(String targetPropertyName) {

View File

@ -27,7 +27,6 @@ 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.ExecutableElement; import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement; import javax.lang.model.element.TypeElement;
@ -500,7 +499,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
Kind.ERROR, Kind.ERROR,
String.format( String.format(
"Can't create implementation of method %s. Found no method nor built-in conversion for mapping " "Can't create implementation of method %s. Found no method nor built-in conversion for mapping "
+ "source element type into target element type.", + "source element type into target element type.",
method method
), ),
method.getExecutable() method.getExecutable()
@ -546,7 +545,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
Kind.ERROR, Kind.ERROR,
String.format( String.format(
"Can't create implementation of method %s. Found no method nor built-in conversion for mapping " "Can't create implementation of method %s. Found no method nor built-in conversion for mapping "
+ "source key type to target key type.", + "source key type to target key type.",
method method
), ),
method.getExecutable() method.getExecutable()
@ -559,7 +558,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
Kind.ERROR, Kind.ERROR,
String.format( String.format(
"Can't create implementation of method %s. Found no method nor built-in conversion for mapping " "Can't create implementation of method %s. Found no method nor built-in conversion for mapping "
+ "source value type to target value type.", + "source value type to target value type.",
method method
), ),
method.getExecutable() method.getExecutable()
@ -610,7 +609,8 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
candidatesWithBestMatchingSourceType, candidatesWithBestMatchingSourceType,
bestMatchingSourceTypeDistance, bestMatchingSourceTypeDistance,
method, method,
sourceTypeDistance ); sourceTypeDistance
);
} }
if ( candidatesWithBestMatchingSourceType.isEmpty() ) { if ( candidatesWithBestMatchingSourceType.isEmpty() ) {
@ -619,11 +619,15 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
// print a warning if we find more than one method with minimum source type distance // print a warning if we find more than one method with minimum source type distance
if ( candidatesWithBestMatchingSourceType.size() > 1 ) { if ( candidatesWithBestMatchingSourceType.size() > 1 ) {
messager.printMessage( Kind.ERROR, String.format( messager.printMessage(
"Ambiguous mapping methods found for mapping from %s to %s: %s.", Kind.ERROR,
parameterType, String.format(
returnType, "Ambiguous mapping methods found for mapping from %s to %s: %s.",
candidatesWithBestMatchingSourceType ) ); parameterType,
returnType,
candidatesWithBestMatchingSourceType
)
);
} }
return new MappingMethodReference( candidatesWithBestMatchingSourceType.get( 0 ) ); return new MappingMethodReference( candidatesWithBestMatchingSourceType.get( 0 ) );

View File

@ -21,7 +21,6 @@ package org.mapstruct.ap.util;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.lang.model.element.TypeElement; import javax.lang.model.element.TypeElement;
import javax.lang.model.element.TypeParameterElement; import javax.lang.model.element.TypeParameterElement;
import javax.lang.model.element.VariableElement; import javax.lang.model.element.VariableElement;
@ -246,7 +245,8 @@ public class MethodMatcher {
TypeMirror superBoundAsDeclared = typeParameter.getBounds().get( 0 ); TypeMirror superBoundAsDeclared = typeParameter.getBounds().get( 0 );
return ( typeUtils.isSubtype( superBoundAsDeclared, p ) || typeUtils.isSameType( return ( typeUtils.isSubtype( superBoundAsDeclared, p ) || typeUtils.isSameType(
p, p,
superBoundAsDeclared ) ); superBoundAsDeclared
) );
default: default:
// does this situation occur? // does this situation occur?
@ -262,6 +262,7 @@ public class MethodMatcher {
* Looks through the list of type parameters of the candidate method for a match * Looks through the list of type parameters of the candidate method for a match
* *
* @param t type parameter to match * @param t type parameter to match
*
* @return matching type parameter * @return matching type parameter
*/ */
private TypeParameterElement getTypeParamFromCandidate(TypeMirror t) { private TypeParameterElement getTypeParamFromCandidate(TypeMirror t) {
@ -278,6 +279,7 @@ public class MethodMatcher {
* *
* @param t * @param t
* @param tpe * @param tpe
*
* @return true if within bounds * @return true if within bounds
*/ */
private boolean isWithinBounds(TypeMirror t, TypeParameterElement tpe) { private boolean isWithinBounds(TypeMirror t, TypeParameterElement tpe) {

View File

@ -18,8 +18,6 @@
*/ */
package org.mapstruct.ap.test.collection; package org.mapstruct.ap.test.collection;
import static org.fest.assertions.Assertions.assertThat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.EnumSet; import java.util.EnumSet;
@ -33,6 +31,8 @@ import org.mapstruct.ap.testutil.MapperTestBase;
import org.mapstruct.ap.testutil.WithClasses; import org.mapstruct.ap.testutil.WithClasses;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import static org.fest.assertions.Assertions.assertThat;
@WithClasses({ Source.class, Target.class, Colour.class, SourceTargetMapper.class }) @WithClasses({ Source.class, Target.class, Colour.class, SourceTargetMapper.class })
public class CollectionMappingTest extends MapperTestBase { public class CollectionMappingTest extends MapperTestBase {

View File

@ -20,6 +20,7 @@ package org.mapstruct.ap.test.inheritance.complex;
public class AdditionalMappingHelper { public class AdditionalMappingHelper {
public Reference asReference(SourceBase source) { public Reference asReference(SourceBase source) {
if ( null == source ) { if ( null == source ) {
return null; return null;

View File

@ -37,14 +37,16 @@ import org.testng.annotations.Test;
* *
* @author Andreas Gudian * @author Andreas Gudian
*/ */
@WithClasses( { Reference.class, SourceBase.class, SourceComposite.class, @WithClasses({
SourceExt.class, SourceExt2.class, Reference.class, SourceBase.class, SourceComposite.class,
TargetComposite.class, AdditionalFooSource.class } ) SourceExt.class, SourceExt2.class,
TargetComposite.class, AdditionalFooSource.class
})
public class ComplexInheritanceTest extends MapperTestBase { public class ComplexInheritanceTest extends MapperTestBase {
@Test @Test
@IssueKey("34") @IssueKey("34")
@WithClasses( { StandaloneSourceCompositeTargetCompositeMapper.class } ) @WithClasses({ StandaloneSourceCompositeTargetCompositeMapper.class })
public void shouldMapAttributesWithSuperTypeInStandaloneMapper() { public void shouldMapAttributesWithSuperTypeInStandaloneMapper() {
SourceComposite source = createComposite(); SourceComposite source = createComposite();
@ -57,8 +59,8 @@ public class ComplexInheritanceTest extends MapperTestBase {
} }
@Test @Test
@IssueKey( "34" ) @IssueKey("34")
@WithClasses( { SourceCompositeTargetCompositeMapper.class, SourceBaseMappingHelper.class } ) @WithClasses({ SourceCompositeTargetCompositeMapper.class, SourceBaseMappingHelper.class })
public void shouldMapAttributesWithSuperTypeUsingOtherMapper() { public void shouldMapAttributesWithSuperTypeUsingOtherMapper() {
SourceComposite source = createComposite(); SourceComposite source = createComposite();
@ -71,16 +73,15 @@ public class ComplexInheritanceTest extends MapperTestBase {
} }
@Test @Test
@IssueKey( "34" ) @IssueKey("34")
@WithClasses( { ErrornousSourceCompositeTargetCompositeMapper.class, AdditionalMappingHelper.class } ) @WithClasses({ ErroneousSourceCompositeTargetCompositeMapper.class, AdditionalMappingHelper.class })
@ExpectedCompilationOutcome( value = CompilationResult.FAILED, @ExpectedCompilationOutcome(value = CompilationResult.FAILED,
diagnostics = @Diagnostic( diagnostics = @Diagnostic(
kind = Kind.ERROR, kind = Kind.ERROR,
messageRegExp = "Ambiguous mapping methods found for mapping from .*SourceExt to .*Reference: \\[" messageRegExp = "Ambiguous mapping methods found for mapping from .*SourceExt to .*Reference: \\["
+ ".*Reference .*AdditionalMappingHelper\\.asReference\\(.*SourceBase source\\), " + ".*Reference .*AdditionalMappingHelper\\.asReference\\(.*SourceBase source\\), "
+ ".*Reference .*AdditionalMappingHelper\\.asReference\\(.*AdditionalFooSource source\\)\\]" ) ) + ".*Reference .*AdditionalMappingHelper\\.asReference\\(.*AdditionalFooSource source\\)\\]"))
public void ambiguousMappingMethodsReportError() { public void ambiguousMappingMethodsReportError() {
} }
private void assertResult(TargetComposite target) { private void assertResult(TargetComposite target) {

View File

@ -23,10 +23,11 @@ import java.util.List;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
@Mapper( uses = { AdditionalMappingHelper.class } ) @Mapper(uses = { AdditionalMappingHelper.class })
public interface ErrornousSourceCompositeTargetCompositeMapper { public interface ErroneousSourceCompositeTargetCompositeMapper {
ErrornousSourceCompositeTargetCompositeMapper INSTANCE =
Mappers.getMapper( ErrornousSourceCompositeTargetCompositeMapper.class ); ErroneousSourceCompositeTargetCompositeMapper INSTANCE =
Mappers.getMapper( ErroneousSourceCompositeTargetCompositeMapper.class );
TargetComposite sourceToTarget(SourceComposite source); TargetComposite sourceToTarget(SourceComposite source);

View File

@ -21,10 +21,10 @@ package org.mapstruct.ap.test.inheritance.complex;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
@Mapper( uses = { SourceBaseMappingHelper.class } ) @Mapper(uses = { SourceBaseMappingHelper.class })
public interface SourceCompositeTargetCompositeMapper { public interface SourceCompositeTargetCompositeMapper {
SourceCompositeTargetCompositeMapper INSTANCE = Mappers.getMapper( SourceCompositeTargetCompositeMapper.class ); SourceCompositeTargetCompositeMapper INSTANCE = Mappers.getMapper( SourceCompositeTargetCompositeMapper.class );
TargetComposite sourceToTarget(SourceComposite source); TargetComposite sourceToTarget(SourceComposite source);
} }

View File

@ -25,6 +25,7 @@ import org.mapstruct.factory.Mappers;
@Mapper @Mapper
public interface StandaloneSourceCompositeTargetCompositeMapper { public interface StandaloneSourceCompositeTargetCompositeMapper {
StandaloneSourceCompositeTargetCompositeMapper INSTANCE = StandaloneSourceCompositeTargetCompositeMapper INSTANCE =
Mappers.getMapper( StandaloneSourceCompositeTargetCompositeMapper.class ); Mappers.getMapper( StandaloneSourceCompositeTargetCompositeMapper.class );