#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) {
this.type = type;
isAnnotatedMapper = type.isAnnotatedMapper();
isAnnotatedMapper = type.isAnnotatedWith( "org.mapstruct.Mapper" );
importTypes = Collections.asSet( type );
if ( isAnnotatedMapper() ) {

View File

@ -21,7 +21,6 @@ package org.mapstruct.ap.model;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ElementKind;
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.Types;
import org.mapstruct.ap.MapperPrism;
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
*
* @return true, if the type is annotated with an annotation of the specified type (super-types are not inspected)
*/
public boolean isAnnotatedWith(String annotationTypeName) {
@ -182,13 +181,6 @@ public class Type extends AbstractModelElement implements Comparable<Type> {
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.
*
@ -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.
*
* @param assignableOther the 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) {

View File

@ -206,7 +206,16 @@ public class Method {
@Override
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) {

View File

@ -27,7 +27,6 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.processing.Messager;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
@ -610,7 +609,8 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
candidatesWithBestMatchingSourceType,
bestMatchingSourceTypeDistance,
method,
sourceTypeDistance );
sourceTypeDistance
);
}
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
if ( candidatesWithBestMatchingSourceType.size() > 1 ) {
messager.printMessage( Kind.ERROR, String.format(
messager.printMessage(
Kind.ERROR,
String.format(
"Ambiguous mapping methods found for mapping from %s to %s: %s.",
parameterType,
returnType,
candidatesWithBestMatchingSourceType ) );
candidatesWithBestMatchingSourceType
)
);
}
return new MappingMethodReference( candidatesWithBestMatchingSourceType.get( 0 ) );

View File

@ -21,7 +21,6 @@ package org.mapstruct.ap.util;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.TypeParameterElement;
import javax.lang.model.element.VariableElement;
@ -246,7 +245,8 @@ public class MethodMatcher {
TypeMirror superBoundAsDeclared = typeParameter.getBounds().get( 0 );
return ( typeUtils.isSubtype( superBoundAsDeclared, p ) || typeUtils.isSameType(
p,
superBoundAsDeclared ) );
superBoundAsDeclared
) );
default:
// 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
*
* @param t type parameter to match
*
* @return matching type parameter
*/
private TypeParameterElement getTypeParamFromCandidate(TypeMirror t) {
@ -278,6 +279,7 @@ public class MethodMatcher {
*
* @param t
* @param tpe
*
* @return true if within bounds
*/
private boolean isWithinBounds(TypeMirror t, TypeParameterElement tpe) {

View File

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

View File

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

View File

@ -37,9 +37,11 @@ import org.testng.annotations.Test;
*
* @author Andreas Gudian
*/
@WithClasses( { Reference.class, SourceBase.class, SourceComposite.class,
@WithClasses({
Reference.class, SourceBase.class, SourceComposite.class,
SourceExt.class, SourceExt2.class,
TargetComposite.class, AdditionalFooSource.class } )
TargetComposite.class, AdditionalFooSource.class
})
public class ComplexInheritanceTest extends MapperTestBase {
@Test
@ -72,7 +74,7 @@ public class ComplexInheritanceTest extends MapperTestBase {
@Test
@IssueKey("34")
@WithClasses( { ErrornousSourceCompositeTargetCompositeMapper.class, AdditionalMappingHelper.class } )
@WithClasses({ ErroneousSourceCompositeTargetCompositeMapper.class, AdditionalMappingHelper.class })
@ExpectedCompilationOutcome(value = CompilationResult.FAILED,
diagnostics = @Diagnostic(
kind = Kind.ERROR,
@ -80,7 +82,6 @@ public class ComplexInheritanceTest extends MapperTestBase {
+ ".*Reference .*AdditionalMappingHelper\\.asReference\\(.*SourceBase source\\), "
+ ".*Reference .*AdditionalMappingHelper\\.asReference\\(.*AdditionalFooSource source\\)\\]"))
public void ambiguousMappingMethodsReportError() {
}
private void assertResult(TargetComposite target) {

View File

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

View File

@ -23,8 +23,8 @@ import org.mapstruct.factory.Mappers;
@Mapper(uses = { SourceBaseMappingHelper.class })
public interface SourceCompositeTargetCompositeMapper {
SourceCompositeTargetCompositeMapper INSTANCE = Mappers.getMapper( SourceCompositeTargetCompositeMapper.class );
TargetComposite sourceToTarget(SourceComposite source);
}

View File

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