Class MappingProcessor
- java.lang.Object
-
- javax.annotation.processing.AbstractProcessor
-
- org.mapstruct.ap.MappingProcessor
-
- All Implemented Interfaces:
javax.annotation.processing.Processor
@SupportedAnnotationTypes("org.mapstruct.Mapper") @SupportedOptions({"mapstruct.suppressGeneratorTimestamp","mapstruct.suppressGeneratorVersionInfoComment","mapstruct.unmappedTargetPolicy","mapstruct.unmappedSourcePolicy","mapstruct.defaultComponentModel","mapstruct.defaultInjectionStrategy","mapstruct.disableBuilders","mapstruct.verbose"}) public class MappingProcessor extends javax.annotation.processing.AbstractProcessorA JSR 269 annotationProcessorwhich generates the implementations for mapper interfaces (interfaces annotated with@Mapper).Implementation notes:
The generation happens by incrementally building up a model representation of each mapper to be generated (a
Mapperobject), which is then written into the resulting Java source file.The model instantiation and processing happens in several phases/passes by applying a sequence of
ModelElementProcessors. The processors to apply are retrieved using the Java service loader mechanism and are processed in order of theirpriority. The general processing flow is this:- retrieve mapping methods
- create the
Mappermodel - perform enrichments and modifications (e.g. add annotations for dependency injection)
- if no error occurred, write out the model into Java source files
For reading annotation attributes, gems as generated with help of Gem Tools. These gems allow comfortable access to annotations and their attributes without depending on their class objects.
The creation of Java source files is done using the FreeMarker template engine. Each node of the mapper model has a corresponding FreeMarker template file which provides the Java representation of that element and can include sub-elements via a custom FreeMarker directive. That way writing out a root node of the model (
Mapper) will recursively include all contained sub-elements (such as its methods, their property mappings etc.).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classMappingProcessor.DeferredMapperprivate static classMappingProcessor.ProcessorComparator
-
Field Summary
Fields Modifier and Type Field Description protected static java.lang.StringALWAYS_GENERATE_SERVICE_FILEprivate AnnotationProcessorContextannotationProcessorContextprivate static booleanANNOTATIONS_CLAIMED_EXCLUSIVELYWhether this processor claims all processed annotations exclusively or not.protected static java.lang.StringDEFAULT_COMPONENT_MODELprotected static java.lang.StringDEFAULT_INJECTION_STRATEGYprivate java.util.Set<MappingProcessor.DeferredMapper>deferredMappersAny mappers for which an implementation cannot be generated in the current round because they have source/target types with incomplete hierarchies (as super-types are to be generated by other processors).protected static java.lang.StringDISABLE_BUILDERSprivate Optionsoptionsprotected static java.lang.StringSUPPRESS_GENERATOR_TIMESTAMPprotected static java.lang.StringSUPPRESS_GENERATOR_VERSION_INFO_COMMENTprotected static java.lang.StringUNMAPPED_SOURCE_POLICYprotected static java.lang.StringUNMAPPED_TARGET_POLICYprotected static java.lang.StringVERBOSE
-
Constructor Summary
Constructors Constructor Description MappingProcessor()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private javax.lang.model.element.TypeElementasTypeElement(javax.lang.model.element.Element element)private OptionscreateOptions()private java.util.Set<javax.lang.model.element.TypeElement>getAndResetDeferredMappers()Gets fresh copies of all mappers deferred from previous rounds (the originals may contain references to erroneous source/target type elements).private java.util.Map<java.lang.String,java.lang.String>getDeclaredTypesNotToBeImported(javax.lang.model.element.TypeElement element)private java.util.Set<javax.lang.model.element.TypeElement>getMappers(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnvironment)private java.lang.Iterable<ModelElementProcessor<?,?>>getProcessors()Retrieves all model element processors, ordered by their priority value (with the method retrieval processor having the lowest priority value (1) and the code generation processor the highest priority value.javax.lang.model.SourceVersiongetSupportedSourceVersion()private voidhandleUncaughtError(javax.lang.model.element.Element element, java.lang.Throwable thrown)voidinit(javax.annotation.processing.ProcessingEnvironment processingEnv)booleanprocess(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnvironment)private <P,R>
Rprocess(ModelElementProcessor.ProcessorContext context, ModelElementProcessor<P,R> processor, javax.lang.model.element.TypeElement mapperTypeElement, java.lang.Object modelElement)private voidprocessMapperElements(java.util.Set<javax.lang.model.element.TypeElement> mapperElements, RoundContext roundContext)private voidprocessMapperTypeElement(ModelElementProcessor.ProcessorContext context, javax.lang.model.element.TypeElement mapperTypeElement)Applies all registeredModelElementProcessors to the given mapper type.
-
-
-
Field Detail
-
ANNOTATIONS_CLAIMED_EXCLUSIVELY
private static final boolean ANNOTATIONS_CLAIMED_EXCLUSIVELY
Whether this processor claims all processed annotations exclusively or not.- See Also:
- Constant Field Values
-
SUPPRESS_GENERATOR_TIMESTAMP
protected static final java.lang.String SUPPRESS_GENERATOR_TIMESTAMP
- See Also:
- Constant Field Values
-
SUPPRESS_GENERATOR_VERSION_INFO_COMMENT
protected static final java.lang.String SUPPRESS_GENERATOR_VERSION_INFO_COMMENT
- See Also:
- Constant Field Values
-
UNMAPPED_TARGET_POLICY
protected static final java.lang.String UNMAPPED_TARGET_POLICY
- See Also:
- Constant Field Values
-
UNMAPPED_SOURCE_POLICY
protected static final java.lang.String UNMAPPED_SOURCE_POLICY
- See Also:
- Constant Field Values
-
DEFAULT_COMPONENT_MODEL
protected static final java.lang.String DEFAULT_COMPONENT_MODEL
- See Also:
- Constant Field Values
-
DEFAULT_INJECTION_STRATEGY
protected static final java.lang.String DEFAULT_INJECTION_STRATEGY
- See Also:
- Constant Field Values
-
ALWAYS_GENERATE_SERVICE_FILE
protected static final java.lang.String ALWAYS_GENERATE_SERVICE_FILE
- See Also:
- Constant Field Values
-
DISABLE_BUILDERS
protected static final java.lang.String DISABLE_BUILDERS
- See Also:
- Constant Field Values
-
VERBOSE
protected static final java.lang.String VERBOSE
- See Also:
- Constant Field Values
-
options
private Options options
-
annotationProcessorContext
private AnnotationProcessorContext annotationProcessorContext
-
deferredMappers
private java.util.Set<MappingProcessor.DeferredMapper> deferredMappers
Any mappers for which an implementation cannot be generated in the current round because they have source/target types with incomplete hierarchies (as super-types are to be generated by other processors). They will be processed in subsequent rounds.If the hierarchy of a mapper's source/target types is never completed (i.e. the missing super-types are not generated by other processors), this mapper will not be generated; That's fine, the compiler will raise an error due to the inconsistent Java types used as source or target anyways.
-
-
Method Detail
-
init
public void init(javax.annotation.processing.ProcessingEnvironment processingEnv)
- Specified by:
initin interfacejavax.annotation.processing.Processor- Overrides:
initin classjavax.annotation.processing.AbstractProcessor
-
createOptions
private Options createOptions()
-
getSupportedSourceVersion
public javax.lang.model.SourceVersion getSupportedSourceVersion()
- Specified by:
getSupportedSourceVersionin interfacejavax.annotation.processing.Processor- Overrides:
getSupportedSourceVersionin classjavax.annotation.processing.AbstractProcessor
-
process
public boolean process(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnvironment)- Specified by:
processin interfacejavax.annotation.processing.Processor- Specified by:
processin classjavax.annotation.processing.AbstractProcessor
-
getAndResetDeferredMappers
private java.util.Set<javax.lang.model.element.TypeElement> getAndResetDeferredMappers()
Gets fresh copies of all mappers deferred from previous rounds (the originals may contain references to erroneous source/target type elements).
-
getMappers
private java.util.Set<javax.lang.model.element.TypeElement> getMappers(java.util.Set<? extends javax.lang.model.element.TypeElement> annotations, javax.annotation.processing.RoundEnvironment roundEnvironment)
-
processMapperElements
private void processMapperElements(java.util.Set<javax.lang.model.element.TypeElement> mapperElements, RoundContext roundContext)
-
getDeclaredTypesNotToBeImported
private java.util.Map<java.lang.String,java.lang.String> getDeclaredTypesNotToBeImported(javax.lang.model.element.TypeElement element)
-
handleUncaughtError
private void handleUncaughtError(javax.lang.model.element.Element element, java.lang.Throwable thrown)
-
processMapperTypeElement
private void processMapperTypeElement(ModelElementProcessor.ProcessorContext context, javax.lang.model.element.TypeElement mapperTypeElement)
Applies all registeredModelElementProcessors to the given mapper type.- Parameters:
context- The processor context.mapperTypeElement- The mapper type element.
-
process
private <P,R> R process(ModelElementProcessor.ProcessorContext context, ModelElementProcessor<P,R> processor, javax.lang.model.element.TypeElement mapperTypeElement, java.lang.Object modelElement)
-
getProcessors
private java.lang.Iterable<ModelElementProcessor<?,?>> getProcessors()
Retrieves all model element processors, ordered by their priority value (with the method retrieval processor having the lowest priority value (1) and the code generation processor the highest priority value.- Returns:
- A list with all model element processors.
-
asTypeElement
private javax.lang.model.element.TypeElement asTypeElement(javax.lang.model.element.Element element)
-
-