Package org.mapstruct.ap.model.assignment

Examples of org.mapstruct.ap.model.assignment.Assignment


            // find mapping method or conversion for key
            Type keySourceType = sourceTypeParams.get( 0 );
            Type keyTargetType = resultTypeParams.get( 0 );

            Assignment keyAssignment = ctx.getMappingResolver().getTargetAssignment(
                method,
                "map key",
                keySourceType,
                keyTargetType,
                null, // there is no targetPropertyName
                keyDateFormat,
                keyQualifiers,
                "entry.getKey()"
            );

            if ( keyAssignment == null ) {
                String message = String.format(
                    "Can't create implementation of method %s. Found no method nor "
                        + "built-in conversion for mapping source key type to target key type.", method
                );
                ctx.getMessager().printMessage( Diagnostic.Kind.ERROR, message, method.getExecutable() );
            }

            // find mapping method or conversion for value
            Type valueSourceType = sourceTypeParams.get( 1 );
            Type valueTargetType = resultTypeParams.get( 1 );

            Assignment valueAssignment = ctx.getMappingResolver().getTargetAssignment(
                method,
                "map value",
                valueSourceType,
                valueTargetType,
                null, // there is no targetPropertyName
View Full Code Here


                method.getResultType().getTypeParameters().get( 0 );
            String conversionStr =
                Strings.getSaveVariableName( sourceElementType.getName(), method.getParameterNames() );


            Assignment assignment = ctx.getMappingResolver().getTargetAssignment(
                method,
                "collection element",
                sourceElementType,
                targetElementType,
                null, // there is no targetPropertyName
View Full Code Here

        }

        private Assignment getTargetAssignment(Type sourceType, Type targetType) {

            // first simple mapping method
            Assignment referencedMethod = resolveViaMethod( sourceType, targetType, false );
            if ( referencedMethod != null ) {
                referencedMethod.setAssignment( AssignmentFactory.createDirect( sourceReference ) );
                return referencedMethod;
            }

            // then direct assignable
            if ( sourceType.isAssignableTo( targetType ) || isPropertyMappable( sourceType, targetType ) ) {
                Assignment simpleAssignment = AssignmentFactory.createDirect( sourceReference );
                return simpleAssignment;
            }

            // then type conversion
            Assignment conversion = resolveViaConversion( sourceType, targetType );
            if ( conversion != null ) {
                conversion.setAssignment( AssignmentFactory.createDirect( sourceReference ) );
                return conversion;
            }

            // check for a built-in method
            Assignment builtInMethod = resolveViaBuiltInMethod( sourceType, targetType );
            if ( builtInMethod != null ) {
                builtInMethod.setAssignment( AssignmentFactory.createDirect( sourceReference ) );
                usedVirtualMappings.addAll( virtualMethodCandidates );
                return builtInMethod;
            }

            // 2 step method, first: method(method(source))
View Full Code Here

                getBestMatch( builtInMethods.getBuiltInMethods(), sourceType, targetType );

            if ( matchingBuiltInMethod != null ) {
                virtualMethodCandidates.add( new VirtualMappingMethod( matchingBuiltInMethod ) );
                ConversionContext ctx = new DefaultConversionContext( typeFactory, targetType, dateFormat );
                Assignment methodReference = AssignmentFactory.createMethodReference( matchingBuiltInMethod, ctx );
                methodReference.setAssignment( AssignmentFactory.createDirect( sourceReference ) );
                return methodReference;
            }

            return null;
        }
View Full Code Here

        private Assignment resolveViaMethodAndMethod(Type sourceType, Type targetType) {

            List<Method> methodYCandidates = new ArrayList<Method>( methods );
            methodYCandidates.addAll( builtInMethods.getBuiltInMethods() );

            Assignment methodRefY = null;

            // Iterate over all source methods. Check if the return type matches with the parameter that we need.
            // so assume we need a method from A to C we look for a methodX from A to B (all methods in the
            // list form such a candidate).
            // For each of the candidates, we need to look if there's  a methodY, either
            // sourceMethod or builtIn that fits the signature B to C. Only then there is a match. If we have a match
            // a nested method call can be called. so C = methodY( methodX (A) )
            for ( Method methodYCandidate : methodYCandidates ) {
                if ( methodYCandidate.getSourceParameters().size() == 1 ) {
                    methodRefY = resolveViaMethod(
                        methodYCandidate.getSourceParameters().get( 0 ).getType(),
                        targetType,
                        true
                    );
                    if ( methodRefY != null ) {
                        Assignment methodRefX = resolveViaMethod(
                            sourceType,
                            methodYCandidate.getSourceParameters().get( 0 ).getType(),
                            true
                        );
                        if ( methodRefX != null ) {
                            methodRefY.setAssignment( methodRefX );
                            methodRefX.setAssignment( AssignmentFactory.createDirect( sourceReference ) );
                            break;
                        }
                        else {
                            // both should match;
                            virtualMethodCandidates.clear();
View Full Code Here

        private Assignment resolveViaConversionAndMethod(Type sourceType, Type targetType) {

            List<Method> methodYCandidates = new ArrayList<Method>( methods );
            methodYCandidates.addAll( builtInMethods.getBuiltInMethods() );

            Assignment methodRefY = null;

            for ( Method methodYCandidate : methodYCandidates ) {
                if ( methodYCandidate.getSourceParameters().size() == 1 ) {
                    methodRefY = resolveViaMethod(
                        methodYCandidate.getSourceParameters().get( 0 ).getType(),
                        targetType,
                        true
                    );
                    if ( methodRefY != null ) {
                        Assignment conversionXRef = resolveViaConversion(
                            sourceType,
                            methodYCandidate.getSourceParameters().get( 0 ).getType()
                        );
                        if ( conversionXRef != null ) {
                            methodRefY.setAssignment( conversionXRef );
                            conversionXRef.setAssignment( AssignmentFactory.createDirect( sourceReference ) );
                            break;
                        }
                        else {
                            // both should match
                            virtualMethodCandidates.clear();
View Full Code Here

        private Assignment resolveViaMethodAndConversion(Type sourceType, Type targetType) {

            List<Method> methodXCandidates = new ArrayList<Method>( methods );
            methodXCandidates.addAll( builtInMethods.getBuiltInMethods() );

            Assignment conversionYRef = null;

            // search the other way around
            for ( Method methodXCandidate : methodXCandidates ) {
                if ( methodXCandidate.getSourceParameters().size() == 1 ) {
                    Assignment methodRefX = resolveViaMethod(
                        sourceType,
                        methodXCandidate.getReturnType(),
                        true
                    );
                    if ( methodRefX != null ) {
                        conversionYRef = resolveViaConversion( methodXCandidate.getReturnType(), targetType );
                        if ( conversionYRef != null ) {
                            conversionYRef.setAssignment( methodRefX );
                            methodRefX.setAssignment( AssignmentFactory.createDirect( sourceReference ) );
                            break;
                        }
                        else {
                            // both should match;
                            virtualMethodCandidates.clear();
View Full Code Here

        private PropertyMapping getPropertyMapping(Type sourceType, Type targetType,
                                                   TargetAccessorType targetAccessorType, String sourceRefStr,
                                                   String sourceElement) {

            Assignment assignment = ctx.getMappingResolver().getTargetAssignment(
                method,
                sourceElement,
                sourceType,
                targetType,
                targetPropertyName,
                dateFormat,
                qualifiers,
                sourceRefStr
            );

            // No mapping found. Try to forge a mapping
            if ( assignment == null ) {
                assignment = forgeMapOrIterableMapping( sourceType, targetType, sourceRefStr, method.getExecutable() );
            }

            if ( assignment != null ) {

                if ( targetType.isCollectionOrMapType() ) {

                    // wrap the setter in the collection / map initializers
                    if ( targetAccessorType == TargetAccessorType.SETTER ) {

                        // wrap the assignment in a new Map or Collection implementation if this is not done in a
                        // mapping method. Note, typeconversons do not apply to collections or maps
                        Assignment newCollectionOrMap = null;
                        if ( assignment.getType() == DIRECT ) {
                            newCollectionOrMap =
                                new NewCollectionOrMapWrapper( assignment, targetType.getImportTypes() );
                            newCollectionOrMap = new SetterWrapper( newCollectionOrMap, method.getThrownTypes() );
                        }
View Full Code Here

        }

        private Assignment forgeMapOrIterableMapping(Type sourceType, Type targetType, String sourceReference,
                                                     ExecutableElement element) {

            Assignment assignment = null;
            if ( sourceType.isCollectionType() && targetType.isCollectionType() ) {

                ForgedMethod methodToGenerate = new ForgedMethod( sourceType, targetType, element );
                IterableMappingMethod.Builder builder = new IterableMappingMethod.Builder();


                IterableMappingMethod iterableMappingMethod = builder
                    .mappingContext( ctx )
                    .method( methodToGenerate )
                    .build();

                if ( !ctx.getMappingsToGenerate().contains( iterableMappingMethod ) ) {
                    ctx.getMappingsToGenerate().add( iterableMappingMethod );
                }
                assignment = AssignmentFactory.createMethodReference( methodToGenerate, null, targetType );
                assignment.setAssignment( AssignmentFactory.createDirect( sourceReference ) );

            }
            else if ( sourceType.isMapType() && targetType.isMapType() ) {

                ForgedMethod methodToGenerate = new ForgedMethod( sourceType, targetType, element );

                MapMappingMethod.Builder builder = new MapMappingMethod.Builder();
                MapMappingMethod mapMappingMethod = builder
                    .mappingContext( ctx )
                    .method( methodToGenerate )
                    .build();

                if ( !ctx.getMappingsToGenerate().contains( mapMappingMethod ) ) {
                    ctx.getMappingsToGenerate().add( mapMappingMethod );
                }
                assignment = AssignmentFactory.createMethodReference( methodToGenerate, null, targetType );
                assignment.setAssignment( AssignmentFactory.createDirect( sourceReference ) );
            }
            return assignment;
        }
View Full Code Here

                targetType = ctx.getTypeFactory().getReturnType( targetAccessor );
            }

            String targetPropertyName = Executables.getPropertyName( targetAccessor );

            Assignment assignment = ctx.getMappingResolver().getTargetAssignment(
                method,
                mappedElement,
                sourceType,
                targetType,
                targetPropertyName,
View Full Code Here

TOP

Related Classes of org.mapstruct.ap.model.assignment.Assignment

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.