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) {
|
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() ) {
|
||||||
|
@ -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) {
|
||||||
|
@ -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) {
|
||||||
|
@ -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;
|
||||||
@ -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(
|
||||||
|
Kind.ERROR,
|
||||||
|
String.format(
|
||||||
"Ambiguous mapping methods found for mapping from %s to %s: %s.",
|
"Ambiguous mapping methods found for mapping from %s to %s: %s.",
|
||||||
parameterType,
|
parameterType,
|
||||||
returnType,
|
returnType,
|
||||||
candidatesWithBestMatchingSourceType ) );
|
candidatesWithBestMatchingSourceType
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new MappingMethodReference( candidatesWithBestMatchingSourceType.get( 0 ) );
|
return new MappingMethodReference( candidatesWithBestMatchingSourceType.get( 0 ) );
|
||||||
|
@ -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) {
|
||||||
|
@ -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 {
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
@ -37,9 +37,11 @@ import org.testng.annotations.Test;
|
|||||||
*
|
*
|
||||||
* @author Andreas Gudian
|
* @author Andreas Gudian
|
||||||
*/
|
*/
|
||||||
@WithClasses( { Reference.class, SourceBase.class, SourceComposite.class,
|
@WithClasses({
|
||||||
|
Reference.class, SourceBase.class, SourceComposite.class,
|
||||||
SourceExt.class, SourceExt2.class,
|
SourceExt.class, SourceExt2.class,
|
||||||
TargetComposite.class, AdditionalFooSource.class } )
|
TargetComposite.class, AdditionalFooSource.class
|
||||||
|
})
|
||||||
public class ComplexInheritanceTest extends MapperTestBase {
|
public class ComplexInheritanceTest extends MapperTestBase {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -72,7 +74,7 @@ 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,
|
||||||
@ -80,7 +82,6 @@ public class ComplexInheritanceTest extends MapperTestBase {
|
|||||||
+ ".*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) {
|
||||||
|
@ -24,9 +24,10 @@ 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);
|
||||||
|
|
@ -23,8 +23,8 @@ 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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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 );
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user