@@ -550,7 +550,9 @@ else if (type instanceof Class) {
550550
551551 }
552552
553- private static void inferTypeVariables (TypeVariable <?> type , Type inferFrom , Map <TypeVariable <?>, TypeMapping > typeMappings , boolean malleable ) {
553+ private static void inferTypeVariables (TypeVariable <?> type , Type inferFrom ,
554+ Map <TypeVariable <?>, TypeMapping > typeMappings , boolean malleable )
555+ {
554556 TypeMapping typeData = typeMappings .get (type );
555557 // If current is not null then we have already encountered that
556558 // variable. If so, we require them to be exactly the same, and throw a
@@ -560,106 +562,118 @@ private static void inferTypeVariables(TypeVariable<?> type, Type inferFrom, Map
560562 }
561563 else {
562564 resolveTypeInMap (type , inferFrom , typeMappings , malleable );
563- }
564-
565- // Bounds could also contain type vars, hence possibly go into
566- // recursion
567- for (Type bound : type .getBounds ()) {
568- if (bound instanceof TypeVariable && typeMappings .get (bound ) != null ) {
569- // If the bound of the current var (let's call it A) to
570- // infer is also a var (let's call it B):
571- // If we already encountered B, we check if the current
572- // type to infer from is assignable to
573- // the already inferred type for B. In this case we do
574- // not require equality as one var is
575- // bounded by another and it is not the same. E.g.
576- // assume we want to infer the types of vars:
577- // - - - A extends Number, B extends A
578- // From types:
579- // - - - Number, Double
580- // First A is bound to Number, next B to Double. Then we
581- // check the bounds for B. We encounter A,
582- // for which we already inferred Number. Hence, it
583- // suffices to check whether Double can be assigned
584- // to Number, it does not have to be equal as it is just
585- // a transitive bound for B.
586- Type typeAssignForBound = typeMappings .get (bound ).getType ();
587- if (!Types .isAssignable (inferFrom , typeAssignForBound )) {
588- throw new TypeInferenceException ();
565+ // Bounds could also contain type vars, hence possibly go into
566+ // recursion
567+ for (Type bound : type .getBounds ()) {
568+ if (bound instanceof TypeVariable && typeMappings .get (bound ) != null ) {
569+ // If the bound of the current var (let's call it A) to
570+ // infer is also a var (let's call it B):
571+ // If we already encountered B, we check if the current
572+ // type to infer from is assignable to
573+ // the already inferred type for B. In this case we do
574+ // not require equality as one var is
575+ // bounded by another and it is not the same. E.g.
576+ // assume we want to infer the types of vars:
577+ // - - - A extends Number, B extends A
578+ // From types:
579+ // - - - Number, Double
580+ // First A is bound to Number, next B to Double. Then we
581+ // check the bounds for B. We encounter A,
582+ // for which we already inferred Number. Hence, it
583+ // suffices to check whether Double can be assigned
584+ // to Number, it does not have to be equal as it is just
585+ // a transitive bound for B.
586+ Type typeAssignForBound = typeMappings .get (bound ).getType ();
587+ if (!Types .isAssignable (inferFrom , typeAssignForBound )) {
588+ throw new TypeInferenceException ();
589+ }
589590 }
590- } else {
591- // Else go into recursion as we encountered a new var.
592- inferTypeVariables ( bound , inferFrom , typeMappings );
593- }
591+ else {
592+ // Else go into recursion as we encountered a new var.
593+ inferTypeVariables (bound , inferFrom , typeMappings );
594+ }
595+ }
596+
594597 }
595598 }
596599
597- private static void inferTypeVariables (ParameterizedType type , Type inferFrom , Map <TypeVariable <?>, TypeMapping > typeMappings ) {
598- // Recursively follow parameterized types
599- if (inferFrom instanceof ParameterizedType ) {
600- // Finding the supertype here is really important. Suppose that we are
601- // inferring from a StrangeThing<Long> extends Thing<Double> and our
602- // Op requires a Thing<T>. We need to ensure that T gets
603- // resolved to a Double and NOT a Long.
604- ParameterizedType paramInferFrom = (ParameterizedType ) Types
605- .getExactSuperType (inferFrom , Types .raw (type ));
606- if (paramInferFrom == null ) {
607- // this means that inferFrom is NOT a superType of type. There is,
608- // however, one more thing that we can try. It is possible for inferFrom
609- // to be a subType of type (this can often happen when
610- // checkGenericAssignability is working with Functions). In this case,
611- // it is possible that inferFrom has information about the typeVariables
612- // of type.
613- ParameterizedType superTypeOfType = (ParameterizedType ) Types
614- .getExactSuperType (type , Types .raw (inferFrom ));
615- if (superTypeOfType == null ) {
616- throw new TypeInferenceException (inferFrom +
617- " cannot be implicitly cast to " + type +
618- ", thus it is impossible to infer type variables for " + inferFrom );
619- }
620- inferTypeVariables (superTypeOfType .getActualTypeArguments (),
621- ((ParameterizedType ) inferFrom ).getActualTypeArguments (),
622- typeMappings , false );
623- }
624- else {
625- inferTypeVariables (type .getActualTypeArguments (), paramInferFrom
626- .getActualTypeArguments (), typeMappings , false );
627- }
600+ private static void inferTypeVariables (ParameterizedType type , Type inferFrom ,
601+ Map <TypeVariable <?>, TypeMapping > typeMappings )
602+ {
603+ if (inferFrom instanceof WildcardType ) {
604+ inferFrom = getInferrableBound ((WildcardType ) inferFrom );
628605 }
629- else {
630- // getExactSuperType does not return Object as a superType for any
631- // interface, so we cover it here.
632- // TODO: clean
633- if (Object .class .equals (inferFrom )) {
634- mapTypeVarsToAny (type , typeMappings );
635- return ;
636- }
637- if (inferFrom instanceof WildcardType ) {
638- WildcardType inferFromWildcard = (WildcardType ) inferFrom ;
639- if (inferFromWildcard .getUpperBounds ().length == 1 ) inferFrom = inferFromWildcard .getUpperBounds ()[0 ];
640- }
606+ if (inferFrom instanceof Any ) {
607+ Any any = (Any ) inferFrom ;
608+ mapTypeVarsToAny (type , any , typeMappings );
609+ return ;
610+ }
611+ // Finding the supertype here is really important. Suppose that we are
612+ // inferring from a StrangeThing<Long> extends Thing<Double> and our
613+ // Op requires a Thing<T>. We need to ensure that T gets
614+ // resolved to a Double and NOT a Long.
615+ Type superInferFrom = Types .getExactSuperType (inferFrom , Types .raw (type ));
616+ if (superInferFrom instanceof ParameterizedType ) {
617+ ParameterizedType paramInferFrom = (ParameterizedType ) superInferFrom ;
618+ inferTypeVariables (type .getActualTypeArguments (), paramInferFrom
619+ .getActualTypeArguments (), typeMappings , false );
620+ }
621+ else if (superInferFrom instanceof Class ) {
641622 TypeVarAssigns typeVarAssigns = new TypeVarAssigns (typeMappings );
642623 Type mappedType = Types .mapVarToTypes (type , typeVarAssigns );
643624 // Use isAssignable to attempt to infer the type variables present in type
644- if (!Types .isAssignable (inferFrom , mappedType , typeVarAssigns )) {
625+ if (!Types .isAssignable (superInferFrom , mappedType , typeVarAssigns )) {
645626 throw new TypeInferenceException (inferFrom +
646627 " cannot be implicitly cast to " + mappedType +
647628 ", thus it is impossible to infer type variables for " + inferFrom );
648629 }
649630 }
631+ // -- edge cases -> do our best -- //
632+ else if (superInferFrom == null ) {
633+ // edge case 1: if inferFrom is an Object, superInferFrom will be null
634+ // when type is some interface.
635+ if (Object .class .equals (inferFrom )) {
636+ mapTypeVarsToAny (type , typeMappings );
637+ return ;
638+ }
639+ // edge case 2: if inferFrom is a superType of type, we can get (some of)
640+ // the types of type by finding the exact superType of type w.r.t.
641+ // inferFrom.
642+ Type superTypeOfType = Types .getExactSuperType (type , Types .raw (
643+ inferFrom ));
644+ if (superTypeOfType == null ) {
645+ throw new TypeInferenceException (inferFrom +
646+ " cannot be implicitly cast to " + type +
647+ ", thus it is impossible to infer type variables for " + inferFrom );
648+ }
649+ inferTypeVariables (superTypeOfType , inferFrom , typeMappings , false );
650+ mapTypeVarsToAny (type , typeMappings );
651+ }
652+ // TODO: elaborate
653+ else throw new IllegalStateException (superInferFrom +
654+ " is the supertype of " + inferFrom + " with respect to " + type +
655+ ", however this cannot be (since " + type +
656+ " is a ParamterizedType)! (Only a ParameterizedType, Class, or null " +
657+ "can be returned from Types.getExactSuperType when it is called with a ParameterizedType!)" );
650658 }
651-
659+
652660 private static void mapTypeVarsToAny (Type type ,
653661 Map <TypeVariable <?>, TypeMapping > typeMappings )
654662 {
655- if (!Types .containsTypeVars (type )) return ;
663+ mapTypeVarsToAny (type , new Any (), typeMappings );
664+ }
656665
657- if (type instanceof TypeVariable ) {
666+ private static void mapTypeVarsToAny (Type type , Any any ,
667+ Map <TypeVariable <?>, TypeMapping > typeMappings )
668+ {
669+ if (!Types .containsTypeVars (type )) return ;
670+
671+ if (type instanceof TypeVariable ) {
658672 if (typeMappings .containsKey (type )) return ;
659673 TypeVariable <?> typeVar = (TypeVariable <?>) type ;
660- typeMappings .put (typeVar , suitableTypeMapping (typeVar , new Any () , true ));
674+ typeMappings .put (typeVar , suitableTypeMapping (typeVar , any , true ));
661675 }
662- else if (type instanceof ParameterizedType ) {
676+ else if (type instanceof ParameterizedType ) {
663677 ParameterizedType pType = (ParameterizedType ) type ;
664678 Type [] typeParams = pType .getActualTypeArguments ();
665679 for (Type typeParam : typeParams ) {
0 commit comments