From 54b8747e095446843f1b7d2fdd5ef7ca34464dad Mon Sep 17 00:00:00 2001 From: Andreas Gudian Date: Mon, 20 Oct 2014 15:33:15 +0200 Subject: [PATCH] #312 fix ClassCastException in Eclipse JDT compiler when trying to compute the erasure of the 'void' type, extend integration test that reproduced the error --- .../simple/SourceTargetAbstractMapper.java | 42 +++++++++++++++++++ .../itest/simple/ConversionTest.java | 15 +++++++ .../org/mapstruct/ap/model/common/Type.java | 2 +- .../ap/model/common/TypeFactory.java | 7 ++-- 4 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/SourceTargetAbstractMapper.java diff --git a/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/SourceTargetAbstractMapper.java b/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/SourceTargetAbstractMapper.java new file mode 100644 index 000000000..a76f453af --- /dev/null +++ b/integrationtest/src/test/resources/simpleTest/src/main/java/org/mapstruct/itest/simple/SourceTargetAbstractMapper.java @@ -0,0 +1,42 @@ +/** + * Copyright 2012-2014 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.itest.simple; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.Mappings; +import org.mapstruct.factory.Mappers; + +@Mapper +public abstract class SourceTargetAbstractMapper { + + public static SourceTargetAbstractMapper INSTANCE = Mappers.getMapper( SourceTargetAbstractMapper.class ); + + @Mappings({ + @Mapping(source = "qax", target = "baz"), + @Mapping(source = "baz", target = "qax") + }) + public abstract Target sourceToTarget(Source source); + + public abstract Source targetToSource(Target target); + + protected void isNeverCalled(Source source) { + throw new RuntimeException("not expected to be called"); + } +} diff --git a/integrationtest/src/test/resources/simpleTest/src/test/java/org/mapstruct/itest/simple/ConversionTest.java b/integrationtest/src/test/resources/simpleTest/src/test/java/org/mapstruct/itest/simple/ConversionTest.java index 0b1f67432..93bcc7cf1 100644 --- a/integrationtest/src/test/resources/simpleTest/src/test/java/org/mapstruct/itest/simple/ConversionTest.java +++ b/integrationtest/src/test/resources/simpleTest/src/test/java/org/mapstruct/itest/simple/ConversionTest.java @@ -93,4 +93,19 @@ public class ConversionTest { assertThat( source.getBaz() ).isEqualTo( 42 ); assertThat( source.getQax() ).isEqualTo( 23 ); } + + @Test + public void shouldWorkWithAbstractClass() { + Source source = new Source(); + source.setFoo( 42 ); + source.setBar( 23L ); + source.setZip( 73 ); + + Target target = SourceTargetAbstractMapper.INSTANCE.sourceToTarget( source ); + + assertThat( target ).isNotNull(); + assertThat( target.getFoo() ).isEqualTo( Long.valueOf( 42 ) ); + assertThat( target.getBar() ).isEqualTo( 23 ); + assertThat( target.getZip() ).isEqualTo( "73" ); + } } diff --git a/processor/src/main/java/org/mapstruct/ap/model/common/Type.java b/processor/src/main/java/org/mapstruct/ap/model/common/Type.java index 93c50bf92..88df2d24d 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/common/Type.java +++ b/processor/src/main/java/org/mapstruct/ap/model/common/Type.java @@ -106,7 +106,7 @@ public class Type extends ModelElement implements Comparable { this.isCollectionType = isCollectionType; this.isMapType = isMapType; this.isImported = isImported; - this.isVoid = typeMirror.getKind().equals( TypeKind.VOID ); + this.isVoid = typeMirror.getKind() == TypeKind.VOID; if ( isEnumType ) { enumConstants = new ArrayList(); diff --git a/processor/src/main/java/org/mapstruct/ap/model/common/TypeFactory.java b/processor/src/main/java/org/mapstruct/ap/model/common/TypeFactory.java index 547409cc9..59e6d4433 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/common/TypeFactory.java +++ b/processor/src/main/java/org/mapstruct/ap/model/common/TypeFactory.java @@ -123,9 +123,10 @@ public class TypeFactory { Type implementationType = getImplementationType( mirror ); - boolean isIterableType = TypeUtilsJDK6Fix.isSubType( typeUtils, mirror, iterableType ); - boolean isCollectionType = TypeUtilsJDK6Fix.isSubType( typeUtils, mirror, collectionType ); - boolean isMapType = TypeUtilsJDK6Fix.isSubType( typeUtils, mirror, mapType ); + boolean isVoid = mirror.getKind() == TypeKind.VOID; + boolean isIterableType = !isVoid && TypeUtilsJDK6Fix.isSubType( typeUtils, mirror, iterableType ); + boolean isCollectionType = !isVoid && TypeUtilsJDK6Fix.isSubType( typeUtils, mirror, collectionType ); + boolean isMapType = !isVoid && TypeUtilsJDK6Fix.isSubType( typeUtils, mirror, mapType ); boolean isEnumType; boolean isInterface;