All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mapstruct.ap.internal.gem.MapperConfigGem Maven / Gradle / Ivy

There is a newer version: 1.6.3
Show newest version
package org.mapstruct.ap.internal.gem;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.AbstractAnnotationValueVisitor8;
import javax.lang.model.util.ElementFilter;
import org.mapstruct.tools.gem.Gem;
import org.mapstruct.tools.gem.GemValue;

import javax.lang.model.type.TypeMirror;

public class MapperConfigGem implements Gem {

    private final GemValue> uses;
    private final GemValue> imports;
    private final GemValue unmappedSourcePolicy;
    private final GemValue unmappedTargetPolicy;
    private final GemValue typeConversionPolicy;
    private final GemValue componentModel;
    private final GemValue implementationName;
    private final GemValue implementationPackage;
    private final GemValue collectionMappingStrategy;
    private final GemValue nullValueMappingStrategy;
    private final GemValue nullValuePropertyMappingStrategy;
    private final GemValue mappingInheritanceStrategy;
    private final GemValue nullValueCheckStrategy;
    private final GemValue injectionStrategy;
    private final GemValue disableSubMappingMethodsGeneration;
    private final GemValue builder;
    private final GemValue mappingControl;
    private final GemValue unexpectedValueMappingException;
    private final boolean isValid;
    private final AnnotationMirror mirror;

    private MapperConfigGem( BuilderImpl builder ) {
        this.uses = builder.uses;
        this.imports = builder.imports;
        this.unmappedSourcePolicy = builder.unmappedSourcePolicy;
        this.unmappedTargetPolicy = builder.unmappedTargetPolicy;
        this.typeConversionPolicy = builder.typeConversionPolicy;
        this.componentModel = builder.componentModel;
        this.implementationName = builder.implementationName;
        this.implementationPackage = builder.implementationPackage;
        this.collectionMappingStrategy = builder.collectionMappingStrategy;
        this.nullValueMappingStrategy = builder.nullValueMappingStrategy;
        this.nullValuePropertyMappingStrategy = builder.nullValuePropertyMappingStrategy;
        this.mappingInheritanceStrategy = builder.mappingInheritanceStrategy;
        this.nullValueCheckStrategy = builder.nullValueCheckStrategy;
        this.injectionStrategy = builder.injectionStrategy;
        this.disableSubMappingMethodsGeneration = builder.disableSubMappingMethodsGeneration;
        this.builder = builder.builder;
        this.mappingControl = builder.mappingControl;
        this.unexpectedValueMappingException = builder.unexpectedValueMappingException;
        isValid = ( this.uses != null ? this.uses.isValid() : false )
               && ( this.imports != null ? this.imports.isValid() : false )
               && ( this.unmappedSourcePolicy != null ? this.unmappedSourcePolicy.isValid() : false )
               && ( this.unmappedTargetPolicy != null ? this.unmappedTargetPolicy.isValid() : false )
               && ( this.typeConversionPolicy != null ? this.typeConversionPolicy.isValid() : false )
               && ( this.componentModel != null ? this.componentModel.isValid() : false )
               && ( this.implementationName != null ? this.implementationName.isValid() : false )
               && ( this.implementationPackage != null ? this.implementationPackage.isValid() : false )
               && ( this.collectionMappingStrategy != null ? this.collectionMappingStrategy.isValid() : false )
               && ( this.nullValueMappingStrategy != null ? this.nullValueMappingStrategy.isValid() : false )
               && ( this.nullValuePropertyMappingStrategy != null ? this.nullValuePropertyMappingStrategy.isValid() : false )
               && ( this.mappingInheritanceStrategy != null ? this.mappingInheritanceStrategy.isValid() : false )
               && ( this.nullValueCheckStrategy != null ? this.nullValueCheckStrategy.isValid() : false )
               && ( this.injectionStrategy != null ? this.injectionStrategy.isValid() : false )
               && ( this.disableSubMappingMethodsGeneration != null ? this.disableSubMappingMethodsGeneration.isValid() : false )
               && ( this.builder != null ? this.builder.isValid() : false )
               && ( this.mappingControl != null ? this.mappingControl.isValid() : false )
               && ( this.unexpectedValueMappingException != null ? this.unexpectedValueMappingException.isValid() : false );
        mirror = builder.mirror;
    }

    /**
    * accessor
    *
    * @return the {@link GemValue} for {@link MapperConfigGem#uses}
    */
    public GemValue> uses( ) {
        return uses;
    }

    /**
    * accessor
    *
    * @return the {@link GemValue} for {@link MapperConfigGem#imports}
    */
    public GemValue> imports( ) {
        return imports;
    }

    /**
    * accessor
    *
    * @return the {@link GemValue} for {@link MapperConfigGem#unmappedSourcePolicy}
    */
    public GemValue unmappedSourcePolicy( ) {
        return unmappedSourcePolicy;
    }

    /**
    * accessor
    *
    * @return the {@link GemValue} for {@link MapperConfigGem#unmappedTargetPolicy}
    */
    public GemValue unmappedTargetPolicy( ) {
        return unmappedTargetPolicy;
    }

    /**
    * accessor
    *
    * @return the {@link GemValue} for {@link MapperConfigGem#typeConversionPolicy}
    */
    public GemValue typeConversionPolicy( ) {
        return typeConversionPolicy;
    }

    /**
    * accessor
    *
    * @return the {@link GemValue} for {@link MapperConfigGem#componentModel}
    */
    public GemValue componentModel( ) {
        return componentModel;
    }

    /**
    * accessor
    *
    * @return the {@link GemValue} for {@link MapperConfigGem#implementationName}
    */
    public GemValue implementationName( ) {
        return implementationName;
    }

    /**
    * accessor
    *
    * @return the {@link GemValue} for {@link MapperConfigGem#implementationPackage}
    */
    public GemValue implementationPackage( ) {
        return implementationPackage;
    }

    /**
    * accessor
    *
    * @return the {@link GemValue} for {@link MapperConfigGem#collectionMappingStrategy}
    */
    public GemValue collectionMappingStrategy( ) {
        return collectionMappingStrategy;
    }

    /**
    * accessor
    *
    * @return the {@link GemValue} for {@link MapperConfigGem#nullValueMappingStrategy}
    */
    public GemValue nullValueMappingStrategy( ) {
        return nullValueMappingStrategy;
    }

    /**
    * accessor
    *
    * @return the {@link GemValue} for {@link MapperConfigGem#nullValuePropertyMappingStrategy}
    */
    public GemValue nullValuePropertyMappingStrategy( ) {
        return nullValuePropertyMappingStrategy;
    }

    /**
    * accessor
    *
    * @return the {@link GemValue} for {@link MapperConfigGem#mappingInheritanceStrategy}
    */
    public GemValue mappingInheritanceStrategy( ) {
        return mappingInheritanceStrategy;
    }

    /**
    * accessor
    *
    * @return the {@link GemValue} for {@link MapperConfigGem#nullValueCheckStrategy}
    */
    public GemValue nullValueCheckStrategy( ) {
        return nullValueCheckStrategy;
    }

    /**
    * accessor
    *
    * @return the {@link GemValue} for {@link MapperConfigGem#injectionStrategy}
    */
    public GemValue injectionStrategy( ) {
        return injectionStrategy;
    }

    /**
    * accessor
    *
    * @return the {@link GemValue} for {@link MapperConfigGem#disableSubMappingMethodsGeneration}
    */
    public GemValue disableSubMappingMethodsGeneration( ) {
        return disableSubMappingMethodsGeneration;
    }

    /**
    * accessor
    *
    * @return the {@link GemValue} for {@link MapperConfigGem#builder}
    */
    public GemValue builder( ) {
        return builder;
    }

    /**
    * accessor
    *
    * @return the {@link GemValue} for {@link MapperConfigGem#mappingControl}
    */
    public GemValue mappingControl( ) {
        return mappingControl;
    }

    /**
    * accessor
    *
    * @return the {@link GemValue} for {@link MapperConfigGem#unexpectedValueMappingException}
    */
    public GemValue unexpectedValueMappingException( ) {
        return unexpectedValueMappingException;
    }

    @Override
    public AnnotationMirror mirror( ) {
        return mirror;
    }

    @Override
    public boolean isValid( ) {
        return isValid;
    }

    public static MapperConfigGem  instanceOn(Element element) {
        return build( element, new BuilderImpl() );
    }

    public static MapperConfigGem instanceOn(AnnotationMirror mirror ) {
        return build( mirror, new BuilderImpl() );
    }

    public static   T  build(Element element, Builder builder) {
        AnnotationMirror mirror = element.getAnnotationMirrors().stream()
            .filter( a ->  "org.mapstruct.MapperConfig".contentEquals( ( ( TypeElement )a.getAnnotationType().asElement() ).getQualifiedName() ) )
            .findAny()
            .orElse( null );
        return build( mirror, builder );
    }

    public static  T build(AnnotationMirror mirror, Builder builder ) {

        // return fast
        if ( mirror == null || builder == null ) {
            return null;
        }

        // fetch defaults from all defined values in the annotation type
        List enclosed = ElementFilter.methodsIn( mirror.getAnnotationType().asElement().getEnclosedElements() );
        Map defaultValues = new HashMap<>( enclosed.size() );
        enclosed.forEach( e -> defaultValues.put( e.getSimpleName().toString(), e.getDefaultValue() ) );

        // fetch all explicitely set annotation values in the annotation instance
        Map values = new HashMap<>( enclosed.size() );
        mirror.getElementValues().entrySet().forEach( e -> values.put( e.getKey().getSimpleName().toString(), e.getValue() ) );

        // iterate and populate builder
        for ( String methodName : defaultValues.keySet() ) {

            if ( "uses".equals( methodName ) ) {
                builder.setUses( GemValue.createArray( values.get( methodName ), defaultValues.get( methodName ), TypeMirror.class ) );
            }
            else if ( "imports".equals( methodName ) ) {
                builder.setImports( GemValue.createArray( values.get( methodName ), defaultValues.get( methodName ), TypeMirror.class ) );
            }
            else if ( "unmappedSourcePolicy".equals( methodName ) ) {
                builder.setUnmappedsourcepolicy( GemValue.createEnum( values.get( methodName ), defaultValues.get( methodName ) ) );
            }
            else if ( "unmappedTargetPolicy".equals( methodName ) ) {
                builder.setUnmappedtargetpolicy( GemValue.createEnum( values.get( methodName ), defaultValues.get( methodName ) ) );
            }
            else if ( "typeConversionPolicy".equals( methodName ) ) {
                builder.setTypeconversionpolicy( GemValue.createEnum( values.get( methodName ), defaultValues.get( methodName ) ) );
            }
            else if ( "componentModel".equals( methodName ) ) {
                builder.setComponentmodel( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), String.class ) );
            }
            else if ( "implementationName".equals( methodName ) ) {
                builder.setImplementationname( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), String.class ) );
            }
            else if ( "implementationPackage".equals( methodName ) ) {
                builder.setImplementationpackage( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), String.class ) );
            }
            else if ( "collectionMappingStrategy".equals( methodName ) ) {
                builder.setCollectionmappingstrategy( GemValue.createEnum( values.get( methodName ), defaultValues.get( methodName ) ) );
            }
            else if ( "nullValueMappingStrategy".equals( methodName ) ) {
                builder.setNullvaluemappingstrategy( GemValue.createEnum( values.get( methodName ), defaultValues.get( methodName ) ) );
            }
            else if ( "nullValuePropertyMappingStrategy".equals( methodName ) ) {
                builder.setNullvaluepropertymappingstrategy( GemValue.createEnum( values.get( methodName ), defaultValues.get( methodName ) ) );
            }
            else if ( "mappingInheritanceStrategy".equals( methodName ) ) {
                builder.setMappinginheritancestrategy( GemValue.createEnum( values.get( methodName ), defaultValues.get( methodName ) ) );
            }
            else if ( "nullValueCheckStrategy".equals( methodName ) ) {
                builder.setNullvaluecheckstrategy( GemValue.createEnum( values.get( methodName ), defaultValues.get( methodName ) ) );
            }
            else if ( "injectionStrategy".equals( methodName ) ) {
                builder.setInjectionstrategy( GemValue.createEnum( values.get( methodName ), defaultValues.get( methodName ) ) );
            }
            else if ( "disableSubMappingMethodsGeneration".equals( methodName ) ) {
                builder.setDisablesubmappingmethodsgeneration( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), Boolean.class ) );
            }
            else if ( "builder".equals( methodName ) ) {
                builder.setBuilder( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), BuilderGem::instanceOn ) );
            }
            else if ( "mappingControl".equals( methodName ) ) {
                builder.setMappingcontrol( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), TypeMirror.class ) );
            }
            else if ( "unexpectedValueMappingException".equals( methodName ) ) {
                builder.setUnexpectedvaluemappingexception( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), TypeMirror.class ) );
            }
        }
        builder.setMirror( mirror );
        return builder.build();
    }

    /**
     * A builder that can be implemented by the user to define custom logic e.g. in the
     * build method, prior to creating the annotation gem.
     */
    public interface Builder {

       /**
        * Sets the {@link GemValue} for {@link MapperConfigGem#uses}
        *
        * @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
        */
        Builder setUses(GemValue> methodName );

       /**
        * Sets the {@link GemValue} for {@link MapperConfigGem#imports}
        *
        * @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
        */
        Builder setImports(GemValue> methodName );

       /**
        * Sets the {@link GemValue} for {@link MapperConfigGem#unmappedSourcePolicy}
        *
        * @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
        */
        Builder setUnmappedsourcepolicy(GemValue methodName );

       /**
        * Sets the {@link GemValue} for {@link MapperConfigGem#unmappedTargetPolicy}
        *
        * @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
        */
        Builder setUnmappedtargetpolicy(GemValue methodName );

       /**
        * Sets the {@link GemValue} for {@link MapperConfigGem#typeConversionPolicy}
        *
        * @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
        */
        Builder setTypeconversionpolicy(GemValue methodName );

       /**
        * Sets the {@link GemValue} for {@link MapperConfigGem#componentModel}
        *
        * @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
        */
        Builder setComponentmodel(GemValue methodName );

       /**
        * Sets the {@link GemValue} for {@link MapperConfigGem#implementationName}
        *
        * @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
        */
        Builder setImplementationname(GemValue methodName );

       /**
        * Sets the {@link GemValue} for {@link MapperConfigGem#implementationPackage}
        *
        * @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
        */
        Builder setImplementationpackage(GemValue methodName );

       /**
        * Sets the {@link GemValue} for {@link MapperConfigGem#collectionMappingStrategy}
        *
        * @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
        */
        Builder setCollectionmappingstrategy(GemValue methodName );

       /**
        * Sets the {@link GemValue} for {@link MapperConfigGem#nullValueMappingStrategy}
        *
        * @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
        */
        Builder setNullvaluemappingstrategy(GemValue methodName );

       /**
        * Sets the {@link GemValue} for {@link MapperConfigGem#nullValuePropertyMappingStrategy}
        *
        * @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
        */
        Builder setNullvaluepropertymappingstrategy(GemValue methodName );

       /**
        * Sets the {@link GemValue} for {@link MapperConfigGem#mappingInheritanceStrategy}
        *
        * @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
        */
        Builder setMappinginheritancestrategy(GemValue methodName );

       /**
        * Sets the {@link GemValue} for {@link MapperConfigGem#nullValueCheckStrategy}
        *
        * @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
        */
        Builder setNullvaluecheckstrategy(GemValue methodName );

       /**
        * Sets the {@link GemValue} for {@link MapperConfigGem#injectionStrategy}
        *
        * @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
        */
        Builder setInjectionstrategy(GemValue methodName );

       /**
        * Sets the {@link GemValue} for {@link MapperConfigGem#disableSubMappingMethodsGeneration}
        *
        * @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
        */
        Builder setDisablesubmappingmethodsgeneration(GemValue methodName );

       /**
        * Sets the {@link GemValue} for {@link MapperConfigGem#builder}
        *
        * @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
        */
        Builder setBuilder(GemValue methodName );

       /**
        * Sets the {@link GemValue} for {@link MapperConfigGem#mappingControl}
        *
        * @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
        */
        Builder setMappingcontrol(GemValue methodName );

       /**
        * Sets the {@link GemValue} for {@link MapperConfigGem#unexpectedValueMappingException}
        *
        * @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
        */
        Builder setUnexpectedvaluemappingexception(GemValue methodName );

        /**
         * Sets the annotation mirror
         *
         * @param mirror the mirror which this gem represents
         *
         * @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
         */
          Builder setMirror( AnnotationMirror mirror );

        /**
         * The build method can be overriden in a custom custom implementation, which allows
         * the user to define his own custom validation on the annotation.
         *
         * @return the representation of the annotation
         */
        T build();
    }

    private static class BuilderImpl implements Builder {

        private GemValue> uses;
        private GemValue> imports;
        private GemValue unmappedSourcePolicy;
        private GemValue unmappedTargetPolicy;
        private GemValue typeConversionPolicy;
        private GemValue componentModel;
        private GemValue implementationName;
        private GemValue implementationPackage;
        private GemValue collectionMappingStrategy;
        private GemValue nullValueMappingStrategy;
        private GemValue nullValuePropertyMappingStrategy;
        private GemValue mappingInheritanceStrategy;
        private GemValue nullValueCheckStrategy;
        private GemValue injectionStrategy;
        private GemValue disableSubMappingMethodsGeneration;
        private GemValue builder;
        private GemValue mappingControl;
        private GemValue unexpectedValueMappingException;
        private AnnotationMirror mirror;

        public Builder setUses(GemValue> uses ) {
            this.uses = uses;
            return this;
        }

        public Builder setImports(GemValue> imports ) {
            this.imports = imports;
            return this;
        }

        public Builder setUnmappedsourcepolicy(GemValue unmappedSourcePolicy ) {
            this.unmappedSourcePolicy = unmappedSourcePolicy;
            return this;
        }

        public Builder setUnmappedtargetpolicy(GemValue unmappedTargetPolicy ) {
            this.unmappedTargetPolicy = unmappedTargetPolicy;
            return this;
        }

        public Builder setTypeconversionpolicy(GemValue typeConversionPolicy ) {
            this.typeConversionPolicy = typeConversionPolicy;
            return this;
        }

        public Builder setComponentmodel(GemValue componentModel ) {
            this.componentModel = componentModel;
            return this;
        }

        public Builder setImplementationname(GemValue implementationName ) {
            this.implementationName = implementationName;
            return this;
        }

        public Builder setImplementationpackage(GemValue implementationPackage ) {
            this.implementationPackage = implementationPackage;
            return this;
        }

        public Builder setCollectionmappingstrategy(GemValue collectionMappingStrategy ) {
            this.collectionMappingStrategy = collectionMappingStrategy;
            return this;
        }

        public Builder setNullvaluemappingstrategy(GemValue nullValueMappingStrategy ) {
            this.nullValueMappingStrategy = nullValueMappingStrategy;
            return this;
        }

        public Builder setNullvaluepropertymappingstrategy(GemValue nullValuePropertyMappingStrategy ) {
            this.nullValuePropertyMappingStrategy = nullValuePropertyMappingStrategy;
            return this;
        }

        public Builder setMappinginheritancestrategy(GemValue mappingInheritanceStrategy ) {
            this.mappingInheritanceStrategy = mappingInheritanceStrategy;
            return this;
        }

        public Builder setNullvaluecheckstrategy(GemValue nullValueCheckStrategy ) {
            this.nullValueCheckStrategy = nullValueCheckStrategy;
            return this;
        }

        public Builder setInjectionstrategy(GemValue injectionStrategy ) {
            this.injectionStrategy = injectionStrategy;
            return this;
        }

        public Builder setDisablesubmappingmethodsgeneration(GemValue disableSubMappingMethodsGeneration ) {
            this.disableSubMappingMethodsGeneration = disableSubMappingMethodsGeneration;
            return this;
        }

        public Builder setBuilder(GemValue builder ) {
            this.builder = builder;
            return this;
        }

        public Builder setMappingcontrol(GemValue mappingControl ) {
            this.mappingControl = mappingControl;
            return this;
        }

        public Builder setUnexpectedvaluemappingexception(GemValue unexpectedValueMappingException ) {
            this.unexpectedValueMappingException = unexpectedValueMappingException;
            return this;
        }

        public Builder  setMirror( AnnotationMirror mirror ) {
            this.mirror = mirror;
            return this;
        }

        public MapperConfigGem build() {
            return new MapperConfigGem( this );
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy