#277 fixing faulty match check in built-in methods on return/target type.

This commit is contained in:
sjaakd 2014-08-16 21:24:34 +02:00
parent b4b4e32ae5
commit 01082a6da3
4 changed files with 123 additions and 12 deletions

View File

@ -76,8 +76,18 @@ public abstract class BuiltInMethod implements Method {
}
Type sourceType = sourceTypes.iterator().next();
if ( targetType.erasure().isAssignableTo( getReturnType().erasure() )
&& sourceType.erasure().isAssignableTo( getParameter().getType().erasure() ) ) {
if ( getReturnType().isAssignableTo( targetType.erasure() )
&& sourceType.erasure().isAssignableTo( getParameter().getType() ) ) {
return doTypeVarsMatch( sourceType, targetType );
}
if ( getReturnType().getFullyQualifiedName().equals( "java.lang.Object" )
&& sourceType.erasure().isAssignableTo( getParameter().getType() ) ) {
// return type could be a type parameter T
return doTypeVarsMatch( sourceType, targetType );
}
if ( getReturnType().isAssignableTo( targetType.erasure() )
&& getParameter().getType().getFullyQualifiedName().equals( "java.lang.Object" ) ) {
// parameter type could be a type parameter T
return doTypeVarsMatch( sourceType, targetType );
}
return false;
@ -168,6 +178,8 @@ public abstract class BuiltInMethod implements Method {
return true;
}
/**
* There's currently only one parameter foreseen instead of a list of parameter
*

View File

@ -18,15 +18,8 @@
*/
package org.mapstruct.ap.test.builtin;
import org.mapstruct.ap.test.builtin.mapper.MapSourceTargetMapper;
import org.mapstruct.ap.test.builtin.mapper.IterableSourceTargetMapper;
import org.mapstruct.ap.test.builtin.mapper.SourceTargetWithDateMapper;
import org.mapstruct.ap.test.builtin.target.TargetWithDate;
import org.mapstruct.ap.test.builtin.target.IterableTarget;
import org.mapstruct.ap.test.builtin.target.MapTarget;
import org.mapstruct.ap.test.builtin.source.SourceWithDate;
import org.mapstruct.ap.test.builtin.source.IterableSource;
import org.mapstruct.ap.test.builtin.source.MapSource;
import org.mapstruct.ap.test.builtin.target.TargetWithSqlDate;
import org.mapstruct.ap.test.builtin.mapper.SourceTargetWithSqlDateMapper;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@ -44,7 +37,6 @@ import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;
import static org.fest.assertions.Assertions.assertThat;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
@ -61,17 +53,31 @@ import org.mapstruct.ap.test.builtin.mapper.CalendarToStringMapper;
import org.mapstruct.ap.test.builtin.mapper.CalendarToXmlGregCalMapper;
import org.mapstruct.ap.test.builtin.mapper.DateToCalendarMapper;
import org.mapstruct.ap.test.builtin.mapper.DateToXmlGregCalMapper;
import org.mapstruct.ap.test.builtin.mapper.IterableSourceTargetMapper;
import org.mapstruct.ap.test.builtin.mapper.JaxbListMapper;
import org.mapstruct.ap.test.builtin.mapper.JaxbMapper;
import org.mapstruct.ap.test.builtin.mapper.MapSourceTargetMapper;
import org.mapstruct.ap.test.builtin.mapper.SourceTargetWithDateMapper;
import org.mapstruct.ap.test.builtin.mapper.StringToCalendarMapper;
import org.mapstruct.ap.test.builtin.mapper.StringToXmlGregCalMapper;
import org.mapstruct.ap.test.builtin.mapper.XmlGregCalToCalendarMapper;
import org.mapstruct.ap.test.builtin.mapper.XmlGregCalToDateMapper;
import org.mapstruct.ap.test.builtin.mapper.XmlGregCalToStringMapper;
import org.mapstruct.ap.test.builtin.source.IterableSource;
import org.mapstruct.ap.test.builtin.source.MapSource;
import org.mapstruct.ap.test.builtin.source.SourceWithDate;
import org.mapstruct.ap.test.builtin.target.IterableTarget;
import org.mapstruct.ap.test.builtin.target.MapTarget;
import org.mapstruct.ap.test.builtin.target.TargetWithDate;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult;
import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic;
import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
import static org.fest.assertions.Assertions.assertThat;
/**
* Test for the generation of built-in mapping methods.
*
@ -303,6 +309,22 @@ public class BuiltInTest {
assertThat( targetWithDate.getDate() ).isNull();
}
@Test
@IssueKey( "277" )
@WithClasses( { SourceWithDate.class, TargetWithSqlDate.class, SourceTargetWithSqlDateMapper.class } )
@ExpectedCompilationOutcome(
value = CompilationResult.FAILED,
diagnostics = {
@Diagnostic( type = SourceTargetWithSqlDateMapper.class,
kind = javax.tools.Diagnostic.Kind.ERROR,
line = 35,
messageRegExp = "Can't map property \"java\\.util\\.Date date\" to "
+ "\"java\\.sql\\.Date date\"" )
}
)
public void shouldNotMapJavaUtilDateToJavaSqlDate() {
}
private JAXBElement<String> createJaxb(String test) {
return new JAXBElement<String>( new QName( "www.mapstruct.org", "test" ), String.class, test );
}

View File

@ -0,0 +1,37 @@
/**
* 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.ap.test.builtin.mapper;
import org.mapstruct.Mapper;
import org.mapstruct.ap.test.builtin.source.SourceWithDate;
import org.mapstruct.ap.test.builtin.target.TargetWithSqlDate;
import org.mapstruct.factory.Mappers;
/**
* @author Andreas Gudian
*
*/
@Mapper
public interface SourceTargetWithSqlDateMapper {
SourceTargetWithSqlDateMapper INSTANCE = Mappers.getMapper( SourceTargetWithSqlDateMapper.class );
TargetWithSqlDate toTargetWithSqlDate(SourceWithDate source);
}

View File

@ -0,0 +1,40 @@
/**
* 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.ap.test.builtin.target;
import java.sql.Date;
/**
*
* @author Sjaak Derksen
*/
public class TargetWithSqlDate {
private Date date;
public Date getDate() {
return date;
}
public void setDate( Date date ) {
this.date = date;
}
}