forked from mapstruct/mapstruct
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mapstruct#1154 Add SPI for excluding types/elements from automatic su…
…b-mapping generation. Default implementation of SPI ignores types in the java/javax packages
- Loading branch information
Showing
17 changed files
with
547 additions
and
17 deletions.
There are no files selected for viewing
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
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
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
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
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
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
46 changes: 46 additions & 0 deletions
46
processor/src/main/java/org/mapstruct/ap/spi/DefaultSubMappingExclusionProvider.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,46 @@ | ||
/** | ||
* 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.ap.spi; | ||
|
||
import java.util.regex.Pattern; | ||
import javax.lang.model.element.Name; | ||
import javax.lang.model.element.TypeElement; | ||
|
||
/** | ||
* The default implementation of the {@link SubMappingExclusionProvider} service provider interface. | ||
* | ||
* With the default implementation, MapStruct will exclude all classes under the under the {@code java} and {@code | ||
* javax} package and will not try to create an automatic sub-mapping method for all classes. The only exception is | ||
* the {@link java.util.Collection}, {@link java.util.Map} and {@link java.util.stream.Stream} types. | ||
* | ||
* @author Filip Hrisafov | ||
*/ | ||
public class DefaultSubMappingExclusionProvider implements SubMappingExclusionProvider { | ||
private static final Pattern JAVA_JAVAX_PACKAGE = Pattern.compile( "^javax?\\..*" ); | ||
|
||
@Override | ||
public boolean isExcluded(TypeElement typeElement) { | ||
Name name = typeElement.getQualifiedName(); | ||
return name.length() != 0 && isFullyQualifiedNameExcluded( name ); | ||
} | ||
|
||
protected boolean isFullyQualifiedNameExcluded(Name name) { | ||
return JAVA_JAVAX_PACKAGE.matcher( name ).matches(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
processor/src/main/java/org/mapstruct/ap/spi/SubMappingExclusionProvider.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,40 @@ | ||
/** | ||
* 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.ap.spi; | ||
|
||
import javax.lang.model.element.TypeElement; | ||
|
||
/** | ||
* A service provider interface that is used to control if MapStruct is allowed to generate automatic sub-mapping for | ||
* a given {@link TypeElement}. | ||
* | ||
* @author Filip Hrisafov | ||
*/ | ||
public interface SubMappingExclusionProvider { | ||
|
||
/** | ||
* Checks if the MapStruct should not generate an automatic sub-mapping for the provided {@link TypeElement}, i.e. | ||
* MapStruct will not try to descent into this class and won't try to automatically map it with some other type. | ||
* | ||
* @param typeElement That needs to be checked | ||
* | ||
* @return {@code true} if MapStruct should exclude the provided {@link TypeElement} from an automatic sub-mapping | ||
*/ | ||
boolean isExcluded(TypeElement typeElement); | ||
} |
30 changes: 30 additions & 0 deletions
30
...c/test/java/org/mapstruct/ap/test/nestedbeans/exclusions/ErroneousJavaInternalMapper.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,30 @@ | ||
/** | ||
* 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.ap.test.nestedbeans.exclusions; | ||
|
||
import org.mapstruct.Mapper; | ||
|
||
/** | ||
* @author Filip Hrisafov | ||
*/ | ||
@Mapper | ||
public interface ErroneousJavaInternalMapper { | ||
|
||
Target map(Source entity); | ||
} |
71 changes: 71 additions & 0 deletions
71
...src/test/java/org/mapstruct/ap/test/nestedbeans/exclusions/ErroneousJavaInternalTest.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,71 @@ | ||
/** | ||
* 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.ap.test.nestedbeans.exclusions; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mapstruct.ap.testutil.IssueKey; | ||
import org.mapstruct.ap.testutil.WithClasses; | ||
import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult; | ||
import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic; | ||
import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome; | ||
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; | ||
|
||
/** | ||
* @author Filip Hrisafov | ||
*/ | ||
@WithClasses({ | ||
Source.class, | ||
Target.class, | ||
ErroneousJavaInternalMapper.class | ||
}) | ||
@RunWith(AnnotationProcessorTestRunner.class) | ||
@IssueKey("1154") | ||
public class ErroneousJavaInternalTest { | ||
|
||
@ExpectedCompilationOutcome(value = CompilationResult.FAILED, | ||
diagnostics = { | ||
@Diagnostic(type = ErroneousJavaInternalMapper.class, | ||
kind = javax.tools.Diagnostic.Kind.ERROR, | ||
line = 29, | ||
messageRegExp = "Can't map property \".*MyType date\" to \"java\\.util\\.Date date\"\\. Consider to " + | ||
"declare/implement a mapping method: \"java\\.util\\.Date map\\(.*MyType value\\)\"\\."), | ||
@Diagnostic(type = ErroneousJavaInternalMapper.class, | ||
kind = javax.tools.Diagnostic.Kind.ERROR, | ||
line = 29, | ||
messageRegExp = "Can't map property \".*MyType calendar\" to \"java\\.util\\.GregorianCalendar " + | ||
"calendar\"\\. Consider to declare/implement a mapping method: \"java\\.util\\.GregorianCalendar " + | ||
"map\\(.*MyType value\\)\"\\."), | ||
@Diagnostic(type = ErroneousJavaInternalMapper.class, | ||
kind = javax.tools.Diagnostic.Kind.ERROR, | ||
line = 29, | ||
messageRegExp = "Can't map property \".*MyType localDate\" to \"java\\.time\\.LocalDate localDate\"\\" + | ||
". Consider to declare/implement a mapping method: \"java\\.time\\.LocalDate map\\(.*MyType " + | ||
"value\\)\"\\."), | ||
@Diagnostic(type = ErroneousJavaInternalMapper.class, | ||
kind = javax.tools.Diagnostic.Kind.ERROR, | ||
line = 29, | ||
messageRegExp = "Can't map property \".*List<.*MyType> types\" to \".*List<.*String> types\"\\" + | ||
". Consider to declare/implement a mapping method: \".*List<.*String> map\\(.*List<.*MyType> " + | ||
"value\\)\"\\.") | ||
}) | ||
@Test | ||
public void shouldNotNestIntoJavaPackageObjects() throws Exception { | ||
} | ||
} |
Oops, something went wrong.