Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#782 Add custom SPI implementations for Immutables in the integration…
… tests
- Loading branch information
Showing
13 changed files
with
225 additions
and
9 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
integrationtest/src/test/resources/immutablesBuilderTest/extras/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<artifactId>immutablesIntegrationTest</artifactId> | ||
<groupId>org.mapstruct</groupId> | ||
<version>1.0.0</version> | ||
</parent> | ||
|
||
<artifactId>itest-immutables-mapping-extras</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.mapstruct</groupId> | ||
<artifactId>mapstruct-processor</artifactId> | ||
<version>${mapstruct.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
34 changes: 34 additions & 0 deletions
34
...src/main/java/org/mapstruct/itest/immutables/extras/ImmutablesAccessorNamingStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/) | ||
* and/or other contributors as indicated by the @authors tag. See the | ||
* copyright.txt file in the distribution for a full listing of all | ||
* contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.mapstruct.itest.immutables.extras; | ||
|
||
import javax.lang.model.element.ExecutableElement; | ||
|
||
import org.mapstruct.ap.spi.DefaultAccessorNamingStrategy; | ||
|
||
/** | ||
* @author Filip Hrisafov | ||
*/ | ||
public class ImmutablesAccessorNamingStrategy extends DefaultAccessorNamingStrategy { | ||
|
||
@Override | ||
protected boolean isBuilderSetter(ExecutableElement method) { | ||
return super.isBuilderSetter( method ) && !method.getSimpleName().toString().equals( "from" ); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
...extras/src/main/java/org/mapstruct/itest/immutables/extras/ImmutablesBuilderProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/** | ||
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/) | ||
* and/or other contributors as indicated by the @authors tag. See the | ||
* copyright.txt file in the distribution for a full listing of all | ||
* contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.mapstruct.itest.immutables.extras; | ||
|
||
import java.util.regex.Pattern; | ||
import javax.lang.model.element.AnnotationMirror; | ||
import javax.lang.model.element.Element; | ||
import javax.lang.model.element.ElementKind; | ||
import javax.lang.model.element.Name; | ||
import javax.lang.model.element.PackageElement; | ||
import javax.lang.model.element.TypeElement; | ||
import javax.lang.model.util.Elements; | ||
import javax.lang.model.util.Types; | ||
|
||
import org.mapstruct.ap.spi.BuilderInfo; | ||
import org.mapstruct.ap.spi.DefaultBuilderProvider; | ||
import org.mapstruct.ap.spi.TypeHierarchyErroneousException; | ||
|
||
/** | ||
* @author Filip Hrisafov | ||
*/ | ||
public class ImmutablesBuilderProvider extends DefaultBuilderProvider { | ||
|
||
private static final Pattern JAVA_JAVAX_PACKAGE = Pattern.compile( "^javax?\\..*" ); | ||
|
||
@Override | ||
protected BuilderInfo findBuilderInfo(TypeElement typeElement, Elements elements, Types types) { | ||
Name name = typeElement.getQualifiedName(); | ||
if ( name.length() == 0 || JAVA_JAVAX_PACKAGE.matcher( name ).matches() ) { | ||
return null; | ||
} | ||
TypeElement immutableAnnotation = elements.getTypeElement( "org.immutables.value.Value.Immutable" ); | ||
if ( immutableAnnotation != null ) { | ||
BuilderInfo info = findBuilderInfoForImmutables( | ||
typeElement, | ||
immutableAnnotation, | ||
elements, | ||
types | ||
); | ||
if ( info != null ) { | ||
return info; | ||
} | ||
} | ||
|
||
return super.findBuilderInfo( typeElement, elements, types ); | ||
} | ||
|
||
protected BuilderInfo findBuilderInfoForImmutables(TypeElement typeElement, | ||
TypeElement immutableAnnotation, Elements elements, Types types) { | ||
for ( AnnotationMirror annotationMirror : elements.getAllAnnotationMirrors( typeElement ) ) { | ||
if ( types.isSameType( annotationMirror.getAnnotationType(), immutableAnnotation.asType() ) ) { | ||
TypeElement immutableElement = asImmutableElement( typeElement, elements ); | ||
if ( immutableElement != null ) { | ||
return super.findBuilderInfo( immutableElement, elements, types ); | ||
} | ||
else { | ||
throw new TypeHierarchyErroneousException( typeElement ); | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private TypeElement asImmutableElement(TypeElement typeElement, Elements elements) { | ||
Element enclosingElement = typeElement.getEnclosingElement(); | ||
StringBuilder builderQualifiedName = new StringBuilder( typeElement.getQualifiedName().length() + 17 ); | ||
if ( enclosingElement.getKind() == ElementKind.PACKAGE ) { | ||
builderQualifiedName.append( ( (PackageElement) enclosingElement ).getQualifiedName().toString() ); | ||
} | ||
else { | ||
builderQualifiedName.append( ( (TypeElement) enclosingElement ).getQualifiedName().toString() ); | ||
} | ||
|
||
if ( builderQualifiedName.length() > 0 ) { | ||
builderQualifiedName.append( "." ); | ||
} | ||
|
||
builderQualifiedName.append( "Immutable" ).append( typeElement.getSimpleName() ); | ||
return elements.getTypeElement( builderQualifiedName ); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
integrationtest/src/test/resources/immutablesBuilderTest/mapper/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<artifactId>immutablesIntegrationTest</artifactId> | ||
<groupId>org.mapstruct</groupId> | ||
<version>1.0.0</version> | ||
</parent> | ||
|
||
<artifactId>itest-immutables-mapper</artifactId> | ||
|
||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.immutables</groupId> | ||
<artifactId>value</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mapstruct</groupId> | ||
<artifactId>itest-immutables-mapping-extras</artifactId> | ||
<version>1.0.0</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions
18
...t/mapper/src/main/resources/META-INF/services/org.mapstruct.ap.spi.AccessorNamingStrategy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/) | ||
# and/or other contributors as indicated by the @authors tag. See the | ||
# copyright.txt file in the distribution for a full listing of all | ||
# contributors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
org.mapstruct.itest.immutables.extras.ImmutablesAccessorNamingStrategy |
18 changes: 18 additions & 0 deletions
18
...lderTest/mapper/src/main/resources/META-INF/services/org.mapstruct.ap.spi.BuilderProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/) | ||
# and/or other contributors as indicated by the @authors tag. See the | ||
# copyright.txt file in the distribution for a full listing of all | ||
# contributors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
org.mapstruct.itest.immutables.extras.ImmutablesBuilderProvider |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters