Skip to content

Commit 22c337a

Browse files
committed
mapstruct#782 Add custom SPI implementations for Immutables in the integration tests
1 parent 252af70 commit 22c337a

13 files changed

Lines changed: 225 additions & 9 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<artifactId>immutablesIntegrationTest</artifactId>
9+
<groupId>org.mapstruct</groupId>
10+
<version>1.0.0</version>
11+
</parent>
12+
13+
<artifactId>itest-immutables-mapping-extras</artifactId>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.mapstruct</groupId>
18+
<artifactId>mapstruct-processor</artifactId>
19+
<version>${mapstruct.version}</version>
20+
<scope>provided</scope>
21+
</dependency>
22+
</dependencies>
23+
24+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright 2012-2017 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.itest.immutables.extras;
20+
21+
import javax.lang.model.element.ExecutableElement;
22+
23+
import org.mapstruct.ap.spi.DefaultAccessorNamingStrategy;
24+
25+
/**
26+
* @author Filip Hrisafov
27+
*/
28+
public class ImmutablesAccessorNamingStrategy extends DefaultAccessorNamingStrategy {
29+
30+
@Override
31+
protected boolean isBuilderSetter(ExecutableElement method) {
32+
return super.isBuilderSetter( method ) && !method.getSimpleName().toString().equals( "from" );
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* Copyright 2012-2017 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.itest.immutables.extras;
20+
21+
import java.util.regex.Pattern;
22+
import javax.lang.model.element.AnnotationMirror;
23+
import javax.lang.model.element.Element;
24+
import javax.lang.model.element.ElementKind;
25+
import javax.lang.model.element.Name;
26+
import javax.lang.model.element.PackageElement;
27+
import javax.lang.model.element.TypeElement;
28+
import javax.lang.model.util.Elements;
29+
import javax.lang.model.util.Types;
30+
31+
import org.mapstruct.ap.spi.BuilderInfo;
32+
import org.mapstruct.ap.spi.DefaultBuilderProvider;
33+
import org.mapstruct.ap.spi.TypeHierarchyErroneousException;
34+
35+
/**
36+
* @author Filip Hrisafov
37+
*/
38+
public class ImmutablesBuilderProvider extends DefaultBuilderProvider {
39+
40+
private static final Pattern JAVA_JAVAX_PACKAGE = Pattern.compile( "^javax?\\..*" );
41+
42+
@Override
43+
protected BuilderInfo findBuilderInfo(TypeElement typeElement, Elements elements, Types types) {
44+
Name name = typeElement.getQualifiedName();
45+
if ( name.length() == 0 || JAVA_JAVAX_PACKAGE.matcher( name ).matches() ) {
46+
return null;
47+
}
48+
TypeElement immutableAnnotation = elements.getTypeElement( "org.immutables.value.Value.Immutable" );
49+
if ( immutableAnnotation != null ) {
50+
BuilderInfo info = findBuilderInfoForImmutables(
51+
typeElement,
52+
immutableAnnotation,
53+
elements,
54+
types
55+
);
56+
if ( info != null ) {
57+
return info;
58+
}
59+
}
60+
61+
return super.findBuilderInfo( typeElement, elements, types );
62+
}
63+
64+
protected BuilderInfo findBuilderInfoForImmutables(TypeElement typeElement,
65+
TypeElement immutableAnnotation, Elements elements, Types types) {
66+
for ( AnnotationMirror annotationMirror : elements.getAllAnnotationMirrors( typeElement ) ) {
67+
if ( types.isSameType( annotationMirror.getAnnotationType(), immutableAnnotation.asType() ) ) {
68+
TypeElement immutableElement = asImmutableElement( typeElement, elements );
69+
if ( immutableElement != null ) {
70+
return super.findBuilderInfo( immutableElement, elements, types );
71+
}
72+
else {
73+
throw new TypeHierarchyErroneousException( typeElement );
74+
}
75+
}
76+
}
77+
return null;
78+
}
79+
80+
private TypeElement asImmutableElement(TypeElement typeElement, Elements elements) {
81+
Element enclosingElement = typeElement.getEnclosingElement();
82+
StringBuilder builderQualifiedName = new StringBuilder( typeElement.getQualifiedName().length() + 17 );
83+
if ( enclosingElement.getKind() == ElementKind.PACKAGE ) {
84+
builderQualifiedName.append( ( (PackageElement) enclosingElement ).getQualifiedName().toString() );
85+
}
86+
else {
87+
builderQualifiedName.append( ( (TypeElement) enclosingElement ).getQualifiedName().toString() );
88+
}
89+
90+
if ( builderQualifiedName.length() > 0 ) {
91+
builderQualifiedName.append( "." );
92+
}
93+
94+
builderQualifiedName.append( "Immutable" ).append( typeElement.getSimpleName() );
95+
return elements.getTypeElement( builderQualifiedName );
96+
}
97+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<artifactId>immutablesIntegrationTest</artifactId>
9+
<groupId>org.mapstruct</groupId>
10+
<version>1.0.0</version>
11+
</parent>
12+
13+
<artifactId>itest-immutables-mapper</artifactId>
14+
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.immutables</groupId>
19+
<artifactId>value</artifactId>
20+
<scope>provided</scope>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.mapstruct</groupId>
24+
<artifactId>itest-immutables-mapping-extras</artifactId>
25+
<version>1.0.0</version>
26+
</dependency>
27+
</dependencies>
28+
</project>

integrationtest/src/test/resources/immutablesBuilderTest/src/main/java/org/mapstruct/itest/immutables/Address.java renamed to integrationtest/src/test/resources/immutablesBuilderTest/mapper/src/main/java/org/mapstruct/itest/immutables/Address.java

File renamed without changes.

integrationtest/src/test/resources/immutablesBuilderTest/src/main/java/org/mapstruct/itest/immutables/AddressDto.java renamed to integrationtest/src/test/resources/immutablesBuilderTest/mapper/src/main/java/org/mapstruct/itest/immutables/AddressDto.java

File renamed without changes.

integrationtest/src/test/resources/immutablesBuilderTest/src/main/java/org/mapstruct/itest/immutables/Person.java renamed to integrationtest/src/test/resources/immutablesBuilderTest/mapper/src/main/java/org/mapstruct/itest/immutables/Person.java

File renamed without changes.

integrationtest/src/test/resources/immutablesBuilderTest/src/main/java/org/mapstruct/itest/immutables/PersonDto.java renamed to integrationtest/src/test/resources/immutablesBuilderTest/mapper/src/main/java/org/mapstruct/itest/immutables/PersonDto.java

File renamed without changes.

integrationtest/src/test/resources/immutablesBuilderTest/src/main/java/org/mapstruct/itest/immutables/PersonMapper.java renamed to integrationtest/src/test/resources/immutablesBuilderTest/mapper/src/main/java/org/mapstruct/itest/immutables/PersonMapper.java

File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
2+
# and/or other contributors as indicated by the @authors tag. See the
3+
# copyright.txt file in the distribution for a full listing of all
4+
# contributors.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
org.mapstruct.itest.immutables.extras.ImmutablesAccessorNamingStrategy

0 commit comments

Comments
 (0)