Skip to content

Commit c494cc3

Browse files
authored
Use of target type annotations as processing instruction (#43)
1 parent 2001c19 commit c494cc3

14 files changed

Lines changed: 633 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Currently, the following examples exist:
2121
* _mapstruct-jpa-parent-child_: Example on how to use @Context in relation to parent / child relations in JPA)
2222
* _mapstruct-suppress-unmapped_: Shows how mapping to target properties can be ignored without warning by default in a mixed scenario. However bean property mappings that have the same name will still be applied.
2323
* _mapstruct-lookup-entity-with-composed-key_: Shows how an object with composite key can be read from the database in a mapping method.
24+
* _mapstruct-metadata-annotations_: Demonstrates how to read annotations and use them as mapping instruction.
2425

2526
## License
2627

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
5+
and/or other contributors as indicated by the @authors tag. See the
6+
copyright.txt file in the distribution for a full listing of all
7+
contributors.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
http://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
21+
-->
22+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
23+
<modelVersion>4.0.0</modelVersion>
24+
<groupId>org.mapstruct.examples.metadata</groupId>
25+
<artifactId>mapstruct-examples-metadata-with-annotations</artifactId>
26+
<version>1.0.0</version>
27+
28+
<properties>
29+
<org.mapstruct.version>1.3.0.Beta1</org.mapstruct.version>
30+
</properties>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>org.mapstruct</groupId>
35+
<artifactId>mapstruct</artifactId>
36+
<version>${org.mapstruct.version}</version>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
<version>4.12</version>
43+
<scope>test</scope>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.assertj</groupId>
48+
<artifactId>assertj-core</artifactId>
49+
<version>3.9.0</version>
50+
<scope>test</scope>
51+
</dependency>
52+
</dependencies>
53+
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-compiler-plugin</artifactId>
59+
<version>3.6.2</version>
60+
<configuration>
61+
<source>1.8</source>
62+
<target>1.8</target>
63+
<annotationProcessorPaths>
64+
<path>
65+
<groupId>org.mapstruct</groupId>
66+
<artifactId>mapstruct-processor</artifactId>
67+
<version>${org.mapstruct.version}</version>
68+
</path>
69+
</annotationProcessorPaths>
70+
</configuration>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
75+
76+
</project>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Copyright 2012-2018 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.example.metadata.dto;
20+
21+
/**
22+
*
23+
* @author Sjaak Derksen
24+
*/
25+
public class LegalEntity {
26+
27+
private String name;
28+
29+
private String address;
30+
31+
private String id;
32+
33+
public String getName() {
34+
return name;
35+
}
36+
37+
public void setName(String name) {
38+
this.name = name;
39+
}
40+
41+
public String getAddress() {
42+
return address;
43+
}
44+
45+
public void setAddress(String address) {
46+
this.address = address;
47+
}
48+
49+
public String getId() {
50+
return id;
51+
}
52+
53+
public void setId(String id) {
54+
this.id = id;
55+
}
56+
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Copyright 2012-2018 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.example.metadata.dto;
20+
21+
/**
22+
*
23+
* @author Sjaak Derksen
24+
*/
25+
public class TaxRecord {
26+
27+
private int number;
28+
29+
private int year;
30+
31+
private LegalEntity legalEntity;
32+
33+
public int getNumber() {
34+
return number;
35+
}
36+
37+
public void setNumber(int number) {
38+
this.number = number;
39+
}
40+
41+
public int getYear() {
42+
return year;
43+
}
44+
45+
public void setYear(int year) {
46+
this.year = year;
47+
}
48+
49+
public LegalEntity getLegalEntity() {
50+
return legalEntity;
51+
}
52+
53+
public void setLegalEntity(LegalEntity legalEntity) {
54+
this.legalEntity = legalEntity;
55+
}
56+
57+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright 2012-2018 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.example.metadata.entities;
20+
21+
22+
/**
23+
*
24+
* @author Sjaak Derksen
25+
*/
26+
public class LegalEntityPE {
27+
28+
private String name;
29+
30+
private String address;
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
public String getAddress() {
41+
return address;
42+
}
43+
44+
public void setAddress(String address) {
45+
this.address = address;
46+
}
47+
48+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright 2012-2018 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.example.metadata.entities;
20+
21+
/**
22+
*
23+
* @author Sjaak Derksen
24+
*/
25+
public class OrganisationPE extends LegalEntityPE {
26+
27+
private String chamberOfCommerceNumber;
28+
29+
public String getChamberOfCommerceNumber() {
30+
return chamberOfCommerceNumber;
31+
}
32+
33+
public void setChamberOfCommerceNumber(String chamberOfCommerceNumber) {
34+
this.chamberOfCommerceNumber = chamberOfCommerceNumber;
35+
}
36+
37+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright 2012-2018 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.example.metadata.entities;
20+
21+
22+
/**
23+
*
24+
* @author Sjaak Derksen
25+
*/
26+
public class PersonPE extends LegalEntityPE {
27+
28+
private String socialSecurityNumber;
29+
30+
public String getSocialSecurityNumber() {
31+
return socialSecurityNumber;
32+
}
33+
34+
public void setSocialSecurityNumber(String socialSecurityNumber) {
35+
this.socialSecurityNumber = socialSecurityNumber;
36+
}
37+
38+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Copyright 2012-2018 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.example.metadata.entities;
20+
21+
import org.mapstruct.example.metadata.mapper.annotations.Treatment;
22+
23+
/**
24+
*
25+
* @author Sjaak Derksen
26+
*/
27+
public class TaxRecordPE {
28+
29+
private int number;
30+
31+
private int year;
32+
33+
private LegalEntityPE legalEntity;
34+
35+
public int getNumber() {
36+
return number;
37+
}
38+
39+
public void setNumber(int number) {
40+
this.number = number;
41+
}
42+
43+
public int getYear() {
44+
return year;
45+
}
46+
47+
public void setYear(int year) {
48+
this.year = year;
49+
}
50+
51+
public LegalEntityPE getLegalEntity() {
52+
return legalEntity;
53+
}
54+
55+
@Treatment( formatAs = "organisation" )
56+
public void setLegalEntity(LegalEntityPE legalEntity) {
57+
this.legalEntity = legalEntity;
58+
}
59+
60+
}

0 commit comments

Comments
 (0)