Skip to content

Commit 0d31049

Browse files
authored
Use multi-catch in generated code (#4021)
1 parent 6d61c3a commit 0d31049

File tree

4 files changed

+18
-33
lines changed

4 files changed

+18
-33
lines changed

processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/Java8FunctionWrapper.ftl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
-->
88
<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.assignment.Java8FunctionWrapper" -->
9+
<#import "../macro/CommonMacros.ftl" as lib>
910
<#assign sourceVarName><#if assignment.sourceLocalVarName?? >${assignment.sourceLocalVarName}<#else>${assignment.sourceReference}</#if></#assign>
1011
<#if (thrownTypes?size == 0) >
1112
<#compress>
@@ -17,14 +18,9 @@
1718
<#else>
1819
<#compress>
1920
${sourceVarName} -> {
20-
try {
21+
<@lib.handleExceptions>
2122
return <@_assignment/>;
22-
}
23-
<#list thrownTypes as exceptionType>
24-
catch ( <@includeModel object=exceptionType/> e ) {
25-
throw new RuntimeException( e );
26-
}
27-
</#list>
23+
</@lib.handleExceptions>
2824
}
2925
</#compress>
3026
</#if>

processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/LocalVarWrapper.ftl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@
66
77
-->
88
<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.assignment.LocalVarWrapper" -->
9+
<#import "../macro/CommonMacros.ftl" as lib>
910
<#if (thrownTypes?size == 0) >
1011
<#if !ext.isTargetDefined?? ><@includeModel object=ext.targetType/></#if> ${ext.targetWriteAccessorName} = <@_assignment/>;
1112
<#else>
1213
<#if !ext.isTargetDefined?? ><@includeModel object=ext.targetType/> ${ext.targetWriteAccessorName};</#if>
13-
try {
14+
<@lib.handleExceptions>
1415
${ext.targetWriteAccessorName} = <@_assignment/>;
15-
}
16-
<#list thrownTypes as exceptionType>
17-
catch ( <@includeModel object=exceptionType/> e ) {
18-
throw new RuntimeException( e );
19-
}
20-
</#list>
16+
</@lib.handleExceptions>
2117
</#if>
2218
<#macro _assignment>
2319
<@includeModel object=assignment

processor/src/main/resources/org/mapstruct/ap/internal/model/macro/CommonMacros.ftl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,16 @@
104104
try {
105105
<#nested>
106106
}
107-
<#list thrownTypes as exceptionType>
108-
catch ( <@includeModel object=exceptionType/> e ) {
107+
<@compress single_line=true>catch (
108+
<#list thrownTypes as exceptionType>
109+
<#if exceptionType_index &gt; 0> | </#if>
110+
<@includeModel object=exceptionType/>
111+
</#list>
112+
e ) {
113+
</@compress>
114+
109115
throw new RuntimeException( e );
110116
}
111-
</#list>
112117
</#if>
113118
</#macro>
114119
<#--

processor/src/test/resources/fixtures/org/mapstruct/ap/test/collection/adder/SourceTargetMapperImpl.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ public Source toSource(Target source) {
5959
try {
6060
source1.setPets( petMapper.toSourcePets( source.getPets() ) );
6161
}
62-
catch ( CatException e ) {
63-
throw new RuntimeException( e );
64-
}
65-
catch ( DogException e ) {
62+
catch ( CatException | DogException e ) {
6663
throw new RuntimeException( e );
6764
}
6865

@@ -82,10 +79,7 @@ public void toExistingTarget(Source source, Target target) {
8279
}
8380
}
8481
}
85-
catch ( CatException e ) {
86-
throw new RuntimeException( e );
87-
}
88-
catch ( DogException e ) {
82+
catch ( CatException | DogException e ) {
8983
throw new RuntimeException( e );
9084
}
9185
}
@@ -161,10 +155,7 @@ public TargetViaTargetType toTargetViaTargetType(Source source) {
161155
}
162156
}
163157
}
164-
catch ( CatException e ) {
165-
throw new RuntimeException( e );
166-
}
167-
catch ( DogException e ) {
158+
catch ( CatException | DogException e ) {
168159
throw new RuntimeException( e );
169160
}
170161

@@ -184,10 +175,7 @@ public Target fromSingleElementSource(SingleElementSource source) {
184175
target.addPet( petMapper.toPet( source.getPet() ) );
185176
}
186177
}
187-
catch ( CatException e ) {
188-
throw new RuntimeException( e );
189-
}
190-
catch ( DogException e ) {
178+
catch ( CatException | DogException e ) {
191179
throw new RuntimeException( e );
192180
}
193181

0 commit comments

Comments
 (0)