mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#34 Fixing some typos; Removing Type#isAnnotatedMapper() in favor of the more generic isAnnotatedWith() method; Formatting
This commit is contained in:
parent
aa3fa638b9
commit
a7be616ee9
@ -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() ) {
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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 ) );
|
||||
|
@ -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) {
|
||||
|
@ -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 {
|
||||
|
||||
|
@ -20,6 +20,7 @@ package org.mapstruct.ap.test.inheritance.complex;
|
||||
|
||||
|
||||
public class AdditionalMappingHelper {
|
||||
|
||||
public Reference asReference(SourceBase source) {
|
||||
if ( null == source ) {
|
||||
return null;
|
||||
|
@ -37,14 +37,16 @@ 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
|
||||
@IssueKey("34")
|
||||
@WithClasses( { StandaloneSourceCompositeTargetCompositeMapper.class } )
|
||||
@WithClasses({ StandaloneSourceCompositeTargetCompositeMapper.class })
|
||||
public void shouldMapAttributesWithSuperTypeInStandaloneMapper() {
|
||||
SourceComposite source = createComposite();
|
||||
|
||||
@ -57,8 +59,8 @@ public class ComplexInheritanceTest extends MapperTestBase {
|
||||
}
|
||||
|
||||
@Test
|
||||
@IssueKey( "34" )
|
||||
@WithClasses( { SourceCompositeTargetCompositeMapper.class, SourceBaseMappingHelper.class } )
|
||||
@IssueKey("34")
|
||||
@WithClasses({ SourceCompositeTargetCompositeMapper.class, SourceBaseMappingHelper.class })
|
||||
public void shouldMapAttributesWithSuperTypeUsingOtherMapper() {
|
||||
SourceComposite source = createComposite();
|
||||
|
||||
@ -71,16 +73,15 @@ public class ComplexInheritanceTest extends MapperTestBase {
|
||||
}
|
||||
|
||||
@Test
|
||||
@IssueKey( "34" )
|
||||
@WithClasses( { ErrornousSourceCompositeTargetCompositeMapper.class, AdditionalMappingHelper.class } )
|
||||
@ExpectedCompilationOutcome( value = CompilationResult.FAILED,
|
||||
@IssueKey("34")
|
||||
@WithClasses({ ErroneousSourceCompositeTargetCompositeMapper.class, AdditionalMappingHelper.class })
|
||||
@ExpectedCompilationOutcome(value = CompilationResult.FAILED,
|
||||
diagnostics = @Diagnostic(
|
||||
kind = Kind.ERROR,
|
||||
messageRegExp = "Ambiguous mapping methods found for mapping from .*SourceExt to .*Reference: \\["
|
||||
+ ".*Reference .*AdditionalMappingHelper\\.asReference\\(.*SourceBase source\\), "
|
||||
+ ".*Reference .*AdditionalMappingHelper\\.asReference\\(.*AdditionalFooSource source\\)\\]" ) )
|
||||
+ ".*Reference .*AdditionalMappingHelper\\.asReference\\(.*AdditionalFooSource source\\)\\]"))
|
||||
public void ambiguousMappingMethodsReportError() {
|
||||
|
||||
}
|
||||
|
||||
private void assertResult(TargetComposite target) {
|
||||
|
@ -23,10 +23,11 @@ import java.util.List;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper( uses = { AdditionalMappingHelper.class } )
|
||||
public interface ErrornousSourceCompositeTargetCompositeMapper {
|
||||
ErrornousSourceCompositeTargetCompositeMapper INSTANCE =
|
||||
Mappers.getMapper( ErrornousSourceCompositeTargetCompositeMapper.class );
|
||||
@Mapper(uses = { AdditionalMappingHelper.class })
|
||||
public interface ErroneousSourceCompositeTargetCompositeMapper {
|
||||
|
||||
ErroneousSourceCompositeTargetCompositeMapper INSTANCE =
|
||||
Mappers.getMapper( ErroneousSourceCompositeTargetCompositeMapper.class );
|
||||
|
||||
TargetComposite sourceToTarget(SourceComposite source);
|
||||
|
@ -21,10 +21,10 @@ package org.mapstruct.ap.test.inheritance.complex;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper( uses = { SourceBaseMappingHelper.class } )
|
||||
@Mapper(uses = { SourceBaseMappingHelper.class })
|
||||
public interface SourceCompositeTargetCompositeMapper {
|
||||
|
||||
SourceCompositeTargetCompositeMapper INSTANCE = Mappers.getMapper( SourceCompositeTargetCompositeMapper.class );
|
||||
|
||||
TargetComposite sourceToTarget(SourceComposite source);
|
||||
|
||||
}
|
||||
|
@ -25,6 +25,7 @@ import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper
|
||||
public interface StandaloneSourceCompositeTargetCompositeMapper {
|
||||
|
||||
StandaloneSourceCompositeTargetCompositeMapper INSTANCE =
|
||||
Mappers.getMapper( StandaloneSourceCompositeTargetCompositeMapper.class );
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user