#147 creating a wrapper class localizing the JDK6 fix to one spot

This commit is contained in:
sjaakd 2014-03-16 13:15:39 +01:00 committed by Andreas Gudian
parent c10a5a6e31
commit 6639447dea
3 changed files with 61 additions and 21 deletions

View File

@ -51,6 +51,7 @@ import javax.lang.model.util.Types;
import org.mapstruct.ap.prism.MappingTargetPrism; import org.mapstruct.ap.prism.MappingTargetPrism;
import org.mapstruct.ap.prism.TargetTypePrism; import org.mapstruct.ap.prism.TargetTypePrism;
import org.mapstruct.ap.util.AnnotationProcessingException; import org.mapstruct.ap.util.AnnotationProcessingException;
import org.mapstruct.ap.util.TypeUtilsJDK6Fix;
/** /**
* Factory creating {@link Type} instances. * Factory creating {@link Type} instances.
@ -122,18 +123,9 @@ public class TypeFactory {
Type implementationType = getImplementationType( mirror ); Type implementationType = getImplementationType( mirror );
boolean isIterableType = typeUtils.isSubtype( boolean isIterableType = TypeUtilsJDK6Fix.isSubType( typeUtils, mirror, iterableType );
typeUtils.erasure( mirror ), boolean isCollectionType = TypeUtilsJDK6Fix.isSubType( typeUtils, mirror, collectionType );
typeUtils.erasure( iterableType ) boolean isMapType = TypeUtilsJDK6Fix.isSubType( typeUtils, mirror, mapType );
);
boolean isCollectionType = typeUtils.isSubtype(
typeUtils.erasure( mirror ),
typeUtils.erasure( collectionType )
);
boolean isMapType = typeUtils.isSubtype(
typeUtils.erasure( mirror ),
typeUtils.erasure( mapType )
);
boolean isEnumType; boolean isEnumType;
boolean isInterface; boolean isInterface;

View File

@ -36,6 +36,7 @@ import javax.lang.model.util.SimpleTypeVisitor6;
import javax.lang.model.util.Types; import javax.lang.model.util.Types;
import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.Type;
import org.mapstruct.ap.util.TypeUtilsJDK6Fix;
/** /**
* SourceMethodMatcher $8.4 of the JavaLanguage specification describes a method body as such: * SourceMethodMatcher $8.4 of the JavaLanguage specification describes a method body as such:
@ -207,8 +208,8 @@ public class MethodMatcher {
} }
else { else {
// check if types are in bound // check if types are in bound
if ( typeUtils.isSubtype( typeUtils.erasure( t.getLowerBound() ), typeUtils.erasure( p ) ) && if ( TypeUtilsJDK6Fix.isSubType( typeUtils, t.getLowerBound(), p) &&
typeUtils.isSubtype( typeUtils.erasure( p ), typeUtils.erasure( t.getUpperBound() ) ) ) { TypeUtilsJDK6Fix.isSubType( typeUtils, p, t.getUpperBound() ) ) {
genericTypesMap.put( t, p ); genericTypesMap.put( t, p );
return Boolean.TRUE; return Boolean.TRUE;
} }
@ -228,7 +229,7 @@ public class MethodMatcher {
case DECLARED: case DECLARED:
// for example method: String method(? extends String) // for example method: String method(? extends String)
// isSubType checks range [subtype, type], e.g. isSubtype [Object, String]==true // isSubType checks range [subtype, type], e.g. isSubtype [Object, String]==true
return typeUtils.isSubtype( typeUtils.erasure( p ), extendsBound ); return TypeUtilsJDK6Fix.isSubType( typeUtils, p, extendsBound );
case TYPEVAR: case TYPEVAR:
// for example method: <T extends String & Serializable> T method(? extends T) // for example method: <T extends String & Serializable> T method(? extends T)
@ -250,8 +251,8 @@ public class MethodMatcher {
// for example method: String method(? super String) // for example method: String method(? super String)
// to check super type, we can simply reverse the argument, but that would initially yield // to check super type, we can simply reverse the argument, but that would initially yield
// a result: <type, superType] (so type not included) so we need to check sameType also. // a result: <type, superType] (so type not included) so we need to check sameType also.
return ( typeUtils.isSubtype( typeUtils.erasure( superBound ), p ) || return TypeUtilsJDK6Fix.isSubType( typeUtils, superBound , p ) ||
typeUtils.isSameType( p, superBound ) ); typeUtils.isSameType( p, superBound );
case TYPEVAR: case TYPEVAR:
@ -269,9 +270,8 @@ public class MethodMatcher {
// to check super type, we can simply reverse the argument, but that would initially yield // to check super type, we can simply reverse the argument, but that would initially yield
// a result: <type, superType] (so type not included) so we need to check sameType also. // a result: <type, superType] (so type not included) so we need to check sameType also.
TypeMirror superBoundAsDeclared = typeParameter.getBounds().get( 0 ); TypeMirror superBoundAsDeclared = typeParameter.getBounds().get( 0 );
return ( typeUtils.isSubtype( typeUtils.erasure( superBoundAsDeclared ), return ( TypeUtilsJDK6Fix.isSubType( typeUtils, superBoundAsDeclared, p ) ||
typeUtils.erasure( p ) ) || typeUtils.isSameType( p, superBoundAsDeclared ) );
typeUtils.isSameType( p, superBoundAsDeclared ) );
default: default:
// does this situation occur? // does this situation occur?
return Boolean.FALSE; return Boolean.FALSE;
@ -311,7 +311,7 @@ public class MethodMatcher {
if ( t != null && bounds != null ) { if ( t != null && bounds != null ) {
for ( TypeMirror bound : bounds ) { for ( TypeMirror bound : bounds ) {
if ( !( bound.getKind().equals( TypeKind.DECLARED ) && if ( !( bound.getKind().equals( TypeKind.DECLARED ) &&
typeUtils.isSubtype( typeUtils.erasure( t ), typeUtils.erasure( bound ) ) ) ) { TypeUtilsJDK6Fix.isSubType( typeUtils, t, bound ) ) ) {
return false; return false;
} }
} }

View File

@ -0,0 +1,48 @@
/**
* 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.util;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Types;
/**
*
* @author Sjaak Derksen
*/
public class TypeUtilsJDK6Fix {
private TypeUtilsJDK6Fix() { }
/**
* Tests whether one type is a subtype of another.
* Any type is considered to be a subtype of itself.
*
* @param types the type utils
* @param t1 the first type
* @param t2 the second type
* @return {@code true} if and only if the first type is a subtype
* of the second
* @throws IllegalArgumentException if given an executable or package type
* @jls 4.10 Subtyping
*/
public static boolean isSubType(Types types, TypeMirror t1, TypeMirror t2) {
return types.isSubtype( types.erasure( t1 ), types.erasure( t2 ) );
}
}