#513, forged method should re-throw exceptions of used mapping methods

This commit is contained in:
sjaakd 2015-05-12 21:48:01 +02:00
parent fa1decd099
commit b4c514d479
16 changed files with 613 additions and 3 deletions

View File

@ -29,6 +29,7 @@ import org.mapstruct.ap.model.assignment.LocalVarWrapper;
import org.mapstruct.ap.model.assignment.SetterWrapper;
import org.mapstruct.ap.model.common.Parameter;
import org.mapstruct.ap.model.common.Type;
import org.mapstruct.ap.model.source.ForgedMethod;
import org.mapstruct.ap.model.source.Method;
import org.mapstruct.ap.prism.NullValueMappingStrategyPrism;
import org.mapstruct.ap.util.Message;
@ -119,7 +120,12 @@ public class IterableMappingMethod extends MappingMethod {
if ( assignment == null ) {
ctx.getMessager().printMessage( method.getExecutable(), Message.ITERABLEMAPPING_MAPPING_NOT_FOUND );
}
else {
if ( method instanceof ForgedMethod ) {
ForgedMethod forgedMethod = (ForgedMethod) method;
forgedMethod.addThrownTypes( assignment.getExceptionTypes() );
}
}
// target accessor is setter, so decorate assignment as setter
if ( resultType.isArrayType() ) {
assignment = new LocalVarWrapper( assignment, method.getThrownTypes() );

View File

@ -27,6 +27,7 @@ import org.mapstruct.ap.model.assignment.Assignment;
import org.mapstruct.ap.model.assignment.LocalVarWrapper;
import org.mapstruct.ap.model.common.Parameter;
import org.mapstruct.ap.model.common.Type;
import org.mapstruct.ap.model.source.ForgedMethod;
import org.mapstruct.ap.model.source.Method;
import org.mapstruct.ap.prism.NullValueMappingStrategyPrism;
import org.mapstruct.ap.util.Message;
@ -150,6 +151,16 @@ public class MapMappingMethod extends MappingMethod {
false
);
if ( method instanceof ForgedMethod ) {
ForgedMethod forgedMethod = (ForgedMethod) method;
if ( keyAssignment != null ) {
forgedMethod.addThrownTypes( keyAssignment.getExceptionTypes() );
}
if ( valueAssignment != null ) {
forgedMethod.addThrownTypes( valueAssignment.getExceptionTypes() );
}
}
if ( valueAssignment == null ) {
ctx.getMessager().printMessage( method.getExecutable(), Message.MAPMAPPING_VALUE_MAPPING_NOT_FOUND );
}

View File

@ -20,7 +20,6 @@ package org.mapstruct.ap.model.source;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.lang.model.element.ExecutableElement;
@ -41,6 +40,7 @@ public class ForgedMethod implements Method {
private final Type returnType;
private final String name;
private final ExecutableElement positionHintElement;
private final List<Type> thrownTypes;
/**
* Creates a new forged method with the given name.
@ -55,6 +55,7 @@ public class ForgedMethod implements Method {
String sourceParamSafeName = Strings.getSaveVariableName( sourceParamName );
this.parameters = Arrays.asList( new Parameter( sourceParamSafeName, sourceType ) );
this.returnType = targetType;
this.thrownTypes = new ArrayList<Type>();
this.name = name;
this.positionHintElement = positionHintElement;
}
@ -67,6 +68,7 @@ public class ForgedMethod implements Method {
public ForgedMethod(String name, ForgedMethod forgedMethod) {
this.parameters = forgedMethod.parameters;
this.returnType = forgedMethod.returnType;
this.thrownTypes = new ArrayList<Type>();
this.positionHintElement = forgedMethod.positionHintElement;
this.name = name;
}
@ -131,7 +133,16 @@ public class ForgedMethod implements Method {
@Override
public List<Type> getThrownTypes() {
return Collections.<Type>emptyList();
return thrownTypes;
}
public void addThrownTypes(List<Type> thrownTypesToAdd) {
for ( Type thrownType : thrownTypesToAdd ) {
// make sure there are no duplicates coming from the keyAssignment thrown types.
if ( !thrownTypes.contains( thrownType ) ) {
thrownTypes.add( thrownType );
}
}
}
@Override

View File

@ -0,0 +1,38 @@
/**
* Copyright 2012-2015 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.bugs._513;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@Mapper
public interface Issue513Mapper {
Issue513Mapper INSTANCE = Mappers.getMapper( Issue513Mapper.class );
Target map(Source source) throws MappingException, MappingValueException, MappingKeyException;
TargetElement mapElement(SourceElement source) throws MappingException;
TargetKey mapKey(SourceKey source) throws MappingException, MappingKeyException;
TargetValue mapValue(SourceValue source) throws MappingException, MappingValueException;
}

View File

@ -0,0 +1,108 @@
/**
* Copyright 2012-2015 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.bugs._513;
import java.util.Arrays;
import java.util.HashMap;
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.runner.AnnotationProcessorTestRunner;
/**
* Reproducer for https://github.com/mapstruct/mapstruct/issues/513.
*
* @author Sjaak Derksen
*/
@IssueKey( "513" )
@WithClasses( {
Issue513Mapper.class,
Source.class,
Target.class,
SourceElement.class,
TargetElement.class,
SourceKey.class,
TargetKey.class,
SourceValue.class,
TargetValue.class,
MappingException.class,
MappingKeyException.class,
MappingValueException.class
} )
@RunWith(AnnotationProcessorTestRunner.class)
public class Issue513Test {
@Test( expected = MappingException.class )
public void shouldThrowMappingException() throws Exception {
Source source = new Source();
SourceElement sourceElement = new SourceElement();
sourceElement.setValue( "test" );
source.setCollection( Arrays.asList( new SourceElement[]{ sourceElement } ) );
Issue513Mapper.INSTANCE.map( source );
}
@Test( expected = MappingKeyException.class )
public void shouldThrowMappingKeyException() throws Exception {
Source source = new Source();
SourceKey sourceKey = new SourceKey();
sourceKey.setValue( MappingKeyException.class.getSimpleName() );
SourceValue sourceValue = new SourceValue();
HashMap<SourceKey, SourceValue> map = new HashMap<SourceKey, SourceValue>();
map.put( sourceKey, sourceValue );
source.setMap( map );
Issue513Mapper.INSTANCE.map( source );
}
@Test( expected = MappingValueException.class )
public void shouldThrowMappingValueException() throws Exception {
Source source = new Source();
SourceKey sourceKey = new SourceKey();
SourceValue sourceValue = new SourceValue();
sourceValue.setValue( MappingValueException.class.getSimpleName() );
HashMap<SourceKey, SourceValue> map = new HashMap<SourceKey, SourceValue>();
map.put( sourceKey, sourceValue );
source.setMap( map );
Issue513Mapper.INSTANCE.map( source );
}
@Test( expected = MappingException.class )
public void shouldThrowMappingCommonException() throws Exception {
Source source = new Source();
SourceKey sourceKey = new SourceKey();
SourceValue sourceValue = new SourceValue();
sourceValue.setValue( MappingException.class.getSimpleName() );
HashMap<SourceKey, SourceValue> map = new HashMap<SourceKey, SourceValue>();
map.put( sourceKey, sourceValue );
source.setMap( map );
Issue513Mapper.INSTANCE.map( source );
}
}

View File

@ -0,0 +1,34 @@
/**
* Copyright 2012-2015 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.bugs._513;
/**
*
* @author Sjaak Derksen
*/
public class MappingException extends Exception {
public MappingException() {
}
public MappingException(String msg) {
super( msg );
}
}

View File

@ -0,0 +1,34 @@
/**
* Copyright 2012-2015 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.bugs._513;
/**
*
* @author Sjaak Derksen
*/
public class MappingKeyException extends Exception {
public MappingKeyException() {
}
public MappingKeyException(String msg) {
super( msg );
}
}

View File

@ -0,0 +1,34 @@
/**
* Copyright 2012-2015 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.bugs._513;
/**
*
* @author Sjaak Derksen
*/
public class MappingValueException extends Exception {
public MappingValueException() {
}
public MappingValueException(String msg) {
super( msg );
}
}

View File

@ -0,0 +1,50 @@
/**
* Copyright 2012-2015 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.bugs._513;
import java.util.Collection;
import java.util.Map;
/**
*
* @author Sjaak Derksen
*/
public class Source {
private Collection<SourceElement> collection;
private Map<SourceKey, SourceValue> map;
public Collection<SourceElement> getCollection() {
return collection;
}
public void setCollection(Collection<SourceElement> collection) {
this.collection = collection;
}
public Map<SourceKey, SourceValue> getMap() {
return map;
}
public void setMap(Map<SourceKey, SourceValue> map) {
this.map = map;
}
}

View File

@ -0,0 +1,37 @@
/**
* Copyright 2012-2015 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.bugs._513;
/**
*
* @author Sjaak Derksen
*/
public class SourceElement {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@ -0,0 +1,37 @@
/**
* Copyright 2012-2015 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.bugs._513;
/**
*
* @author Sjaak Derksen
*/
public class SourceKey {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@ -0,0 +1,37 @@
/**
* Copyright 2012-2015 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.bugs._513;
/**
*
* @author Sjaak Derksen
*/
public class SourceValue {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@ -0,0 +1,49 @@
/**
* Copyright 2012-2015 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.bugs._513;
import java.util.Collection;
import java.util.Map;
/**
*
* @author Sjaak Derksen
*/
public class Target {
private Collection<TargetElement> collection;
private Map<TargetKey, TargetValue> map;
public Collection<TargetElement> getCollection() {
return collection;
}
public void setCollection( Collection<TargetElement> collection ) {
this.collection = collection;
}
public Map<TargetKey, TargetValue> getMap() {
return map;
}
public void setMap(Map<TargetKey, TargetValue> map) {
this.map = map;
}
}

View File

@ -0,0 +1,38 @@
/**
* Copyright 2012-2015 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.bugs._513;
/**
*
* @author Sjaak Derksen
*/
public class TargetElement {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) throws MappingException {
throw new MappingException();
}
}

View File

@ -0,0 +1,43 @@
/**
* Copyright 2012-2015 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.bugs._513;
/**
*
* @author Sjaak Derksen
*/
public class TargetKey {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) throws MappingException, MappingKeyException {
if ( MappingKeyException.class.getSimpleName().equals( value ) ) {
throw new MappingKeyException();
}
else if ( MappingException.class.getSimpleName().equals( value ) ) {
throw new MappingException();
}
}
}

View File

@ -0,0 +1,43 @@
/**
* Copyright 2012-2015 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.bugs._513;
/**
*
* @author Sjaak Derksen
*/
public class TargetValue {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) throws MappingException, MappingValueException {
if ( MappingValueException.class.getSimpleName().equals( value ) ) {
throw new MappingValueException();
}
else if ( MappingException.class.getSimpleName().equals( value ) ) {
throw new MappingException();
}
}
}