From aef1e3b14bb945b73a120f92540011f02ce77f88 Mon Sep 17 00:00:00 2001 From: Filip Hrisafov Date: Tue, 17 Oct 2017 21:10:59 +0200 Subject: [PATCH] #1304 Add thrown exceptions to the generated nested mapping methods --- .../ap/internal/model/BeanMappingMethod.java | 4 ++ .../model/ContainerMappingMethodBuilder.java | 11 ++-- .../nestedbeans/exceptions/EntityFactory.java | 49 ++++++++++++++++++ .../exceptions/MappingException.java | 30 +++++++++++ .../NestedMappingsWithExceptionTest.java | 51 +++++++++++++++++++ .../nestedbeans/exceptions/ProjectMapper.java | 33 ++++++++++++ .../exceptions/_target/DeveloperDto.java | 36 +++++++++++++ .../exceptions/_target/ProjectDto.java | 38 ++++++++++++++ .../exceptions/source/Developer.java | 36 +++++++++++++ .../exceptions/source/Project.java | 38 ++++++++++++++ 10 files changed, 320 insertions(+), 6 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/EntityFactory.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/MappingException.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/NestedMappingsWithExceptionTest.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/ProjectMapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/_target/DeveloperDto.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/_target/ProjectDto.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/source/Developer.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/source/Project.java diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java index 21e918eb5..9be385b62 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java @@ -226,6 +226,10 @@ public class BeanMappingMethod extends NormalTypeMappingMethod { List afterMappingMethods = LifecycleCallbackFactory.afterMappingMethods( method, selectionParameters, ctx, existingVariableNames ); + if (factoryMethod != null && method instanceof ForgedMethod ) { + ( (ForgedMethod) method ).addThrownTypes( factoryMethod.getThrownTypes() ); + } + return new BeanMappingMethod( method, existingVariableNames, diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/ContainerMappingMethodBuilder.java b/processor/src/main/java/org/mapstruct/ap/internal/model/ContainerMappingMethodBuilder.java index 5e7ce4995..722594720 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/ContainerMappingMethodBuilder.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/ContainerMappingMethodBuilder.java @@ -107,12 +107,7 @@ public abstract class ContainerMappingMethodBuilder T createEntity(@TargetType Class entityClass) throws MappingException { + T entity; + + try { + entity = entityClass.newInstance(); + } + catch ( IllegalAccessException exception ) { + throw new MappingException( "Rare exception thrown, refer to stack trace", exception ); + } + catch ( InstantiationException exception ) { + throw new MappingException( "Rare exception thrown, refer to stack trace", exception ); + } + catch ( Exception exception ) { + throw new MappingException( "I don't know how you got here, refer to stack trace", exception ); + } + + return entity; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/MappingException.java b/processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/MappingException.java new file mode 100644 index 000000000..7e72786ff --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/MappingException.java @@ -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.exceptions; + +/** + * @author Filip Hrisafov + * @author Darren Rambaud + */ +public class MappingException extends Exception { + + public MappingException(String message, Throwable cause) { + super( message, cause ); + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/NestedMappingsWithExceptionTest.java b/processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/NestedMappingsWithExceptionTest.java new file mode 100644 index 000000000..acb135345 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/NestedMappingsWithExceptionTest.java @@ -0,0 +1,51 @@ +/** + * 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.exceptions; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mapstruct.ap.test.nestedbeans.exceptions._target.DeveloperDto; +import org.mapstruct.ap.test.nestedbeans.exceptions._target.ProjectDto; +import org.mapstruct.ap.test.nestedbeans.exceptions.source.Developer; +import org.mapstruct.ap.test.nestedbeans.exceptions.source.Project; +import org.mapstruct.ap.testutil.IssueKey; +import org.mapstruct.ap.testutil.WithClasses; +import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; + +/** + * @author Filip Hrisafov + */ +@WithClasses({ + DeveloperDto.class, + ProjectDto.class, + Developer.class, + Project.class, + ProjectMapper.class, + MappingException.class, + EntityFactory.class +}) +@IssueKey("1304") +@RunWith(AnnotationProcessorTestRunner.class) +public class NestedMappingsWithExceptionTest { + + @Test + public void shouldGenerateCodeThatCompiles() { + + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/ProjectMapper.java b/processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/ProjectMapper.java new file mode 100644 index 000000000..58ce29973 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/ProjectMapper.java @@ -0,0 +1,33 @@ +/** + * 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.exceptions; + +import org.mapstruct.Mapper; +import org.mapstruct.ap.test.nestedbeans.exceptions._target.ProjectDto; +import org.mapstruct.ap.test.nestedbeans.exceptions.source.Project; + +/** + * @author Filip Hrisafov + * @author Darren Rambaud + */ +@Mapper(uses = EntityFactory.class) +public interface ProjectMapper { + + Project map(ProjectDto source) throws MappingException; +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/_target/DeveloperDto.java b/processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/_target/DeveloperDto.java new file mode 100644 index 000000000..c813bca13 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/_target/DeveloperDto.java @@ -0,0 +1,36 @@ +/** + * 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.exceptions._target; + +/** + * @author Filip Hrisafov + * @author Darren Rambaud + */ +public class DeveloperDto { + + private String firstName; + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/_target/ProjectDto.java b/processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/_target/ProjectDto.java new file mode 100644 index 000000000..9cd1386ca --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/_target/ProjectDto.java @@ -0,0 +1,38 @@ +/** + * 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.exceptions._target; + +import java.util.List; + +/** + * @author Filip Hrisafov + * @author Darren Rambaud + */ +public class ProjectDto { + + private List assignedDevelopers; + + public List getAssignedDevelopers() { + return assignedDevelopers; + } + + public void setAssignedDevelopers(List assignedDevelopers) { + this.assignedDevelopers = assignedDevelopers; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/source/Developer.java b/processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/source/Developer.java new file mode 100644 index 000000000..6c453480e --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/source/Developer.java @@ -0,0 +1,36 @@ +/** + * 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.exceptions.source; + +/** + * @author Filip Hrisafov + * @author Darren Rambaud + */ +public class Developer { + + private String firstName; + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/source/Project.java b/processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/source/Project.java new file mode 100644 index 000000000..8b5e0771e --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/nestedbeans/exceptions/source/Project.java @@ -0,0 +1,38 @@ +/** + * 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.exceptions.source; + +import java.util.List; + +/** + * @author Filip Hrisafov + * @author Darren Rambaud + */ +public class Project { + + private List assignedDevelopers; + + public List getAssignedDevelopers() { + return assignedDevelopers; + } + + public void setAssignedDevelopers(List assignedDevelopers) { + this.assignedDevelopers = assignedDevelopers; + } +}