MCPcopy
hub / github.com/mapstruct/mapstruct

github.com/mapstruct/mapstruct @1.6.3 sqlite

repository ↗ · DeepWiki ↗ · release 1.6.3 ↗
18,799 symbols 58,115 edges 3,751 files 3,035 documented · 16%
README

MapStruct - Java bean mappings, the easy way!

Latest Stable Version Latest Version License

Build Status Coverage Status

What is MapStruct?

MapStruct is a Java annotation processor designed to generate type-safe and high-performance mappers for Java bean classes, including support for Java 16+ records. By automating the creation of mappings, MapStruct eliminates the need for tedious and error-prone manual coding. The generator provides sensible defaults and built-in type conversions, allowing it to handle standard mappings effortlessly, while also offering flexibility for custom configurations or specialized mapping behaviors. With seamless integration into modern Java projects, MapStruct can map between conventional beans, records, and even complex hierarchies, making it an adaptable tool for diverse Java applications.

Compared to mapping frameworks working at runtime, MapStruct offers the following advantages:

  • Fast execution by using plain method invocations instead of reflection
  • Compile-time type safety. Only objects and attributes mapping to each other can be mapped, so there's no accidental mapping of an order entity into a customer DTO, etc.
  • Self-contained code—no runtime dependencies
  • Clear error reports at build time if:
  • mappings are incomplete (not all target properties are mapped)
  • mappings are incorrect (cannot find a proper mapping method or type conversion)
  • Easily debuggable mapping code (or editable by hand—e.g. in case of a bug in the generator)

To create a mapping between two types, declare a mapper interface like this:

@Mapper
public interface CarMapper {

    CarMapper INSTANCE = Mappers.getMapper( CarMapper.class );

    @Mapping(target = "seatCount", source = "numberOfSeats")
    CarDto carToCarDto(Car car);
}

At compile time MapStruct will generate an implementation of this interface. The generated implementation uses plain Java method invocations for mapping between source and target objects, i.e. no reflection is involved. By default, properties are mapped if they have the same name in source and target, but you can control this and many other aspects using @Mapping and a handful of other annotations.

Requirements

MapStruct requires Java 1.8 or later.

Using MapStruct

MapStruct works in command line builds (plain javac, via Maven, Gradle, Ant, etc.) and IDEs.

For Eclipse, a dedicated plug-in is in development (see https://github.com/mapstruct/mapstruct-eclipse). It goes beyond what's possible with an annotation processor, providing content assist for annotation attributes, quick fixes and more.

For IntelliJ the plug-in is available within the IntelliJ marketplace (see https://plugins.jetbrains.com/plugin/10036-mapstruct-support).

Maven

For Maven-based projects, add the following to your POM file in order to use MapStruct (the dependencies are available at Maven Central):

...
<properties>
    <org.mapstruct.version>1.6.2</org.mapstruct.version>
</properties>
...
<dependencies>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>${org.mapstruct.version}</version>
    </dependency>
</dependencies>
...
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.13.0</version>
            <configuration>


17


                <target>17</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${org.mapstruct.version}</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>
...

Gradle

For Gradle, you need something along the following lines:

plugins {
    ...
    id "com.diffplug.eclipse.apt" version "3.26.0" // Only for Eclipse
}

dependencies {
    ...
    implementation 'org.mapstruct:mapstruct:1.6.2'

    annotationProcessor 'org.mapstruct:mapstruct-processor:1.6.2'
    testAnnotationProcessor 'org.mapstruct:mapstruct-processor:1.6.2' // if you are using mapstruct in test code
}
...

If you don't work with a dependency management tool, you can obtain a distribution bundle from Releases page.

Documentation and getting help

To learn more about MapStruct, refer to the project homepage. The reference documentation covers all provided functionality in detail. If you need help please ask it in the Discussions.

Building from Source

MapStruct uses Maven for its build. Java 11 is required for building MapStruct from source. To build the complete project, run

./mvnw clean install

from the root of the project directory. To skip the distribution module, run

./mvnw clean install -DskipDistribution=true

Importing into IDE

MapStruct uses the gem annotation processor to generate mapping gems for its own annotations. Therefore, for seamless integration within an IDE annotation processing needs to be enabled.

IntelliJ

Make sure that you have at least IntelliJ 2018.2.x (needed since support for annotationProcessors from the maven-compiler-plugin is from that version). Enable annotation processing in IntelliJ (Build, Execution, Deployment -> Compiler -> Annotation Processors)

Eclipse

Make sure that you have the m2e_apt plugin installed.

Links

Licensing

MapStruct is licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0.

Extension points exported contracts — how you extend this code

ArtistToChartEntry (Interface)
@author Sjaak Derksen [67 implementers]
processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/ArtistToChartEntry.java
Issue1121Mapper (Interface)
@author Sjaak Derksen [65 implementers]
integrationtest/src/test/resources/externalbeanjar/mapper/src/main/java/org/mapstruct/itest/externalbeanjar/Issue1121Mapper.java
Foo (Interface)
(no doc) [1 implementers]
core/src/test/java/org/mapstruct/test/model/Foo.java
ArtistToChartEntryGetter (Interface)
@author Sjaak Derksen [65 implementers]
processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/ArtistToChartEntryGetter.java
CareProviderMapper (Interface)
@author Filip Hrisafov [65 implementers]
integrationtest/src/test/resources/recordsTest/src/main/java/org/mapstruct/itest/records/nested/CareProviderMapper.java
Foo (Interface)
(no doc)
core/src/test/java/org/mapstruct/test/model/SomeClass.java
ArtistToChartEntryAdder (Interface)
@author Sjaak Derksen [65 implementers]
processor/src/test/java/org/mapstruct/ap/test/nestedsourceproperties/ArtistToChartEntryAdder.java
Person (Interface)
(no doc) [13 implementers]
integrationtest/src/test/resources/immutablesBuilderTest/mapper/src/main/java/org/mapstruct/itest/immutables/Person.java

Core symbols most depended-on inside this repo

getMapper
called by 819
core/src/main/java/org/mapstruct/factory/Mappers.java
get
called by 426
processor/src/main/java/org/mapstruct/ap/internal/writer/Writable.java
getType
called by 324
processor/src/main/java/org/mapstruct/ap/internal/model/common/Assignment.java
getName
called by 291
processor/src/test/java/org/mapstruct/ap/test/bugs/_846/Mapper846.java
getValue
called by 202
processor/src/test/java/org/mapstruct/ap/test/bugs/_1482/ValueWrapper.java
toString
called by 182
processor/src/test/java/org/mapstruct/ap/test/annotatewith/ErroneousMapperWithClassOnMethod.java
printMessage
called by 160
processor/src/main/java/org/mapstruct/ap/internal/util/FormattingMessager.java
isEmpty
called by 152
processor/src/main/java/org/mapstruct/ap/internal/util/Strings.java

Shape

Method 14,339
Class 3,286
Interface 1,081
Enum 93

Languages

Java100%

Modules by API surface

processor/src/main/java/org/mapstruct/ap/internal/model/common/Type.java111 symbols
processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java74 symbols
processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java71 symbols
processor/src/main/java/org/mapstruct/ap/internal/model/source/SourceMethod.java70 symbols
processor/src/main/java/org/mapstruct/ap/internal/util/NativeTypes.java53 symbols
processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java51 symbols
processor/src/main/java/org/mapstruct/ap/internal/processor/MethodRetrievalProcessor.java49 symbols
processor/src/main/java/org/mapstruct/ap/internal/model/MethodReference.java44 symbols
processor/src/test/java/org/mapstruct/ap/internal/model/source/SelectionParametersTest.java42 symbols
processor/src/main/java/org/mapstruct/ap/internal/model/source/MappingOptions.java42 symbols
processor/src/main/java/org/mapstruct/ap/internal/model/ValueMappingMethod.java40 symbols
processor/src/main/java/org/mapstruct/ap/internal/model/ForgedMethod.java38 symbols

Dependencies from manifests, versioned

${project.groupId}:mapstruct-build-config
${project.groupId}:mapstruct-processor
${project.groupId}:namingStrategyTest-strategy1.0.0 · 1×
ch.qos.cal10n:cal10n-api0.7.4 · 1×
com.google.auto.value:auto-value1.5 · 1×
com.google.guava:guava32.0.0-jre · 1×
com.google.protobuf:protobuf-java
com.puppycrawl.tools:checkstyle
com.sun.xml.bind:jaxb-impl3.0.2 · 1×
commons-logging:commons-logging1.1.3 · 1×

For agents

$ claude mcp add mapstruct \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact