#1304 Add thrown exceptions to the generated nested mapping methods

This commit is contained in:
Filip Hrisafov 2017-10-17 21:10:59 +02:00 committed by GitHub
parent 22e17f9c4b
commit aef1e3b14b
10 changed files with 320 additions and 6 deletions

View File

@ -226,6 +226,10 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
List<LifecycleCallbackMethodReference> afterMappingMethods =
LifecycleCallbackFactory.afterMappingMethods( method, selectionParameters, ctx, existingVariableNames );
if (factoryMethod != null && method instanceof ForgedMethod ) {
( (ForgedMethod) method ).addThrownTypes( factoryMethod.getThrownTypes() );
}
return new BeanMappingMethod(
method,
existingVariableNames,

View File

@ -107,12 +107,7 @@ public abstract class ContainerMappingMethodBuilder<B extends ContainerMappingMe
if ( assignment == null ) {
assignment = forgeMapping( sourceRHS, sourceElementType, targetElementType );
}
else {
if ( method instanceof ForgedMethod ) {
ForgedMethod forgedMethod = (ForgedMethod) method;
forgedMethod.addThrownTypes( assignment.getThrownTypes() );
}
}
if ( assignment == null ) {
if ( method instanceof ForgedMethod ) {
// leave messaging to calling property mapping
@ -128,6 +123,10 @@ public abstract class ContainerMappingMethodBuilder<B extends ContainerMappingMe
);
}
}
else if ( method instanceof ForgedMethod ) {
ForgedMethod forgedMethod = (ForgedMethod) method;
forgedMethod.addThrownTypes( assignment.getThrownTypes() );
}
assignment = getWrapper( assignment, method );

View File

@ -0,0 +1,49 @@
/**
* 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.ObjectFactory;
import org.mapstruct.TargetType;
/**
* @author Filip Hrisafov
* @author Darren Rambaud
*/
public class EntityFactory {
@ObjectFactory
public <T> T createEntity(@TargetType Class<T> 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;
}
}

View File

@ -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 );
}
}

View File

@ -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() {
}
}

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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<DeveloperDto> assignedDevelopers;
public List<DeveloperDto> getAssignedDevelopers() {
return assignedDevelopers;
}
public void setAssignedDevelopers(List<DeveloperDto> assignedDevelopers) {
this.assignedDevelopers = assignedDevelopers;
}
}

View File

@ -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;
}
}

View File

@ -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<Developer> assignedDevelopers;
public List<Developer> getAssignedDevelopers() {
return assignedDevelopers;
}
public void setAssignedDevelopers(List<Developer> assignedDevelopers) {
this.assignedDevelopers = assignedDevelopers;
}
}