@@ -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,86 +562,140 @@ 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+ }
590+ }
591+ else {
592+ // Else go into recursion as we encountered a new var.
593+ inferTypeVariables (bound , inferFrom , typeMappings );
589594 }
590- } else {
591- // Else go into recursion as we encountered a new var.
592- inferTypeVariables ( bound , inferFrom , typeMappings );
593- }
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 ) throw new TypeInferenceException (
616- inferFrom + " cannot be implicitly cast to " + type +
617- ", thus it is impossible to infer type variables for " + inferFrom );
618- inferTypeVariables (superTypeOfType .getActualTypeArguments (),
619- ((ParameterizedType ) inferFrom ).getActualTypeArguments (),
620- typeMappings , false );
621- }
622- else {
623- inferTypeVariables (type .getActualTypeArguments (), paramInferFrom
624- .getActualTypeArguments (), typeMappings , false );
625- }
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 );
626605 }
627- else {
628- if (inferFrom instanceof WildcardType ) {
629- WildcardType inferFromWildcard = (WildcardType ) inferFrom ;
630- if (inferFromWildcard .getUpperBounds ().length == 1 ) inferFrom = inferFromWildcard .getUpperBounds ()[0 ];
631- }
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 ) {
632622 TypeVarAssigns typeVarAssigns = new TypeVarAssigns (typeMappings );
633623 Type mappedType = Types .mapVarToTypes (type , typeVarAssigns );
634624 // Use isAssignable to attempt to infer the type variables present in type
635- if (!Types .isAssignable (inferFrom , mappedType , typeVarAssigns )) {
625+ if (!Types .isAssignable (superInferFrom , mappedType , typeVarAssigns )) {
636626 throw new TypeInferenceException (inferFrom +
637627 " cannot be implicitly cast to " + mappedType +
638628 ", thus it is impossible to infer type variables for " + inferFrom );
639629 }
630+ // for all remaining unmapped type vars, map to Any
631+ mapTypeVarsToAny (type , typeMappings );
632+ }
633+ // -- edge cases -> do our best -- //
634+ else if (superInferFrom == null ) {
635+ // edge case 1: if inferFrom is an Object, superInferFrom will be null
636+ // when type is some interface.
637+ if (Object .class .equals (inferFrom )) {
638+ mapTypeVarsToAny (type , typeMappings );
639+ return ;
640+ }
641+ // edge case 2: if inferFrom is a superType of type, we can get (some of)
642+ // the types of type by finding the exact superType of type w.r.t.
643+ // inferFrom.
644+ Type superTypeOfType = Types .getExactSuperType (type , Types .raw (
645+ inferFrom ));
646+ if (superTypeOfType == null ) {
647+ throw new TypeInferenceException (inferFrom +
648+ " cannot be implicitly cast to " + type +
649+ ", thus it is impossible to infer type variables for " + inferFrom );
650+ }
651+ inferTypeVariables (superTypeOfType , inferFrom , typeMappings , false );
652+ mapTypeVarsToAny (type , typeMappings );
640653 }
654+ // TODO: elaborate
655+ else throw new IllegalStateException (superInferFrom +
656+ " is the supertype of " + inferFrom + " with respect to " + type +
657+ ", however this cannot be (since " + type +
658+ " is a ParamterizedType)! (Only a ParameterizedType, Class, or null " +
659+ "can be returned from Types.getExactSuperType when it is called with a ParameterizedType!)" );
641660 }
642-
661+
662+ private static void mapTypeVarsToAny (Type type ,
663+ Map <TypeVariable <?>, TypeMapping > typeMappings )
664+ {
665+ mapTypeVarsToAny (type , new Any (), typeMappings );
666+ }
667+
668+ private static void mapTypeVarsToAny (Type type , Any any ,
669+ Map <TypeVariable <?>, TypeMapping > typeMappings )
670+ {
671+ if (!Types .containsTypeVars (type )) return ;
672+
673+ if (type instanceof TypeVariable ) {
674+ if (typeMappings .containsKey (type )) return ;
675+ TypeVariable <?> typeVar = (TypeVariable <?>) type ;
676+ typeMappings .put (typeVar , suitableTypeMapping (typeVar , any , true ));
677+ }
678+ else if (type instanceof ParameterizedType ) {
679+ ParameterizedType pType = (ParameterizedType ) type ;
680+ Type [] typeParams = pType .getActualTypeArguments ();
681+ for (Type typeParam : typeParams ) {
682+ mapTypeVarsToAny (typeParam , typeMappings );
683+ }
684+ }
685+ else if (type instanceof WildcardType ) {
686+ WildcardType wildcard = (WildcardType ) type ;
687+ for (Type lowerBound : wildcard .getLowerBounds ())
688+ mapTypeVarsToAny (lowerBound , typeMappings );
689+ for (Type upperBound : wildcard .getUpperBounds ())
690+ mapTypeVarsToAny (upperBound , typeMappings );
691+ }
692+ else if (type instanceof Class ) {
693+ Class <?> clazz = (Class <?>) type ;
694+ for (Type typeParam : clazz .getTypeParameters ())
695+ mapTypeVarsToAny (typeParam , typeMappings );
696+ }
697+ }
698+
643699 private static void inferTypeVariables (WildcardType type , Type inferFrom , Map <TypeVariable <?>, TypeMapping > typeMappings ) {
644700 Type inferrableBound = getInferrableBound (type );
645701 if (inferFrom instanceof WildcardType ) {
0 commit comments